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 | /* |
---|
114 | void testProblemModification() { |
---|
115 | |
---|
116 | Cbc_Model *model = Cbc_newModel(); |
---|
117 | |
---|
118 | / * Simple knapsack problem |
---|
119 | Maximize 5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5] |
---|
120 | s.t. 2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10 |
---|
121 | All x binary |
---|
122 | * / |
---|
123 | |
---|
124 | CoinBigIndex start[] = {0, 1, 2, 3, 4, 5, 6}; |
---|
125 | int rowindex[] = {0, 0, 0, 0, 0}; |
---|
126 | double value[] = {2, 8, 4, 2, 5}; |
---|
127 | double collb[] = {0,0,0,0,0}; |
---|
128 | double colub[] = {1,1,1,1,1}; |
---|
129 | double obj[] = {5, 3, 2, 7, 4}; |
---|
130 | double rowlb[] = {-INFINITY}; |
---|
131 | double rowub[] = {10}; |
---|
132 | const double *sol; |
---|
133 | int i; |
---|
134 | |
---|
135 | printf("Interface reports Cbc version %s\n", Cbc_getVersion()); |
---|
136 | |
---|
137 | Cbc_loadProblem(model, 5, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
138 | |
---|
139 | for (i = 0; i < 5; i++) { |
---|
140 | Cbc_setInteger(model, i); |
---|
141 | assert(Cbc_isInteger(model,i)); |
---|
142 | } |
---|
143 | |
---|
144 | Cbc_setObjSense(model, -1); |
---|
145 | assert(Cbc_getObjSense(model) == -1); |
---|
146 | |
---|
147 | Cbc_solve(model); |
---|
148 | |
---|
149 | assert(Cbc_isProvenOptimal(model)); |
---|
150 | assert(fabs( Cbc_getObjValue(model)- (16.0) < 1e-6)); |
---|
151 | |
---|
152 | sol = Cbc_getColSolution(model); |
---|
153 | |
---|
154 | assert(fabs(sol[0] - 1.0) < 1e-6); |
---|
155 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|
156 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
157 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
158 | assert(fabs(sol[4] - 1.0) < 1e-6); |
---|
159 | |
---|
160 | Cbc_setColUpper(model, 0, 0.0); |
---|
161 | Cbc_solve(model); |
---|
162 | |
---|
163 | assert(Cbc_isProvenOptimal(model)); |
---|
164 | assert(fabs( Cbc_getObjValue(model)- (11.0) < 1e-6)); |
---|
165 | |
---|
166 | sol = Cbc_getColSolution(model); |
---|
167 | |
---|
168 | assert(fabs(sol[0] - 0.0) < 1e-6); |
---|
169 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|
170 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
171 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
172 | assert(fabs(sol[4] - 1.0) < 1e-6); |
---|
173 | |
---|
174 | |
---|
175 | Cbc_setColLower(model, 1, 1.0); |
---|
176 | |
---|
177 | assert(Cbc_isProvenOptimal(model)); |
---|
178 | assert(fabs( Cbc_getObjValue(model)- (10.0) < 1e-6)); |
---|
179 | |
---|
180 | sol = Cbc_getColSolution(model); |
---|
181 | |
---|
182 | assert(fabs(sol[0] - 0.0) < 1e-6); |
---|
183 | assert(fabs(sol[1] - 1.0) < 1e-6); |
---|
184 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
185 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
186 | assert(fabs(sol[4] - 0.0) < 1e-6); |
---|
187 | |
---|
188 | |
---|
189 | Cbc_deleteModel(model); |
---|
190 | |
---|
191 | } |
---|
192 | */ |
---|
193 | |
---|
194 | |
---|
195 | void testSOS() { |
---|
196 | |
---|
197 | Cbc_Model *model = Cbc_newModel(); |
---|
198 | |
---|
199 | /* |
---|
200 | Maximize 5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5] |
---|
201 | s.t. x[1] + x[2] + x[3] + x[4] + x[5] == 1 |
---|
202 | All x binary |
---|
203 | */ |
---|
204 | |
---|
205 | CoinBigIndex start[] = {0, 0, 0, 0, 0, 0, 0}; |
---|
206 | double collb[] = {0,0,0,0,0}; |
---|
207 | double colub[] = {1,1,1,1,1}; |
---|
208 | double obj[] = {5, 3, 2, 7, 4}; |
---|
209 | int sosrowstart[] = {0,5}; |
---|
210 | int soscolindex[] = {0,1,2,3,4}; |
---|
211 | const double *sol; |
---|
212 | int i; |
---|
213 | |
---|
214 | Cbc_loadProblem(model, 5, 0, start, NULL, NULL, collb, colub, obj, NULL, NULL); |
---|
215 | |
---|
216 | assert(Cbc_getNumCols(model) == 5); |
---|
217 | assert(Cbc_getNumRows(model) == 0); |
---|
218 | |
---|
219 | for (i = 0; i < 5; i++) { |
---|
220 | Cbc_setInteger(model, i); |
---|
221 | assert(Cbc_isInteger(model,i)); |
---|
222 | } |
---|
223 | |
---|
224 | Cbc_setObjSense(model, -1); |
---|
225 | assert(Cbc_getObjSense(model) == -1); |
---|
226 | |
---|
227 | Cbc_addSOS(model,1,sosrowstart,soscolindex,obj,1); |
---|
228 | |
---|
229 | Cbc_solve(model); |
---|
230 | |
---|
231 | assert(Cbc_isProvenOptimal(model)); |
---|
232 | assert(!Cbc_isAbandoned(model)); |
---|
233 | assert(!Cbc_isProvenInfeasible(model)); |
---|
234 | assert(!Cbc_isContinuousUnbounded(model)); |
---|
235 | assert(!Cbc_isNodeLimitReached(model)); |
---|
236 | assert(!Cbc_isSecondsLimitReached(model)); |
---|
237 | assert(!Cbc_isSolutionLimitReached(model)); |
---|
238 | assert(fabs( Cbc_getObjValue(model)- (7.0) < 1e-6)); |
---|
239 | assert(fabs( Cbc_getBestPossibleObjValue(model)- (7.0) < 1e-6)); |
---|
240 | |
---|
241 | sol = Cbc_getColSolution(model); |
---|
242 | |
---|
243 | assert(fabs(sol[0] - 0.0) < 1e-6); |
---|
244 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|
245 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
246 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
247 | assert(fabs(sol[4] - 0.0) < 1e-6); |
---|
248 | |
---|
249 | Cbc_deleteModel(model); |
---|
250 | |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | void testIntegerInfeasible() { |
---|
255 | |
---|
256 | Cbc_Model *model = Cbc_newModel(); |
---|
257 | |
---|
258 | /* Minimize x |
---|
259 | * s.t. x <= -10 |
---|
260 | * x binary */ |
---|
261 | |
---|
262 | CoinBigIndex start[] = {0, 1}; |
---|
263 | int rowindex[] = {0}; |
---|
264 | double value[] = {1.0}; |
---|
265 | double rowlb[] = {-INFINITY}; |
---|
266 | double rowub[] = {-10}; |
---|
267 | |
---|
268 | double collb[] = {0.0}; |
---|
269 | double colub[] = {1.0}; |
---|
270 | double obj[] = {1.0}; |
---|
271 | |
---|
272 | Cbc_loadProblem(model, 1, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
273 | |
---|
274 | Cbc_setInteger(model, 0); |
---|
275 | |
---|
276 | assert(Cbc_getNumCols(model) == 1); |
---|
277 | assert(Cbc_getNumRows(model) == 1); |
---|
278 | |
---|
279 | Cbc_solve(model); |
---|
280 | |
---|
281 | assert(!Cbc_isProvenOptimal(model)); |
---|
282 | assert(Cbc_isProvenInfeasible(model)); |
---|
283 | |
---|
284 | Cbc_deleteModel(model); |
---|
285 | |
---|
286 | } |
---|
287 | |
---|
288 | void testIntegerUnbounded() { |
---|
289 | |
---|
290 | Cbc_Model *model = Cbc_newModel(); |
---|
291 | |
---|
292 | /* http://list.coin-or.org/pipermail/cbc/2014-March/001276.html |
---|
293 | * Minimize x |
---|
294 | * s.t. x + y <= 3 |
---|
295 | * x - y == 0 |
---|
296 | * x,y Free |
---|
297 | * x integer */ |
---|
298 | |
---|
299 | CoinBigIndex start[] = {0,2,4}; |
---|
300 | int rowindex[] = {0, 1, 0, 1}; |
---|
301 | double value[] = {1, 1, 1, -1}; |
---|
302 | double rowlb[] = {-INFINITY, 0.0}; |
---|
303 | double rowub[] = {3.0,0.0}; |
---|
304 | double collb[] = {-INFINITY, -INFINITY}; |
---|
305 | double colub[] = {INFINITY, INFINITY}; |
---|
306 | double obj[] = {1.0,0.0}; |
---|
307 | |
---|
308 | Cbc_loadProblem(model, 2, 2, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
309 | |
---|
310 | Cbc_setInteger(model, 0); |
---|
311 | |
---|
312 | Cbc_setParameter(model, "log", "0"); |
---|
313 | |
---|
314 | printf("About to solve problem silently. You should see no output except \"Done\".\n"); |
---|
315 | Cbc_solve(model); |
---|
316 | printf("Done\n"); |
---|
317 | |
---|
318 | assert(!Cbc_isProvenOptimal(model)); |
---|
319 | assert(!Cbc_isProvenInfeasible(model)); |
---|
320 | assert(Cbc_isContinuousUnbounded(model)); |
---|
321 | |
---|
322 | Cbc_deleteModel(model); |
---|
323 | |
---|
324 | |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | int main() { |
---|
329 | |
---|
330 | printf("Knapsack test\n"); |
---|
331 | testKnapsack(); |
---|
332 | printf("SOS test\n"); |
---|
333 | testSOS(); |
---|
334 | printf("Infeasible test\n"); |
---|
335 | testIntegerInfeasible(); |
---|
336 | printf("Unbounded test\n"); |
---|
337 | testIntegerUnbounded(); |
---|
338 | /*printf("Problem modification test\n"); |
---|
339 | testProblemModification();*/ |
---|
340 | |
---|
341 | return 0; |
---|
342 | } |
---|