# C接口

(1)$\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
5/*
6 * The problem to solve:
7 *
8 *  Maximize:
9 *    1.2 x + 1.8 y + 2.1 z
10 *
11 *  Subject to:
12 *    1.5 x + 1.2 y + 1.8 z <= 2.6
13 *    0.8 x + 0.6 y + 0.9 z >= 1.2
14 *
15 *  where:
16 *    0.1 <= x <= 0.6
17 *    0.2 <= y <= 1.5
18 *    0.3 <= z <= 2.8
19 */
20
21#include "copt.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25
26int main(int argc, char* argv[])
27{
28  int errcode = 0;
29
30  copt_env* env = NULL;
31  copt_prob* prob = NULL;
32
33  // Create COPT environment
34  errcode = COPT_CreateEnv(&env);
35  if (errcode)
36    goto COPT_EXIT;
37
38  // Create COPT problem
39  errcode = COPT_CreateProb(env, &prob);
40  if (errcode)
41    goto COPT_EXIT;
42
43  /*
45   *
46   *   obj: 1.2 C0 + 1.8 C1 + 2.1 C2
47   *
48   *   var:
49   *     0.1 <= C0 <= 0.6
50   *     0.2 <= C1 <= 1.5
51   *     0.3 <= C2 <= 2.8
52   *
53   */
54  int ncol = 3;
55  double colcost[] = {1.2, 1.8, 2.1};
56  double collb[] = {0.1, 0.2, 0.3};
57  double colub[] = {0.6, 1.5, 1.8};
58
59  errcode = COPT_AddCols(prob, ncol, colcost, NULL, NULL, NULL, NULL, NULL, collb, colub, NULL);
60  if (errcode)
61    goto COPT_EXIT;
62
63  /*
65   *
66   *   r0: 1.5 C0 + 1.2 C1 + 1.8 C2 <= 2.6
67   *   r1: 0.8 C0 + 0.6 C1 + 0.9 C2 >= 1.2
68   */
69  int nrow = 2;
70  int rowbeg[] = {0, 3};
71  int rowcnt[] = {3, 3};
72  int rowind[] = {0, 1, 2, 0, 1, 2};
73  double rowelem[] = {1.5, 1.2, 1.8, 0.8, 0.6, 0.9};
74  char rowsen[] = {COPT_LESS_EQUAL, COPT_GREATER_EQUAL};
75  double rowrhs[] = {2.6, 1.2};
76
77  errcode = COPT_AddRows(prob, nrow, rowbeg, rowcnt, rowind, rowelem, rowsen, rowrhs, NULL, NULL);
78  if (errcode)
79    goto COPT_EXIT;
80
81  // Set parameters and attributes
82  errcode = COPT_SetDblParam(prob, COPT_DBLPARAM_TIMELIMIT, 10);
83  if (errcode)
84    goto COPT_EXIT;
85  errcode = COPT_SetObjSense(prob, COPT_MAXIMIZE);
86  if (errcode)
87    goto COPT_EXIT;
88
89  // Solve problem
90  errcode = COPT_SolveLp(prob);
91  if (errcode)
92    goto COPT_EXIT;
93
94  // Analyze solution
95  int lpstat = COPT_LPSTATUS_UNSTARTED;
96  double lpobjval;
97  double* lpsol = NULL;
98  int* colstat = NULL;
99
100  errcode = COPT_GetIntAttr(prob, COPT_INTATTR_LPSTATUS, &lpstat);
101  if (errcode)
102    goto COPT_EXIT;
103
104  if (lpstat == COPT_LPSTATUS_OPTIMAL)
105  {
106    lpsol = (double*)malloc(ncol * sizeof(double));
107    colstat = (int*)malloc(ncol * sizeof(int));
108
109    errcode = COPT_GetLpSolution(prob, lpsol, NULL, NULL, NULL);
110    if (errcode)
111      goto COPT_EXIT;
112
113    errcode = COPT_GetBasis(prob, colstat, NULL);
114    if (errcode)
115      goto COPT_EXIT;
116
117    errcode = COPT_GetDblAttr(prob, COPT_DBLATTR_LPOBJVAL, &lpobjval);
118    if (errcode)
119      goto COPT_EXIT;
120
121    printf("\nObjective value: %.6f\n", lpobjval);
122
123    printf("Variable solution: \n");
124    for (int i = 0; i < ncol; ++i)
125      printf("  x[%d] = %.6f\n", i, lpsol[i]);
126
127    printf("Variable basis status: \n");
128    for (int i = 0; i < ncol; ++i)
129      printf("  x[%d]: %d\n", i, colstat[i]);
130
131    free(lpsol);
132    free(colstat);
133  }
134
135  // Write problem, solution and modified parameters to files
136  errcode = COPT_WriteMps(prob, "lp_ex1.mps");
137  if (errcode)
138    goto COPT_EXIT;
139  errcode = COPT_WriteBasis(prob, "lp_ex1.bas");
140  if (errcode)
141    goto COPT_EXIT;
142  errcode = COPT_WriteSol(prob, "lp_ex1.sol");
143  if (errcode)
144    goto COPT_EXIT;
145  errcode = COPT_WriteParam(prob, "lp_ex1.par");
146  if (errcode)
147    goto COPT_EXIT;
148
149  // Error handling
150COPT_EXIT:
151  if (errcode)
152  {
153    char errmsg[COPT_BUFFSIZE];
154
155    COPT_GetRetcodeMsg(errcode, errmsg, COPT_BUFFSIZE);
156    printf("ERROR %d: %s\n", errcode, errmsg);
157
158    return 0;
159  }
160
161  // Delete problem and environment
162  COPT_DeleteProb(&prob);
163
164  COPT_DeleteEnv(&env);
165
166  return 0;
167}


### 创建环境

  // Create COPT environment
errcode = COPT_CreateEnv(&env);
if (errcode)
goto COPT_EXIT;


### 创建问题

  // Create COPT problem
errcode = COPT_CreateProb(env, &prob);
if (errcode)
goto COPT_EXIT;


### 添加变量

  /*
*
*   obj: 1.2 C0 + 1.8 C1 + 2.1 C2
*
*   var:
*     0.1 <= C0 <= 0.6
*     0.2 <= C1 <= 1.5
*     0.3 <= C2 <= 2.8
*
*/
int ncol = 3;
double colcost[] = {1.2, 1.8, 2.1};
double collb[] = {0.1, 0.2, 0.3};
double colub[] = {0.6, 1.5, 1.8};

errcode = COPT_AddCols(prob, ncol, colcost, NULL, NULL, NULL, NULL, NULL, collb, colub, NULL);
if (errcode)
goto COPT_EXIT;


### 添加约束

  /*
*
*   r0: 1.5 C0 + 1.2 C1 + 1.8 C2 <= 2.6
*   r1: 0.8 C0 + 0.6 C1 + 0.9 C2 >= 1.2
*/
int nrow = 2;
int rowbeg[] = {0, 3};
int rowcnt[] = {3, 3};
int rowind[] = {0, 1, 2, 0, 1, 2};
double rowelem[] = {1.5, 1.2, 1.8, 0.8, 0.6, 0.9};
char rowsen[] = {COPT_LESS_EQUAL, COPT_GREATER_EQUAL};
double rowrhs[] = {2.6, 1.2};

errcode = COPT_AddRows(prob, nrow, rowbeg, rowcnt, rowind, rowelem, rowsen, rowrhs, NULL, NULL);
if (errcode)
goto COPT_EXIT;


### 设置参数和属性

  // Set parameters and attributes
errcode = COPT_SetDblParam(prob, COPT_DBLPARAM_TIMELIMIT, 10);
if (errcode)
goto COPT_EXIT;
errcode = COPT_SetObjSense(prob, COPT_MAXIMIZE);
if (errcode)
goto COPT_EXIT;


### 求解问题

  // Solve problem
errcode = COPT_SolveLp(prob);
if (errcode)
goto COPT_EXIT;


### 分析结果

  // Analyze solution
int lpstat = COPT_LPSTATUS_UNSTARTED;
double lpobjval;
double* lpsol = NULL;
int* colstat = NULL;

errcode = COPT_GetIntAttr(prob, COPT_INTATTR_LPSTATUS, &lpstat);
if (errcode)
goto COPT_EXIT;

if (lpstat == COPT_LPSTATUS_OPTIMAL)
{
lpsol = (double*)malloc(ncol * sizeof(double));
colstat = (int*)malloc(ncol * sizeof(int));

errcode = COPT_GetLpSolution(prob, lpsol, NULL, NULL, NULL);
if (errcode)
goto COPT_EXIT;

errcode = COPT_GetBasis(prob, colstat, NULL);
if (errcode)
goto COPT_EXIT;

errcode = COPT_GetDblAttr(prob, COPT_DBLATTR_LPOBJVAL, &lpobjval);
if (errcode)
goto COPT_EXIT;

printf("\nObjective value: %.6f\n", lpobjval);

printf("Variable solution: \n");
for (int i = 0; i < ncol; ++i)
printf("  x[%d] = %.6f\n", i, lpsol[i]);

printf("Variable basis status: \n");
for (int i = 0; i < ncol; ++i)
printf("  x[%d]: %d\n", i, colstat[i]);

free(lpsol);
free(colstat);
}


### 问题与结果文件输出

  // Write problem, solution and modified parameters to files
errcode = COPT_WriteMps(prob, "lp_ex1.mps");
if (errcode)
goto COPT_EXIT;
errcode = COPT_WriteBasis(prob, "lp_ex1.bas");
if (errcode)
goto COPT_EXIT;
errcode = COPT_WriteSol(prob, "lp_ex1.sol");
if (errcode)
goto COPT_EXIT;
errcode = COPT_WriteParam(prob, "lp_ex1.par");
if (errcode)
goto COPT_EXIT;


### 错误处理

    goto COPT_EXIT;
errcode = COPT_WriteParam(prob, "lp_ex1.par");
if (errcode)
goto COPT_EXIT;

// Error handling
COPT_EXIT:
if (errcode)
{
char errmsg[COPT_BUFFSIZE];



### 删除环境与问题

  // Delete problem and environment
COPT_DeleteProb(&prob);

COPT_DeleteEnv(&env);