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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// Copyright (C) 2015 - IIT Bombay - FOSSEE
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// Author: Keyur Joshi
// Organization: FOSSEE, IIT Bombay
// Email: toolbox@scilab.in
#include "symphony.h"
#include "sci_iofunc.hpp"
extern sym_environment* global_sym_env; //defined in globals.cpp
extern "C" {
#include "api_scilab.h"
#include "Scierror.h"
#include "sciprint.h"
#include "BOOL.h"
#include <localization.h>
#include <string.h>
//error management variables
static SciErr sciErr;
static int iRet;
//data declarations
static int *varAddress=NULL,numVars,numConstr,*conMatrixColStart=NULL,*conMatrixRowIndex=NULL,*isIntVarBool=NULL,colIter,rowIter,inputMatrixCols,inputMatrixRows;
static double inputDouble,objSense,*objective=NULL,*lowerBounds=NULL,*upperBounds=NULL,*conLower=NULL,*conUpper=NULL,*conRange=NULL,*conRHS=NULL,*conMatrix=NULL;
static char *conType=NULL,*isIntVar=NULL;
//delete all allocd arrays before exit, and return output argument
static void cleanupBeforeExit()
{
if(conMatrixColStart) delete[] conMatrixColStart;
if(conMatrixRowIndex) delete[] conMatrixRowIndex;
if(isIntVar) delete[] isIntVar;
if(conType) delete[] conType;
if(conRange) delete[] conRange;
if(conRHS) delete[] conRHS;
iRet = createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx)+1,0);
if(iRet)
{
/* If error, no return variable */
AssignOutputVariable(pvApiCtx, 1) = 0;
return;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx)+1;
//ReturnArguments(pvApiCtx);
}
static int checkNumArgs()
{
CheckInputArgument(pvApiCtx,10,10);
CheckOutputArgument(pvApiCtx,1,1);
return 1;
}
//both basic and advanced loader use this code
static int commonCodePart1()
{
//ensure that environment is active
if(global_sym_env==NULL)
{
sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n");
return 1;
}
//code to check arguments and get them
if(checkNumArgs()==0)
return 1;
//get input 1: number of variables
if(getUIntFromScilab(1,&numVars))
return 1;
//get input 2: number of constraints
if(getUIntFromScilab(2,&numConstr))
return 1;
//allocate and prepare some arrays
isIntVar=new char[numVars]; //is the variable constrained to be an integer?
conType=new char[numConstr]; //char representing constraint type
conRange=new double[numConstr]; //range of each constraint
conRHS=new double[numConstr]; //RHS to be given to Symphony
return 0;
}
//both basic and advanced loader use this code
static int commonCodePart2()
{
//get input 3: lower bounds of variables
if(getFixedSizeDoubleMatrixFromScilab(3,1,numVars,&lowerBounds))
{
cleanupBeforeExit();
return 1;
}
//get input 4: upper bounds of variables
if(getFixedSizeDoubleMatrixFromScilab(4,1,numVars,&upperBounds))
{
cleanupBeforeExit();
return 1;
}
//get input 5: coefficients of variables in objective function to be minimized
if(getFixedSizeDoubleMatrixFromScilab(5,1,numVars,&objective))
{
cleanupBeforeExit();
return 1;
}
//get input 6: array that specifies wether a variable is constrained to be an integer
sciErr = getVarAddressFromPosition(pvApiCtx, 6, &varAddress);
if (sciErr.iErr)
{
printError(&sciErr, 0);
cleanupBeforeExit();return 1;
}
if ( !isBooleanType(pvApiCtx, varAddress) )
{
Scierror(999, "Wrong type for input argument #6: A matrix of booleans is expected.\n");
cleanupBeforeExit();return 1;
}
sciErr = getMatrixOfBoolean(pvApiCtx, varAddress, &inputMatrixRows, &inputMatrixCols, &isIntVarBool);
if (sciErr.iErr)
{
printError(&sciErr, 0);
cleanupBeforeExit();return 1;
}
if(inputMatrixRows!=1 || inputMatrixCols!=numVars)
{
Scierror(999, "Wrong type for input argument #6: Incorrectly sized matrix.\n");
cleanupBeforeExit();return 1;
}
for(colIter=0;colIter<numVars;colIter++)
{
if(isIntVarBool[colIter])
isIntVar[colIter]=TRUE;
else
isIntVar[colIter]=FALSE;
}
//get input 7: wether to minimize or maximize objective
sciErr = getVarAddressFromPosition(pvApiCtx, 7, &varAddress);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 1;
}
if ( !isDoubleType(pvApiCtx,varAddress) || isVarComplex(pvApiCtx,varAddress) )
{
Scierror(999, "Wrong type for input argument #7: Either 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
return 1;
}
iRet = getScalarDouble(pvApiCtx, varAddress, &objSense);
if(iRet || (objSense!=-1 && objSense!=1))
{
Scierror(999, "Wrong type for input argument #7: Either 1 (sym_minimize) or -1 (sym_maximize) is expected.\n");
return 1;
}
iRet=sym_set_obj_sense(global_sym_env,objSense);
if(iRet==FUNCTION_TERMINATED_ABNORMALLY)
{
Scierror(999, "An error occured.\n");
return 1;
}
//get input 9: constraint lower bound
if(!(numConstr == 0))
{
if(getFixedSizeDoubleMatrixFromScilab(9,numConstr,1,&conLower) )
{
cleanupBeforeExit();
return 1;
}
}
//get input 10: constraint upper bound
if(!(numConstr == 0))
{
if(getFixedSizeDoubleMatrixFromScilab(10,numConstr,1,&conUpper) && (numConstr == 0) )
{
cleanupBeforeExit();
return 1;
}
}
//deduce type of constraint
for(rowIter=0;rowIter<numConstr;rowIter++)
{
if(conLower[rowIter]>conUpper[rowIter])
{
Scierror(999, "Error: the lower bound of constraint %d is more than its upper bound.\n",rowIter);
cleanupBeforeExit();
return 1;
}
#ifdef _MSC_VER
double INFINITY = sym_get_infinity();
#endif
if(conLower[rowIter]==(-INFINITY) && conUpper[rowIter]==INFINITY){
conType[rowIter]='N';
conRange[rowIter]=0;
conRHS[rowIter]=0;
}else if(conLower[rowIter]==(-INFINITY)){
conType[rowIter]='L';
conRange[rowIter]=0;
conRHS[rowIter]=conUpper[rowIter];
}else if(conUpper[rowIter]==INFINITY){
conType[rowIter]='G';
conRange[rowIter]=0;
conRHS[rowIter]=conLower[rowIter];
}else if(conUpper[rowIter]==conLower[rowIter]){
conType[rowIter]='E';
conRange[rowIter]=0;
conRHS[rowIter]=conLower[rowIter];
}else{
conType[rowIter]='R';
conRange[rowIter]=conUpper[rowIter]-conLower[rowIter];
conRHS[rowIter]=conUpper[rowIter];
}
}
/*
//for debug: show all data
sciprint("Vars: %d Constr: %d ObjType: %lf\n",numVars,numConstr,objSense);
for(colIter=0;colIter<numVars;colIter++)
sciprint("Var %d: upper: %lf lower: %lf isInt: %d ObjCoeff: %lf\n",colIter,lowerBounds[colIter],upperBounds[colIter],isIntVar[colIter],objective[colIter]);
for(rowIter=0;rowIter<numConstr;rowIter++)
sciprint("Constr %d: type: %c lower: %lf upper: %lf range: %lf\n",rowIter,conType[rowIter],conLower[rowIter],conRange[rowIter]);
*/
//call problem loader
sym_explicit_load_problem(global_sym_env,numVars,numConstr,conMatrixColStart,conMatrixRowIndex,conMatrix,lowerBounds,upperBounds,isIntVar,objective,NULL,conType,conRHS,conRange,TRUE);
sciprint("Problem loaded into environment.\n");
//code to give output
cleanupBeforeExit();
return 0;
}
//basic problem loader, expects normal matrix. Not suitable for large problems
int sci_sym_loadProblemBasic(char *fname)
{
if(commonCodePart1())
return 1;
if(!(numConstr == 0))
{
//get input 8: matrix of constraint equation coefficients
if(getFixedSizeDoubleMatrixFromScilab(8,numConstr,numVars,&conMatrix) )
{
cleanupBeforeExit();
return 1;
}
}
conMatrixColStart=new int[numVars+1]; //start of each column of constraint matrix, used internally
conMatrixRowIndex=new int[numVars*numConstr]; //index of column elements in each column, used internally
for(colIter=0;colIter<numVars;colIter++)
{
conMatrixColStart[colIter]=colIter*numConstr;
for(rowIter=0;rowIter<numConstr;rowIter++)
{
conMatrixRowIndex[colIter*numConstr+rowIter]=rowIter;
}
}
conMatrixColStart[numVars]=numVars*numConstr;
if(commonCodePart2())
return 1;
return 0;
}
//advanced problem loader, expects sparse matrix. For use with larger problems (>10 vars)
int sci_sym_loadProblem(char *fname)
{
int retVal,nonZeros,*itemsPerRow,*colIndex,matrixIter,newPos,*oldRowIndex,*colStartCopy;
double *data;
if(commonCodePart1())
return 1;
//get input 8: matrix of constraint equation coefficients
sciErr = getVarAddressFromPosition(pvApiCtx, 8, &varAddress);
if (sciErr.iErr)
{
printError(&sciErr, 0);
cleanupBeforeExit();return 1;
}
if(!(numConstr == 0))
{
if ( (!isSparseType(pvApiCtx,varAddress) || isVarComplex(pvApiCtx,varAddress)) && (numConstr == 0))
{
Scierror(999, "Wrong type for input argument #8: A sparse matrix of doubles is expected.\n");
cleanupBeforeExit();return 1;
}
}
sciErr = getSparseMatrix(pvApiCtx,varAddress,&inputMatrixRows,&inputMatrixCols,&nonZeros,&itemsPerRow,&colIndex,&data);
if (sciErr.iErr)
{
printError(&sciErr, 0);
cleanupBeforeExit();return 1;
}
if(inputMatrixRows!=numConstr || inputMatrixCols!=numVars)
{
Scierror(999, "Wrong type for input argument #8: Incorrectly sized matrix.\n");
cleanupBeforeExit();return 1;
}
//convert SciLab format sparse matrix into the format required by Symphony
conMatrix=new double[nonZeros]; //matrix contents
conMatrixColStart=new int[numVars+1]; //where each column of the matrix starts
conMatrixRowIndex=new int[nonZeros]; //row number of each element
oldRowIndex=new int[nonZeros]; //row number in old matrix
colStartCopy=new int[numVars+1]; //temporary copy of conMatrixColStart
for(rowIter=matrixIter=0;rowIter<numConstr;rowIter++) //assign row number to each element in old matrix
for(colIter=0;colIter<itemsPerRow[rowIter];colIter++,matrixIter++)
oldRowIndex[matrixIter]=rowIter;
for(colIter=0;colIter<=numVars;colIter++) //initialize to 0
conMatrixColStart[colIter]=0;
for(matrixIter=0;matrixIter<nonZeros;matrixIter++) //get number of elements in each column
conMatrixColStart[colIndex[matrixIter]]++;
for(colIter=1;colIter<=numVars;colIter++) //perfrom cumulative addition to get final data about where each column starts
{
conMatrixColStart[colIter]+=conMatrixColStart[colIter-1];
colStartCopy[colIter]=conMatrixColStart[colIter];
}
colStartCopy[0]=0;
for(matrixIter=0;matrixIter<nonZeros;matrixIter++) //move data from old matrix to new matrix
{
newPos=colStartCopy[colIndex[matrixIter]-1]++; //calculate its position in the new matrix
conMatrix[newPos]=data[matrixIter]; //move the data
conMatrixRowIndex[newPos]=oldRowIndex[matrixIter]; //assign it its row number
}
retVal=commonCodePart2();
//cleanup some more allocd memory
if(conMatrix) delete[] conMatrix;
if(oldRowIndex) delete[] oldRowIndex;
if(colStartCopy) delete[] colStartCopy;
return retVal;
}
}
|