# C++接口

(2)$\begin{split}\text{最大化：} & \\ & 1.2 x + 1.8 y + 2.1 z \\ \text{约束：} & \\ & 1.5 x + 1.2 y + 1.8 z \leq 2.6 \\ & 0.8 x + 0.6 y + 0.9 z \geq 1.2 \\ \text{变量范围：} & \\ & 0.1 \leq x \leq 0.6 \\ & 0.2 \leq y \leq 1.5 \\ & 0.3 \leq z \leq 2.8 \\\end{split}$

## 示例解析

 1/*
3 */
4#include "coptcpp_pch.h"
5
6using namespace std;
7
8/*
9 * This example solves the following LP model:
10 *
11 *  Maximize:
12 *    1.2 x + 1.8 y + 2.1 z
13 *
14 *  Subject to:
15 *    R0: 1.5 x + 1.2 y + 1.8 z <= 2.6
16 *    R1: 0.8 x + 0.6 y + 0.9 z >= 1.2
17 *
18 *  Where:
19 *    0.1 <= x <= 0.6
20 *    0.2 <= y <= 1.5
21 *    0.3 <= z <= 2.8
22 */
23int main(int argc, char* argv[])
24{
25  try
26  {
27    Envr env;
28    Model model = env.CreateModel("lp_ex1");
29
31    Var x = model.AddVar(0.1, 0.6, 0.0, COPT_CONTINUOUS, "x");
32    Var y = model.AddVar(0.2, 1.5, 0.0, COPT_CONTINUOUS, "y");
33    Var z = model.AddVar(0.3, 2.8, 0.0, COPT_CONTINUOUS, "z");
34
35    // Set objective
36    model.SetObjective(1.2 * x + 1.8 * y + 2.1 * z, COPT_MAXIMIZE);
37
38    // Add linear constraints using linear expression
39    model.AddConstr(1.5 * x + 1.2 * y + 1.8 * z <= 2.6, "R0");
40
41    Expr expr(x, 0.8);
43    expr += 0.9 * z;
45
46    // Set parameters
47    model.SetDblParam(COPT_DBLPARAM_TIMELIMIT, 10);
48
49    // Solve problem
50    model.Solve();
51
52    // Output solution
53    if (model.GetIntAttr(COPT_INTATTR_HASLPSOL) != 0)
54    {
55      cout << "\nFound optimal solution:" << endl;
56      VarArray vars = model.GetVars();
57      for (int i = 0; i < vars.Size(); i++)
58      {
59        Var var = vars.GetVar(i);
60        cout << "  " << var.GetName() << " = " << var.Get(COPT_DBLINFO_VALUE) << endl;
61      }
62      cout << "Obj = " << model.GetDblAttr(COPT_DBLATTR_LPOBJVAL) << endl;
63    }
64  }
65  catch (CoptException e)
66  {
67    cout << "Error Code = " << e.GetCode() << endl;
68    cout << e.what() << endl;
69  }
70  catch (...)
71  {
72    cout << "Unknown exception occurs!";
73  }
74}


### 导入COPT的C++头文件

#include "coptcpp_pch.h"


### 创建求解环境和建模

    Envr env;
Model model = env.CreateModel("lp_ex1");


### 添加变量

    // Add variables
Var x = model.AddVar(0.1, 0.6, 0.0, COPT_CONTINUOUS, "x");
Var y = model.AddVar(0.2, 1.5, 0.0, COPT_CONTINUOUS, "y");
Var z = model.AddVar(0.3, 2.8, 0.0, COPT_CONTINUOUS, "z");


### 添加约束

    // Add linear constraints using linear expression
model.AddConstr(1.5 * x + 1.2 * y + 1.8 * z <= 2.6, "R0");

Expr expr(x, 0.8);
expr += 0.9 * z;


### 设置参数和属性

    // Set parameters
model.SetDblParam(COPT_DBLPARAM_TIMELIMIT, 10);


### 求解问题

    // Solve problem
model.Solve();


### 输出结果

    // Output solution
if (model.GetIntAttr(COPT_INTATTR_HASLPSOL) != 0)
{
cout << "\nFound optimal solution:" << endl;
VarArray vars = model.GetVars();
for (int i = 0; i < vars.Size(); i++)
{
Var var = vars.GetVar(i);
cout << "  " << var.GetName() << " = " << var.Get(COPT_DBLINFO_VALUE) << endl;
}
cout << "Obj = " << model.GetDblAttr(COPT_DBLATTR_LPOBJVAL) << endl;
}


### 错误处理

  catch (CoptException e)
{
cout << "Error Code = " << e.GetCode() << endl;
cout << e.what() << endl;
}
catch (...)
{
cout << "Unknown exception occurs!";
}