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 | #include "Cbc_C_Interface.h" |
---|
7 | #include <assert.h> |
---|
8 | #include <math.h> |
---|
9 | #include <stdio.h> |
---|
10 | |
---|
11 | void testKnapsack() { |
---|
12 | |
---|
13 | Cbc_Model *model = Cbc_newModel(); |
---|
14 | |
---|
15 | /* Simple knapsack problem |
---|
16 | Minimize -5x[1] - 3x[2] - 2x[3] - 7x[4] - 4x[5] |
---|
17 | s.t. 2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10 |
---|
18 | All x binary |
---|
19 | */ |
---|
20 | |
---|
21 | CoinBigIndex start[] = {0, 1, 2, 3, 4, 5, 6}; |
---|
22 | int rowindex[] = {0, 0, 0, 0, 0}; |
---|
23 | double value[] = {2, 8, 4, 2, 5}; |
---|
24 | double collb[] = {0,0,0,0,0}; |
---|
25 | double colub[] = {1,1,1,1,1}; |
---|
26 | double obj[] = {-5, -3, -2, -7, -4}; |
---|
27 | double rowlb[] = {-INFINITY}; |
---|
28 | double rowub[] = {10}; |
---|
29 | char integer[] = {1,1,1,1,1}; |
---|
30 | char *information; |
---|
31 | const double *sol; |
---|
32 | int i; |
---|
33 | |
---|
34 | Cbc_loadProblem(model, 5, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|
35 | |
---|
36 | Cbc_copyInIntegerInformation(model, integer); |
---|
37 | |
---|
38 | assert(Cbc_getNumCols(model) == 5); |
---|
39 | assert(Cbc_getNumRows(model) == 1); |
---|
40 | |
---|
41 | information = Cbc_integerInformation(model); |
---|
42 | for (i = 0; i < 5; i++) { |
---|
43 | assert(information[i] == 1); |
---|
44 | } |
---|
45 | |
---|
46 | assert(Cbc_optimizationDirection(model) == 1); |
---|
47 | |
---|
48 | Cbc_branchAndBound(model); |
---|
49 | |
---|
50 | assert(Cbc_isProvenOptimal(model)); |
---|
51 | assert(abs( Cbc_objectiveValue(model)- (-16.0) < 1e-6)); |
---|
52 | |
---|
53 | sol = Cbc_getColSolution(model); |
---|
54 | |
---|
55 | assert(fabs(sol[0] - 1.0) < 1e-6); |
---|
56 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|
57 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|
58 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|
59 | assert(fabs(sol[4] - 1.0) < 1e-6); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | int main() { |
---|
64 | |
---|
65 | testKnapsack(); |
---|
66 | |
---|
67 | return 0; |
---|
68 | } |
---|