00001
00022 #include <iostream>
00023 #include "OSiLWriter.h"
00024 #include "OSnl2osil.h"
00025 #include "OSErrorClass.h"
00026
00027 #include "CoinHelperFunctions.hpp"
00028
00029
00030 #include "nlp.h"
00031 #include "getstub.h"
00032 #include "r_opn.hd"
00033 #include "opcode.hd"
00034
00035
00036 #ifdef HAVE_CMATH
00037 # include <cmath>
00038 #else
00039 # ifdef HAVE_CMATH_H
00040 # include <cmath.h>
00041 # endif
00042 #endif
00043
00044 #include <sstream>
00045
00046
00047 #define R_OPS ((ASL_fg*)asl)->I.r_ops_
00048 #define OBJ_DE ((ASL_fg*)asl)->I.obj_de_
00049 #define VAR_E ((ASL_fg*)asl)->I.var_e_
00050 #define CON_DE ((ASL_fg*)asl)->I.con_de_
00051
00052
00053 #include <asl.h>
00054
00055 using std::cerr;
00056 using std::cout;
00057 using std::endl;
00058
00059 #ifdef HAVE_STDINT_H
00060 #include <stdint.h>
00061 #endif
00062
00063 struct cgrad;
00064
00065
00066
00067 OSnl2osil::OSnl2osil(std::string nlfilename)
00068 : osinstance(0), stub(nlfilename)
00069 {
00070 efunc *r_ops_int[N_OPS];
00071 FILE *nl;
00072
00073
00074 asl = cw = ASL_alloc( ASL_read_fg);
00075
00076
00077 nl = jac0dim(const_cast<char*>(stub.c_str()), (fint)stub.length());
00078
00079
00080 A_vals = (real *)Malloc(nzc*sizeof(real));
00081
00082 #ifdef AMPLDEBUG
00083 cout << "number of nonzeros = " << nzc << endl;
00084 cout << "number of variables = " << n_var << endl;
00085 cout << "number of constraints = " << n_con << endl;
00086 cout << "number of objectives = " << n_obj << endl;
00087 cout << "number of ranges = " << nranges << endl;
00088 cout << "number of equations = " << n_eqn << endl;
00089 #endif
00090 if(N_OPS > 0)
00091 {
00092 for(int i = 0; i < N_OPS; i++)
00093 {
00094 r_ops_int[i] = (efunc*)(unsigned long)i;
00095 }
00096 R_OPS = r_ops_int;
00097 want_derivs = 0;
00098 fg_read(nl, 0);
00099 R_OPS = 0;
00100 }
00101
00102
00103 asl = rw = ASL_alloc( ASL_read_fg);
00104 nl = jac0dim((char*)stub.c_str(), (fint)stub.length());
00105 want_derivs = 0;
00106 qp_read(nl, 0);
00107
00108 asl = cw;
00109 numkount = 0;
00110 }
00111
00112 OSnl2osil::~OSnl2osil()
00113 {
00114 delete osinstance;
00115 osinstance = NULL;
00116
00117 free( A_vals);
00118 ASL_free(&cw);
00119 ASL_free(&rw);
00120 }
00121
00122 OSnLNode* OSnl2osil::walkTree (expr *e)
00123 {
00124 OSnLNode *nlNodePoint;
00125 OSnLNodeVariable *nlNodeVariablePoint;
00126 OSnLNodeNumber *nlNodeNumberPoint;
00127 efunc *op;
00128 expr **ep;
00129 int opnum;
00130 int i;
00131
00132 int j = ((expr_v *)e - VAR_E) - osinstance->getVariableNumber() ;
00133
00134 op = e->op;
00135 opnum = Intcast op;
00136
00137
00138 try
00139 {
00140 switch( opnum)
00141 {
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 op_type.push_back( "PLUS");
00148 return nlNodePoint;
00149
00150 case OPSUMLIST:
00151 i = 0;
00152
00153 nlNodePoint = new OSnLNodeSum();
00154 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00155 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00156 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00157 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00158 return nlNodePoint;
00159
00160 case MAXLIST:
00161 i = 0;
00162
00163 nlNodePoint = new OSnLNodeMax();
00164 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00165 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00166 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00167 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00168 return nlNodePoint;
00169
00170 case OPMINUS:
00171 nlNodePoint = new OSnLNodeMinus();
00172
00173 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00174 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00175 op_type.push_back( "MINUS");
00176
00177 return nlNodePoint;
00178
00179 case OPUMINUS:
00180 nlNodePoint = new OSnLNodeNegate();
00181 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00182 return nlNodePoint;
00183
00184 case OPMULT:
00185
00186 nlNodePoint = new OSnLNodeTimes();
00187 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00188 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00189 op_type.push_back( "TIMES");
00190 return nlNodePoint;
00191
00192 case OPDIV:
00193 nlNodePoint = new OSnLNodeDivide();
00194 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00195 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00196 return nlNodePoint;
00197
00198 case OPPOW:
00199
00200 nlNodePoint = new OSnLNodePower();
00201 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00202 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00203 op_type.push_back( "POWER");
00204 return nlNodePoint;
00205
00206
00207 case OP1POW:
00208
00209
00210 nlNodePoint = new OSnLNodePower();
00211
00212
00213 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00214 nlNodeNumberPoint = new OSnLNodeNumber();
00215 nlNodeNumberPoint->value = e->R.en->v;
00216 nlNodePoint->m_mChildren[1] = nlNodeNumberPoint;
00217
00218 op_type.push_back( "NUMBER");
00219 op_type.push_back( os_dtoa_format( numkount) );
00220 operand.push_back( e->R.en->v );
00221 numkount++;
00222
00223 op_type.push_back( "POWER");
00224
00225
00226 return nlNodePoint;
00227
00228 case OP2POW:
00229
00230
00231
00232
00233
00234
00235 nlNodePoint = new OSnLNodeSquare();
00236
00237 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00238 op_type.push_back( "SQUARE");
00239 return nlNodePoint;
00240
00241 case OPCPOW:
00242
00243
00244 nlNodePoint = new OSnLNodePower();
00245 nlNodeNumberPoint = new OSnLNodeNumber();
00246 nlNodeNumberPoint->value = e->L.en->v;
00247 nlNodePoint->m_mChildren[0] = nlNodeNumberPoint;
00248 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00249 return nlNodePoint;
00250
00251 case OP_log:
00252 nlNodePoint = new OSnLNodeLn();
00253 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00254 op_type.push_back( "LOG");
00255 return nlNodePoint;
00256
00257 case OP_sqrt:
00258 nlNodePoint = new OSnLNodeSqrt();
00259 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00260 return nlNodePoint;
00261
00262 case OP_cos:
00263 nlNodePoint = new OSnLNodeCos();
00264 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00265 return nlNodePoint;
00266
00267 case OP_sin:
00268 nlNodePoint = new OSnLNodeSin();
00269 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00270 return nlNodePoint;
00271
00272 case OP_exp:
00273 nlNodePoint = new OSnLNodeExp();
00274 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00275 return nlNodePoint;
00276
00277 case ABS:
00278 nlNodePoint = new OSnLNodeAbs();
00279 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00280 return nlNodePoint;
00281
00282 case OPNUM:
00283
00284 nlNodeNumberPoint = new OSnLNodeNumber;
00285
00286 nlNodeNumberPoint->value = (double) ((expr_n*)e)->v;
00287 op_type.push_back( "NUMBER");
00288 op_type.push_back( os_dtoa_format( numkount ) );
00289
00290 operand.push_back( (double) ((expr_n*)e)->v );
00291 numkount++;
00292
00293
00294 return nlNodeNumberPoint;
00295
00296 case OPVARVAL:
00297
00298
00299 if( j >= 0 )
00300 {
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 if(j < ncom0)
00314 {
00315 struct cexp *common = ((const ASL_fg *) asl) -> I.cexps_ + j ;
00316
00317
00318
00319 int nlin = common -> nlin;
00320
00321
00322 if( nlin > 0)
00323 {
00324 nlNodePoint = new OSnLNodeSum();
00325 nlNodePoint->inumberOfChildren = nlin + 1;
00326 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00327
00328
00329 linpart *L = common -> L;
00330 for(int kj = 0; kj < nlin; kj++)
00331 {
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 else
00350 {
00351 struct cexp1 *common = ((const ASL_fg *) asl) -> I.cexps1_ + (j - ncom0);
00352
00353
00354
00355 int nlin = common -> nlin;
00356
00357
00358 if( nlin > 0)
00359 {
00360 nlNodePoint = new OSnLNodeSum();
00361 nlNodePoint->inumberOfChildren = nlin + 1;
00362 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00363
00364
00365 linpart *L = common -> L;
00366 for(int kj = 0; kj < nlin; kj++)
00367 {
00368
00369
00370
00371
00372
00373
00374
00375 nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00376 nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00377 nlNodeVariablePoint->coef = L [kj]. fac;
00378 nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00379 }
00380 nlNodePoint->m_mChildren[ nlin] = walkTree( common->e);
00381 return nlNodePoint;
00382 }
00383 else return walkTree( common->e);
00384 }
00385 }
00386
00387 nlNodeVariablePoint = new OSnLNodeVariable;
00388 nlNodeVariablePoint->idx = e->a;
00389 nlNodeVariablePoint->coef = 1.0;
00390
00391 op_type.push_back( "VARIABLE");
00392 op_type.push_back( os_dtoa_format( e->a ) );
00393
00394
00395 return nlNodeVariablePoint;
00396 break;
00397 default:
00398 std::ostringstream outStr;
00399 std::string error;
00400 outStr << endl;
00401 outStr << endl;
00402 error = "ERROR: An unsupported operator found, AMPL operator number = " ;
00403 outStr << error;
00404 outStr << opnum;
00405 outStr << endl;
00406 error = outStr.str();
00407 throw ErrorClass( error );
00408 }
00409 }
00410 catch(const ErrorClass& eclass)
00411 {
00412 throw;
00413 }
00414 }
00415
00416 static inline char integerOrBinary(real upper, real lower)
00417 {
00418 if (lower > -1.0 + OS_EPS && upper < 2.0 - OS_EPS)
00419 return 'B';
00420 return 'I';
00421 }
00422
00423 bool OSnl2osil::createOSInstance()
00424 {
00425 int *A_rowstarts = NULL;
00426 int *A_colptr = NULL;
00427 double *A_nzelem = NULL;
00428 osinstance = new OSInstance();
00429 int i, j;
00430
00431
00432
00433 osinstance->setInstanceDescription("Generated from AMPL nl file");
00434
00435
00436
00437
00438
00439 std::string colName;
00440 char vartype = 'C';
00441 osinstance->setVariableNumber( n_var);
00442
00443
00444
00445
00446 int lower;
00447 int upper;
00448 lower = 0;
00449 upper = nlvb - nlvbi;
00450 #ifdef AMPLDEBUG
00451 std::cout << "LOWER = " << lower << std::endl;
00452 std::cout << "UPPER = " << upper << std::endl;
00453 #endif
00454 for(i = lower; i < upper; i++)
00455 {
00456 vartype = 'C';
00457 osinstance->addVariable(i, var_name(i),
00458 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00459 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00460 vartype);
00461 }
00462
00463 lower = nlvb - nlvbi;
00464 upper = (nlvb - nlvbi) + nlvbi;
00465 upper = nlvb;
00466 #ifdef AMPLDEBUG
00467 std::cout << "LOWER = " << lower << std::endl;
00468 std::cout << "UPPER = " << upper << std::endl;
00469 #endif
00470 for(i = lower; i < upper; i++)
00471 {
00472 vartype = integerOrBinary(LUv[2*i], LUv[2*i+1]);
00473 osinstance->addVariable(i, var_name(i),
00474 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00475 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00476 vartype);
00477 }
00478
00479 lower = nlvb;
00480 upper = nlvb + (nlvc - (nlvb + nlvci)) ;
00481 upper = nlvc - nlvci;
00482 #ifdef AMPLDEBUG
00483 std::cout << "LOWER = " << lower << std::endl;
00484 std::cout << "UPPER = " << upper << std::endl;
00485 #endif
00486 for(i = lower; i < upper; i++)
00487 {
00488 vartype = 'C';
00489 osinstance->addVariable(i, var_name(i),
00490 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00491 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00492 vartype);
00493 }
00494
00495
00496 lower = nlvc - nlvci;
00497 upper = nlvc - nlvci + nlvci;
00498 upper = nlvc;
00499 #ifdef AMPLDEBUG
00500 std::cout << "LOWER = " << lower << std::endl;
00501 std::cout << "UPPER = " << upper << std::endl;
00502 #endif
00503 for(i = lower; i < upper; i++)
00504 {
00505 vartype = integerOrBinary(LUv[2*i], LUv[2*i+1]);
00506 osinstance->addVariable(i, var_name(i),
00507 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00508 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00509 vartype);
00510 }
00511
00512 lower = nlvc;
00513 upper = nlvc + ( nlvo - (nlvc + nlvoi) );
00514 upper = nlvo - nlvoi;
00515 #ifdef AMPLDEBUG
00516 std::cout << "LOWER = " << lower << std::endl;
00517 std::cout << "UPPER = " << upper << std::endl;
00518 #endif
00519 for(i = lower; i < upper; i++)
00520 {
00521 vartype = 'C';
00522 osinstance->addVariable(i, var_name(i),
00523 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00524 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00525 vartype);
00526 }
00527
00528 lower = nlvo - nlvoi;
00529 upper = nlvo - nlvoi + nlvoi;
00530 upper = nlvo ;
00531 #ifdef AMPLDEBUG
00532 std::cout << "LOWER = " << lower << std::endl;
00533 std::cout << "UPPER = " << upper << std::endl;
00534 #endif
00535 for(i = lower; i < upper; i++)
00536 {
00537 vartype = integerOrBinary(LUv[2*i], LUv[2*i+1]);
00538 osinstance->addVariable(i, var_name(i),
00539 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00540 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00541 vartype);
00542 }
00543
00544
00545
00546
00547 lower = CoinMax(nlvc, nlvo);
00548 upper = CoinMax(nlvc, nlvo) + nwv;
00549 #ifdef AMPLDEBUG
00550 std::cout << "LOWER = " << lower << std::endl;
00551 std::cout << "UPPER = " << upper << std::endl;
00552 #endif
00553 for(i = lower; i < upper; i++)
00554 {
00555 vartype = 'C';
00556 osinstance->addVariable(i, var_name(i),
00557 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00558 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00559 vartype);
00560 }
00561
00562
00563 lower = CoinMax(nlvc, nlvo) + nwv;
00564 upper = CoinMax(nlvc, nlvo) + nwv + (n_var - (CoinMax(nlvc, nlvo) + niv + nbv + nwv) );
00565 upper = n_var - niv - nbv;
00566 #ifdef AMPLDEBUG
00567 std::cout << "LOWER = " << lower << std::endl;
00568 std::cout << "UPPER = " << upper << std::endl;
00569 #endif
00570 for(i = lower; i < upper; i++)
00571 {
00572 vartype = 'C';
00573 osinstance->addVariable(i, var_name(i),
00574 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00575 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00576 vartype);
00577 }
00578
00579
00580 lower = n_var - niv - nbv;
00581 upper = n_var - niv - nbv + nbv;
00582 upper = n_var - niv ;
00583 #ifdef AMPLDEBUG
00584 std::cout << "LOWER = " << lower << std::endl;
00585 std::cout << "UPPER = " << upper << std::endl;
00586 #endif
00587 for(i = lower; i < upper; i++)
00588 {
00589 vartype = 'B';
00590 osinstance->addVariable(i, var_name(i),
00591 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00592 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00593 vartype);
00594 }
00595
00596
00597
00598 lower = n_var - niv;
00599 upper = n_var - niv + niv;
00600 upper = n_var;
00601 #ifdef AMPLDEBUG
00602 std::cout << "LOWER = " << lower << std::endl;
00603 std::cout << "UPPER = " << upper << std::endl;
00604 #endif
00605 for(i = lower; i < upper; i++)
00606 {
00607 vartype = 'I';
00608 osinstance->addVariable(i, var_name(i),
00609 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00610 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00611 vartype);
00612 }
00613
00614
00615
00616
00617
00625 std::vector<int> fidxs, v1idxs, v2idxs;
00626 std::vector<double> coeffs;
00627 std::vector<Nl> nlExprs;
00628 real* delsqp;
00629 fint* colqp;
00630 fint* rowqp;
00631 int osNLIdx;
00632 int aNLIdx;
00633
00634
00635 asl = rw;
00636
00637
00638
00639
00640 for (osNLIdx = -nlo, aNLIdx = nlo-1; osNLIdx < 0; osNLIdx++, aNLIdx--)
00641 {
00642 if (nqpcheck(aNLIdx, &rowqp, &colqp, &delsqp) > 0)
00643 {
00644 for (int v1 = 0; v1 < n_var; v1++)
00645 {
00646 for (int* psV2 = &rowqp[colqp[v1]]; psV2 < &rowqp[colqp[v1+1]]; psV2++, delsqp++)
00647 {
00648 if (std::abs(*delsqp) > OS_EPS)
00649 {
00650 fidxs.push_back(osNLIdx);
00651 v1idxs.push_back(v1);
00652 v2idxs.push_back(*psV2);
00653 coeffs.push_back(0.5 * *delsqp);
00654 }
00655 }
00656 }
00657 }
00658 else
00659 {
00660 Nl nl;
00661 expr* e = aNLIdx < 0 ? CON_DE[osNLIdx].e : OBJ_DE[aNLIdx].e;
00662 nl.idx = osNLIdx;
00663 nl.osExpressionTree = new OSExpressionTree();
00664 nl.osExpressionTree->m_treeRoot = walkTree (e);
00665 nl.m_bDeleteExpressionTree = false;
00666
00667
00668
00669
00670 nlExprs.push_back(nl);
00671 }
00672 }
00673
00674 bool isQP = true;
00675 bool fill_in = false;
00676 int nqpchk;
00677 cgrad *cg;
00678 int n_prev_A_coef, n_curr_A_coef;
00679
00680
00681 for (osNLIdx = 0, aNLIdx = -1; osNLIdx < nlc; osNLIdx++, aNLIdx--)
00682 {
00683 if (isQP)
00684 {
00685
00686 if (!fill_in)
00687 {
00688 n_prev_A_coef = 0;
00689 for(cg = Cgrad[osNLIdx]; cg; cg = cg->next)
00690 {
00691 if (cg->coef != 0) n_prev_A_coef++;
00692 }
00693 }
00694
00695 nqpchk = nqpcheck(aNLIdx, &rowqp, &colqp, &delsqp);
00696 if (nqpchk > 0)
00697 {
00698 for (int v1 = 0; v1 < n_var; v1++)
00699 {
00700 for (int* psV2 = &rowqp[colqp[v1]]; psV2 < &rowqp[colqp[v1+1]]; psV2++, delsqp++)
00701 {
00702 if (std::abs(*delsqp) > OS_EPS)
00703 {
00704 fidxs.push_back(osNLIdx);
00705 v1idxs.push_back(v1);
00706 v2idxs.push_back(*psV2);
00707 coeffs.push_back(0.5 * *delsqp);
00708 }
00709 }
00710 }
00711 if (!fill_in)
00712 {
00713 n_curr_A_coef = 0;
00714 for(cg = Cgrad[osNLIdx]; cg; cg = cg->next)
00715 {
00716 if (cg->coef != 0) n_curr_A_coef++;
00717 }
00718 if (n_prev_A_coef != n_curr_A_coef) fill_in = true;
00719 }
00720 continue;
00721 }
00722 if (nqpchk < 0) isQP = false;
00723 }
00724
00725
00726 {
00727 Nl nl;
00728 expr* e = aNLIdx < 0 ? CON_DE[osNLIdx].e : OBJ_DE[aNLIdx].e;
00729 nl.idx = osNLIdx;
00730 nl.osExpressionTree = new OSExpressionTree();
00731 nl.osExpressionTree->m_treeRoot = walkTree (e);
00732 nl.m_bDeleteExpressionTree = false;
00733
00734
00735
00736
00737 nlExprs.push_back(nl);
00738 }
00739 }
00740
00741 if (nlExprs.size())
00742 {
00743 Nl** ppsNl = new Nl*[ nlExprs.size() ];
00744 for (i = 0; i < nlExprs.size(); i++)
00745 {
00746 ppsNl[i] = new Nl(nlExprs[i]);
00747 ppsNl[i]->m_bDeleteExpressionTree = true;
00748 }
00749 osinstance->instanceData->nonlinearExpressions->nl = ppsNl;
00750 }
00751 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nlExprs.size();
00752 if (fidxs.size())
00753 {
00754 osinstance->setQuadraticTerms((int)fidxs.size(), &fidxs[0], &v1idxs[0], &v2idxs[0], &coeffs[0], 0, (int)fidxs.size()-1);
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 double objWeight = 1.0;
00768
00769 SparseVector* objectiveCoefficients = NULL;
00770
00771 osinstance->setObjectiveNumber( n_obj) ;
00772 for(i = 0; i < n_obj; i++)
00773 {
00774 int n_obj_coef = 0;
00775 for(og = Ograd[i]; og; og = og->next)
00776 {
00777 if (og->coef != 0) n_obj_coef++;
00778 }
00779 objectiveCoefficients = new SparseVector( n_obj_coef);
00780 int i_obj_coef = -1;
00781 for(og = Ograd[i]; og; og = og->next)
00782 {
00783 if (fabs(og->coef) > OS_EPS)
00784 {
00785 i_obj_coef++;
00786 objectiveCoefficients->values[i_obj_coef] = og->coef;
00787 objectiveCoefficients->indexes[i_obj_coef] = og->varno;
00788 }
00789 }
00790 osinstance->addObjective(-i-1, obj_name(i),
00791 (objtype[i] == 1)?"max":"min",
00792 objconst( i), objWeight, objectiveCoefficients) ;
00793 delete objectiveCoefficients;
00794 objectiveCoefficients = NULL;
00795 }
00796
00797
00798
00799
00800
00801 osinstance->setConstraintNumber( n_con);
00802
00803 double constant = 0.0;
00804 std::string rowName;
00805 for(i = 0; i < n_con; i++)
00806 {
00807 osinstance->addConstraint(i, con_name(i),
00808 LUrhs[2*i] > -OSDBL_MAX ? LUrhs[2*i] : -OSDBL_MAX,
00809 LUrhs[2*i+1] < OSDBL_MAX ? LUrhs[2*i+1] : OSDBL_MAX,
00810 constant);
00811 }
00812
00813
00814
00815
00816 if (fill_in)
00817 {
00818 int row_len;
00819 A_rowstarts = new int[n_con+1];
00820 A_rowstarts[0] = 0;
00821 for (int i=0; i < n_con; i++)
00822 {
00823 row_len = 0;
00824 for(cg = Cgrad[i]; cg; cg = cg->next)
00825 {
00826 if (cg->coef != 0) row_len++;
00827 }
00828 A_rowstarts[i+1] = A_rowstarts[i] + row_len;
00829 }
00830 A_colptr = new int[A_rowstarts[n_con]];
00831 A_nzelem = new double[A_rowstarts[n_con]];
00832 for (int i=0; i < n_con; i++)
00833 {
00834 row_len = 0;
00835 for(cg = Cgrad[i]; cg; cg = cg->next)
00836 {
00837 if (cg->coef != 0)
00838 {
00839 A_colptr[A_rowstarts[i]+row_len] = cg->varno;
00840 A_nzelem[A_rowstarts[i]+row_len] = cg->coef;
00841 row_len++;
00842 }
00843 }
00844 }
00845
00846 if(A_rowstarts[ n_con] > 0)
00847 {
00848 osinstance->setLinearConstraintCoefficients(A_rowstarts[ n_con], false,
00849 A_nzelem, 0, A_rowstarts[n_con] - 1,
00850 A_colptr, 0, A_rowstarts[n_con] - 1,
00851 A_rowstarts, 0, n_con);
00852 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00853 osinstance->instanceData->linearConstraintCoefficients->colIdx->bDeleteArrays = false;
00854 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00855 }
00856
00857 #ifdef AMPLDEBUG
00858 cout << "A-matrix elements: ";
00859 for (i = 0; i < A_rowstarts[ n_con]; i++)
00860 cout << A_nzelem[i] << " ";
00861 cout << endl;
00862 cout << "A-matrix col index: ";
00863 for (i = 0; i < A_rowstarts[ n_con]; i++)
00864 cout << A_colptr[i] << " ";
00865 cout << endl;
00866 cout << "A-matrix rowstart: ";
00867 for (i = 0; i <= n_con; i++)
00868 cout << A_rowstarts[i] << " ";
00869 cout << endl;
00870 #endif
00871 }
00872
00873 else
00874 {
00875 asl=cw;
00876 int colStart, colEnd, nCoefSqueezed;
00877 nCoefSqueezed = 0;
00878
00879 #ifdef AMPLDEBUG
00880 cout << "A-matrix elements: ";
00881 for (i = 0; i < A_colstarts[ n_var]; i++)
00882 cout << A_vals[i] << " ";
00883 cout << endl;
00884 cout << "A-matrix rowinfo: ";
00885 for (i = 0; i < A_colstarts[ n_var]; i++)
00886 cout << A_rownos[i] << " ";
00887 cout << endl;
00888 cout << "A-matrix colstart: ";
00889 for (i = 0; i <= n_var; i++)
00890 cout << A_colstarts[i] << " ";
00891 cout << endl;
00892 #endif
00893
00894 colEnd = 0;
00895 for (i = 0; i < n_var; i++)
00896 {
00897 colStart = colEnd;
00898 colEnd = A_colstarts[i+1];
00899 #ifdef AMPLDEBUG
00900 cout << "col " << i << " from " << colStart << " to " << colEnd - 1 << endl;
00901 #endif
00902 for (j = colStart; j < colEnd; j++)
00903 {
00904 if (fabs(A_vals[ j]) > OS_EPS)
00905 {
00906 A_vals[ j-nCoefSqueezed] = A_vals[ j];
00907 A_rownos[ j-nCoefSqueezed] = A_rownos[j];
00908 }
00909 else
00910 {
00911 #ifdef AMPLDEBUG
00912 cout << "squeeze out element " << j << endl;
00913 #endif
00914 nCoefSqueezed++;
00915 }
00916 }
00917 A_colstarts[i+1] = A_colstarts[i+1] - nCoefSqueezed;
00918 }
00919
00920 #ifdef AMPLDEBUG
00921 cout << "A-matrix elements: ";
00922 for (i = 0; i < A_colstarts[ n_var]; i++)
00923 cout << A_vals[i] << " ";
00924 cout << endl;
00925 cout << "A-matrix rowinfo: ";
00926 for (i = 0; i < A_colstarts[ n_var]; i++)
00927 cout << A_rownos[i] << " ";
00928 cout << endl;
00929 cout << "A-matrix colstart: ";
00930 for (i = 0; i <= n_var; i++)
00931 cout << A_colstarts[i] << " ";
00932 cout << endl;
00933 cout << "A-matrix nonzeroes: " << A_colstarts[ n_var] << "; nsqueezed: " << nCoefSqueezed << endl;
00934 #endif
00935
00936 if(A_colstarts[ n_var] > 0)
00937 {
00938 osinstance->setLinearConstraintCoefficients(A_colstarts[ n_var], true,
00939 A_vals, 0, A_colstarts[n_var] - 1,
00940 A_rownos, 0, A_colstarts[n_var] - 1,
00941 A_colstarts, 0, n_var);
00942 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00943 osinstance->instanceData->linearConstraintCoefficients->rowIdx->bDeleteArrays = false;
00944 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00945 }
00946 }
00947
00948 #ifdef AMPLDEBUG
00949 OSiLWriter osilwriter;
00950 std::cout << "WRITE THE INSTANCE" << std::endl;
00951 osilwriter.m_bWhiteSpace = true;
00952 std::cout << osilwriter.writeOSiL( osinstance) << std::endl;
00953 std::cout << "DONE WRITE THE INSTANCE" << std::endl;
00954
00955 std::cout << osinstance->printModel() << std::endl;
00956
00957 #endif
00958
00959 return true;
00960 }