3. TutorialΒΆ
Import the
conv_optpackage:import conv_opt
Create a model and, optionally, name the model:
model = conv_opt.Model(name='model')
The default name is
None.Create variables; optionally set their names, types, and upper and lower bounds; and add them to models:
var_1 = conv_opt.Variable(name='var_1', type=conv_opt.VariableType.continuous, lower_bound=0, upper_bound=1) model.variables.append(var_1) var_2 = conv_opt.Variable(name='var_2', type=conv_opt.VariableType.continuous, lower_bound=0, upper_bound=1) model.variables.append(var_2)
The default name is
None.The
typeargument must be one of the following values. The default type is continuous.conv_opt.VariableType.binaryconv_opt.VariableType.integerconv_opt.VariableType.continuousconv_opt.VariableType.semi_integerconv_opt.VariableType.semi_continuousconv_opt.VariableType.partially_integer
The default upper and lower bounds are
None.Set the objective expression and direction:
model.objective_terms = [ conv_opt.LinearTerm(var_1, 1.), conv_opt.QuadraticTerm(var_2, var_2, 1.), ] model.objective_direction = conv_opt.ObjectiveDirection.maximize
objective_termsshould be a list of linear (conv_opt.LinearTerm) and quadratic terms (conv_opt.QuadraticTerm). The arguments to the constructors of these class are the variables involved in the term and coefficient for the term.objective_directioncan be either of following values. The default direction is minimize.conv_opt.ObjectiveDirection.maximizeconv_opt.ObjectiveDirection.minimize
Create constraints; optionally set their names and upper and lower bounds; and add them to models:
contraint_1 = conv_opt.Constraint([ conv_opt.LinearTerm(var_1, 1), conv_opt.LinearTerm(var_2, -1), ], name='contraint_1', upper_bound=0, lower_bound=0) model.constraints.append(contraint_1)
The first argument should be a list of linear terms.
The default name and upper and lower bounds are
None.Configure the options for solving the model:
options = conv_opt.SolveOptions(solver=conv_opt.Solver.cplex, presolve=Presolve.off, verbosity=False)
The
solverargument allows you to select a specific solver. The argument must be one of the following values. The default solver is GLPK.conv_opt.Solver.cbcconv_opt.Solver.cplexconv_opt.Solver.cvxoptconv_opt.Solver.glpkconv_opt.Solver.gurobiconv_opt.Solver.minosconv_opt.Solver.mosekconv_opt.Solver.quadprogconv_opt.Solver.scipyconv_opt.Solver.soplexconv_opt.Solver.xpress
The
presolvearegument must be one of the following values. The default value is off.conv_opt.Presolve.autoconv_opt.Presolve.onconv_opt.Presolve.off
Please see the
API documentationfor information about additional options.Solve the model:
result = model.solve(options) if result.status_code != conv_opt.StatusCode.optimal: raise Exception(result.status_message) value = result.value primals = result.primals
The result will be an instance of
conv_opt.Result. The attributes of this class include:status_codestatus_messagevalueprimalsreduced_costsduals
status_codewill be an instance of theconv_opt.StatusCodeenumerated type.Get diagnostic information about the model:
stats = model.get_stats()
Convert the model to the lower level API of one of the solvers:
options = conv_opt.SolveOptions(solver=conv_opt.Solver.cplex) cplex_model = model.convert(options)
Export the model to a file:
filename='/path/to/file.ext' model.export(filename)
conv_optsupports the following extensions:- alp
- cbf
- dpe: dual perturbed model
- dua: dual
- jtask: Jtask format
- lp
- mps
- opf
- ppe: perturbed model
- rew: model with generic names in mps format
- rlp: model with generic names in lp format
- sav
- task: Task format
- xml: OSiL