summaryrefslogtreecommitdiff
path: root/newstructure/sci_gateway/cpp/cbcintlinprog_mpscpp.cpp
blob: 8292ab171b2eebbb56073c2e0ebde0763ba97519 (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
// MILP with CBC library, mps
// Finds the solution by using CBC Library
// 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>

int mps_cppintlinprog()
{
    OsiClpSolverInterface solver;  
    
    // Path to the MPS file
    char *mpsFilePath;

    // Options to set maximum iterations
    double *options;

    // Input - 1 or 2 arguments allowed.
    CheckInputArgument(pvApiCtx, 2, 2);

    // Get the MPS File Path from Scilab
    getStringFromScilab(1, &mpsFilePath);
    
    // Receive the options for setting the maximum number of iterations etc.
    if( getFixedSizeDoubleMatrixFromScilab(2, 1, 4, &options))
    {
        return 1;
    }
    
    // Read the MPS file
    solver.readMps(mpsFilePath);

    // Cbc Library used from here
    CbcModel model(solver);

    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();

    int nVars = model.getNumCols();
    int nCons = model.getNumRows();
    
    const 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;
}

}