[2009] | 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 <coin/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 | CoinBigIndex start[] = {0, 1, 2, 3, 4, 5, 6}; |
---|

| 21 | int rowindex[] = {0, 0, 0, 0, 0}; |
---|

| 22 | double value[] = {2, 8, 4, 2, 5}; |
---|

| 23 | double collb[] = {0,0,0,0,0}; |
---|

| 24 | double colub[] = {1,1,1,1,1}; |
---|

| 25 | double obj[] = {-5, -3, -2, -7, -4}; |
---|

| 26 | double rowlb[] = {-INFINITY}; |
---|

| 27 | double rowub[] = {10}; |
---|

| 28 | char integer[] = {1,1,1,1,1}; |
---|

| 29 | char *information; |
---|

| 30 | const double *sol; |
---|

| 31 | int i; |
---|

| 32 | |
---|

| 33 | Cbc_loadProblem(model, 5, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub); |
---|

| 34 | |
---|

| 35 | Cbc_copyInIntegerInformation(model, integer); |
---|

| 36 | |
---|

| 37 | assert(Cbc_getNumCols(model) == 5); |
---|

| 38 | assert(Cbc_getNumRows(model) == 1); |
---|

| 39 | |
---|

| 40 | information = Cbc_integerInformation(model); |
---|

| 41 | for (i = 0; i < 5; i++) { |
---|

| 42 | assert(information[i] == 1); |
---|

| 43 | } |
---|

| 44 | |
---|

| 45 | assert(Cbc_optimizationDirection(model) == 1); |
---|

| 46 | |
---|

| 47 | Cbc_branchAndBound(model); |
---|

| 48 | |
---|

| 49 | assert(Cbc_isProvenOptimal(model)); |
---|

| 50 | assert(abs( Cbc_objectiveValue(model)- (-16.0) < 1e-6)); |
---|

| 51 | |
---|

| 52 | sol = Cbc_getColSolution(model); |
---|

| 53 | |
---|

| 54 | assert(fabs(sol[0] - 1.0) < 1e-6); |
---|

| 55 | assert(fabs(sol[1] - 0.0) < 1e-6); |
---|

| 56 | assert(fabs(sol[2] - 0.0) < 1e-6); |
---|

| 57 | assert(fabs(sol[3] - 1.0) < 1e-6); |
---|

| 58 | assert(fabs(sol[4] - 1.0) < 1e-6); |
---|

| 59 | |
---|

| 60 | } |
---|

| 61 | |
---|

| 62 | int main() { |
---|

| 63 | |
---|

| 64 | testKnapsack(); |
---|

| 65 | |
---|

| 66 | return 0; |
---|

| 67 | } |
---|