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 #define AMPLDEBUG
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 osinstance = new OSInstance();
00426 int i, j;
00427
00428
00429
00430 osinstance->setInstanceDescription("Generated from AMPL nl file");
00431
00432
00433
00434
00435
00436 std::string colName;
00437 char vartype = 'C';
00438 osinstance->setVariableNumber( n_var);
00439
00440
00441
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
00618
00619
00620 double objWeight = 1.0;
00621
00622 SparseVector* objectiveCoefficients = NULL;
00623
00624 osinstance->setObjectiveNumber( n_obj) ;
00625 for(i = 0; i < n_obj; i++)
00626 {
00627 int n_obj_coef = 0;
00628 for(og = Ograd[i]; og; og = og->next)
00629 {
00630 if (og->coef != 0) n_obj_coef++;
00631 }
00632 objectiveCoefficients = new SparseVector( n_obj_coef);
00633 int i_obj_coef = -1;
00634 for(og = Ograd[i]; og; og = og->next)
00635 {
00636 if (fabs(og->coef) > OS_EPS)
00637 {
00638 i_obj_coef++;
00639 objectiveCoefficients->values[i_obj_coef] = og->coef;
00640 objectiveCoefficients->indexes[i_obj_coef] = og->varno;
00641 std::cout << "obj coeff in obj row " << i << " col " << og->varno << ": " << og->coef << std::endl;
00642 }
00643 }
00644 osinstance->addObjective(-i-1, obj_name(i),
00645 (objtype[i] == 1)?"max":"min",
00646 objconst( i), objWeight, objectiveCoefficients) ;
00647 delete objectiveCoefficients;
00648 objectiveCoefficients = NULL;
00649 }
00650
00651
00652 cgrad *cg;
00653 asl=rw;
00654 for(i = 0; i < n_con; i++)
00655 {
00656 std::cout << "Cgrad[i]: " << Cgrad[i] << std::endl;
00657 for(cg = Cgrad[i]; cg; cg = cg->next)
00658 {
00659 if (fabs(cg->coef) > OS_EPS)
00660 {
00661 std::cout << "con coeff in obj row " << i << " col " << cg->varno << ": " << cg->coef << std::endl;
00662 }
00663 }
00664 }
00665 asl=cw;
00666
00667
00668
00669
00670 osinstance->setConstraintNumber( n_con);
00671
00672 double constant = 0.0;
00673 std::string rowName;
00674 for(i = 0; i < n_con; i++)
00675 {
00676 osinstance->addConstraint(i, con_name(i),
00677 LUrhs[2*i] > -OSDBL_MAX ? LUrhs[2*i] : -OSDBL_MAX,
00678 LUrhs[2*i+1] < OSDBL_MAX ? LUrhs[2*i+1] : OSDBL_MAX,
00679 constant);
00680 }
00681
00682
00683
00684 int colStart, colEnd, nCoefSqueezed;
00685 nCoefSqueezed = 0;
00686
00687 #ifdef AMPLDEBUG
00688 cout << "A-matrix elements: ";
00689 for (i = 0; i < A_colstarts[ n_var]; i++)
00690 cout << A_vals[i] << " ";
00691 cout << endl;
00692 cout << "A-matrix rowinfo: ";
00693 for (i = 0; i < A_colstarts[ n_var]; i++)
00694 cout << A_rownos[i] << " ";
00695 cout << endl;
00696 cout << "A-matrix colstart: ";
00697 for (i = 0; i <= n_var; i++)
00698 cout << A_colstarts[i] << " ";
00699 cout << endl;
00700 #endif
00701
00702 colEnd = 0;
00703 for (i = 0; i < n_var; i++)
00704 {
00705 colStart = colEnd;
00706 colEnd = A_colstarts[i+1];
00707 #ifdef AMPLDEBUG
00708 cout << "col " << i << " from " << colStart << " to " << colEnd - 1 << endl;
00709 #endif
00710 for (j = colStart; j < colEnd; j++)
00711 {
00712 if (fabs(A_vals[ j]) > OS_EPS)
00713 {
00714 A_vals[ j-nCoefSqueezed] = A_vals[ j];
00715 A_rownos[ j-nCoefSqueezed] = A_rownos[j];
00716 }
00717 else
00718 {
00719 #ifdef AMPLDEBUG
00720 cout << "squeeze out element " << j << endl;
00721 #endif
00722 nCoefSqueezed++;
00723 }
00724 }
00725 A_colstarts[i+1] = A_colstarts[i+1] - nCoefSqueezed;
00726 }
00727
00728 #ifdef AMPLDEBUG
00729 cout << "A-matrix elements: ";
00730 for (i = 0; i < A_colstarts[ n_var]; i++)
00731 cout << A_vals[i] << " ";
00732 cout << endl;
00733 cout << "A-matrix rowinfo: ";
00734 for (i = 0; i < A_colstarts[ n_var]; i++)
00735 cout << A_rownos[i] << " ";
00736 cout << endl;
00737 cout << "A-matrix colstart: ";
00738 for (i = 0; i <= n_var; i++)
00739 cout << A_colstarts[i] << " ";
00740 cout << endl;
00741 cout << "A-matrix nonzeroes: " << A_colstarts[ n_var] << "; nsqueezed: " << nCoefSqueezed << endl;
00742 #endif
00743
00744 if(A_colstarts[ n_var] > 0)
00745 {
00746 osinstance->setLinearConstraintCoefficients(A_colstarts[ n_var], true,
00747 A_vals, 0, A_colstarts[n_var] - 1,
00748 A_rownos, 0, A_colstarts[n_var] - 1,
00749 A_colstarts, 0, n_var);
00750 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00751 osinstance->instanceData->linearConstraintCoefficients->rowIdx->bDeleteArrays = false;
00752 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00753 }
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780 std::vector<int> fidxs, v1idxs, v2idxs;
00781 std::vector<double> coeffs;
00782 std::vector<Nl> nlExprs;
00783 real* delsqp;
00784 fint* colqp;
00785 fint* rowqp;
00786 int osNLIdx;
00787 int aNLIdx;
00788
00789
00790 asl = rw;
00791
00792
00793 for (osNLIdx = -nlo, aNLIdx = nlo-1; osNLIdx < nlc; osNLIdx++, aNLIdx--)
00794 {
00795 if (nqpcheck(aNLIdx, &rowqp, &colqp, &delsqp) > 0)
00796 {
00797 for (int v1 = 0; v1 < n_var; v1++)
00798 {
00799 for (int* psV2 = &rowqp[colqp[v1]]; psV2 < &rowqp[colqp[v1+1]]; psV2++, delsqp++)
00800 {
00801 if (std::abs(*delsqp) > OS_EPS)
00802 {
00803 fidxs.push_back(osNLIdx);
00804 v1idxs.push_back(v1);
00805 v2idxs.push_back(*psV2);
00806 coeffs.push_back(0.5 * *delsqp);
00807 }
00808 }
00809 }
00810 }
00811 else
00812 {
00813 Nl nl;
00814 expr* e = aNLIdx < 0 ? CON_DE[osNLIdx].e : OBJ_DE[aNLIdx].e;
00815 nl.idx = osNLIdx;
00816 nl.osExpressionTree = new OSExpressionTree();
00817 nl.osExpressionTree->m_treeRoot = walkTree (e);
00818 nl.m_bDeleteExpressionTree = false;
00819
00820
00821
00822
00823 nlExprs.push_back(nl);
00824 }
00825 }
00826
00827 for(i = 0; i < n_obj; i++)
00828 {
00829 for(og = Ograd[i]; og; og = og->next)
00830 {
00831 if (fabs(og->coef) > OS_EPS)
00832 {
00833 std::cout << "obj coeff in obj row " << i << " col " << og->varno << ": " << og->coef << std::endl;
00834 }
00835 }
00836 }
00837
00838
00839
00840 for(i = 0; i < n_con; i++)
00841 {
00842 for(cg = Cgrad[i]; cg; cg = cg->next)
00843 {
00844 if (fabs(cg->coef) > OS_EPS)
00845 {
00846 std::cout << "con coeff in obj row " << i << " col " << cg->varno << ": " << cg->coef << std::endl;
00847 }
00848 }
00849 }
00850
00851 if (nlExprs.size())
00852 {
00853 Nl** ppsNl = new Nl*[ nlExprs.size() ];
00854 for (i = 0; i < nlExprs.size(); i++)
00855 {
00856 ppsNl[i] = new Nl(nlExprs[i]);
00857 ppsNl[i]->m_bDeleteExpressionTree = true;
00858 }
00859 osinstance->instanceData->nonlinearExpressions->nl = ppsNl;
00860 }
00861 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nlExprs.size();
00862 if (fidxs.size())
00863 {
00864 osinstance->setQuadraticTerms((int)fidxs.size(), &fidxs[0], &v1idxs[0], &v2idxs[0], &coeffs[0], 0, (int)fidxs.size()-1);
00865 }
00866
00867 asl = cw;
00868
00869
00870
00871
00872
00873
00874 #ifdef AMPLDEBUG
00875 OSiLWriter osilwriter;
00876 std::cout << "WRITE THE INSTANCE" << std::endl;
00877 osilwriter.m_bWhiteSpace = true;
00878 std::cout << osilwriter.writeOSiL( osinstance) << std::endl;
00879 std::cout << "DONE WRITE THE INSTANCE" << std::endl;
00880
00881 std::cout << osinstance->printModel() << std::endl;
00882
00883 #endif
00884
00885 return true;
00886 }