Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Example Tour
Light LogoDark Logo
Gurobi
Back to top

feasopt.py#

#!/usr/bin/env python3# Copyright 2025, Gurobi Optimization, LLC# This example reads a MIP model from a file, adds artificial# variables to each constraint, and then minimizes the sum of the# artificial variables.  A solution with objective zero corresponds# to a feasible solution to the input model.## We can also use FeasRelax feature to do it. In this example, we# use minrelax=1, i.e. optimizing the returned model finds a solution# that minimizes the original objective, but only from among those# solutions that minimize the sum of the artificial variables.importsysimportgurobipyasgpiflen(sys.argv)<2:print("Usage: feasopt.py filename")sys.exit(0)feasmodel=gp.read(sys.argv[1])# create a copy to use FeasRelax feature laterfeasmodel1=feasmodel.copy()# clear objectivefeasmodel.setObjective(0.0)# add slack variablesforcinfeasmodel.getConstrs():sense=c.Senseifsense!=">":feasmodel.addVar(obj=1.0,name=f"ArtN_{c.ConstrName}",column=gp.Column([-1],[c]))ifsense!="<":feasmodel.addVar(obj=1.0,name=f"ArtP_{c.ConstrName}",column=gp.Column([1],[c]))# optimize modified modelfeasmodel.optimize()feasmodel.write("feasopt.lp")# use FeasRelax featurefeasmodel1.feasRelaxS(0,True,False,True)feasmodel1.write("feasopt1.lp")feasmodel1.optimize()

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp