00001
00022 #include <iostream>
00023 #include "OSiLWriter.h"
00024 #include "OSoLWriter.h"
00025 #include "OSoLReader.h"
00026 #include "OSnl2OS.h"
00027 #include "OSOption.h"
00028 #include "OSOutput.h"
00029 #include "OSErrorClass.h"
00030 #include "OSMathUtil.h"
00031
00032 #include "CoinHelperFunctions.hpp"
00033
00034
00035 #include "nlp.h"
00036 #include "getstub.h"
00037 #include "r_opn.hd"
00038 #include "opcode.hd"
00039
00040 #ifdef HAVE_CMATH
00041 # include <cmath>
00042 #else
00043 # ifdef HAVE_CMATH_H
00044 # include <cmath.h>
00045 # endif
00046 #endif
00047
00048 #include <sstream>
00049
00050
00051 #define R_OPS ((ASL_fg*)asl)->I.r_ops_
00052 #define OBJ_DE ((ASL_fg*)asl)->I.obj_de_
00053 #define VAR_E ((ASL_fg*)asl)->I.var_e_
00054 #define CON_DE ((ASL_fg*)asl)->I.con_de_
00055
00056
00057 #include <asl.h>
00058
00059
00060 using std::endl;
00061
00062 #ifdef HAVE_STDINT_H
00063 #include <stdint.h>
00064 #endif
00065
00066 #ifdef HAVE_STDLIB_H
00067 #include <stdlib.h>
00068 #endif
00069
00070
00071 OSnl2OS::OSnl2OS()
00072 : osolreader (NULL), osinstance(NULL), osoption(NULL), stub("")
00073 {
00074 cw = ASL_alloc( ASL_read_fg);
00075 rw = ASL_alloc( ASL_read_fg);
00076 asl = cw;
00077 }
00078
00079 OSnl2OS::OSnl2OS(ASL *cw, ASL *rw, ASL *asl)
00080 : osolreader (NULL), osinstance(NULL), osoption(NULL), stub("")
00081 {
00082 this->asl = asl;
00083 this->cw = cw;
00084 this->rw = rw;
00085 }
00086
00087 ASL* OSnl2OS::getASL(std::string name)
00088 {
00089 if (name == "asl")
00090 return asl;
00091 else if (name == "cw")
00092 return cw;
00093 else if (name == "rw")
00094 return rw;
00095 else
00096 return NULL;
00097 }
00098
00099 void OSnl2OS::setOsol(std::string osol)
00100 {
00101 this->osol = osol;
00102 }
00103
00104 void OSnl2OS::setJobID(std::string jobID)
00105 {
00106 this->jobID = jobID;
00107 }
00108
00109 bool OSnl2OS::readNl(std::string stub)
00110 {
00111 std::ostringstream outStr;
00112
00113 try
00114 {
00115 efunc *r_ops_int[N_OPS];
00116 FILE *nl;
00117
00118
00119
00120 asl = cw;
00121
00122
00123 nl = jac0dim(const_cast<char*>(stub.c_str()), (fint)stub.length());
00124
00125
00126 A_vals = (real *)Malloc(nzc*sizeof(real));
00127
00128
00129 want_xpi0 = 3;
00130
00131
00132 X0 = new real[n_var];
00133 havex0 = new char[n_var];
00134 pi0 = new real[n_con];
00135 havepi0 = new char[n_con];
00136
00137 #ifndef NDEBUG
00138 outStr.str("");
00139 outStr.clear();
00140 outStr << "number of nonzeros = " << nzc << endl;
00141 outStr << "number of variables = " << n_var << endl;
00142 outStr << "number of constraints = " << n_con << endl;
00143 outStr << "number of objectives = " << n_obj << endl;
00144 outStr << "number of ranges = " << nranges << endl;
00145 outStr << "number of equations = " << n_eqn << endl;
00146 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00147 #endif
00148 if(N_OPS > 0)
00149 {
00150 for(int i = 0; i < N_OPS; i++)
00151 {
00152 r_ops_int[i] = (efunc*)(unsigned long)i;
00153 }
00154 R_OPS = r_ops_int;
00155 want_derivs = 0;
00156 fg_read(nl, ASL_keep_all_suffixes);
00157 R_OPS = 0;
00158 }
00159
00160
00161
00162 asl = rw;
00163 nl = jac0dim((char*)stub.c_str(), (fint)stub.length());
00164 want_derivs = 0;
00165 qp_read(nl, 0);
00166
00167 asl = cw;
00168 numkount = 0;
00169 return true;
00170 }
00171 catch(const ErrorClass& eclass)
00172 {
00173 return false;
00174 }
00175 }
00176
00177
00178 OSnl2OS::~OSnl2OS()
00179 {
00180 if (osinstance != NULL) delete osinstance;
00181 osinstance = NULL;
00182
00183 if (osolreader != NULL)
00184 {
00185 delete osolreader;
00186 osolreader = NULL;
00187 }
00188 else
00189 {
00190 if (osoption != NULL) delete osoption;
00191 osoption = NULL;
00192 }
00193
00194 free( A_vals);
00195 if (X0) delete [] X0; X0 = NULL;
00196 if (havex0) delete [] havex0; havex0 = NULL;
00197 if (pi0) delete [] pi0; pi0 = NULL;
00198 if (havepi0) delete [] havepi0; havepi0 = NULL;
00199 if (cw != NULL) ASL_free(&cw);
00200 if (rw != NULL) ASL_free(&rw);
00201 cw = NULL;
00202 rw = NULL;
00203 asl = NULL;
00204 }
00205
00206 OSnLNode* OSnl2OS::walkTree (expr *e)
00207 {
00208 OSnLNode *nlNodePoint;
00209 OSnLNodeVariable *nlNodeVariablePoint;
00210 OSnLNodeNumber *nlNodeNumberPoint;
00211 efunc *op;
00212 expr **ep;
00213 int opnum;
00214 int i;
00215 int j = ((expr_v *)e - VAR_E) - osinstance->getVariableNumber() ;
00216 op = e->op;
00217 opnum = Intcast op;
00218 try
00219 {
00220 switch( opnum)
00221 {
00222 case OPPLUS:
00223 nlNodePoint = new OSnLNodePlus();
00224 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00225 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00226 op_type.push_back( "PLUS");
00227 return nlNodePoint;
00228
00229 case OPSUMLIST:
00230 i = 0;
00231 nlNodePoint = new OSnLNodeSum();
00232 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00233 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00234 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00235 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00236 return nlNodePoint;
00237
00238 case MAXLIST:
00239 i = 0;
00240 nlNodePoint = new OSnLNodeMax();
00241 nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00242 nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00243 for (ep = e->L.ep; ep < e->R.ep; *ep++)
00244 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00245 return nlNodePoint;
00246
00247 case OPMINUS:
00248 nlNodePoint = new OSnLNodeMinus();
00249
00250 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00251 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00252 op_type.push_back( "MINUS");
00253
00254 return nlNodePoint;
00255
00256 case OPUMINUS:
00257 nlNodePoint = new OSnLNodeNegate();
00258 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00259 return nlNodePoint;
00260
00261 case OPMULT:
00262 nlNodePoint = new OSnLNodeTimes();
00263 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00264 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00265 op_type.push_back( "TIMES");
00266 return nlNodePoint;
00267
00268 case OPDIV:
00269 nlNodePoint = new OSnLNodeDivide();
00270 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00271 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00272 return nlNodePoint;
00273
00274 case OPPOW:
00275 nlNodePoint = new OSnLNodePower();
00276 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00277 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00278 op_type.push_back( "POWER");
00279 return nlNodePoint;
00280
00281
00282 case OP1POW:
00283 nlNodePoint = new OSnLNodePower();
00284
00285
00286 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00287 nlNodeNumberPoint = new OSnLNodeNumber();
00288 nlNodeNumberPoint->value = e->R.en->v;
00289 nlNodePoint->m_mChildren[1] = nlNodeNumberPoint;
00290
00291 op_type.push_back( "NUMBER");
00292 op_type.push_back( os_dtoa_format( numkount) );
00293 operand.push_back( e->R.en->v );
00294 numkount++;
00295
00296 op_type.push_back( "POWER");
00297
00298
00299 return nlNodePoint;
00300
00301 case OP2POW:
00302 nlNodePoint = new OSnLNodeSquare();
00303
00304 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00305 op_type.push_back( "SQUARE");
00306 return nlNodePoint;
00307
00308 case OPCPOW:
00309 nlNodePoint = new OSnLNodePower();
00310 nlNodeNumberPoint = new OSnLNodeNumber();
00311 nlNodeNumberPoint->value = e->L.en->v;
00312 nlNodePoint->m_mChildren[0] = nlNodeNumberPoint;
00313 nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00314 return nlNodePoint;
00315
00316 case OP_log:
00317 nlNodePoint = new OSnLNodeLn();
00318 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00319 op_type.push_back( "LOG");
00320 return nlNodePoint;
00321
00322 case OP_sqrt:
00323 nlNodePoint = new OSnLNodeSqrt();
00324 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00325 return nlNodePoint;
00326
00327 case OP_cos:
00328 nlNodePoint = new OSnLNodeCos();
00329 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00330 return nlNodePoint;
00331
00332 case OP_sin:
00333 nlNodePoint = new OSnLNodeSin();
00334 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00335 return nlNodePoint;
00336
00337 case OP_exp:
00338 nlNodePoint = new OSnLNodeExp();
00339 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00340 return nlNodePoint;
00341
00342 case ABS:
00343 nlNodePoint = new OSnLNodeAbs();
00344 nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00345 return nlNodePoint;
00346
00347 case OPNUM:
00348 nlNodeNumberPoint = new OSnLNodeNumber;
00349 nlNodeNumberPoint->value = (double) ((expr_n*)e)->v;
00350 op_type.push_back( "NUMBER");
00351 op_type.push_back( os_dtoa_format( numkount ) );
00352
00353 operand.push_back( (double) ((expr_n*)e)->v );
00354 numkount++;
00355
00356
00357 return nlNodeNumberPoint;
00358
00359 case OPVARVAL:
00360
00361 if( j >= 0 )
00362 {
00363
00364
00365 if(j < ncom0)
00366 {
00367 struct cexp *common = ((const ASL_fg *) asl) -> I.cexps_ + j ;
00368
00369
00370
00371 int nlin = common -> nlin;
00372
00373 if( nlin > 0)
00374 {
00375 nlNodePoint = new OSnLNodeSum();
00376 nlNodePoint->inumberOfChildren = nlin + 1;
00377 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00378
00379
00380 linpart *L = common -> L;
00381 for(int kj = 0; kj < nlin; kj++)
00382 {
00383
00384 nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00385 nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00386 nlNodeVariablePoint->coef = L [kj]. fac;
00387 nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00388 }
00389 nlNodePoint->m_mChildren[ nlin] = walkTree( common->e);
00390 return nlNodePoint;
00391 }
00392 else return walkTree( common->e);
00393 }
00394 else
00395 {
00396 struct cexp1 *common = ((const ASL_fg *) asl) -> I.cexps1_ + (j - ncom0);
00397
00398
00399
00400 int nlin = common -> nlin;
00401
00402 if( nlin > 0)
00403 {
00404 nlNodePoint = new OSnLNodeSum();
00405 nlNodePoint->inumberOfChildren = nlin + 1;
00406 nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00407
00408
00409 linpart *L = common -> L;
00410 for(int kj = 0; kj < nlin; kj++)
00411 {
00412
00413 nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00414 nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00415 nlNodeVariablePoint->coef = L [kj]. fac;
00416 nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00417 }
00418 nlNodePoint->m_mChildren[ nlin] = walkTree( common->e);
00419 return nlNodePoint;
00420 }
00421 else return walkTree( common->e);
00422 }
00423 }
00424
00425 nlNodeVariablePoint = new OSnLNodeVariable;
00426 nlNodeVariablePoint->idx = e->a;
00427 nlNodeVariablePoint->coef = 1.0;
00428
00429 op_type.push_back( "VARIABLE");
00430 op_type.push_back( os_dtoa_format( e->a ) );
00431
00432
00433 return nlNodeVariablePoint;
00434 break;
00435 default:
00436 std::ostringstream outStr;
00437 std::string error;
00438 outStr << endl;
00439 outStr << endl;
00440 error = "ERROR: An unsupported operator found, AMPL operator number = " ;
00441 outStr << error;
00442 outStr << opnum;
00443 outStr << endl;
00444 error = outStr.str();
00445 throw ErrorClass( error );
00446 }
00447 }
00448 catch(const ErrorClass& eclass)
00449 {
00450 throw;
00451 }
00452 }
00453
00454 static inline char integerOrBinary(real upper, real lower)
00455 {
00456 if (lower > -1.0 + OS_EPS && upper < 2.0 - OS_EPS)
00457 return 'B';
00458 return 'I';
00459 }
00460
00461 void OSnl2OS::setVar(OSInstance *osinstance, int lower, int upper, char vartype)
00462 {
00463 std::ostringstream outStr;
00464 int i;
00465 #ifndef NDEBUG
00466 outStr.str("");
00467 outStr.clear();
00468 outStr << "LOWER = " << lower << std::endl;
00469 outStr << "UPPER = " << upper << std::endl;
00470 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00471 #endif
00472 for(i = lower; i < upper; i++)
00473 {
00474 osinstance->addVariable(i, var_name(i),
00475 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00476 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00477 vartype);
00478 }
00479 }
00480
00481 void OSnl2OS::setIBVar(OSInstance *osinstance, int lower, int upper)
00482 {
00483 std::ostringstream outStr;
00484 int i;
00485 #ifndef NDEBUG
00486 outStr.str("");
00487 outStr.clear();
00488 outStr << "LOWER = " << lower << std::endl;
00489 outStr << "UPPER = " << upper << std::endl;
00490 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00491 #endif
00492 for(i = lower; i < upper; i++)
00493 {
00494 if (LUv[2*i] > -1.0 + OS_EPS && LUv[2*i+1] < 2.0 - OS_EPS)
00495 osinstance->addVariable(i, var_name(i),0,1,'B');
00496 else
00497 osinstance->addVariable(i, var_name(i),
00498 LUv[2*i] > -OSDBL_MAX ? LUv[2*i] : -OSDBL_MAX,
00499 LUv[2*i+1] < OSDBL_MAX ? LUv[2*i+1] : OSDBL_MAX,
00500 'I');
00501 }
00502 }
00503
00504 bool OSnl2OS::createOSObjects()
00505 {
00506 int *A_rowstarts = NULL;
00507 int *A_colptr = NULL;
00508 double *A_nzelem = NULL;
00509 int i, j;
00510 std::ostringstream outStr;
00511
00512 osinstance = new OSInstance();
00513
00514
00515
00516 osinstance->setInstanceDescription("Generated from AMPL nl file");
00517
00518
00519
00520
00521
00522 std::string colName;
00523
00524 osinstance->setVariableNumber(n_var);
00525
00526
00527 setVar(osinstance,0,nlvb - nlvbi,'C');
00528 setIBVar(osinstance,nlvb - nlvbi,nlvb);
00529 setVar(osinstance,nlvb,nlvc - nlvci,'C');
00530 setIBVar(osinstance,nlvc - nlvci,nlvc);
00531 setVar(osinstance,nlvc,nlvo - nlvoi,'C');
00532 setIBVar(osinstance,nlvo - nlvoi,nlvo);
00533
00534
00535 setVar(osinstance,CoinMax(nlvc, nlvo),CoinMax(nlvc, nlvo) + nwv,'C');
00536 setVar(osinstance,CoinMax(nlvc, nlvo) + nwv,n_var - niv - nbv, 'C');
00537 setVar(osinstance,n_var - niv - nbv, n_var - niv, 'B') ;
00538 setVar(osinstance,n_var - niv, n_var, 'I');
00539
00540
00547 std::vector<int> fidxs, v1idxs, v2idxs;
00548 std::vector<double> coeffs;
00549 std::vector<Nl> nlExprs;
00550 real* delsqp;
00551 fint* colqp;
00552 fint* rowqp;
00553 int osNLIdx;
00554 int aNLIdx;
00555 cgrad *cg;
00556
00557 bool isQP = true;
00558 bool fill_in = false;
00559
00560 if (nlvc > 0 || nlvo > 0)
00561 {
00562
00563 asl = rw;
00564
00565
00566
00567
00568 for (osNLIdx = -nlo, aNLIdx = nlo-1; osNLIdx < 0; osNLIdx++, aNLIdx--)
00569 {
00570 if (nqpcheck(aNLIdx, &rowqp, &colqp, &delsqp) > 0)
00571 {
00572 for (int v1 = 0; v1 < n_var; v1++)
00573 {
00574 for (int* psV2 = (int*)&rowqp[colqp[v1]]; psV2 < (int*)&rowqp[colqp[v1+1]]; psV2++, delsqp++)
00575 {
00576 if (std::abs(*delsqp) > OS_EPS)
00577 {
00578 fidxs.push_back(osNLIdx);
00579 v1idxs.push_back(v1);
00580 v2idxs.push_back(*psV2);
00581 coeffs.push_back(0.5 * *delsqp);
00582 }
00583 }
00584 }
00585 }
00586 else
00587 {
00588 Nl nl;
00589 expr* e = aNLIdx < 0 ? CON_DE[osNLIdx].e : OBJ_DE[aNLIdx].e;
00590 nl.idx = osNLIdx;
00591 nl.osExpressionTree = new ScalarExpressionTree();
00592 nl.osExpressionTree->m_treeRoot = walkTree (e);
00593 nl.m_bDeleteExpressionTree = false;
00594
00595
00596
00597
00598 nlExprs.push_back(nl);
00599 }
00600 }
00601
00602 int nqpchk;
00603
00604
00605 double* A_row_temp = new double[n_var];
00606
00607 for (osNLIdx = 0, aNLIdx = -1; osNLIdx < nlc; osNLIdx++, aNLIdx--)
00608 {
00609 if (isQP)
00610 {
00611
00612 if (!fill_in)
00613 {
00614 for(cg = Cgrad[osNLIdx]; cg; cg = cg->next)
00615 {
00616 if (cg->coef != 0) A_row_temp[cg->varno] = cg->coef;
00617 }
00618 }
00619
00620 nqpchk = nqpcheck(aNLIdx, &rowqp, &colqp, &delsqp);
00621 if (nqpchk > 0)
00622 {
00623 for (int v1 = 0; v1 < n_var; v1++)
00624 {
00625 for (int* psV2 = (int*)&rowqp[colqp[v1]]; psV2 < (int*)&rowqp[colqp[v1+1]]; psV2++, delsqp++)
00626 {
00627 if (std::abs(*delsqp) > OS_EPS)
00628 {
00629 fidxs.push_back(osNLIdx);
00630 v1idxs.push_back(v1);
00631 v2idxs.push_back(*psV2);
00632 coeffs.push_back(0.5 * *delsqp);
00633 }
00634 }
00635 }
00636 if (!fill_in)
00637 {
00638 for(cg = Cgrad[osNLIdx]; cg; cg = cg->next)
00639 {
00640 if (cg->coef != 0)
00641 if (cg->coef != A_row_temp[cg->varno])
00642 {
00643 fill_in = true;
00644 break;
00645 }
00646 }
00647 }
00648 continue;
00649 }
00650 if (nqpchk < 0) isQP = false;
00651 }
00652
00653
00654 {
00655 Nl nl;
00656 expr* e = aNLIdx < 0 ? CON_DE[osNLIdx].e : OBJ_DE[aNLIdx].e;
00657 nl.idx = osNLIdx;
00658 nl.osExpressionTree = new ScalarExpressionTree();
00659 nl.osExpressionTree->m_treeRoot = walkTree (e);
00660 nl.m_bDeleteExpressionTree = false;
00661
00662
00663
00664
00665 nlExprs.push_back(nl);
00666 }
00667 }
00668 delete [] A_row_temp;
00669
00670 if (nlExprs.size())
00671 {
00672 Nl** ppsNl = new Nl*[ nlExprs.size() ];
00673 for (i = 0; i < nlExprs.size(); i++)
00674 {
00675 ppsNl[i] = new Nl(nlExprs[i]);
00676 ppsNl[i]->m_bDeleteExpressionTree = true;
00677 }
00678 if (osinstance->instanceData->nonlinearExpressions == NULL)
00679 osinstance->instanceData->nonlinearExpressions = new NonlinearExpressions();
00680 osinstance->instanceData->nonlinearExpressions->nl = ppsNl;
00681 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nlExprs.size();
00682 }
00683 if (fidxs.size())
00684 {
00685 osinstance->setQuadraticCoefficients((int)fidxs.size(), &fidxs[0], &v1idxs[0], &v2idxs[0], &coeffs[0], 0, (int)fidxs.size()-1);
00686 }
00687
00688
00689
00690
00691
00692 }
00693
00694
00695
00696
00697 double objWeight = 1.0;
00698
00699 SparseVector* objectiveCoefficients = NULL;
00700
00701 osinstance->setObjectiveNumber( n_obj) ;
00702 for(i = 0; i < n_obj; i++)
00703 {
00704 int n_obj_coef = 0;
00705 for(og = Ograd[i]; og; og = og->next)
00706 {
00707 if (og->coef != 0) n_obj_coef++;
00708 }
00709 objectiveCoefficients = new SparseVector( n_obj_coef);
00710 int i_obj_coef = -1;
00711 for(og = Ograd[i]; og; og = og->next)
00712 {
00713 if (fabs(og->coef) > OS_EPS)
00714 {
00715 i_obj_coef++;
00716 objectiveCoefficients->values[i_obj_coef] = og->coef;
00717 objectiveCoefficients->indexes[i_obj_coef] = og->varno;
00718 }
00719 }
00720 osinstance->addObjective(-i-1, obj_name(i),
00721 (objtype[i] == 1)?"max":"min",
00722 objconst( i), objWeight, objectiveCoefficients) ;
00723 delete objectiveCoefficients;
00724 objectiveCoefficients = NULL;
00725 }
00726
00727
00728
00729
00730 osinstance->setConstraintNumber( n_con);
00731
00732 double constant = 0.0;
00733 std::string rowName;
00734 for(i = 0; i < n_con; i++)
00735 {
00736 osinstance->addConstraint(i, con_name(i),
00737 LUrhs[2*i] > -OSDBL_MAX ? LUrhs[2*i] : -OSDBL_MAX,
00738 LUrhs[2*i+1] < OSDBL_MAX ? LUrhs[2*i+1] : OSDBL_MAX,
00739 constant);
00740 }
00741
00742
00743
00744
00745
00746 if (fill_in)
00747 {
00748 int row_len;
00749 A_rowstarts = new int[n_con+1];
00750 A_rowstarts[0] = 0;
00751 for (int i=0; i < n_con; i++)
00752 {
00753 row_len = 0;
00754 for(cg = Cgrad[i]; cg; cg = cg->next)
00755 {
00756 if (cg->coef != 0) row_len++;
00757 }
00758 A_rowstarts[i+1] = A_rowstarts[i] + row_len;
00759 }
00760 A_colptr = new int[A_rowstarts[n_con]];
00761 A_nzelem = new double[A_rowstarts[n_con]];
00762 for (int i=0; i < n_con; i++)
00763 {
00764 row_len = 0;
00765 for(cg = Cgrad[i]; cg; cg = cg->next)
00766 {
00767 if (cg->coef != 0)
00768 {
00769 A_colptr[A_rowstarts[i]+row_len] = cg->varno;
00770 A_nzelem[A_rowstarts[i]+row_len] = cg->coef;
00771 row_len++;
00772 }
00773 }
00774 }
00775
00776 if(A_rowstarts[ n_con] > 0)
00777 {
00778 osinstance->setLinearConstraintCoefficients(A_rowstarts[ n_con], false,
00779 A_nzelem, 0, A_rowstarts[n_con] - 1,
00780 A_colptr, 0, A_rowstarts[n_con] - 1,
00781 A_rowstarts, 0, n_con);
00782
00783
00784 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = true;
00785 osinstance->instanceData->linearConstraintCoefficients->colIdx->bDeleteArrays = true;
00786 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = true;
00787 }
00788
00789
00790 #ifndef NDEBUG
00791 outStr.str("");
00792 outStr.clear();
00793 outStr << "A-matrix elements: ";
00794 for (int i = 0; i < A_rowstarts[ n_con]; i++)
00795 outStr << A_nzelem[i] << " ";
00796 outStr << endl;
00797 outStr << "A-matrix col index: ";
00798 for (int i = 0; i < A_rowstarts[ n_con]; i++)
00799 outStr << A_colptr[i] << " ";
00800 outStr << endl;
00801 outStr << "A-matrix rowstart: ";
00802 for (int i = 0; i <= n_con; i++)
00803 outStr << A_rowstarts[i] << " ";
00804 outStr << endl;
00805 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00806 #endif
00807 }
00808
00809 else
00810 {
00811 asl=cw;
00812 int colStart, colEnd, nCoefSqueezed;
00813 nCoefSqueezed = 0;
00814
00815 #ifndef NDEBUG
00816 outStr.str("");
00817 outStr.clear();
00818 outStr << "A-matrix elements: ";
00819 for (int i = 0; i < A_colstarts[ n_var]; i++)
00820 outStr << A_vals[i] << " ";
00821 outStr << endl;
00822 outStr << "A-matrix rowinfo: ";
00823 for (int i = 0; i < A_colstarts[ n_var]; i++)
00824 outStr << A_rownos[i] << " ";
00825 outStr << endl;
00826 outStr << "A-matrix colstart: ";
00827 for (int i = 0; i <= n_var; i++)
00828 outStr << A_colstarts[i] << " ";
00829 outStr << endl;
00830 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00831 #endif
00832
00833 colEnd = 0;
00834 for (i = 0; i < n_var; i++)
00835 {
00836 colStart = colEnd;
00837 colEnd = A_colstarts[i+1];
00838 #ifndef NDEBUG
00839 outStr.str("");
00840 outStr.clear();
00841 outStr << "col " << i << " from " << colStart << " to " << colEnd - 1 << endl;
00842 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00843 #endif
00844 for (j = colStart; j < colEnd; j++)
00845 {
00846 if (fabs(A_vals[ j]) > OS_EPS)
00847 {
00848 A_vals[ j-nCoefSqueezed] = A_vals[ j];
00849 A_rownos[ j-nCoefSqueezed] = A_rownos[j];
00850 }
00851 else
00852 {
00853 #ifndef NDEBUG
00854 outStr.str("");
00855 outStr.clear();
00856 outStr << "squeeze out element " << j << endl;
00857 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00858 #endif
00859 nCoefSqueezed++;
00860 }
00861 }
00862 A_colstarts[i+1] = A_colstarts[i+1] - nCoefSqueezed;
00863 }
00864
00865 #ifndef NDEBUG
00866 outStr.str("");
00867 outStr.clear();
00868 outStr << "A-matrix elements: ";
00869 for (i = 0; i < A_colstarts[ n_var]; i++)
00870 outStr << A_vals[i] << " ";
00871 outStr << endl;
00872 outStr << "A-matrix rowinfo: ";
00873 for (i = 0; i < A_colstarts[ n_var]; i++)
00874 outStr << A_rownos[i] << " ";
00875 outStr << endl;
00876 outStr << "A-matrix colstart: ";
00877 for (i = 0; i <= n_var; i++)
00878 outStr << A_colstarts[i] << " ";
00879 outStr << endl;
00880 outStr << "A-matrix nonzeroes: " << A_colstarts[ n_var] << "; nsqueezed: " << nCoefSqueezed << endl;
00881 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00882 #endif
00883
00884 if(A_colstarts[ n_var] > 0)
00885 {
00886 osinstance->setLinearConstraintCoefficients(A_colstarts[ n_var], true,
00887 A_vals, 0, A_colstarts[n_var] - 1,
00888 A_rownos, 0, A_colstarts[n_var] - 1,
00889 A_colstarts, 0, n_var);
00890 osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00891 osinstance->instanceData->linearConstraintCoefficients->rowIdx->bDeleteArrays = false;
00892 osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00893
00894 A_vals = NULL;
00895 A_rownos = NULL;
00896 A_colstarts = NULL;
00897 }
00898 }
00899
00900
00908 SufDesc *d;
00909 int suffixType, nOther, nOtherIdx;
00910
00911 asl = cw;
00912
00913 try
00914 {
00915 osolreader = new OSoLReader();
00916
00917
00918 bool have_primal = false;
00919 for (i=0; i < n_var; i++)
00920 {
00921 if (havex0[i] != 0)
00922 {
00923 have_primal = true;
00924 break;
00925 }
00926 }
00927
00928 bool have_dual = false;
00929 for (i=0; i < n_con; i++)
00930 {
00931 if (havepi0[i] != 0)
00932 {
00933 have_dual = true;
00934 break;
00935 }
00936 }
00937
00938
00939
00940 if ((asl->i.suffixes[ASL_Sufkind_var] != NULL) ||
00941 (asl->i.suffixes[ASL_Sufkind_con] != NULL) ||
00942 (asl->i.suffixes[ASL_Sufkind_obj] != NULL) ||
00943 (asl->i.suffixes[ASL_Sufkind_prob] != NULL) ||
00944 ( have_primal ) || ( have_dual ) )
00945 {
00946 OSOption* tmpoption = new OSOption();
00947 tmpoption = osolreader->readOSoL(osol);
00956 osoption = new OSOption();
00957 osoption->deepCopyFrom(tmpoption);
00958
00959
00960 if (jobID != "") osoption->setJobID(jobID);
00961 }
00962
00963 bool found;
00964 bool extend;
00965 int nOther;
00966 int nIndexes;
00967 std::string *otherOptionNames = NULL;
00968
00969
00970 suffixType = ASL_Sufkind_var;
00971 if ( (asl->i.suffixes[suffixType] != NULL) )
00972 {
00973
00974 nOther = 0;
00975 if (osoption != NULL &&
00976 osoption->optimization != NULL &&
00977 osoption->optimization->variables != NULL &&
00978 osoption->optimization->variables->numberOfOtherVariableOptions > 0)
00979 {
00980 otherOptionNames = new std::string[osoption->optimization->variables->numberOfOtherVariableOptions];
00981 for (int i=0; i < osoption->optimization->variables->numberOfOtherVariableOptions; i++)
00982 if (osoption->optimization->variables->other[i]->numberOfVar > 0)
00983 otherOptionNames[nOther++] = osoption->optimization->variables->other[i]->name;
00984 }
00985 OtherVariableOption* varopt;
00986 for (d=asl->i.suffixes[suffixType]; d; d=d->next)
00987 {
00988 #ifndef NDEBUG
00989 outStr.str("");
00990 outStr.clear();
00991 outStr << "Detected suffix " << d->sufname << "; kind = " << d->kind << std::endl;
00992 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00993 #endif
00994
00995
00996 if (strcmp(d->sufname,"sstatus") == 0)
00997 {
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007 int basCode[ENUM_BASIS_STATUS_NUMBER_OF_STATES] =
01008 { ENUM_BASIS_STATUS_unknown,
01009 ENUM_BASIS_STATUS_basic,
01010 ENUM_BASIS_STATUS_superbasic,
01011 ENUM_BASIS_STATUS_atLower,
01012 ENUM_BASIS_STATUS_atUpper,
01013 ENUM_BASIS_STATUS_atEquality,
01014 ENUM_BASIS_STATUS_isFree
01015 };
01016
01017 int* IBS;
01018
01019 #ifndef NDEBUG
01020 outStr.str("");
01021 outStr.clear();
01022 outStr << "Original basis (in AMPL codes):";
01023 for (int k=0; k<n_var; k++)
01024 outStr << " " << d->u.i[k];
01025 outStr << std::endl;
01026 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01027 #endif
01028
01029
01030 if (osoption != NULL &&
01031 osoption->optimization != NULL &&
01032 osoption->optimization->variables != NULL &&
01033 osoption->optimization->variables->initialBasisStatus != NULL)
01034 {
01035
01036 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01037 {
01038 nIndexes = osoption->getNumberOfInitialBasisElements(ENUM_PROBLEM_COMPONENT_variables, basCode[i]);
01039 if (nIndexes > 0)
01040 {
01041 IBS = new int[nIndexes];
01042 osoption->getInitialBasisElements(ENUM_PROBLEM_COMPONENT_variables, basCode[i], IBS);
01043
01044 for (int k=0; k < nIndexes; k++)
01045 d->u.i[IBS[k]] = i;
01046 delete[] IBS;
01047 }
01048 #ifndef NDEBUG
01049 outStr.str("");
01050 outStr.clear();
01051 outStr << "After processing state " << i << ":";
01052 for (int k=0; k<n_var; k++)
01053 outStr << " " << d->u.i[k];
01054 outStr << std::endl;
01055 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
01056 #endif
01057 }
01058 }
01059 #ifndef NDEBUG
01060 outStr.str("");
01061 outStr.clear();
01062 outStr << "Merged basis (in AMPL codes):";
01063 for (int k=0; k<n_var; k++)
01064 outStr << " " << d->u.i[k];
01065 outStr << std::endl;
01066 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01067 #endif
01068
01069
01070 int nidx[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01071 int kidx[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01072 for (i=0; i<ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01073 {
01074 nidx[i] = 0;
01075 kidx[i] = 0;
01076 }
01077
01078 for (int k=0; k < n_var; k++)
01079 nidx[d->u.i[k]]++;
01080
01081
01082 int **IBS2;
01083 IBS2 = new int*[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01084 for (int i=0; i<ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01085 {
01086 IBS2[i] = new int[nidx[i]];
01087 }
01088
01089
01090 for (int k=0; k < n_var; k++)
01091 {
01092 IBS2[d->u.i[k]][kidx[d->u.i[k]]++] = k;
01093 }
01094
01095
01096 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01097 if (nidx[i] > 0)
01098 osoption->setInitBasisStatus(ENUM_PROBLEM_COMPONENT_variables, basCode[i], IBS2[i], nidx[i]);
01099
01100
01101 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01102 delete [] IBS2[i];
01103 delete [] IBS2;
01104 }
01105
01106 else
01107 {
01108
01109 varopt = new OtherVariableOption();
01110
01111 varopt->name = d->sufname;
01112 varopt->numberOfEnumerations = 0;
01113 varopt->var = new OtherVarOption*[n_var];
01114
01115
01116 found = false;
01117 int iopt;
01118 for (iopt=0; iopt < nOther; iopt++)
01119 {
01120 if (d->sufname == otherOptionNames[iopt])
01121 {
01122 found = true;
01123 break;
01124 }
01125 }
01126
01127
01128 if (found)
01129 {
01130 OtherVariableOption* otherOption;
01131 otherOption = osoption->getOtherVariableOption(iopt);
01132 for (int i=0; i < otherOption->numberOfVar; i++)
01133 {
01134 if (d->kind & 4)
01135 {
01136 d->u.r[otherOption->var[i]->idx] = os_strtod(otherOption->var[i]->value.c_str(), NULL);
01137 }
01138 else
01139 d->u.i[otherOption->var[i]->idx] = (int)os_strtod(otherOption->var[i]->value.c_str(), NULL) + 0.1;
01140 }
01141 if (otherOption->description == "")
01142 varopt->description = "combined from osol and .nl data";
01143 else
01144 varopt->description = otherOption->description + "; merged with .nl data";
01145 }
01146 else
01147 varopt->description = "transferred from .nl file";
01148
01149
01150 if (d->kind & 4)
01151 {
01152 varopt->varType = "real";
01153 nOtherIdx = 0;
01154 for (int k=0; k < n_var; k++)
01155 {
01156 if (d->u.r[k] != 0)
01157 {
01158 varopt->var[nOtherIdx] = new OtherVarOption();
01159 varopt->var[nOtherIdx]->idx = k;
01160 varopt->var[nOtherIdx]->value = os_dtoa_format(d->u.r[k]);
01161 nOtherIdx++;
01162 }
01163 }
01164 }
01165 else
01166 {
01167 varopt->varType = "integer";
01168 nOtherIdx = 0;
01169 for (int k=0; k < n_var; k++)
01170 {
01171 if (d->u.i[k] != 0)
01172 {
01173 varopt->var[nOtherIdx] = new OtherVarOption();
01174 varopt->var[nOtherIdx]->idx = k;
01175 varopt->var[nOtherIdx]->value = os_dtoa_format((double)d->u.i[k]);
01176 nOtherIdx++;
01177 }
01178 }
01179 }
01180
01181 varopt->numberOfVar = nOtherIdx;
01182
01183 if (found)
01184 {
01185
01186 for (int k=0; k < osoption->optimization->variables->other[iopt]->numberOfVar; k++)
01187 delete osoption->optimization->variables->other[iopt]->var[k];
01188 delete [] osoption->optimization->variables->other[iopt]->var;
01189 osoption->optimization->variables->other[iopt]->var = varopt->var;
01190 osoption->optimization->variables->other[iopt]->numberOfVar = nOtherIdx;
01191 varopt->var = NULL;
01192 osoption->optimization->variables->other[iopt]->description = varopt->description;
01193 }
01194 else
01195 {
01196 if (!osoption->setAnOtherVariableOption(varopt))
01197 throw ErrorClass( "OSnl2OS: Error transfering suffixes on variables" );
01198 }
01199
01200 delete varopt;
01201 varopt = NULL;
01202 }
01203 }
01204 delete [] otherOptionNames;
01205 otherOptionNames = NULL;
01206 }
01207
01208
01209 suffixType = ASL_Sufkind_con;
01210 if ( (asl->i.suffixes[suffixType] != NULL) )
01211 {
01212
01213 nOther = 0;
01214 if (osoption != NULL &&
01215 osoption->optimization != NULL &&
01216 osoption->optimization->constraints != NULL &&
01217 osoption->optimization->constraints->numberOfOtherConstraintOptions > 0)
01218 {
01219 otherOptionNames = new std::string[osoption->optimization->constraints->numberOfOtherConstraintOptions];
01220 for (int i=0; i < osoption->optimization->constraints->numberOfOtherConstraintOptions; i++)
01221 if (osoption->optimization->constraints->other[i]->numberOfCon > 0)
01222 otherOptionNames[nOther++] = osoption->optimization->constraints->other[i]->name;
01223 }
01224 OtherConstraintOption* conopt;
01225 for (d=asl->i.suffixes[suffixType]; d; d=d->next)
01226 {
01227 #ifndef NDEBUG
01228 outStr.str("");
01229 outStr.clear();
01230 outStr << "Detected suffix " << d->sufname << "; kind = " << d->kind << std::endl;
01231 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01232 #endif
01233
01234 if (strcmp(d->sufname,"sstatus") == 0)
01235 {
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245 int basCode[ENUM_BASIS_STATUS_NUMBER_OF_STATES] =
01246 { ENUM_BASIS_STATUS_unknown,
01247 ENUM_BASIS_STATUS_basic,
01248 ENUM_BASIS_STATUS_superbasic,
01249 ENUM_BASIS_STATUS_atLower,
01250 ENUM_BASIS_STATUS_atUpper,
01251 ENUM_BASIS_STATUS_atEquality,
01252 ENUM_BASIS_STATUS_isFree
01253 };
01254
01255
01256 int* IBS;
01257
01258 #ifndef NDEBUG
01259 outStr.str("");
01260 outStr.clear();
01261 outStr << "Original basis (in AMPL codes):";
01262 for (int k=0; k<n_con; k++)
01263 outStr << " " << d->u.i[k];
01264 outStr << std::endl;
01265 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01266 #endif
01267
01268 if (osoption != NULL &&
01269 osoption->optimization != NULL &&
01270 osoption->optimization->constraints != NULL &&
01271 osoption->optimization->constraints->initialBasisStatus != NULL)
01272 {
01273
01274
01275 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01276 {
01277 nIndexes = osoption->getNumberOfInitialBasisElements(ENUM_PROBLEM_COMPONENT_constraints, basCode[i]);
01278 if (nIndexes > 0)
01279 {
01280 IBS = new int[nIndexes];
01281 osoption->getInitialBasisElements(ENUM_PROBLEM_COMPONENT_constraints, basCode[i], IBS);
01282
01283 for (int k=0; k < nIndexes; k++)
01284 d->u.i[IBS[k]] = i;
01285 delete[] IBS;
01286 }
01287 #ifndef NDEBUG
01288 outStr.str("");
01289 outStr.clear();
01290 outStr << "After processing state " << i << ":";
01291 for (int k=0; k<n_con; k++)
01292 outStr << " " << d->u.i[k];
01293 outStr << std::endl;
01294 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
01295 #endif
01296 }
01297 }
01298 #ifndef NDEBUG
01299 outStr.str("");
01300 outStr.clear();
01301 outStr << "Merged basis (in AMPL codes):";
01302 for (int k=0; k<n_con; k++)
01303 outStr << " " << d->u.i[k];
01304 outStr << std::endl;
01305 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01306 #endif
01307
01308
01309 int nidx[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01310 int kidx[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01311 for (i=0; i<ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01312 {
01313 nidx[i] = 0;
01314 kidx[i] = 0;
01315 }
01316
01317 for (int k=0; k < n_con; k++)
01318 nidx[d->u.i[k]]++;
01319
01320
01321 int **IBS2;
01322 IBS2 = new int*[ENUM_BASIS_STATUS_NUMBER_OF_STATES];
01323 for (int i=0; i<ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01324 {
01325 IBS2[i] = new int[nidx[i]];
01326 }
01327
01328
01329 for (int k=0; k < n_con; k++)
01330 {
01331 IBS2[d->u.i[k]][kidx[d->u.i[k]]++] = k;
01332 }
01333
01334
01335 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01336 if (nidx[i] > 0)
01337 osoption->setInitBasisStatus(ENUM_PROBLEM_COMPONENT_constraints, basCode[i], IBS2[i], nidx[i]);
01338
01339
01340 for (int i=0; i < ENUM_BASIS_STATUS_NUMBER_OF_STATES; i++)
01341 delete [] IBS2[i];
01342 delete [] IBS2;
01343 }
01344
01345 else
01346 {
01347
01348 conopt = new OtherConstraintOption();
01349
01350 conopt->name = d->sufname;
01351 conopt->numberOfEnumerations = 0;
01352 conopt->con = new OtherConOption*[n_con];
01353
01354
01355 found = false;
01356 int iopt;
01357 for (iopt=0; iopt < nOther; iopt++)
01358 {
01359 if (d->sufname == otherOptionNames[iopt])
01360 {
01361 found = true;
01362 break;
01363 }
01364 }
01365
01366
01367 if (found)
01368 {
01369 OtherConstraintOption* otherOption;
01370 otherOption = osoption->getOtherConstraintOption(iopt);
01371 for (int i=0; i < otherOption->numberOfCon; i++)
01372 {
01373 if (d->kind & 4)
01374 {
01375 d->u.r[otherOption->con[i]->idx] = os_strtod(otherOption->con[i]->value.c_str(), NULL);
01376 }
01377 else
01378 d->u.i[otherOption->con[i]->idx] = (int)os_strtod(otherOption->con[i]->value.c_str(), NULL) + 0.1;
01379 }
01380 if (otherOption->description == "")
01381 conopt->description = "combined from osol and .nl data";
01382 else
01383 conopt->description = otherOption->description + "; merged with .nl data";
01384 }
01385 else
01386 conopt->description = "transferred from .nl file";
01387
01388
01389 if (d->kind & 4)
01390 {
01391 conopt->conType = "real";
01392 nOtherIdx = 0;
01393 for (int k=0; k < n_con; k++)
01394 {
01395 if (d->u.r[k] != 0)
01396 {
01397 conopt->con[nOtherIdx] = new OtherConOption();
01398 conopt->con[nOtherIdx]->idx = k;
01399 conopt->con[nOtherIdx]->value = os_dtoa_format(d->u.r[k]);
01400 nOtherIdx++;
01401 }
01402 }
01403 }
01404 else
01405 {
01406 conopt->conType = "integer";
01407 nOtherIdx = 0;
01408 for (int k=0; k < n_con; k++)
01409 {
01410 if (d->u.i[k] != 0)
01411 {
01412 conopt->con[nOtherIdx] = new OtherConOption();
01413 conopt->con[nOtherIdx]->idx = k;
01414 conopt->con[nOtherIdx]->value = os_dtoa_format((double)d->u.i[k]);
01415 nOtherIdx++;
01416 }
01417 }
01418 }
01419
01420 conopt->numberOfCon = nOtherIdx;
01421
01422 if (found)
01423 {
01424
01425 for (int k=0; k < osoption->optimization->constraints->other[iopt]->numberOfCon; k++)
01426 delete osoption->optimization->constraints->other[iopt]->con[k];
01427 delete [] osoption->optimization->constraints->other[iopt]->con;
01428 osoption->optimization->constraints->other[iopt]->con = conopt->con;
01429 osoption->optimization->constraints->other[iopt]->numberOfCon = nOtherIdx;
01430 conopt->con = NULL;
01431 osoption->optimization->constraints->other[iopt]->description = conopt->description;
01432 }
01433 else
01434 {
01435 if (!osoption->setAnOtherConstraintOption(conopt))
01436 throw ErrorClass( "OSnl2OS: Error transfering suffixes on constraints" );
01437 }
01438
01439 delete conopt;
01440 conopt = NULL;
01441 }
01442 }
01443 delete [] otherOptionNames;
01444 otherOptionNames = NULL;
01445 }
01446
01447
01448 suffixType = ASL_Sufkind_obj;
01449 if ( (asl->i.suffixes[suffixType] != NULL) )
01450 {
01451
01452 nOther = 0;
01453 if (osoption != NULL &&
01454 osoption->optimization != NULL &&
01455 osoption->optimization->objectives != NULL &&
01456 osoption->optimization->objectives->numberOfOtherObjectiveOptions > 0)
01457 {
01458 otherOptionNames = new std::string[osoption->optimization->objectives->numberOfOtherObjectiveOptions];
01459 for (int i=0; i < osoption->optimization->objectives->numberOfOtherObjectiveOptions; i++)
01460 if (osoption->optimization->objectives->other[i]->numberOfObj > 0)
01461 otherOptionNames[nOther++] = osoption->optimization->objectives->other[i]->name;
01462 }
01463 OtherObjectiveOption* objopt;
01464 for (d=asl->i.suffixes[suffixType]; d; d=d->next)
01465 {
01466 #ifndef NDEBUG
01467 outStr.str("");
01468 outStr.clear();
01469 outStr << "Detected suffix " << d->sufname << "; kind = " << d->kind << std::endl;
01470 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01471 #endif
01472
01473
01474 objopt = new OtherObjectiveOption();
01475
01476 objopt->name = d->sufname;
01477 objopt->numberOfEnumerations = 0;
01478 objopt->obj = new OtherObjOption*[n_obj];
01479
01480
01481 found = false;
01482 int iopt;
01483 for (iopt=0; iopt < nOther; iopt++)
01484 {
01485 if (d->sufname == otherOptionNames[iopt])
01486 {
01487 found = true;
01488 break;
01489 }
01490 }
01491
01492
01493 if (found)
01494 {
01495 OtherObjectiveOption* otherOption;
01496 otherOption = osoption->getOtherObjectiveOption(iopt);
01497 #ifndef NDEBUG
01498 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, "Merge data\n");
01499 #endif
01500 for (int i=0; i < otherOption->numberOfObj; i++)
01501 {
01502 if (d->kind & 4)
01503 {
01504 d->u.r[-1 - otherOption->obj[i]->idx] = os_strtod(otherOption->obj[i]->value.c_str(), NULL);
01505 }
01506 else
01507 d->u.i[-1 - otherOption->obj[i]->idx] = atoi(otherOption->obj[i]->value.c_str());
01508 }
01509 if (otherOption->description == "")
01510 objopt->description = "combined from osol and .nl data";
01511 else
01512 objopt->description = otherOption->description + "; merged with .nl data";
01513 }
01514 else
01515 objopt->description = "transferred from .nl file";
01516
01517
01518 if (d->kind & 4)
01519 {
01520 objopt->objType = "real";
01521 nOtherIdx = 0;
01522 for (int k=0; k < n_obj; k++)
01523 {
01524 if (d->u.r[k] != 0)
01525 {
01526 objopt->obj[nOtherIdx] = new OtherObjOption();
01527 objopt->obj[nOtherIdx]->idx = -1 - k;
01528 objopt->obj[nOtherIdx]->value = os_dtoa_format(d->u.r[k]);
01529 nOtherIdx++;
01530 }
01531 }
01532 }
01533 else
01534 {
01535 objopt->objType = "integer";
01536 nOtherIdx = 0;
01537 for (int k=0; k < n_obj; k++)
01538 {
01539 if (d->u.i[k] != 0)
01540 {
01541 objopt->obj[nOtherIdx] = new OtherObjOption();
01542 objopt->obj[nOtherIdx]->idx = -1 - k;
01543 objopt->obj[nOtherIdx]->value = os_dtoa_format((double)d->u.i[k]);
01544 nOtherIdx++;
01545 }
01546 }
01547 }
01548
01549 objopt->numberOfObj = nOtherIdx;
01550
01551 if (found)
01552 {
01553
01554 for (int k=0; k < osoption->optimization->objectives->other[iopt]->numberOfObj; k++)
01555 delete osoption->optimization->objectives->other[iopt]->obj[k];
01556 delete [] osoption->optimization->objectives->other[iopt]->obj;
01557 osoption->optimization->objectives->other[iopt]->obj = objopt->obj;
01558 osoption->optimization->objectives->other[iopt]->numberOfObj = nOtherIdx;
01559 objopt->obj = NULL;
01560 osoption->optimization->objectives->other[iopt]->description = objopt->description;
01561 }
01562 else
01563 {
01564 if (!osoption->setAnOtherObjectiveOption(objopt))
01565 throw ErrorClass( "OSnl2OS: Error transfering suffixes on objectives" );
01566 }
01567
01568 delete objopt;
01569 objopt = NULL;
01570 }
01571 delete [] otherOptionNames;
01572 otherOptionNames = NULL;
01573 }
01574
01575
01576
01577 suffixType = ASL_Sufkind_prob;
01578 if ( (asl->i.suffixes[suffixType] != NULL) )
01579 {
01580 std::string opttype, optvalue, optdesc;
01581 optdesc = "transferred from .nl file";
01582 for (d=asl->i.suffixes[suffixType]; d; d=d->next)
01583 {
01584 #ifndef NDEBUG
01585 outStr.str("");
01586 outStr.clear();
01587 outStr << "Detected suffix " << d->sufname << "; kind = " << d->kind << std::endl;
01588 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01589 #endif
01590
01591 if (d->kind & 4)
01592 {
01593 opttype = "real";
01594 optvalue = os_dtoa_format(d->u.r[0]);
01595 }
01596 else
01597 {
01598 opttype = "integer";
01599 optvalue = os_dtoa_format((double)d->u.i[0]);
01600 }
01601
01602 if (!osoption->setAnotherSolverOption(d->sufname,optvalue,"","",opttype,optdesc))
01603 throw ErrorClass( "OSnl2OS: Error transfering problem-indexed suffixes" );
01604 }
01605 }
01606
01607
01608
01609
01610 if (osoption != NULL &&
01611 osoption->optimization != NULL &&
01612 osoption->optimization->variables != NULL &&
01613 osoption->optimization->variables->initialVariableValues != NULL &&
01614 osoption->optimization->variables->initialVariableValues->numberOfVar > 0)
01615 {
01616 int n_prev = osoption->optimization->variables->initialVariableValues->numberOfVar;
01617 for (int i=0; i < n_prev; i++)
01618 havex0[osoption->optimization->variables->initialVariableValues->var[i]->idx] = 0;
01619 }
01620
01621
01622 int n_x0 = 0;
01623 for (int i=0; i < n_var; i++)
01624 if (havex0[i] != 0) n_x0++;
01625
01626 if (n_x0 > 0)
01627 {
01628
01629 InitVarValue **x_init = new InitVarValue*[n_x0];
01630
01631
01632 n_x0 = 0;
01633 for (int i=0; i < n_var; i++)
01634 {
01635 if (havex0[i] != 0)
01636 {
01637 x_init[n_x0] = new InitVarValue();
01638 x_init[n_x0]->idx = i;
01639 x_init[n_x0]->value = X0[i];
01640 n_x0++;
01641 }
01642 }
01643
01644
01645 if (!osoption->setInitVarValuesSparse(n_x0, x_init, ENUM_COMBINE_ARRAYS_merge))
01646 throw ErrorClass( "OSnl2OS: Error merging initial primal variable values" );
01647
01648 for (int i=0; i < n_x0; i++)
01649 delete x_init[i];
01650
01651 delete [] x_init;
01652 x_init = NULL;
01653 }
01654
01655
01656
01657
01658 if (osoption != NULL &&
01659 osoption->optimization != NULL &&
01660 osoption->optimization->constraints != NULL &&
01661 osoption->optimization->constraints->initialDualValues != NULL &&
01662 osoption->optimization->constraints->initialDualValues->numberOfCon > 0)
01663 {
01664 int n_prev = osoption->optimization->constraints->initialDualValues->numberOfCon;
01665 for (int i=0; i < n_prev; i++)
01666 havepi0[osoption->optimization->constraints->initialDualValues->con[i]->idx] = 0;
01667 }
01668
01669
01670 int n_pi0 = 0;
01671 for (int i=0; i < n_con; i++)
01672 if (havepi0[i] != 0) n_pi0++;
01673
01674 if (n_pi0 > 0)
01675 {
01676
01677 InitDualVarValue **pi_init = new InitDualVarValue*[n_pi0];
01678
01679
01680 n_pi0 = 0;
01681 for (int i=0; i < n_con; i++)
01682 {
01683 if (havepi0[i] != 0)
01684 {
01685 pi_init[n_pi0] = new InitDualVarValue();
01686 pi_init[n_pi0]->idx = i;
01687 pi_init[n_pi0]->lbDualValue = pi0[i];
01688 pi_init[n_pi0]->ubDualValue = pi0[i];
01689 n_pi0++;
01690 }
01691 }
01692
01693
01694 if (!osoption->setInitDualVarValuesSparse(n_pi0, pi_init, ENUM_COMBINE_ARRAYS_merge))
01695 throw ErrorClass( "OSnl2OS: Error merging initial dual variable values" );
01696
01697 for (int i=0; i < n_pi0; i++)
01698 delete pi_init[i];
01699
01700 delete [] pi_init;
01701 pi_init = NULL;
01702 }
01703
01704
01705
01706
01707
01708 #ifndef NDEBUG
01709 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, "At end of OSnl2OS\n");
01710 OSiLWriter* osilwriter = new OSiLWriter();
01711 std::string tmposil = osilwriter->writeOSiL(osinstance);
01712 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, tmposil);
01713
01714 delete osilwriter;
01715 osilwriter = NULL;
01716
01717 #endif
01718 }
01719
01720 catch(const ErrorClass& eclass)
01721 {
01722
01723 }
01724
01725 #ifndef NDEBUG
01726 OSiLWriter *osilwriter = new OSiLWriter();
01727 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, "WRITE THE INSTANCE\n");
01728 osilwriter->m_bWhiteSpace = true;
01729 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, osilwriter->writeOSiL( osinstance));
01730 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, "DONE WRITING THE INSTANCE\n");
01731 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, osinstance->printModel());
01732 delete osilwriter;
01733 osilwriter = NULL;
01734
01735 OSoLWriter *osolwriter = new OSoLWriter();
01736 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, "WRITE THE OPTIONS\n");
01737 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, osolwriter->writeOSoL( osoption));
01738 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_trace, "DONE WRITING THE OPTIONS\n");
01739 delete osolwriter;
01740 osolwriter = NULL;
01741 #endif
01742
01743 return true;
01744 }
01745
01746