5.1.3. LP Modeling and Optimization in C++ΒΆ
In this section, we will utilize MindOpt C++ API to model and solve the linear optimization problem in Example of Linear Programming.
First of all, include the header files:
24#include "MindoptCpp.h"
Create an optimization model model
:
33 MDOEnv env = MDOEnv();
34 MDOModel model = MDOModel(env);
Next, we set the optimization sense to minimization via MDOModel::set()
and add four decision variables using MDOModel::addVar()
(please refer to C++ API for the detailed usage of MDOModel::set()
and MDOModel::addVar()
):
41 /* Change to minimization problem. */
42 model.set(MDO_IntAttr_ModelSense, MDO_MINIMIZE);
43
44 /* Add variables. */
45 std::vector<MDOVar> x;
46 x.push_back(model.addVar(0.0, 10.0, 1.0, MDO_CONTINUOUS, "x0"));
47 x.push_back(model.addVar(0.0, MDO_INFINITY, 2.0, MDO_CONTINUOUS, "x1"));
48 x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x2"));
49 x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x3"));
We call MDOModel::addConstr()
to add the linear constraints to the model:
51 /* Add constraints. */
52 model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1.0, "c0");
53 model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3] == 1.0, "c1");
Once the model is constructed, we call MDOModel::optimize()
to solve the problem:
58 model.optimize();
Then, we can retrieive the optimal objective value and solutions via getting attributes:
59 if (model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
60 {
61 cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
62 cout << "Decision variables: " << endl;
63 int i = 0;
64 for (auto v : x)
65 {
66 cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
67 }
68 }
69 else
70 {
71 cout << "No feasible solution." << endl;
72 }
Complete example codes are provided in MdoLoEx1.cpp.
1/**
2 * Description
3 * -----------
4 *
5 * Linear optimization (row-wise input).
6 *
7 * Formulation
8 * -----------
9 *
10 * Minimize
11 * obj: 1 x0 + 2 x1 + 1 x2 + 1 x3
12 * Subject To
13 * c0 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
14 * c1 : 1 x0 - 1 x2 + 6 x3 = 1
15 * Bounds
16 * 0 <= x0 <= 10
17 * 0 <= x1
18 * 0 <= x2
19 * 0 <= x3
20 * End
21 */
22#include <iostream>
23#include <vector>
24#include "MindoptCpp.h"
25
26using namespace std;
27
28int main(void)
29{
30 /*------------------------------------------------------------------*/
31 /* Step 1. Create environment and model. */
32 /*------------------------------------------------------------------*/
33 MDOEnv env = MDOEnv();
34 MDOModel model = MDOModel(env);
35
36 try
37 {
38 /*------------------------------------------------------------------*/
39 /* Step 2. Input model. */
40 /*------------------------------------------------------------------*/
41 /* Change to minimization problem. */
42 model.set(MDO_IntAttr_ModelSense, MDO_MINIMIZE);
43
44 /* Add variables. */
45 std::vector<MDOVar> x;
46 x.push_back(model.addVar(0.0, 10.0, 1.0, MDO_CONTINUOUS, "x0"));
47 x.push_back(model.addVar(0.0, MDO_INFINITY, 2.0, MDO_CONTINUOUS, "x1"));
48 x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x2"));
49 x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, MDO_CONTINUOUS, "x3"));
50
51 /* Add constraints. */
52 model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1.0, "c0");
53 model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3] == 1.0, "c1");
54
55 /*------------------------------------------------------------------*/
56 /* Step 3. Solve the problem and populate optimization result. */
57 /*------------------------------------------------------------------*/
58 model.optimize();
59 if (model.get(MDO_IntAttr_Status) == MDO_OPTIMAL)
60 {
61 cout << "Optimal objective value is: " << model.get(MDO_DoubleAttr_ObjVal) << endl;
62 cout << "Decision variables: " << endl;
63 int i = 0;
64 for (auto v : x)
65 {
66 cout << "x[" << i++ << "] = " << v.get(MDO_DoubleAttr_X) << endl;
67 }
68 }
69 else
70 {
71 cout << "No feasible solution." << endl;
72 }
73 }
74 catch (MDOException& e)
75 {
76 std::cout << "Error code = " << e.getErrorCode() << std::endl;
77 std::cout << e.getMessage() << std::endl;
78 }
79 catch (...)
80 {
81 std::cout << "Error during optimization." << std::endl;
82 }
83
84 return static_cast<int>(MDO_OKAY);
85}