3. TutorialΒΆ

  1. Import the conv_opt package:

    import conv_opt
    
  2. Create a model and, optionally, name the model:

    model = conv_opt.Model(name='model')
    

    The default name is None.

  3. 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 type argument must be one of the following values. The default type is continuous.

    • conv_opt.VariableType.binary

    • conv_opt.VariableType.integer

    • conv_opt.VariableType.continuous

    • conv_opt.VariableType.semi_integer

    • conv_opt.VariableType.semi_continuous

    • conv_opt.VariableType.partially_integer

    The default upper and lower bounds are None.

  4. 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_terms should 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_direction can be either of following values. The default direction is minimize.

    • conv_opt.ObjectiveDirection.maximize

    • conv_opt.ObjectiveDirection.minimize

  5. 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.

  6. Configure the options for solving the model:

    options = conv_opt.SolveOptions(solver=conv_opt.Solver.cplex, presolve=Presolve.off, verbosity=False)
    

    The solver argument allows you to select a specific solver. The argument must be one of the following values. The default solver is GLPK.

    • conv_opt.Solver.cbc

    • conv_opt.Solver.cplex

    • conv_opt.Solver.cvxopt

    • conv_opt.Solver.glpk

    • conv_opt.Solver.gurobi

    • conv_opt.Solver.minos

    • conv_opt.Solver.mosek

    • conv_opt.Solver.quadprog

    • conv_opt.Solver.scipy

    • conv_opt.Solver.soplex

    • conv_opt.Solver.xpress

    The presolve aregument must be one of the following values. The default value is off.

    • conv_opt.Presolve.auto

    • conv_opt.Presolve.on

    • conv_opt.Presolve.off

    Please see the API documentation for information about additional options.

  7. 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_code

    • status_message

    • value

    • primals

    • reduced_costs

    • duals

    status_code will be an instance of the conv_opt.StatusCode enumerated type.

  8. Get diagnostic information about the model:

    stats = model.get_stats()
    
  9. 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)
    
  10. Export the model to a file:

    filename='/path/to/file.ext'
    model.export(filename)
    

    conv_opt supports 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