6.2. Modeling and optimization by Pyomo¶
Pyomo is a third-party open-source modeling language developed based on Python. It supports modeling and analysis for linear programming, mixed-integer programming, and nonlinear programming problems, and it calls other commercial or open-source solvers to solve the problems.
At present, MindOpt allows you to build a linear programming model by using Pyomo on Windows, Linux, or OSX and call MindOpt to solve problems. For more information about Pyomo, see the official document .
This topic describes how to use Pyomo API to build a model for the optimization problem in Examples of linear programming problems and call MindOpt to solve the problem.
6.2.1. Install Pyomo¶
To use Pyomo, you need to install MindOpt first. For more information about how to install and configure MindOpt, see Installation. After installing MindOpt, execute the following pip
command to install Pyomo:
pip install pyomo
For more information about how to install Pyomo, visit the official website of Pyomo.
6.2.2. Call the Pyomo API document¶
The Pyomo API document (mindopt_pyomo.py
) of MindOpt defines the API required for Pyomo to call MindOpt. This API document is inherited from the DirectSolver
category of Pyomo and the implementation code is included in the installation package.
<MDOHOME>\<VERSION>\<PLATFORM>\lib\pyomo\mindopt_pyomo.py
Move this API document to the current directory and load it in the code of Python.
30from mindopt_pyomo import MindoDirect
Call the Pyomo API to build a model for the optimization problem in Examples of linear programming problems. For more information about the Pyomo API, see the official API document .
41 # Define variables and constraints.
42 variable_names = ['x0', 'x1', 'x2', 'x3']
43 var_lb = {'x0':0, 'x1':0, 'x2':0, 'x3':0}
44 var_ub = {'x0':10, 'x1':None, 'x2':None, 'x3':None}
45 objective_coeff = {'x0': 1, 'x1': 1, 'x2': 1, 'x3': 1}
46 constraint_names = ['c0', 'c1']
47 constraint_bound = {'c0': 1, 'c1': 1}
48 constraint_coeff = {('x0', 'c0'): 1, ('x1', 'c0'): 1, ('x2', 'c0'): 2, ('x3', 'c0'): 3,
49 ('x0', 'c1'): 1, ('x1', 'c1'): -1, ('x2', 'c1'): 0, ('x3', 'c1'): 6}
50
51 # Create model.
52 model = ConcreteModel(name="ex1")
53
54 # Build decision variables.
55 model.Set = Set(initialize=variable_names)
56 model.Variable = Var(model.Set, within=NonNegativeReals, bounds=fb)
57
58 # Objective.
59 model.obj = Objective(expr=sum(objective_coeff[var_name] * model.Variable[var_name] for var_name in variable_names), sense=minimize)
60
61 # Constraints.
62 model.dual = Suffix(direction=Suffix.IMPORT)
63 model.cons1 = Constraint(expr = ConstraintsRule(model, 'c0') >= constraint_bound['c0'])
64 model.cons2 = Constraint(expr = ConstraintsRule(model, 'c1') == constraint_bound['c1'])
Specify the solver to MindOpt and set the related parameters. For more information about parameters, see Optional input parameters.
69 # Solve problem by MindOpt solver.
70 opt = SolverFactory("mindo_direct")
71
72 # Set options.
73 opt.options['Method'] = -1
74 opt.options['IPM/PrimalTolerance'] = 1e-10
Call the solve()
function of Pyomo and obtain related results.
79 # Solve.
80 results = opt.solve(model)
81
82 # Summary of result.
83 results.write()
84
85 if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
86 print("The solution is optimal and feasible.")
87 model.Variable.display()
88 model.dual.display()
89 model.obj.display()
90 elif (results.solver.termination_condition == TerminationCondition.infeasible):
91 print("The model is infeasible.")
92 print("Solver Status: ", results.solver.status)
93 else:
94 print("Something else is wrong.")
95 print("Solver Status: ", results.solver.status)
6.2.3. Modeling example: mdo_pyomo_lo_ex1¶
The linked file mdo_pyomo_lo_ex1.py provides complete code.
1"""
2/**
3 * Description
4 * -----------
5 *
6 * Linear optimization.
7 *
8 * Formulation
9 * -----------
10 *
11 * Minimize
12 * obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
13 * Subject To
14 * c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
15 * c2 : 1 x0 - 1 x2 + 6 x3 == 1
16 * Bounds
17 * 0 <= x0 <= 10
18 * 0 <= x1
19 * 0 <= x2
20 * 0 <= x3
21 * End
22 */
23"""
24
25from pyomo.environ import *
26from pyomo.core.base.util import Initializer
27from pyomo.opt import SolverFactory
28from pyomo.opt import SolverStatus, TerminationCondition
29
30from mindopt_pyomo import MindoDirect
31from mindoptpy import *
32
33def ConstraintsRule(model, p):
34 return sum(constraint_coeff[i, p] * model.Variable[i] for i in variable_names)
35
36def fb(model, i):
37 return (var_lb[i], var_ub[i])
38
39if __name__ == '__main__':
40
41 # Define variables and constraints.
42 variable_names = ['x0', 'x1', 'x2', 'x3']
43 var_lb = {'x0':0, 'x1':0, 'x2':0, 'x3':0}
44 var_ub = {'x0':10, 'x1':None, 'x2':None, 'x3':None}
45 objective_coeff = {'x0': 1, 'x1': 1, 'x2': 1, 'x3': 1}
46 constraint_names = ['c0', 'c1']
47 constraint_bound = {'c0': 1, 'c1': 1}
48 constraint_coeff = {('x0', 'c0'): 1, ('x1', 'c0'): 1, ('x2', 'c0'): 2, ('x3', 'c0'): 3,
49 ('x0', 'c1'): 1, ('x1', 'c1'): -1, ('x2', 'c1'): 0, ('x3', 'c1'): 6}
50
51 # Create model.
52 model = ConcreteModel(name="ex1")
53
54 # Build decision variables.
55 model.Set = Set(initialize=variable_names)
56 model.Variable = Var(model.Set, within=NonNegativeReals, bounds=fb)
57
58 # Objective.
59 model.obj = Objective(expr=sum(objective_coeff[var_name] * model.Variable[var_name] for var_name in variable_names), sense=minimize)
60
61 # Constraints.
62 model.dual = Suffix(direction=Suffix.IMPORT)
63 model.cons1 = Constraint(expr = ConstraintsRule(model, 'c0') >= constraint_bound['c0'])
64 model.cons2 = Constraint(expr = ConstraintsRule(model, 'c1') == constraint_bound['c1'])
65
66 # Print formulation of model.
67 model.pprint()
68
69 # Solve problem by MindOpt solver.
70 opt = SolverFactory("mindo_direct")
71
72 # Set options.
73 opt.options['Method'] = -1
74 opt.options['IPM/PrimalTolerance'] = 1e-10
75
76 # Solve.
77 results = opt.solve(model)
78
79 # Summary of result.
80 results.write()
81
82 if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
83 print("The solution is optimal and feasible.")
84 model.Variable.display()
85 model.dual.display()
86 model.obj.display()
87 elif (results.solver.termination_condition == TerminationCondition.infeasible):
88 print("The model is infeasible.")
89 print("Solver Status: ", results.solver.status)
90 else:
91 print("Something else is wrong.")
92 print("Solver Status: ", results.solver.status)
For more information about other examples of Pyomo, visit Pyomo Gallery.