My Project
OSMatlabSolverMex.cpp
Go to the documentation of this file.
1
2#include <iostream>
3#include <string>
4#include "mex.h"
5#include "matrix.h"
6
7
8
9// OS includes
10#include "OSDataStructures.h"
11//#include "OSParameters.h"
12#include "OSMatlabSolver.h"
13
14
15
16/* If you are using a compiler that equates NaN to be zero, you must
17 * compile this example using the flag -DNAN_EQUALS_ZERO. For example:
18 *
19 * mex -DNAN_EQUALS_ZERO fulltosparse.c
20 *
21 * This will correctly define the IsNonZero macro for your C compiler.
22 */
23
24#if defined(NAN_EQUALS_ZERO)
25#define IsNonZero(d) ((d)!=0.0 || mxIsNaN(d))
26#else
27#define IsNonZero(d) ((d)!=0.0)
28#endif
29
30using namespace std;
31using std::cout;
32using std::endl;
33
34
35void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
36
37
38
49 int i;
50 double *pr;
51 OSMatlab *matlabModel = new OSMatlab();
52 string sTest = "";
53 char *buf;
54 SparseMatrix* getConstraintMatrix( const mxArray *prhs);
55 //
56 // Check for proper number of input and output arguments
57 mexPrintf("BEGIN PROCESSING DATA\n");
58 if (nrhs != 15) {
59 mexErrMsgTxt("Fifteen input arguments are required.");
60 }
61
62
63 //
64 // get number of variables and number of constraints
65 matlabModel->numVar = (int) mxGetScalar( prhs[ 0]) ;
66 //printf("variable numVar = %i\n", matlabModel->numVar );
67 matlabModel->numCon = (int) mxGetScalar( prhs[ 1]);
68 //
69 // get the constraint matrix
70 // check the data type
71 if (!(mxIsDouble(prhs[2 ]))){
72 mexErrMsgTxt("Constraint matrix A must be of type double.");
73 }
74 // check the dimension
75 if ( (mxGetN(prhs[ 2]) != matlabModel->numVar) || (mxGetM(prhs[2]) != matlabModel->numCon) ){
76 mexErrMsgTxt(" Constraint matrix A must have number of rows equal to number of constraints and columns equal number of variables \n");
77 }
78 matlabModel->sparseMat = getConstraintMatrix( prhs[2]) ;
79 // get the rest of the model
80 //
81 // both bl and bu should equal the number of rows
82 //
83 if( !mxIsEmpty( prhs[ 3]) ){
84 if (mxGetN(prhs[3]) != matlabModel->numCon ){
85 mexErrMsgTxt(" Vector BL size must equal the number of rows\n");
86 }
87 matlabModel->bl = mxGetPr( prhs[ 3]);
88 // convert Matlab -infinity to OS -infinity
89 for(i = 0; i < matlabModel->numCon; i++){
90 if( mxIsInf( -(matlabModel->bl[i]) ) ) matlabModel->bl[ i] = -OSDBL_MAX;
91 }
92 }
93 //
94 //
95 if( !mxIsEmpty( prhs[ 4]) ){
96 if (mxGetN(prhs[4]) != matlabModel->numCon ){
97 mexErrMsgTxt(" Vector BU size must equal the number of rows\n");
98 }
99 matlabModel->bu = mxGetPr( prhs[ 4]);
100 // convert Matlab infinity to OS infinity
101 for(i = 0; i < matlabModel->numCon; i++){
102 if( mxIsInf( matlabModel->bu[i]) ) matlabModel->bu[ i] = OSDBL_MAX;
103 }
104 }
105 //
106 //
107 if (mxGetN(prhs[5]) != matlabModel->numVar ){
108 mexErrMsgTxt(" Vector OBJ size must equal the number of variables\n");
109 }
110 matlabModel->obj = mxGetPr( prhs[ 5]);
111 //
112 //
113 if( !mxIsEmpty( prhs[ 6]) ){
114 if (mxGetN(prhs[6]) != matlabModel->numVar ){
115 mexErrMsgTxt(" Vector VL size must equal the number of variables\n");
116 }
117 matlabModel->vl = mxGetPr( prhs[ 6]);
118 // convert Matlab -infinity to OS -infinity
119 for(i = 0; i < matlabModel->numVar; i++){
120 //printf("variable lb = %f\n", matlabModel->vl[i] );
121 if( mxIsInf( -(matlabModel->vl[i]) ) ) matlabModel->vl[ i] = -OSDBL_MAX;
122 }
123 }
124 //
125 //
126 if( !mxIsEmpty( prhs[ 7]) ){
127 if (mxGetN(prhs[7]) != matlabModel->numVar ){
128 mexErrMsgTxt(" Vector VU size must equal the number of variables\n");
129 }
130 matlabModel->vu = mxGetPr( prhs[ 7]);
131 // convert Matlab infinity to OS infinity
132 for(i = 0; i < matlabModel->numVar; i++){
133 //printf("variable ub = %f\n", matlabModel->vu[i] );
134 if( mxIsInf( matlabModel->vu[i]) ) matlabModel->vu[ i] = OSDBL_MAX;
135 }
136 }
137 //
138 //
139 if ( (mxGetScalar( prhs[ 8]) != 0) && (mxGetScalar( prhs[ 8]) != 1)){
140 mexErrMsgTxt(" The objective type must be either 1 (max) or 0 (min)\n");
141 }
142 mxGetScalar( prhs[ 8]) > 0 ? matlabModel->objType = 1 : matlabModel->objType = 0;
143 //printf("Objective Function Type = %d\n", matlabModel->objType);
144 //
145 // get the variable types, this is character data
146 if(!mxIsChar( prhs[ 9])){
147 mexErrMsgTxt(" Vector VarType must be a character array\n");
148 }
149 if (mxGetN(prhs[ 9]) != matlabModel->numVar ){
150 mexErrMsgTxt(" Vector VarType size must equal the number of variables\n");
151 }
152 buf = mxArrayToString( prhs[ 9]);
153 matlabModel->varType = buf;
154
155 //
156 // get the quadratic terms
157
158 int j;
159 int k = 0;
160 if( !mxIsEmpty( prhs[ 10]) ){
161 if( mxGetM( prhs[ 10]) != 4) mexErrMsgTxt(" Vector Q Must have 4 rows\n");
162 int numQTerms = mxGetN( prhs[ 10]);
163 //printf("NUMBER OF Q TERMS = %d \n", numQTerms);
164 matlabModel->numQTerms = numQTerms;
165 matlabModel->qRows = new int[ numQTerms];
166 matlabModel->qIndex1 = new int[ numQTerms];
167 matlabModel->qIndex2 = new int[ numQTerms];
168 matlabModel->qVal = new double[ numQTerms];
169 pr= mxGetPr( prhs[ 10]);
170 for(i = 0; i < numQTerms; i++){
171 for(j = 0; j <= 3; j++){
172 //printf(" Q COMP = %f\n", pr[ k]) ;
173 switch( j){
174 case 0:
175 matlabModel->qRows[ i] = (int) pr[ k];
176 break;
177 case 1:
178 matlabModel->qIndex1[ i] = (int) pr[ k];
179 break;
180 case 2:
181 matlabModel->qIndex2[ i] = (int) pr[ k];
182 break;
183 case 3:
184 matlabModel->qVal[ i] = pr[ k];
185 break;
186 }
187 k++;
188 }
189 }
190 }
191 if( !mxIsEmpty( prhs[ 11]) ){
192 matlabModel->instanceName = mxArrayToString( prhs[ 11]);
193 }
194 //
195 buf = mxArrayToString( prhs[ 12]);
196 //const char *password = "";
197 //if( strcmp(buf, password) != 0) mexErrMsgTxt(" Incorrect Password\n");
198 //
199 // get the name of the solver
200 matlabModel->sSolverName = mxArrayToString( prhs[ 13]);
201 printf("WE ARE USING SOLVER %s\n", &matlabModel->sSolverName[0]);
202 //
203 // get the name of the solver service
204 matlabModel->sAgentAddress = mxArrayToString( prhs[ 14]);
205 printf("WE ARE USING AGENT %s\n", &matlabModel->sAgentAddress[0]);
206 //
207 // create the OSInstance
208 mexPrintf("CREATE THE INSTANCE \n");
209
210 matlabModel->createOSInstance();
211 mexPrintf("CALL THE REMOTE SERVER \n");
212 sTest = matlabModel->solve();
213 std::string osil = matlabModel->osil;
214 char *ch = &osil[0];
215 printf("HERE IS THE INSTANCE %s\n", ch);
216 mexPrintf("DONE WITH THE REMOTE CALL \n");
217 mexPrintf("HERE IS THE SOLUTION \n");
218 mexPrintf(&sTest[0] );
219 // *str[100];
220 //plhs[0]= mxCreateCharMatrixFromStrings( 1, (const char **)str);
221 //plhs = 'DOES THIS WORK';
222 // garbage collection
223 char *str[ 1];
224 //str[ 0] = mxArrayToString( prhs[ 9]);
225 str[ 0] = &sTest[0] ;
226 plhs[0]= mxCreateCharMatrixFromStrings( 1, (const char **)str);
227 //delete matlabModel;
228 return ;
229}
230SparseMatrix* getConstraintMatrix( const mxArray *prhs){
231 SparseMatrix *sparseMat = NULL;
232
233 sparseMat = new SparseMatrix();
234 /* Declare variable */
235 mxArray *plhs;
236 //mwSize m,n;
237 // mwSize nzmax;
238 //mwIndex *irs, *jcs, j, k;
239 //size_t *irs, *jcs, j, k;
240 int m,n;
241 int nzmax;
242 int *irs, *jcs, j, k;
243 int cmplx;
244 double *pr, *pi, *si, *sr;
245 /* Get the size and pointers to input data */
246 m = mxGetM( prhs);
247 n = mxGetN( prhs);
248 pr = mxGetPr( prhs);
249 pi = mxGetPi( prhs);
250 cmplx = (pi==NULL ? 0 : 1);
251 nzmax = n*m;
252 plhs = mxCreateSparse(m, n, nzmax, (mxComplexity)cmplx);
253 sr = mxGetPr( plhs);
254 si = mxGetPi( plhs);
255 irs = (int*)mxGetIr( plhs);
256 jcs = (int*)mxGetJc( plhs);
257 /* Copy nonzeros */
258 k = 0;
259 for (j=0; (j<n); j++) {
260 int i;
261 jcs[j] = k;
262 for (i=0; (i<m ); i++) {
263 if (IsNonZero(pr[i]) || (cmplx && IsNonZero(pi[i]))) {
264 sr[k] = pr[i];
265 if (cmplx){
266 si[k]=pi[i];
267 }
268 irs[k] = i;
269 k++;
270 }
271 }
272 pr += m;
273 pi += m;
274 }
275 jcs[ n] = k;
276 int km;
277 for(j = 0; j < n; j++){
278 //printf("Column start = %d\n", jcs[ j]);
279 for(km = jcs[ j]; km < jcs[ j+1]; km++ ){
280 // printf("row index= %d\n", irs[ km]);
281 // printf("nonzero value = %f\n", sr[ km]);
282 }
283 }
284 // now fill in a sparse matrix data structure
285 sparseMat->bDeleteArrays = false;
286 sparseMat->isColumnMajor = true;
287 sparseMat->startSize = n + 1;
288 sparseMat->valueSize = jcs[ n];
289 sparseMat->starts = jcs;
290 sparseMat->indexes = irs;
291 sparseMat->values = sr;
292 return sparseMat;
293}
SparseMatrix * getConstraintMatrix(const mxArray *prhs)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
#define IsNonZero(d)
The OSMatlab Class.
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
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
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
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)
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
a sparse matrix data structure
Definition OSGeneral.h:224
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
bool bDeleteArrays
bDeleteArrays is true if we delete the arrays in garbage collection set to true by default
Definition OSGeneral.h:230
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
bool isColumnMajor
isColumnMajor holds whether the coefMatrix (AMatrix) holding linear program data is stored by column.
Definition OSGeneral.h:236
int startSize
startSize is the dimension of the starts array
Definition OSGeneral.h:241
#define OSDBL_MAX