source: trunk/Cbc/test/CInterfaceTest.c @ 2020

Last change on this file since 2020 was 2020, checked in by mlubin, 5 years ago

Allow passing arbitrary command-line parameters using the C interface

File size: 3.9 KB
Line 
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
14void testKnapsack() {
15
16    Cbc_Model *model = Cbc_newModel();
17
18    /* Simple knapsack problem
19       Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
20       s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
21       All x binary
22       */
23   
24    CoinBigIndex start[] = {0, 1, 2, 3, 4, 5, 6};
25    int rowindex[] = {0, 0, 0, 0, 0};
26    double value[] = {2, 8, 4, 2, 5};
27    double collb[] = {0,0,0,0,0};
28    double colub[] = {1,1,1,1,1};
29    double obj[] = {5, 3, 2, 7, 4};
30    double rowlb[] = {-INFINITY};
31    double rowub[] = {10};
32    const double *sol;
33    const char* setname = "test model";
34    char *getname = malloc(20);
35    int i;
36
37    Cbc_loadProblem(model, 5, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub);
38
39    assert(Cbc_getNumCols(model) == 5);
40    assert(Cbc_getNumRows(model) == 1);
41
42    for (i = 0; i < 5; i++) {
43        Cbc_setInteger(model, i);
44        assert(Cbc_isInteger(model,i));
45    }
46
47    Cbc_setObjSense(model, -1);
48    assert(Cbc_getObjSense(model) == -1);
49
50    Cbc_setProblemName(model, setname);
51
52    Cbc_solve(model);
53
54    assert(Cbc_isProvenOptimal(model));
55    assert(!Cbc_isAbandoned(model));
56    assert(!Cbc_isProvenInfeasible(model));
57    assert(!Cbc_isContinuousUnbounded(model));
58    assert(!Cbc_isNodeLimitReached(model));
59    assert(!Cbc_isSecondsLimitReached(model));
60    assert(!Cbc_isSolutionLimitReached(model));
61    assert(fabs( Cbc_getObjValue(model)- (16.0) < 1e-6));
62   
63    sol = Cbc_getColSolution(model);
64   
65    assert(fabs(sol[0] - 1.0) < 1e-6);
66    assert(fabs(sol[1] - 0.0) < 1e-6);
67    assert(fabs(sol[2] - 0.0) < 1e-6);
68    assert(fabs(sol[3] - 1.0) < 1e-6);
69    assert(fabs(sol[4] - 1.0) < 1e-6);
70
71    Cbc_problemName(model, 20, getname);
72    i = strcmp(getname,setname);
73    assert( (i == 0) );
74   
75    Cbc_deleteModel(model);
76
77}
78
79void testIntegerInfeasible() {
80
81    Cbc_Model *model = Cbc_newModel();
82
83    /* Minimize x
84     * s.t.     x <= -10
85     * x binary */
86
87    CoinBigIndex start[] = {0, 1};
88    int rowindex[] = {0};
89    double value[] = {1.0};
90    double rowlb[] = {-INFINITY};
91    double rowub[] = {-10};
92
93    double collb[] = {0.0};
94    double colub[] = {1.0};
95    double obj[] = {1.0};
96
97    Cbc_loadProblem(model, 1, 1, start, rowindex, value, collb, colub, obj, rowlb, rowub);
98
99    Cbc_setInteger(model, 0);
100
101    assert(Cbc_getNumCols(model) == 1);
102    assert(Cbc_getNumRows(model) == 1);
103
104    Cbc_solve(model);
105   
106    assert(!Cbc_isProvenOptimal(model));
107    assert(Cbc_isProvenInfeasible(model));
108   
109    Cbc_deleteModel(model);
110
111}
112
113void testIntegerUnbounded() {
114
115    Cbc_Model *model = Cbc_newModel();
116
117    /* http://list.coin-or.org/pipermail/cbc/2014-March/001276.html
118     * Minimize x
119     * s.t. x + y <= 3
120     *      x - y == 0
121     *      x,y Free
122     *      x integer */
123
124    CoinBigIndex start[] = {0,2,4};
125    int rowindex[] = {0, 1, 0, 1};
126    double value[] = {1, 1, 1, -1};
127    double rowlb[] = {-INFINITY, 0.0};
128    double rowub[] = {3.0,0.0};
129    double collb[] = {-INFINITY, -INFINITY};
130    double colub[] = {INFINITY, INFINITY};
131    double obj[] = {1.0};
132
133    Cbc_loadProblem(model, 2, 2, start, rowindex, value, collb, colub, obj, rowlb, rowub);
134
135    Cbc_setInteger(model, 0);
136
137    Cbc_setParameter(model, "log", "0");
138   
139    printf("About to solve problem silently. You should see no output except \"Done\".\n");
140    Cbc_solve(model);
141    printf("Done\n");
142   
143    assert(!Cbc_isProvenOptimal(model));
144    assert(!Cbc_isProvenInfeasible(model));
145    assert(Cbc_isContinuousUnbounded(model));
146
147    Cbc_deleteModel(model);
148
149
150}
151
152
153int main() {
154
155    testKnapsack();
156    testIntegerInfeasible();
157    testIntegerUnbounded();
158
159    return 0;
160}
Note: See TracBrowser for help on using the repository browser.