5.2.4. MILP modeling and optimization in Python¶
This topic describes how to use the Python API of MindOpt to build a model and solve the problem in Examples of MILP problems.
5.2.4.1. Input by row: mdo_milo_ex1¶
First, import MindOpt Python package.
26from mindoptpy import *
Create an optimization model.
33 # Step 1. Create a model and change the parameters.
34 model = MdoModel()
Call mindoptpy.MdoModel.set_int_attr()
to set the target function to Minimization, call mindoptpy.MdoModel.add_var()
to add four optimization variables, and define the upper bound, lower bound, names, and types of the variables. For more information about how to use mindoptpy.MdoModel.set_int_attr()
and mindoptpy.MdoModel.add_var()
, see Python API.
37 # Step 2. Input model.
38 # Change to minimization problem.
39 model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
40
41 # Add variables.
42 x = []
43 x.append(model.add_var(0.0, 10.0, 1.0, None, "x0", True))
44 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", True))
45 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", True))
46 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))
Note
In the function mindoptpy.MdoModel.add_var()
, the last parameter is is_integer
which means the variable is integer when it is set as True
.
Then, add non-zero elements and their upper and lower bounds of linear constraints.
48 # Add constraints.
49 # Note that the nonzero elements are inputted in a row-wise order here.
50 model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
51 model.add_cons(1.0, 1.0, 1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], "c1")
After the modeling is completed, call mindoptpy.MdoModel.solve_prob()
to solve the optimization problem, and call mindoptpy.MdoModel.display_results()
to view the optimization result.
53 # Step 3. Solve the problem and populate the result.
54 model.solve_prob()
55 model.display_results()
At the end, Call mindoptpy.MdoModel.free_mdl()
to release the memory.
65 # Step 4. Free the model.
66 model.free_mdl()
The linked file mdo_milo_ex1.py provides the complete source code:
1"""
2/**
3 * Description
4 * -----------
5 *
6 * Linear optimization (row-wise input).
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 * Integers
22 * x0 x1 x2
23 * End
24 */
25"""
26from mindoptpy import *
27
28
29if __name__ == "__main__":
30
31 MDO_INFINITY = MdoModel.get_infinity()
32
33 # Step 1. Create a model and change the parameters.
34 model = MdoModel()
35
36 try:
37 # Step 2. Input model.
38 # Change to minimization problem.
39 model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
40
41 # Add variables.
42 x = []
43 x.append(model.add_var(0.0, 10.0, 1.0, None, "x0", True))
44 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", True))
45 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", True))
46 x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))
47
48 # Add constraints.
49 # Note that the nonzero elements are inputted in a row-wise order here.
50 model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
51 model.add_cons(1.0, 1.0, 1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], "c1")
52
53 # Step 3. Solve the problem and populate the result.
54 model.solve_prob()
55 model.display_results()
56
57 except MdoError as e:
58 print("Received Mindopt exception.")
59 print(" - Code : {}".format(e.code))
60 print(" - Reason : {}".format(e.message))
61 except Exception as e:
62 print("Received exception.")
63 print(" - Reason : {}".format(e))
64 finally:
65 # Step 4. Free the model.
66 model.free_mdl()