My Project
OSMatlabSolver.cpp
Go to the documentation of this file.
1/* $Id$ */
16#include <iostream>
17#include <sstream>
18#include <string>
19#include "OSMatlabSolver.h"
20#include "OSConfig.h"
21#include "OSOutput.h"
22
23#ifdef COIN_HAS_LINDO
24#include "OSLindoSolver.h"
25#endif
26
27#ifdef COIN_HAS_IPOPT
28#include "OSIpoptSolver.h"
29#endif
30
31
32using std::endl;
33using std::ostringstream;
34
35
37{
38 sparseMat = NULL;
39 bu = NULL;
40 bl = NULL;
41 obj = NULL;
42 vl = NULL;
43 vu = NULL;
44 objType = 0;
45 osinstance = NULL;
46 varType = NULL;
47 numQTerms = 0;
48 qRows = NULL;
49 qIndex1 = NULL;
50 qIndex2 = NULL;
51 qVal = NULL;
52 numVar = 0;
53 numCon = 0;
54 instanceName=" ";
55 sSolverName="";
57 solverType = NULL;
58}//end OSMatlab
59
60
62{
63 delete osinstance;
64}//end ~OSMatlab
65
66std::string OSMatlab::solve()
67{
68 ostringstream outStr;
69 //std::string osol = "<osol> <optimization> <other name=\"os_solver\">" +
70 //sSolverName + "</other> </optimization></osol>";
71 std::string osol = "";
72 osil = "";
73//
74//
75//
76//
77 try
78 {
79 if(osinstance == NULL ) throw ErrorClass( "there is no problem instance");
80 else
81 {
82 OSiLWriter *osilwriter;
83 osilwriter = new OSiLWriter();
84 osilwriter->m_bWhiteSpace = true;
85 osil = osilwriter->writeOSiL( osinstance);
86 if( sSolverName.find( "lindo") != std::string::npos)
87 {
88 // we are requesting the Lindo solver
89 bool bLindoIsPresent = false;
90#ifdef COIN_HAS_LINDO
91 bLindoIsPresent = true;
92 solverType = new LindoSolver();
93#endif
94 if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
95 }
96 else
97 {
98 if( sSolverName.find( "clp") != std::string::npos)
99 {
100 solverType = new CoinSolver();
101 solverType->sSolverName = "clp";
102 }
103 else
104 {
105 if( sSolverName.find( "cbc") != std::string::npos)
106 {
107 solverType = new CoinSolver();
108 solverType->sSolverName = "cbc";
109 }
110 else
111 {
112 if( sSolverName.find( "cplex") != std::string::npos)
113 {
114 solverType = new CoinSolver();
115 solverType->sSolverName = "cplex";
116 }
117 else
118 {
119 if( sSolverName.find( "glpk") != std::string::npos)
120 {
121 solverType = new CoinSolver();
122 solverType->sSolverName = "glpk";
123
124
125 }
126 else
127 {
128 if( sSolverName.find( "ipopt") != std::string::npos)
129 {
130 // have to act differently since Ipopt uses smart pointers
131 // we are requesting the Ipopt solver
132 bool bIpoptIsPresent = false;
133 if(sAgentAddress == "" )
134 {
135#ifdef COIN_HAS_IPOPT
136 bIpoptIsPresent = true;
137 IpoptSolver *ipoptSolver = new IpoptSolver();
138 ipoptSolver->osol = osol;
139 ipoptSolver->osinstance = osinstance;
140 ipoptSolver->solve();
141 return ipoptSolver->osrl ;
142#endif
143 if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
144 }
145 }
146 else
147 {
148 if( sSolverName.find( "symphony") != std::string::npos)
149 {
150 solverType = new CoinSolver();
151 solverType->sSolverName = "symphony";
152 }
153 else
154 {
155 if( sSolverName.find( "dylp") != std::string::npos)
156 {
157 solverType = new CoinSolver();
158 solverType->sSolverName = "dylp";
159 }
160 else
161 {
162 throw ErrorClass( "a supported solver is not present");
163 }
164 }
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 // do a local solve
173 if( (sSolverName.find( "ipopt") == std::string::npos) && (sAgentAddress == ""))
174 {
175 solverType->osol = osol;
177 solverType->solve();
178 return solverType->osrl ;
179 }
180 }
181 catch(const ErrorClass& eclass)
182 {
183 return eclass.errormsg;
184 }
185
186 // do the following for a remote solve
187 if(sAgentAddress != "")
188 {
189 OSSolverAgent* osagent = NULL;
190 OSiLWriter *osilwriter = NULL;
191 osilwriter = new OSiLWriter();
192 std::string osil = osilwriter->writeOSiL( osinstance);
193 osagent = new OSSolverAgent( sAgentAddress);
194 return osagent->solve(osil, osol);
195 }
196 return "";
197
198}//end solve
199
201{
202 ostringstream outStr;
203
204 int i;
205 osinstance = new OSInstance();
206 //
207 // put in some of the OSInstance <instanceHeader> information
208 //osinstance->setInstanceSource("An example from the LINDO API samples directory");
209 osinstance->setInstanceDescription("A MATLAB Created Problem");
211 //
212 // now put in the OSInstance <instanceData> information
213 //
214 // first the variables
215 std::string *varNames;
216 varNames = new std::string[ numVar];
218 for(i = 0; i < numVar; i++)
219 {
220 outStr << "x";
221 outStr << i ;
222 varNames[ i] = outStr.str();
223 osinstance->addVariable(i, varNames[ i] , vl[ i], vu[ i], varType[ i]);
224 outStr.str("");
225 }
226
227 // now add the objective function
229 // now the coefficient
230 SparseVector *objcoeff;
231 objcoeff = new SparseVector( numVar);
232 objcoeff->indexes = new int[ numVar];
233 objcoeff->values = new double[ numVar];
234 for(i = 0; i < numVar; i++)
235 {
236 objcoeff->indexes[ i] = i;
237 objcoeff->values[ i] = obj[ i];
238 }
239 std::string maxOrMin = "min";
240 if( objType == true) maxOrMin = "max";
241 osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
242
243 // now the constraints
244 std::string *conNames;
245 conNames = new std::string[ numCon];
246 outStr.str("");
248 for(i = 0; i < numCon; i++)
249 {
250 outStr << "r";
251 outStr << i;
252 conNames[ i] = outStr.str();
253 osinstance->addConstraint(i, "row" , bl[ i], bu[ i], 0);
254
255 outStr.str("");
256 }
259 if(numQTerms > 0)
260 {
262 }
263 return;
264}// end createOSInstance
Implements a solve method for the Coin solvers.
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
std::string osol
osol holds the options for the solver
virtual void solve()=0
solve is a virtual function – the actual solvers will implement their own solve method
std::string osrl
osrl holds the solution or result of the model
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
used for throwing exceptions.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
The IpoptSolver class solves problems using Ipopt.
virtual void solve()
solve results in an instance being read into the Ipopt data structures and optimize
the LindoSolver class solves problems using Lindo.
The in-memory representation of an OSiL instance..
bool setConstraintNumber(int number)
set the number of constraints.
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type)
add a variable.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool setQuadraticTermsInNonlinearExpressions(int number, int *rowIndexes, int *varOneIndexes, int *varTwoIndexes, double *coefficients)
set quadratic terms in nonlinearExpressions
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, double *values, int valuesBegin, int valuesEnd, int *indexes, int indexesBegin, int indexesEnd, int *starts, int startsBegin, int startsEnd)
set linear constraint coefficients
bool setInstanceDescription(std::string description)
set the instance description.
bool setInstanceName(std::string name)
set the instance name.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
bool setObjectiveNumber(int number)
set the number of objectives.
bool setVariableNumber(int number)
set the number of variables.
std::string instanceName
instanceName is the name of the problem instance
double * qVal
qVal is a pointer to the coefficient value of each of the quadratic terms.
double * bl
bl is a pointer to the lower bounds on the constraints
double * obj
obj is a pointer to the objective function coefficients
DefaultSolver * solverType
solverType is the a pointer to the sovler that will be requested
int numQTerms
numQTerms is the number of quadratic terms
std::string sAgentAddress
is the address of the solver service
char * varType
varType is a pointer to the variable type eg C, B, I
double * vl
vl is a pointer to the lower bounds on the varialbes
double * bu
bu is a pointer to the upper bounds on the constraints
SparseMatrix * sparseMat
sparseMat is a pointer to an OS Sprase Matrix data structure
void createOSInstance()
Create an OSInstance.
std::string sSolverName
sSolverName is the name of the solver
std::string osil
is the osil instance that gets created from the MATLAB data structures
~OSMatlab()
the OSMatlab class destructor
int * qRows
qRows is a pointer to the row index of each quadratic term
int * qIndex2
qIndex2 is a pointer to the index of the second variable in each of the quadratic terms
OSMatlab()
the OSMatlab class constructor
int * qIndex1
qIndex1 is a pointer to the index of the first variable in each of the quadratic terms
double * vu
vu is a pointer to the upper bounds on the variables
bool objType
objType indicates whether or not we have a max (1) or a min (0)
OSInstance * osinstance
osinstance is a pointer to an OSInstance object that gets created from the MATLAB data structures
int numVar
numVar is the number of variables in the problem
std::string solve()
Solve the problem instance.
int numCon
numCon is the number of constraints in the problem
Used by a client to invoke a remote solver.
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition OSiLWriter.h:30
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
bool m_bWhiteSpace
m_bWhiteSpace is set to true if we write white space in the file
Definition OSiLWriter.h:68
int * indexes
indexes holds an integer array of rowIdx (or colIdx) elements in coefMatrix (AMatrix).
Definition OSGeneral.h:258
int valueSize
valueSize is the dimension of the indexes and values arrays
Definition OSGeneral.h:246
int * starts
starts holds an integer array of start elements in coefMatrix (AMatrix), which points to the start of...
Definition OSGeneral.h:252
double * values
values holds a double array of value elements in coefMatrix (AMatrix), which contains nonzero element...
Definition OSGeneral.h:264
int startSize
startSize is the dimension of the starts array
Definition OSGeneral.h:241
a sparse vector data structure
Definition OSGeneral.h:123
double * values
values holds a double array of nonzero values.
Definition OSGeneral.h:164
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero.
Definition OSGeneral.h:159