summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/sci_LinProg.cpp
blob: 6be41cd87bae15bcc51a8ddb316bf73432863cc4 (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
/*
 * Linear Solver Toolbox for Scilab using CLP library
 * Authors :
	Guru Pradeep Reddy
    Bhanu Priya Sayal
*/

#include "sci_iofunc.hpp"
#include"OsiSolverInterface.hpp"
#include "OsiClpSolverInterface.hpp"
#include "CoinPackedMatrix.hpp"
#include "CoinPackedVector.hpp"

extern "C"{
#include <api_scilab.h>
#include <Scierror.h>
#include <localization.h>
#include <sciprint.h>

//Solver function
int sci_linearprog(char *fname) 
{
	//Objective function
	double* obj;  
	//Constraint matrix coefficients
	double* conMatrix;  
	//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;
	//Error structure in Scilab  
	SciErr sciErr;
	//Number of rows and columns in objective function
	int nVars=0, nCons=0,temp1=0,temp2=0;
	
	CheckInputArgument(pvApiCtx , 9 , 9);             //Checking the input arguments
	CheckOutputArgument(pvApiCtx , 6, 6);               //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;
	}

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

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

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

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

	}

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


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

	//get options from scilab
	if(getFixedSizeDoubleMatrixInList(9 , 2 , 1 , 1 , &options))
	{
		return 1;      
	}

	OsiSolverInterface* si = new OsiClpSolverInterface();
   //Defining the constraint matrix
   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);
   	}
	//setting options for maximum iterations
    si->setIntParam(OsiMaxNumIteration,options[0]);

    //Load the problem to OSI
    si->loadProblem(*matrix , lb , ub, obj , conlb , conub);

    //Solve the problem
    si->initialSolve();
	//Output the solution to Scilab
	//get solution for x
	const double* xValue = si->getColSolution();
	for(int i=0;i<nVars;i++)
	{
		sciprint("%lf",xValue[i]);
	}
	//get objective value
	double objValue = si->getObjValue();
	
		//get Status value
	double status_ = 0;
	if(si->isProvenOptimal())
			status_=0;
	else if(si->isProvenPrimalInfeasible())
			status_=1;
	else if(si->isProvenDualInfeasible())
			status_=2;
	else if(si->isIterationLimitReached())
			status_=3;
	else if(si->isAbandoned())
			status_=4;
	else if(si->isPrimalObjectiveLimitReached())
			status_=5;
	else if(si->isDualObjectiveLimitReached())
			status_=6;
		
	//get number of iterations
	double iterations  = si->getIterationCount(); 	
	
	//get reduced cost
	const double* Zl = si->getReducedCost();
	
	//get dual vector
	const double* dual = si->getRowPrice();

	returnDoubleMatrixToScilab(1 , 1 , nVars , xValue);
	returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue);
	returnDoubleMatrixToScilab(3 , 1 , 1 , &status_);
	returnDoubleMatrixToScilab(4 , 1 , 1 , &iterations);
	returnDoubleMatrixToScilab(5 , 1 , nVars , Zl);
	returnDoubleMatrixToScilab(6 , 1 , nCons , dual);
	
	}
}