1 | /* $Id$ */ |
---|
2 | /* Copyright (C) 2014, International Business Machines |
---|
3 | Corporation and others. All Rights Reserved. |
---|
4 | This code is licensed under the terms of the Eclipse Public License (EPL). */ |
---|
5 | |
---|
6 | #undef NDEBUG /* force asserts to work */ |
---|
7 | #include "Cbc_C_Interface.h" |
---|
8 | #include <assert.h> |
---|
9 | #include <math.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | |
---|
13 | |
---|
14 | static int callback_called = 0; |
---|
15 | |
---|
16 | void (COINLINKAGE_CB test_callback)(Cbc_Model * model,int msgno, int ndouble, |
---|
17 | const double * dvec, int nint, const int * ivec, |
---|
18 | int nchar, char ** cvec) { |
---|
19 | |
---|
20 | callback_called = 1; |
---|
21 | printf("In callback: message %d\n", msgno); |
---|
22 | |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | void testKnapsack() { |
---|
27 | |
---|
28 | Cbc_Model *model = Cbc_newModel(); |
---|
29 | |
---|
30 | /* Simple knapsack problem |
---|
31 | Maximize 5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5] |
---|
32 | s.t. 2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10 |
---|
33 | All x binary |
---|
34 | */ |
---|
35 | |
---|
36 | CoinBigIndex start[] = {0, 1, 2, 3, 4, 5, 6}; |
---|
37 | int rowindex[] = {0, 0, 0, 0, 0}; |
---|
38 | double value[] = {2, 8, 4, 2, 5}; |
---|
39 | double collb[] = {0,0,0,0,0}; |
---|
40 | double colub[] = {1,1,1,1,1}; |
---|
41 | double obj[] = {5, 3, 2, 7, 4}; |
---|
42 | double feasible[] = {1,1,0,0,0}; |
---|
43 | double rowlb[] = {-INFINITY}; |
---|
44 | double rowub[] = {10}; |
---|
45 | const double *sol; |
---|
46 | const char* setname = "test model"; |
---|
47 | char *getname = malloc(20); |
---|
48 | int i; |
---|
49 | |
---|
50 | printf("Interface reports Cbc version %s\n", Cbc_getVersion()); |
---|
51 | |
---|
52 | Cbc_loadProblem(model, 5, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
53 | |
---|
54 | Cbc_setColName(model, 2, "var2"); |
---|
55 | Cbc_setRowName(model, 0, "constr0"); |
---|
56 | |
---|
57 | |
---|
58 | assert(Cbc_getNumCols(model) == 5); |
---|
59 | assert(Cbc_getNumRows(model) == 1); |
---|
60 | |
---|
61 | for (i = 0; i < 5; i++) { |
---|
62 | Cbc_setInteger(model, i); |
---|
63 | assert(Cbc_isInteger(model,i)); |
---|
64 | } |
---|
65 | |
---|
66 | Cbc_setObjSense(model, -1); |
---|
67 | assert(Cbc_getObjSense(model) == -1); |
---|
68 | |
---|
69 | Cbc_setProblemName(model, setname); |
---|
70 | |
---|
71 | Cbc_registerCallBack(model, test_callback); |
---|
72 | |
---|
73 | Cbc_setInitialSolution(model, feasible); |
---|
74 | |
---|
75 | Cbc_solve(model); |
---|
76 | |
---|
77 | assert(Cbc_isProvenOptimal(model)); |
---|
78 | assert(!Cbc_isAbandoned(model)); |
---|
79 | assert(!Cbc_isProvenInfeasible(model)); |
---|
80 | assert(!Cbc_isContinuousUnbounded(model)); |
---|
81 | assert(!Cbc_isNodeLimitReached(model)); |
---|
82 | assert(!Cbc_isSecondsLimitReached(model)); |
---|
83 | assert(!Cbc_isSolutionLimitReached(model)); |
---|
84 | assert(fabs( Cbc_getObjValue(model)- (16.0) < 1e-6)); |
---|
85 | assert(fabs( Cbc_getBestPossibleObjValue(model)- (16.0) < 1e-6)); |
---|
86 | |
---|
87 | assert(callback_called == 1); |
---|
88 | |
---|
89 | sol = Cbc_getColSolution(model); |
---|
90 | |
---|
91 | assert(fabs(sol[0] - 1.0) < 1e-6); |
---|
92 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|
93 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
94 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
95 | assert(fabs(sol[4] - 1.0) < 1e-6); |
---|
96 | |
---|
97 | Cbc_problemName(model, 20, getname); |
---|
98 | i = strcmp(getname,setname); |
---|
99 | assert( (i == 0) ); |
---|
100 | |
---|
101 | Cbc_getColName(model, 2, getname, 20); |
---|
102 | i = strcmp(getname, "var2"); |
---|
103 | assert( (i == 0) ); |
---|
104 | Cbc_getRowName(model, 0, getname, 20); |
---|
105 | i = strcmp(getname, "constr0"); |
---|
106 | assert( (i == 0) ); |
---|
107 | assert( Cbc_maxNameLength(model) >= 7 ); |
---|
108 | |
---|
109 | Cbc_deleteModel(model); |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | void testIntegerInfeasible() { |
---|
114 | |
---|
115 | Cbc_Model *model = Cbc_newModel(); |
---|
116 | |
---|
117 | /* Minimize x |
---|
118 | * s.t. x <= -10 |
---|
119 | * x binary */ |
---|
120 | |
---|
121 | CoinBigIndex start[] = {0, 1}; |
---|
122 | int rowindex[] = {0}; |
---|
123 | double value[] = {1.0}; |
---|
124 | double rowlb[] = {-INFINITY}; |
---|
125 | double rowub[] = {-10}; |
---|
126 | |
---|
127 | double collb[] = {0.0}; |
---|
128 | double colub[] = {1.0}; |
---|
129 | double obj[] = {1.0}; |
---|
130 | |
---|
131 | Cbc_loadProblem(model, 1, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
132 | |
---|
133 | Cbc_setInteger(model, 0); |
---|
134 | |
---|
135 | assert(Cbc_getNumCols(model) == 1); |
---|
136 | assert(Cbc_getNumRows(model) == 1); |
---|
137 | |
---|
138 | Cbc_solve(model); |
---|
139 | |
---|
140 | assert(!Cbc_isProvenOptimal(model)); |
---|
141 | assert(Cbc_isProvenInfeasible(model)); |
---|
142 | |
---|
143 | Cbc_deleteModel(model); |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | void testIntegerUnbounded() { |
---|
148 | |
---|
149 | Cbc_Model *model = Cbc_newModel(); |
---|
150 | |
---|
151 | /* http://list.coin-or.org/pipermail/cbc/2014-March/001276.html |
---|
152 | * Minimize x |
---|
153 | * s.t. x + y <= 3 |
---|
154 | * x - y == 0 |
---|
155 | * x,y Free |
---|
156 | * x integer */ |
---|
157 | |
---|
158 | CoinBigIndex start[] = {0,2,4}; |
---|
159 | int rowindex[] = {0, 1, 0, 1}; |
---|
160 | double value[] = {1, 1, 1, -1}; |
---|
161 | double rowlb[] = {-INFINITY, 0.0}; |
---|
162 | double rowub[] = {3.0,0.0}; |
---|
163 | double collb[] = {-INFINITY, -INFINITY}; |
---|
164 | double colub[] = {INFINITY, INFINITY}; |
---|
165 | double obj[] = {1.0}; |
---|
166 | |
---|
167 | Cbc_loadProblem(model, 2, 2, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
168 | |
---|
169 | Cbc_setInteger(model, 0); |
---|
170 | |
---|
171 | Cbc_setParameter(model, "log", "0"); |
---|
172 | |
---|
173 | printf("About to solve problem silently. You should see no output except \"Done\".\n"); |
---|
174 | Cbc_solve(model); |
---|
175 | printf("Done\n"); |
---|
176 | |
---|
177 | assert(!Cbc_isProvenOptimal(model)); |
---|
178 | assert(!Cbc_isProvenInfeasible(model)); |
---|
179 | assert(Cbc_isContinuousUnbounded(model)); |
---|
180 | |
---|
181 | Cbc_deleteModel(model); |
---|
182 | |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | int main() { |
---|
188 | |
---|
189 | testKnapsack(); |
---|
190 | testIntegerInfeasible(); |
---|
191 | testIntegerUnbounded(); |
---|
192 | |
---|
193 | return 0; |
---|
194 | } |
---|