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