summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/cbcintlinprog_matrixcpp.cpp
blob: a73abbae5009ce8dfac16bc47853b11e86da68a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// MILP with CBC library, Matrix
// Code Authors: Akshay Miterani and Pranav Deshpande

#include <sci_iofunc.hpp>

// For Branch and bound
#include "OsiSolverInterface.hpp"
#include "CbcModel.hpp"
#include "CbcCutGenerator.hpp"
#include "CbcHeuristicLocal.hpp"
#include "OsiClpSolverInterface.hpp"
extern "C"{
#include <api_scilab.h>
#include "sciprint.h"

int matrix_cppintlinprog(){

    //Objective function
    double* obj;  
    //Constraint matrix coefficients
    double* conMatrix;
    //intcon Matrix
    double* intcon;  
    //Constraints upper bound
    double* conlb;
    //Constraints lower bound
    double* conub;
    //Lower bounds for variables
    double* lb;  
    //Upper bounds for variables
    double* ub;
    //options for maximum iterations and writing mps
    double* options;
    //Flag for Mps
    double flagMps;
    //mps file path
    char * mpsFile;
    //Error structure in Scilab  
    SciErr sciErr;
    //Number of rows and columns in objective function
    int nVars=0, nCons=0,temp1=0,temp2=0;
    int numintcons=0;
    double valobjsense;
    
    CheckInputArgument(pvApiCtx , 11 , 11);             //Checking the input arguments
    CheckOutputArgument(pvApiCtx , 8, 8);               //Checking the output arguments

    ////////// Manage the input argument //////////
    
    //Number of Variables
    if(getIntFromScilab(1,&nVars))
    {
        return 1;
    }

    //Number of Constraints
    if (getIntFromScilab(2,&nCons))
    {
        return 1;
    }

    //Objective function from Scilab
    temp1 = nVars;
    temp2 = nCons;
    if (getFixedSizeDoubleMatrixFromScilab(3,1,temp1,&obj))
    {
        return 1;
    }

    //intcon matrix
    if (getDoubleMatrixFromScilab(4,&numintcons,&temp2,&intcon))
    {
        return 1;
    }

    if (nCons!=0)
    {
        //conMatrix matrix from scilab
        temp1 = nCons;
        temp2 = nVars;

        if (getFixedSizeDoubleMatrixFromScilab(5,temp1,temp2,&conMatrix))
        {
            return 1;
        }

        //conLB matrix from scilab
        temp1 = nCons;
        temp2 = 1;
        if (getFixedSizeDoubleMatrixFromScilab(6,temp1,temp2,&conlb))
        {
            return 1;
        }

        //conUB matrix from scilab
        if (getFixedSizeDoubleMatrixFromScilab(7,temp1,temp2,&conub))
        {
            return 1;
        }

    }

    //lb matrix from scilab
    temp1 = 1;
    temp2 = nVars;
    if (getFixedSizeDoubleMatrixFromScilab(8,temp1,temp2,&lb))
    {
        return 1;
    }


    //ub matrix from scilab
    if (getFixedSizeDoubleMatrixFromScilab(9,temp1,temp2,&ub))
    {
        return 1;
    }

    //Object Sense
    if(getDoubleFromScilab(10,&valobjsense))
    {
        return 1;
    }

    //get options from scilab
    if(getFixedSizeDoubleMatrixFromScilab(11 , 1 , 4 , &options))
    {
        return 1;      
    }

    //------------Temporary Version to make coin packed matrix------
    OsiClpSolverInterface solver1;  
    
    CoinPackedMatrix *matrix =  new CoinPackedMatrix(false , 0 , 0);
    matrix->setDimensions(0 , nVars);
    for(int i=0 ; i<nCons ; i++)
    {
        CoinPackedVector row;
        for(int j=0 ; j<nVars ; j++)
        {
            row.insert(j, conMatrix[i+j*nCons]);
        }
        matrix->appendRow(row);
    }


    solver1.loadProblem(*matrix, lb, ub, obj, conlb, conub);
    
    for(int i=0;i<numintcons;i++)
        solver1.setInteger(intcon[i]-1);

    solver1.setObjSense(valobjsense);

    //-------------------------------------------------------------
    
    CbcModel model(solver1);

    model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
    
    if((int)options[0]!=0)
            model.setIntegerTolerance(options[0]);
    if((int)options[1]!=0)
            model.setMaximumNodes((int)options[1]); 
    if((int)options[2]!=0)
            model.setMaximumSeconds(options[2]);
    if((int)options[3]!=0)
            model.setAllowableGap(options[3]);
    
    model.branchAndBound();
    
    double *val = model.getColSolution();
    
    //Output the solution to Scilab
    
    //get solution for x
    double* xValue = model.getColSolution();

    //get objective value
    double objValue = model.getObjValue();

    //Output status
    double status_=-1;
    if(model.isProvenOptimal()){
        status_=0;
    }
    else if(model.isProvenInfeasible()){
        status_=1;
    }
    else if(model.isSolutionLimitReached()){
        status_=2;
    }
    else if(model. isNodeLimitReached()){
        status_=3;
    }
    else if(model.isAbandoned()){
        status_=4;
    }
    else if(model.isSecondsLimitReached()){
        status_=5;
    }
    else if(model.isContinuousUnbounded()){
        status_=6;
    }
    else if(model.isProvenDualInfeasible()){
        status_=7;
    }
    double nodeCount=model.getNodeCount();
    double nfps=model.numberIntegers();
    double U=model.getObjValue();
    double L=model.getBestPossibleObjValue();
    double iterCount=model.getIterationCount();

    returnDoubleMatrixToScilab(1 , nVars, 1 , xValue);
    returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue);
    returnDoubleMatrixToScilab(3 , 1 , 1 , &status_);
    returnDoubleMatrixToScilab(4 , 1 , 1 , &nodeCount);
    returnDoubleMatrixToScilab(5 , 1 , 1 , &nfps);
    returnDoubleMatrixToScilab(6 , 1 , 1 , &L);
    returnDoubleMatrixToScilab(7 , 1 , 1 , &U);
    returnDoubleMatrixToScilab(8 , 1 , 1 , &iterCount);

    //-------------------------------------------------------------
    
    return 0;
}
}