# C#接口

(3)$\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 */
4using Copt;
5using System;
6
7/*
8 * This C# example solves the following LP model:
9 *
10 *
11 * Maximize:
12 *  1.2 x + 1.8 y + 2.1 z
13 *
14 * Subject to:
15 *  1.5 x + 1.2 y + 1.8 z <= 2.6
16 *  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 */
23public class lp_ex1
24{
25  public static void Main()
26  {
27    try
28    {
29      Envr env = new Envr();
30      Model model = env.CreateModel("lp_ex1");
31
32      /*
33       * Add variables x, y, z
34       *
35       * obj: 1.2 x + 1.8 y + 2.1 z
36       *
37       * var:
38       *  0.1 <= x <= 0.6
39       *  0.2 <= y <= 1.5
40       *  0.3 <= z <= 2.8
41       */
42      Var x = model.AddVar(0.1, 0.6, 0.0, Copt.Consts.CONTINUOUS, "x");
43      Var y = model.AddVar(0.2, 1.5, 0.0, Copt.Consts.CONTINUOUS, "y");
44      Var z = model.AddVar(0.3, 2.8, 0.0, Copt.Consts.CONTINUOUS, "z");
45
46      model.SetObjective(1.2 * x + 1.8 * y + 2.1 * z, Copt.Consts.MAXIMIZE);
47
48      /*
49       * Add two constraints using linear expression
50       *
51       * r0: 1.5 x + 1.2 y + 1.8 z <= 2.6
52       * r1: 0.8 x + 0.6 y + 0.9 z >= 1.2
53       */
54      model.AddConstr(1.5 * x + 1.2 * y + 1.8 * z <= 2.6, "r0");
55
56      Expr expr = new Expr(x, 0.8);
58      expr += 0.9 * z;
60
61      // Set parameters
62      model.SetDblParam(Copt.DblParam.TimeLimit, 10);
63
64      // Solve problem
65      model.Solve();
66
67      // Output solution
68      if (model.GetIntAttr(Copt.IntAttr.LpStatus) == Copt.Status.OPTIMAL)
69      {
70        Console.WriteLine("\nFound optimal solution:");
71        VarArray vars = model.GetVars();
72        for (int i = 0; i < vars.Size(); i++)
73        {
74          Var x = vars.GetVar(i);
75          Console.WriteLine("  {0} = {1}", x.GetName(), x.Get(Copt.DblInfo.Value));
76        }
77        Console.WriteLine("Obj = {0}", model.GetDblAttr(Copt.DblAttr.LpObjVal));
78      }
79
80      Console.WriteLine("\nDone");
81    }
82    catch (CoptException e)
83    {
84      Console.WriteLine("Error Code = {0}", e.GetCode());
85      Console.WriteLine(e.Message);
86    }
87  }
88}


### 引用 COPT的C# 命名空间

using Copt;


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

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


### 添加变量

      /*
* Add variables x, y, z
*
* obj: 1.2 x + 1.8 y + 2.1 z
*
* var:
*  0.1 <= x <= 0.6
*  0.2 <= y <= 1.5
*  0.3 <= z <= 2.8
*/
Var x = model.AddVar(0.1, 0.6, 0.0, Copt.Consts.CONTINUOUS, "x");
Var y = model.AddVar(0.2, 1.5, 0.0, Copt.Consts.CONTINUOUS, "y");
Var z = model.AddVar(0.3, 2.8, 0.0, Copt.Consts.CONTINUOUS, "z");

model.SetObjective(1.2 * x + 1.8 * y + 2.1 * z, Copt.Consts.MAXIMIZE);


### 添加约束

      /*
* Add two constraints using linear expression
*
* r0: 1.5 x + 1.2 y + 1.8 z <= 2.6
* r1: 0.8 x + 0.6 y + 0.9 z >= 1.2
*/
model.AddConstr(1.5 * x + 1.2 * y + 1.8 * z <= 2.6, "r0");

Expr expr = new 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.LpStatus) == Copt.Status.OPTIMAL)
{
Console.WriteLine("\nFound optimal solution:");
VarArray vars = model.GetVars();
for (int i = 0; i < vars.Size(); i++)
{
Var x = vars.GetVar(i);
Console.WriteLine("  {0} = {1}", x.GetName(), x.Get(Copt.DblInfo.Value));
}
Console.WriteLine("Obj = {0}", model.GetDblAttr(Copt.DblAttr.LpObjVal));
}

Console.WriteLine("\nDone");


### 错误处理

    catch (CoptException e)
{
Console.WriteLine("Error Code = {0}", e.GetCode());
Console.WriteLine(e.Message);
}