[PYTHON] Gurobi-like optimization description method

1. Preferences

-Install the library with pip install mypulp

2. Implementation method

#① Call the optimization library
from mypulp import *

#② Define the model
model = Model("The name can be anything")

#③ Declare variables
X =model.addVar(lb=0,ub=GRB.INFINITY,vtype=‘C’,name=‘’)
#C:Continuous,B:Binary,I:Inter

#④ Update the model(After variable declaration)
model.update()
#Required for gurobi but not for pulp

#⑤ Define constraints
model.addConstr(Left side formula==Equation on the right side, name='name') 

#⑥ Define the objective function
model.setObjective(Objective function expression, GRB.MINIMIZE)

#⑦ Solve the model
model.optimize()

#⑧ Output the result
print('Opt. Value=', model.ObjVal)
print('x=', x.X)

#Store optimization results in status
status = model.Status()
#If status == GRB.Status.OPTIMAL:
#Process with


#How to check the formula result
model.write("name.lp")

for v in model.getVars():
	print(v, v.X)#Variable in v, v.The optimal solution is in X

for c in model.getConstr():
	print(c.ConstrName, c.Slack, c.Pi)
	#C.Constraint name in ConstrName, c.Slack variables in Slack,
	#c.Lagrange multiplier is entered in Pi

Recommended Posts

Gurobi-like optimization description method
SVM optimization by active set method
Description method for reusing variables in shellscript
Horse racing winning method by combinatorial optimization