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