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

#include "LinCLP.hpp"
extern "C"{
#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include "sciprint.h"
#include "sci_iofunc.hpp"

//creating a problem pointer using Base class of OsiSolverInterface and
//Instantiate the object using specific derived class of ClpSolver
OsiSolverInterface* si = new OsiClpSolverInterface();

LinCLP::~LinCLP()
		 {
		 	free(objMatrix_);
		 	free(conMatrix_);
		 	free(conlb_);
		 	free(conub_);
		 	free(lb_);
		 	free(ub_);
			free(xValue_);
			free(reducedCost_);
			free(dual_);}

//Clp Solver function definition
LinCLP::LinCLP(int numVars_ , int numCons_ ,double objMatrix_[] , double conMatrix_[] , double conlb_[] , double conub_[] ,double lb_[] , double ub_[], double options_[])
{
   
   //Defining the constraint matrix
   CoinPackedMatrix *matrix =  new CoinPackedMatrix(false , 0 , 0);
   matrix->setDimensions(0 , numVars_);
   for(int i=0 ; i<numCons_ ; i++)
   	{
    	CoinPackedVector row;
	 	for(int j=0 ; j<numVars_ ; j++)
	 		{
   				row.insert(j, conMatrix_[i+j*numCons_]);
   	 		}
			
        matrix->appendRow(row);
   	}

   //setting options for maximum iterations
   si->setIntParam(OsiMaxNumIteration,options_[0]);

   //Load the problem to OSI
   si->loadProblem(*matrix , lb_ , ub_, objMatrix_ , conlb_ , conub_);

   //Solve the problem
   si->initialSolve();
  
}

   //Output the solution to Scilab
   //get solution for x
   const double* LinCLP::getX()
	{ 
        xValue_ = si->getColSolution();
		return xValue_;
	}

   //get objective value
   double LinCLP::getObjVal()
	{
		objValue_ = si->getObjValue();
		return objValue_;
	}
   
   //get exit status 
   int LinCLP::returnStatus()
	{
   		status_;
   		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;
		return status_;
	}

   //get number of iterations
   double LinCLP::iterCount()
	{
		iterations_ = si->getIterationCount(); 
		return iterations_;
	}

   //get lower vector
   const double* LinCLP::getReducedCost()
	{
		reducedCost_ = si->getReducedCost();
		return reducedCost_;
	}

   //get dual vector
   double* LinCLP::getDual()
	{
		dual_ = si->getRowPrice();
        return dual_;
	}

}