00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <iostream>
00050 #include "OSiLWriter.h"
00051 #include "OSnl2osil.h"
00052 #include "OSErrorClass.h"
00053
00054
00055 #include "nlp.h"
00056 #include "getstub.h"
00057 #include "r_opn.hd"
00058 #include "opcode.hd"
00059
00060 #define R_OPS ((ASL_fg*)asl)->I.r_ops_
00061 #define OBJ_DE ((ASL_fg*)asl)->I.obj_de_
00062 #define VAR_E ((ASL_fg *) asl) -> I.var_e_
00063 #define CON_DE ((ASL_fg*)asl)->I.con_de_
00064
00065 efunc *r_ops_int[N_OPS];
00066
00067 #include <asl.h>
00068
00069 using std::cerr;
00070 using std::cout;
00071 using std::endl;
00072
00073
00074
00075
00076
00077
00078 OSnl2osil::OSnl2osil(std::string nlfilename){
00079
00080 asl = ASL_alloc( ASL_read_fg);
00081 stub = &nlfilename[ 0];
00082
00083
00084 nl = jac0dim(stub, (fint)strlen(stub));
00085
00086 A_vals = (real *)Malloc(nzc*sizeof(real));
00087
00088 #ifdef AMPLDEBUG
00089 cout << "number of nonzeros = " << nzc << endl;
00090 cout << "number of variable = " << n_var << endl;
00091 cout << "number of constraints = " << n_con << endl;
00092 cout << "number of objs = " << n_obj << endl;
00093 cout << "number of ranges = " << nranges << endl;
00094 cout << "number of equations = " << n_eqn << endl;
00095
00096 #endif
00097 #ifdef AMPLDEBUG
00098 cout << "Start f_read()" << endl;
00099 #endif
00100 X0 = (real *)Malloc( n_var*sizeof(real));
00101 if(N_OPS > 0){
00102 for(int i = 0; i < N_OPS; i++){
00103 r_ops_int[i] = (efunc*)(unsigned long)i;
00104
00105 }
00106 R_OPS = r_ops_int;
00107 want_derivs = 0;
00108 fg_read(nl, 0);
00109 R_OPS = 0;
00110 }
00111 }
00112
00113 OSnl2osil::~OSnl2osil(){
00114 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00115 osinstance->instanceData->linearConstraintCoefficients->rowIdx->bDeleteArrays = false;
00116 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00117 delete osinstance;
00118 osinstance = NULL;
00119 free( X0);
00120 free( A_vals);
00121 ASL_free(&asl);
00122
00123 }
00124
00125 OSnLNode* OSnl2osil::walkTree (expr *e){
00126 OSnLNode *nlNodePoint;
00127 OSnLNodeVariable *nlNodeVariablePoint;
00128 OSnLNodeNumber *nlNodeNumberPoint;
00129 efunc *op;
00130 expr **ep;
00131 int opnum;
00132 int i;
00133
00134 int j = ((expr_v *)e - VAR_E) - osinstance->getVariableNumber() ;
00135
00136 op = e->op;
00137 opnum = Intcast op;
00138
00139
00140 try{
00141 switch( opnum) {
00142 case OPPLUS:
00143
00144 nlNodePoint = new OSnLNodePlus();
00145 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00146 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00147 return nlNodePoint;
00148
00149 case OPSUMLIST:
00150 i = 0;
00151
00152 nlNodePoint = new OSnLNodeSum();
00153 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00154 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00155 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00156 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00157 return nlNodePoint;
00158
00159 case MAXLIST:
00160 i = 0;
00161
00162 nlNodePoint = new OSnLNodeMax();
00163 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00164 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00165 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00166 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00167 return nlNodePoint;
00168
00169 case OPMINUS:
00170 nlNodePoint = new OSnLNodeMinus();
00171 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00172 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00173 return nlNodePoint;
00174
00175 case OPUMINUS:
00176 nlNodePoint = new OSnLNodeNegate();
00177 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00178 return nlNodePoint;
00179
00180 case OPMULT:
00181
00182 nlNodePoint = new OSnLNodeTimes();
00183 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00184 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00185 return nlNodePoint;
00186
00187 case OPDIV:
00188 nlNodePoint = new OSnLNodeDivide();
00189 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00190 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00191 return nlNodePoint;
00192
00193 case OPPOW:
00194
00195 nlNodePoint = new OSnLNodePower();
00196 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00197 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00198 return nlNodePoint;
00199
00200
00201 case OP1POW:
00202
00203
00204 nlNodePoint = new OSnLNodePower();
00205 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00206 nlNodeNumberPoint = new OSnLNodeNumber();
00207 nlNodeNumberPoint->value = e->R.en->v;
00208 nlNodePoint->m_mChildren[1] = nlNodeNumberPoint;
00209 return nlNodePoint;
00210
00211 case OP2POW:
00212
00213
00214
00215
00216
00217
00218 nlNodePoint = new OSnLNodeSquare();
00219 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00220 return nlNodePoint;
00221
00222 case OPCPOW:
00223
00224
00225 nlNodePoint = new OSnLNodePower();
00226 nlNodeNumberPoint = new OSnLNodeNumber();
00227 nlNodeNumberPoint->value = e->L.en->v;
00228 nlNodePoint->m_mChildren[0] = nlNodeNumberPoint;
00229 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00230 return nlNodePoint;
00231
00232 case OP_log:
00233 nlNodePoint = new OSnLNodeLn();
00234 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00235 return nlNodePoint;
00236
00237 case OP_sqrt:
00238 nlNodePoint = new OSnLNodeSqrt();
00239 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00240 return nlNodePoint;
00241
00242 case OP_cos:
00243 nlNodePoint = new OSnLNodeCos();
00244 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00245 return nlNodePoint;
00246
00247 case OP_sin:
00248 nlNodePoint = new OSnLNodeSin();
00249 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00250 return nlNodePoint;
00251
00252 case OP_exp:
00253 nlNodePoint = new OSnLNodeExp();
00254 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00255 return nlNodePoint;
00256
00257 case ABS:
00258 nlNodePoint = new OSnLNodeAbs();
00259 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00260 return nlNodePoint;
00261
00262 case OPNUM:
00263
00264 nlNodeNumberPoint = new OSnLNodeNumber;
00265
00266 nlNodeNumberPoint->value = (double) ((expr_n*)e)->v;
00267 return nlNodeNumberPoint;
00268
00269 case OPVARVAL:
00270
00271
00272 if( j >= 0 ){
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 if(j < ncom0){
00284 struct cexp *common = ((const ASL_fg *) asl) -> I.cexps_ + j ;
00285
00286
00287
00288 int nlin = common -> nlin;
00289
00290
00291 if( nlin > 0) {
00292 nlNodePoint = new OSnLNodeSum();
00293 nlNodePoint->inumberOfChildren = nlin + 1;
00294 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00295
00296
00297 linpart *L = common -> L;
00298 for(int kj = 0; kj < nlin; kj++) {
00299
00300
00301
00302
00303
00304
00305
00306 nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00307 nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00308 nlNodeVariablePoint->coef = L [kj]. fac;
00309 nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00310 }
00311 nlNodePoint->m_mChildren[ nlin] = walkTree( common->e);
00312 return nlNodePoint;
00313 }
00314 else return walkTree( common->e);
00315 }
00316 else{
00317 struct cexp1 *common = ((const ASL_fg *) asl) -> I.cexps1_ + (j - ncom0);
00318
00319
00320
00321 int nlin = common -> nlin;
00322
00323
00324 if( nlin > 0) {
00325 nlNodePoint = new OSnLNodeSum();
00326 nlNodePoint->inumberOfChildren = nlin + 1;
00327 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00328
00329
00330 linpart *L = common -> L;
00331 for(int kj = 0; kj < nlin; kj++) {
00332
00333
00334
00335
00336
00337
00338
00339 nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00340 nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00341 nlNodeVariablePoint->coef = L [kj]. fac;
00342 nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00343 }
00344 nlNodePoint->m_mChildren[ nlin] = walkTree( common->e);
00345 return nlNodePoint;
00346 }
00347 else return walkTree( common->e);
00348 }
00349 }
00350
00351 nlNodeVariablePoint = new OSnLNodeVariable;
00352 nlNodeVariablePoint->idx = e->a;
00353 nlNodeVariablePoint->coef = 1.0;
00354 return nlNodeVariablePoint;
00355 break;
00356 default:
00357 std::ostringstream outStr;
00358 std::string error;
00359 outStr << endl;
00360 outStr << endl;
00361 error = "ERROR: An unsupported operator found, AMPL operator number = " ;
00362 outStr << error;
00363 outStr << opnum;
00364 outStr << endl;
00365 error = outStr.str();
00366 throw ErrorClass( error );
00367 }
00368 }
00369 catch(const ErrorClass& eclass){
00370 throw;
00371 }
00372 }
00373
00374
00375 bool OSnl2osil::createOSInstance(){
00376 osinstance = new OSInstance();
00377 int i, j;
00378
00379
00380 osinstance->setInstanceDescription("Generated from AMPL nl file");
00381
00382
00383
00384
00385
00386 std::string initString;
00387 std::string colName;
00388 double init = OSNAN;
00389 char vartype = 'C';
00390 osinstance->setVariableNumber( n_var);
00391 int firstBinaryVar = n_var - nbv - niv;
00392 int firstIntegerVar = n_var - niv;
00393 for(i = 0; i < n_var; i++){
00394 if(i >= firstBinaryVar) vartype = 'B';
00395 if(i >= firstIntegerVar) vartype = 'I';
00396 if(X0 != NULL) init = X0[ i];
00397 osinstance->addVariable(i, var_name(i),
00398 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00399 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00400 vartype, init, initString);
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 double objWeight = 1.0;
00410
00411 std::string objName="";
00412 SparseVector* objectiveCoefficients = NULL;
00413 objectiveCoefficients = new SparseVector( n_var);
00414 for(i = 0; i < n_var; i++){
00415 objectiveCoefficients->indexes[i] = i;
00416 }
00417 osinstance->setObjectiveNumber( n_obj) ;
00418 for(i = 0; i < n_obj; i++){
00419 for(j = 0; j < n_var; j++){
00420 objectiveCoefficients->values[j] = 0;
00421 }
00422 for(og = Ograd[i]; og; og = og->next){
00423 objectiveCoefficients->values[og->varno] = og->coef;
00424 }
00425 osinstance->addObjective(-n_obj + i, objName,
00426 (objtype[i] == 1)?"max":"min",
00427 objconst( i), objWeight, objectiveCoefficients) ;
00428 }
00429
00430
00431
00432
00433
00434 osinstance->setConstraintNumber( n_con);
00435
00436 double constant = 0.0;
00437 std::string rowName;
00438 for(i = 0; i < n_con; i++)
00439 {
00440 osinstance->addConstraint(i, con_name(i),
00441 LUrhs[2*i] > -OSDBL_MAX ? LUrhs[2*i] : -OSDBL_MAX,
00442 LUrhs[2*i+1] < OSDBL_MAX ? LUrhs[2*i+1] : OSDBL_MAX,
00443 constant);
00444 }
00445 int valuesBegin = 0;
00446 int valuesEnd = A_colstarts[ n_var] - 1;
00447 int startsBegin = 0;
00448 int indexesBegin = 0;
00449 int indexesEnd = A_colstarts[n_var] - 1;
00450
00451
00452 bool bNumAvalsIsPositive = false;
00453 i = valuesBegin;
00454 while( (i < valuesEnd) && (bNumAvalsIsPositive == false) ){
00455 if(A_vals[ i] != 0) bNumAvalsIsPositive = true;
00456 i++;
00457 }
00458 if(bNumAvalsIsPositive == true){
00459 osinstance->setLinearConstraintCoefficients(nzc, true,
00460 A_vals, valuesBegin, valuesEnd,
00461 A_rownos, indexesBegin, indexesEnd,
00462 A_colstarts, startsBegin, n_var);
00463 }
00464
00465
00466
00467
00468
00469
00470 if((nlc + nlo) > 0){
00471 OSnLNode* m_treeRoot;
00472
00473
00474 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nlc + nlo;
00475 osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ nlc + nlo ];
00476 int iNLidx = 0;
00477
00478 if(nlc > 0){
00479 while (iNLidx < nlc) {
00480 m_treeRoot = walkTree ((CON_DE + iNLidx)->e);
00481 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx] = new Nl();
00482 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->idx = iNLidx;
00483 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree = new OSExpressionTree();
00484 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree->m_treeRoot = m_treeRoot;
00485 iNLidx++;
00486
00487 }
00488 }
00489
00490 if(nlo > 0){
00491 while ( iNLidx < nlc + nlo){
00492 m_treeRoot = walkTree ((OBJ_DE + iNLidx - nlc)->e);
00493
00494 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx] = new Nl();
00495 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->idx = -1 - (iNLidx - nlc);
00496 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree = new OSExpressionTree();
00497 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree->m_treeRoot = m_treeRoot;
00498 iNLidx++;
00499
00500 }
00501 }
00502
00503
00504
00505 }
00506 delete objectiveCoefficients;
00507 objectiveCoefficients = NULL;
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 return true;
00518 }