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

#include "sci_iofunc.hpp"
#include "LinCLP.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;
	//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;
	
	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;      
	}

	//Call to the Clp Solver
	LinCLP* Prob = new LinCLP(nVars,nCons,obj,conMatrix,conlb,conub,lb,ub,options);

	//Output the solution to Scilab
	//get solution for x
	double* xValue = Prob->getX();

	//get objective value
	double objValue = Prob->getObjVal();

	//get Status value
	double status = Prob->returnStatus();

	//get number of iterations
	double iterations = Prob->iterCount();

	//get reduced cost
	double* reducedCost = Prob->getReducedCost();

	//get dual vector
	double* dual = Prob->getDual();

	returnDoubleMatrixToScilab(1 , 1 , nVars , xValue);
	returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue);
	returnDoubleMatrixToScilab(3 , 1 , 1 , &status);
	returnDoubleMatrixToScilab(4 , 1 , 1 , &iterations);
	returnDoubleMatrixToScilab(5 , 1 , nVars , reducedCost);
	returnDoubleMatrixToScilab(6 , 1 , nCons , dual);

	}
}