/home/coin/SVN-release/OS-2.4.1/OS/src/OSModelInterfaces/OSnl2osil.cpp

Go to the documentation of this file.
00001 /* $Id: OSnl2osil.cpp 4381 2011-11-09 09:10:35Z kmartin $ */
00022 #include <iostream>
00023 #include "OSiLWriter.h"
00024 #include "OSnl2osil.h"
00025 #include "OSErrorClass.h"
00026 
00027 #include "CoinHelperFunctions.hpp"
00028 
00029 
00030 #include "nlp.h"
00031 #include "getstub.h"
00032 #include "r_opn.hd" /* for N_OPS */
00033 #include "opcode.hd"
00034 
00035 
00036 #ifdef HAVE_CMATH
00037 # include <cmath>
00038 #else
00039 # ifdef HAVE_CMATH_H
00040 #  include <cmath.h>
00041 # endif
00042 #endif
00043 
00044 #include <sstream>
00045 
00046 
00047 #define R_OPS  ((ASL_fg*)asl)->I.r_ops_
00048 #define OBJ_DE ((ASL_fg*)asl)->I.obj_de_
00049 #define VAR_E  ((ASL_fg*)asl)->I.var_e_
00050 #define CON_DE ((ASL_fg*)asl)->I.con_de_
00051 
00052 efunc *r_ops_int[N_OPS];
00053 
00054 #include <asl.h>
00055 
00056 using std::cerr;
00057 using std::cout;
00058 using std::endl;
00059 
00060 #ifdef HAVE_STDINT_H
00061 #include <stdint.h>
00062 #endif
00063 
00064 
00065 
00066 //#define AMPLDEBUG
00067 
00068 OSnl2osil::OSnl2osil(std::string nlfilename)
00069 {
00070     //Initialize the AMPL library
00071     asl = ASL_alloc( ASL_read_fg);
00072     stub = &nlfilename[ 0];
00073     //cout << "READING FILE " << stub << endl;
00074     //Initialize the nl file reading
00075     nl = jac0dim(stub, (fint)strlen(stub));
00076     //Prepare *columnwise* parsing of nl file
00077     A_vals = (real *)Malloc(nzc*sizeof(real));
00078     //read nl file as a linear program
00079 #ifdef AMPLDEBUG
00080     cout << "number of nonzeros =   " << nzc << endl;
00081     cout << "number of variable =   " << n_var << endl;
00082     cout << "number of constraints =   " << n_con << endl;
00083     cout << "number of objs =   " << n_obj << endl;
00084     cout << "number of ranges =   " << nranges << endl;
00085     cout << "number of equations =   " << n_eqn << endl;
00086     //cout << "objective function type" << *objtype << endl;
00087 #endif
00088 #ifdef AMPLDEBUG
00089     cout << "Start f_read()" << endl;
00090 #endif
00091     //X0 = (real *)Malloc( n_var*sizeof(real));
00092     if(N_OPS > 0)
00093     {
00094         for(int i = 0; i < N_OPS; i++)
00095         {
00096             r_ops_int[i] = (efunc*)(unsigned long)i;
00097 
00098         }
00099         R_OPS = r_ops_int;
00100         want_derivs = 0;
00101         fg_read(nl, 0);
00102         R_OPS = 0;
00103     }
00104     numkount = 0;
00105 }
00106 
00107 OSnl2osil::~OSnl2osil()
00108 {
00109     osinstance->instanceData->linearConstraintCoefficients->start->bDeleteArrays = false;
00110     osinstance->instanceData->linearConstraintCoefficients->rowIdx->bDeleteArrays = false;
00111     osinstance->instanceData->linearConstraintCoefficients->value->bDeleteArrays = false;
00112     delete osinstance;
00113     osinstance = NULL;
00114  //   free( X0);
00115     free( A_vals);
00116     ASL_free(&asl);
00117 
00118 }
00119 
00120 OSnLNode* OSnl2osil::walkTree (expr *e)
00121 {
00122     OSnLNode *nlNodePoint;
00123     OSnLNodeVariable *nlNodeVariablePoint;
00124     OSnLNodeNumber *nlNodeNumberPoint;
00125     efunc *op;
00126     expr **ep;
00127     int opnum;
00128     int i;
00129     //std::cout << "Variable Index " << varIdx << std::endl;
00130     int j = ((expr_v *)e - VAR_E) - osinstance->getVariableNumber() ;
00131     //std::cout << "GET OPERATOR NUMBER" << std::endl;
00132     op = e->op;
00133     opnum = Intcast op;
00134     //std::cout << "OPERATOR NUMBER = " << opnum << std::endl;
00135     //Printf ("op %d  optype %d  ", opnum, optype[opnum]);
00136     try
00137     {
00138         switch( opnum)
00139         {
00140         case OPPLUS:
00141             //cout << "FOUND  PLUS NODE"  << endl;
00142             nlNodePoint = new OSnLNodePlus();
00143             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00144             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00145             op_type.push_back( "PLUS");
00146             return nlNodePoint;
00147 
00148         case OPSUMLIST:
00149             i = 0;
00150             //cout << "INSIDE SUM OPERATOR" << endl;
00151             nlNodePoint = new OSnLNodeSum();
00152             nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00153             nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00154             for (ep = e->L.ep; ep < e->R.ep; *ep++)
00155                 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00156             return nlNodePoint;
00157 
00158         case MAXLIST:
00159             i = 0;
00160             //cout << "INSIDE MAX OPERATOR" << endl;
00161             nlNodePoint = new OSnLNodeMax();
00162             nlNodePoint->inumberOfChildren = e->R.ep - e->L.ep;
00163             nlNodePoint->m_mChildren = new OSnLNode*[ e->R.ep - e->L.ep];
00164             for (ep = e->L.ep; ep < e->R.ep; *ep++)
00165                 nlNodePoint->m_mChildren[i++] = walkTree ( *ep);
00166             return nlNodePoint;
00167 
00168         case OPMINUS:
00169             nlNodePoint = new OSnLNodeMinus();
00170 
00171             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00172             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00173             op_type.push_back( "MINUS");
00174 
00175             return nlNodePoint;
00176 
00177         case OPUMINUS:
00178             nlNodePoint = new OSnLNodeNegate();
00179             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00180             return nlNodePoint;
00181 
00182         case OPMULT:
00183             //cout << "FOUND MULT NODE"  << endl;
00184             nlNodePoint = new OSnLNodeTimes();
00185             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00186             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00187             op_type.push_back( "TIMES");
00188             return nlNodePoint;
00189 
00190         case OPDIV:
00191             nlNodePoint = new OSnLNodeDivide();
00192             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00193             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00194             return nlNodePoint;
00195 
00196         case OPPOW:
00197             //cout << "FOUND OPPOW NODE"  << endl;
00198             nlNodePoint = new OSnLNodePower();
00199             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00200             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00201             op_type.push_back( "POWER");
00202             return nlNodePoint;
00203 
00204 
00205         case OP1POW:
00206             //cout << "FOUND OP1POW NODE"  << endl;
00207             //cout << "OP1POW EXPONENT =  "  << e->R.en->v<<  endl;
00208             nlNodePoint = new OSnLNodePower();
00209 
00210 
00211             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00212             nlNodeNumberPoint = new OSnLNodeNumber();
00213             nlNodeNumberPoint->value = e->R.en->v;
00214             nlNodePoint->m_mChildren[1] = nlNodeNumberPoint;
00215 
00216             op_type.push_back( "NUMBER");
00217             op_type.push_back( os_dtoa_format( numkount) );
00218             operand.push_back( e->R.en->v );
00219             numkount++;
00220 
00221             op_type.push_back( "POWER");
00222 
00223 
00224             return nlNodePoint;
00225 
00226         case OP2POW:
00227             //cout << "FOUND OP2POW NODE"  << endl;
00228             //nlNodePoint = new OSnLNodePower();
00229             //nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00230             //nlNodeNumberPoint = new OSnLNodeNumber();
00231             //nlNodeNumberPoint->value = 2;
00232             //nlNodePoint->m_mChildren[1] = nlNodeNumberPoint;
00233             nlNodePoint = new OSnLNodeSquare();
00234 
00235             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00236             op_type.push_back( "SQUARE");
00237             return nlNodePoint;
00238 
00239         case OPCPOW:
00240             //cout << "FOUND OPCPOW NODE"  << endl;
00241             //cout << "OPCPOW EXPONENT =  "  << e->R.en->v<<  endl;
00242             nlNodePoint = new OSnLNodePower();
00243             nlNodeNumberPoint = new OSnLNodeNumber();
00244             nlNodeNumberPoint->value = e->L.en->v;
00245             nlNodePoint->m_mChildren[0] = nlNodeNumberPoint;
00246             nlNodePoint->m_mChildren[1] = walkTree (e->R.e);
00247             return nlNodePoint;
00248 
00249         case OP_log:
00250             nlNodePoint = new OSnLNodeLn();
00251             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00252             op_type.push_back( "LOG");
00253             return nlNodePoint;
00254 
00255         case OP_sqrt:
00256             nlNodePoint = new OSnLNodeSqrt();
00257             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00258             return nlNodePoint;
00259 
00260         case OP_cos:
00261             nlNodePoint = new OSnLNodeCos();
00262             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00263             return nlNodePoint;
00264 
00265         case OP_sin:
00266             nlNodePoint = new OSnLNodeSin();
00267             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00268             return nlNodePoint;
00269 
00270         case OP_exp:
00271             nlNodePoint = new OSnLNodeExp();
00272             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00273             return nlNodePoint;
00274 
00275         case ABS:
00276             nlNodePoint = new OSnLNodeAbs();
00277             nlNodePoint->m_mChildren[0] = walkTree (e->L.e);
00278             return nlNodePoint;
00279 
00280         case OPNUM:
00281             //cout << "found a number node" << endl;
00282             nlNodeNumberPoint = new OSnLNodeNumber;
00283             //cout << "THE NUMBER" << (double) ((expr_n*)e)->v << endl;
00284             nlNodeNumberPoint->value = (double) ((expr_n*)e)->v;
00285             op_type.push_back( "NUMBER");
00286             op_type.push_back( os_dtoa_format(  numkount ) );
00287 
00288             operand.push_back( (double) ((expr_n*)e)->v   );
00289             numkount++;
00290 
00291 
00292             return nlNodeNumberPoint;
00293 
00294         case OPVARVAL:
00295             //cout << "found a variable node" << endl;
00296             // treat the common expression or defined variables
00297             if( j >= 0 )
00298             {
00299                 // process common expression
00300                 /*
00301                 std::cout << "como = "  << como  << std::endl;
00302                 std::cout << "comc = "  << comc  << std::endl;
00303                 std::cout << "comb = "  << comb  << std::endl;
00304                 std::cout << "como1 = "  << como1  << std::endl;
00305                 std::cout << "comc1 = "  << comc1  << std::endl;
00306                 std::cout << "ncom0 = "  << ncom0  << std::endl;
00307                 std::cout << "jjjjjjjjjjjjjjjjjj = "  << j  << std::endl;
00308                 */
00309 
00310                 // Orban: http://www.gerad.ca/~orban/drampl/def-vars.html
00311                 if(j < ncom0)
00312                 {
00313                     struct cexp *common = ((const ASL_fg *) asl) -> I.cexps_ + j ;
00314                     //walk the tree for the non-linear stuff
00315 
00316                     // now add the linear terms
00317                     int nlin = common -> nlin;
00318                     //std::cout << "Number of linear terms in common expression " << nlin << std::endl;
00319 
00320                     if( nlin > 0)
00321                     {
00322                         nlNodePoint = new OSnLNodeSum();
00323                         nlNodePoint->inumberOfChildren = nlin + 1;
00324                         nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00325                         // we have linear variables
00326                         // get the index and coefficient
00327                         linpart *L = common -> L;
00328                         for(int kj = 0; kj < nlin; kj++)
00329                         {
00330 
00331                             // get the coefficient
00332                             //std::cout << "Linear coefficient  "  << L [kj].fac << std::endl;
00333 
00334                             // get the index
00335                             //std::cout  << "Variable index  "  << ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v) << std::endl;
00336                             // add an OSnLSumNode with the linear terms
00337                             nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00338                             nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00339                             nlNodeVariablePoint->coef = L [kj]. fac;
00340                             nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00341                         }
00342                         nlNodePoint->m_mChildren[ nlin] = walkTree(  common->e);
00343                         return nlNodePoint;
00344                     }
00345                     else return walkTree(  common->e);
00346                 }
00347                 else
00348                 {
00349                     struct cexp1 *common = ((const ASL_fg *) asl) -> I.cexps1_ + (j - ncom0);
00350                     //walk the tree for the non-linear stuff
00351 
00352                     // now add the linear terms
00353                     int nlin = common -> nlin;
00354                     //std::cout << "Number of linear terms in common expression " << nlin << std::endl;
00355 
00356                     if( nlin > 0)
00357                     {
00358                         nlNodePoint = new OSnLNodeSum();
00359                         nlNodePoint->inumberOfChildren = nlin + 1;
00360                         nlNodePoint->m_mChildren = new OSnLNode*[ nlin + 1];
00361                         // we have linear variables
00362                         // get the index and coefficient
00363                         linpart *L = common -> L;
00364                         for(int kj = 0; kj < nlin; kj++)
00365                         {
00366 
00367                             // get the coefficient
00368                             //std::cout << "Linear coefficient  "  << L [kj].fac << std::endl;
00369 
00370                             // get the index
00371                             //std::cout  << "Variable index  "  << ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v) << std::endl;
00372                             // add an OSnLSumNode with the linear terms
00373                             nlNodePoint->m_mChildren[ kj] = new OSnLNodeVariable;
00374                             nlNodeVariablePoint = (OSnLNodeVariable*)nlNodePoint->m_mChildren[ kj];
00375                             nlNodeVariablePoint->coef = L [kj]. fac;
00376                             nlNodeVariablePoint->idx = ((uintptr_t) (L [kj].v.rp) - (uintptr_t) VAR_E) / sizeof (expr_v);
00377                         }
00378                         nlNodePoint->m_mChildren[ nlin] = walkTree(  common->e);
00379                         return nlNodePoint;
00380                     }
00381                     else return walkTree(  common->e);
00382                 }
00383             }
00384             //if(e->a > osinstance->getVariableNumber() ) throw ErrorClass("OS cannot handle AMPL user defined variables, please reformulate");
00385             nlNodeVariablePoint = new OSnLNodeVariable;
00386             nlNodeVariablePoint->idx = e->a;
00387             nlNodeVariablePoint->coef = 1.0;
00388 
00389             op_type.push_back( "VARIABLE");
00390             op_type.push_back( os_dtoa_format(  e->a  ) );
00391 
00392 
00393             return nlNodeVariablePoint;
00394             break;
00395         default:
00396             std::ostringstream outStr;
00397             std::string error;
00398             outStr  << endl;
00399             outStr  << endl;
00400             error = "ERROR:  An unsupported operator found, AMPL operator number =  "  ;
00401             outStr << error;
00402             outStr << opnum;
00403             outStr << endl;
00404             error = outStr.str();
00405             throw ErrorClass( error );
00406         }//end switch
00407     }//end try
00408     catch(const ErrorClass& eclass)
00409     {
00410         throw;
00411     }
00412 }//walkTree
00413 
00414 
00415 bool OSnl2osil::createOSInstance()
00416 {
00417     osinstance = new OSInstance();
00418     int i, j;
00419     // put in instanceHeader information
00420     //
00421     osinstance->setInstanceDescription("Generated from AMPL nl file");
00422     //
00423     // put in instanceData information
00424     //
00425     // get the variable information
00426     //
00427     std::string colName;
00428     char vartype = 'C';
00429     osinstance->setVariableNumber( n_var);
00430 
00431 
00432 
00433 
00434     //first the nonlinear variables
00435     //welcome to the world of the ASL API
00436 
00437     int lower;
00438     int upper;
00439     lower = 0;
00440     upper = nlvb - nlvbi;
00441 #ifdef AMPLDEBUG
00442     std::cout << "LOWER = " << lower << std::endl;
00443     std::cout << "UPPER = " << upper << std::endl;
00444 #endif
00445     for(i = lower; i < upper; i++)  //continuous in an objective and in a constraint
00446     {
00447         vartype = 'C';
00448         osinstance->addVariable(i, var_name(i),
00449                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00450                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00451                                 vartype);
00452     }
00453 
00454     lower = nlvb - nlvbi;
00455     upper = (nlvb - nlvbi) + nlvbi;
00456     upper = nlvb; //
00457 #ifdef AMPLDEBUG
00458     std::cout << "LOWER = " << lower << std::endl;
00459     std::cout << "UPPER = " << upper << std::endl;
00460 #endif
00461     for(i = lower; i < upper; i++)  //integer in an objective and in a constraint
00462     {
00463         vartype = 'I';
00464         osinstance->addVariable(i, var_name(i),
00465                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00466                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00467                                 vartype);
00468     }
00469 
00470     lower = nlvb;
00471     upper = nlvb + (nlvc - (nlvb + nlvci)) ;
00472     upper = nlvc - nlvci;
00473 #ifdef AMPLDEBUG
00474     std::cout << "LOWER = " << lower << std::endl;
00475     std::cout << "UPPER = " << upper << std::endl;
00476 #endif
00477     for(i = lower; i < upper; i++)  //continuous just in constraints
00478     {
00479         vartype = 'C';
00480         osinstance->addVariable(i, var_name(i),
00481                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00482                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00483                                 vartype);
00484     }
00485 
00486 
00487     lower = nlvc - nlvci;
00488     upper = nlvc - nlvci + nlvci;
00489     upper = nlvc;
00490 #ifdef AMPLDEBUG
00491     std::cout << "LOWER = " << lower << std::endl;
00492     std::cout << "UPPER = " << upper << std::endl;
00493 #endif
00494     for(i = lower; i < upper; i++)  //integer just in constraints
00495     {
00496         vartype = 'I';
00497         osinstance->addVariable(i, var_name(i),
00498                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00499                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00500                                 vartype);
00501     }
00502 
00503     lower = nlvc;
00504     upper = nlvc + ( nlvo - (nlvc + nlvoi) );
00505     upper = nlvo - nlvoi;
00506 #ifdef AMPLDEBUG
00507     std::cout << "LOWER = " << lower << std::endl;
00508     std::cout << "UPPER = " << upper << std::endl;
00509 #endif
00510     for(i = lower; i < upper; i++)  //continuous just in objectives
00511     {
00512         vartype = 'C';
00513         osinstance->addVariable(i, var_name(i),
00514                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00515                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00516                                 vartype);
00517     }
00518 
00519     lower = nlvo - nlvoi;
00520     upper = nlvo - nlvoi + nlvoi;
00521     upper = nlvo ;
00522 #ifdef AMPLDEBUG
00523     std::cout << "LOWER = " << lower << std::endl;
00524     std::cout << "UPPER = " << upper << std::endl;
00525 #endif
00526     for(i = lower; i < upper; i++)  //integer just in objectives
00527     {
00528         vartype = 'I';
00529         osinstance->addVariable(i, var_name(i),
00530                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00531                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00532                                 vartype);
00533     }
00534 
00535 
00536     //now the other variables
00537 
00538     lower = CoinMax(nlvc, nlvo);
00539     upper = CoinMax(nlvc, nlvo) + nwv;
00540 #ifdef AMPLDEBUG
00541     std::cout << "LOWER = " << lower << std::endl;
00542     std::cout << "UPPER = " << upper << std::endl;
00543 #endif
00544     for(i = lower; i < upper; i++)  //linear arc variables
00545     {
00546         vartype = 'C';
00547         osinstance->addVariable(i, var_name(i),
00548                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00549                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00550                                 vartype);
00551     }
00552 
00553 
00554     lower = CoinMax(nlvc, nlvo) + nwv;
00555     upper =  CoinMax(nlvc, nlvo) + nwv + (n_var - (CoinMax(nlvc, nlvo) + niv + nbv + nwv) );
00556     upper = n_var -  niv - nbv;
00557 #ifdef AMPLDEBUG
00558     std::cout << "LOWER = " << lower << std::endl;
00559     std::cout << "UPPER = " << upper << std::endl;
00560 #endif
00561     for(i = lower; i < upper; i++)  //other linear
00562     {
00563         vartype = 'C';
00564         osinstance->addVariable(i, var_name(i),
00565                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00566                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00567                                 vartype);
00568     }
00569 
00570 
00571     lower = n_var -  niv - nbv;
00572     upper = n_var -  niv - nbv + nbv;
00573     upper = n_var -  niv ;
00574 #ifdef AMPLDEBUG
00575     std::cout << "LOWER = " << lower << std::endl;
00576     std::cout << "UPPER = " << upper << std::endl;
00577 #endif
00578     for(i = lower; i < upper; i++)  //linear binary
00579     {
00580         vartype = 'B';
00581         osinstance->addVariable(i, var_name(i),
00582                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00583                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00584                                 vartype);
00585     }
00586 
00587 
00588 
00589     lower = n_var -  niv;
00590     upper = n_var -  niv  + niv;
00591     upper =   n_var;
00592 #ifdef AMPLDEBUG
00593     std::cout << "LOWER = " << lower << std::endl;
00594     std::cout << "UPPER = " << upper << std::endl;
00595 #endif
00596     for(i = lower; i < upper; i++)  //linear integer
00597     {
00598         vartype = 'I';
00599         osinstance->addVariable(i, var_name(i),
00600                                 LUv[2*i]   > -OSDBL_MAX ? LUv[2*i]   : -OSDBL_MAX,
00601                                 LUv[2*i+1] <  OSDBL_MAX ? LUv[2*i+1] :  OSDBL_MAX,
00602                                 vartype);
00603     }
00604 
00605 
00606     // end of variables -- thank goodness!!!
00607 
00608 
00609 
00610 
00611     //
00612     //
00613     //(expr_v *)e;
00614     //
00615     //
00616     // now create the objective function
00617     // in the nl file, this is stored in dense form; convert to sparse.
00618     //
00619     double objWeight = 1.0;
00620     //  char    *objtype;       /* object type array: 0 == min, 1 == max */
00621     std::string objName="";
00622     SparseVector* objectiveCoefficients = NULL;
00623 
00624     osinstance->setObjectiveNumber( n_obj) ;
00625     for(i = 0; i < n_obj; i++)
00626     {
00627         int n_obj_coef = 0;
00628         for(og = Ograd[i]; og; og = og->next)
00629         {
00630             if (og->coef != 0) n_obj_coef++;
00631         }
00632         objectiveCoefficients = new SparseVector( n_obj_coef);
00633         int i_obj_coef = -1;
00634         for(og = Ograd[i]; og; og = og->next)
00635         {
00636             if (fabs(og->coef) > OS_EPS)
00637             {
00638                 i_obj_coef++;
00639                 objectiveCoefficients->values[i_obj_coef] = og->coef;
00640                 objectiveCoefficients->indexes[i_obj_coef] = og->varno;
00641             }
00642         }
00643         osinstance->addObjective(-n_obj + i, objName,
00644                                  (objtype[i] == 1)?"max":"min",
00645                                  objconst( i),  objWeight, objectiveCoefficients) ;
00646         delete objectiveCoefficients; // delete the temporary sparse vector
00647         objectiveCoefficients = NULL;
00648     }
00649     //
00650     // now fill in row information
00651     //
00652     osinstance->setConstraintNumber( n_con);
00653     // kipp -- important  -- figure out where the nl file stores a rhs constant
00654     double constant = 0.0;
00655     std::string rowName;
00656     for(i = 0; i < n_con; i++)
00657     {
00658         osinstance->addConstraint(i, con_name(i),
00659                                   LUrhs[2*i] > -OSDBL_MAX ? LUrhs[2*i] : -OSDBL_MAX,
00660                                   LUrhs[2*i+1] < OSDBL_MAX ? LUrhs[2*i+1] : OSDBL_MAX,
00661                                   constant);
00662     }
00663     //
00664     // Now the A-matrix
00665     //
00666     int colStart, colEnd, nCoefSqueezed;
00667     nCoefSqueezed = 0;
00668 
00669 #ifdef AMPLDEBUG
00670     cout << "A-matrix elements: ";
00671     for (i = 0; i < A_colstarts[ n_var]; i++)
00672         cout << A_vals[i] << " ";
00673     cout << endl;
00674     cout << "A-matrix rowinfo: ";
00675     for (i = 0; i < A_colstarts[ n_var]; i++)
00676         cout << A_rownos[i] << " ";
00677     cout << endl;
00678     cout << "A-matrix colstart: ";
00679     for (i = 0; i <= n_var; i++)
00680         cout << A_colstarts[i] << " ";
00681     cout << endl;
00682 #endif
00683 
00684     colEnd = 0;
00685     for (i = 0; i < n_var; i++)
00686     {
00687         colStart = colEnd;
00688         colEnd   = A_colstarts[i+1];
00689 #ifdef AMPLDEBUG
00690         cout << "col " << i << " from " << colStart << " to " << colEnd - 1 << endl;
00691 #endif
00692         for (j = colStart; j < colEnd; j++)
00693         {
00694             if (fabs(A_vals[ j]) > OS_EPS)
00695             {
00696                 A_vals[ j-nCoefSqueezed] = A_vals[ j];
00697                 A_rownos[ j-nCoefSqueezed] = A_rownos[j];
00698             }
00699             else
00700             {
00701 #ifdef AMPLDEBUG
00702                 cout << "squeeze out element " << j << endl;
00703 #endif
00704                 nCoefSqueezed++;
00705             }
00706         }
00707         A_colstarts[i+1] = A_colstarts[i+1] - nCoefSqueezed;
00708     }
00709 
00710 #ifdef AMPLDEBUG
00711     cout << "A-matrix elements: ";
00712     for (i = 0; i < A_colstarts[ n_var]; i++)
00713         cout << A_vals[i] << " ";
00714     cout << endl;
00715     cout << "A-matrix rowinfo: ";
00716     for (i = 0; i < A_colstarts[ n_var]; i++)
00717         cout << A_rownos[i] << " ";
00718     cout << endl;
00719     cout << "A-matrix colstart: ";
00720     for (i = 0; i <= n_var; i++)
00721         cout << A_colstarts[i] << " ";
00722     cout << endl;
00723     cout << "A-matrix nonzeroes: " << A_colstarts[ n_var] << "; nsqueezed: " << nCoefSqueezed << endl;
00724 #endif
00725 
00726     if(A_colstarts[ n_var] > 0)
00727     {
00728         osinstance->setLinearConstraintCoefficients(A_colstarts[ n_var],  true,
00729                 A_vals,   0,  A_colstarts[n_var] - 1,
00730                 A_rownos, 0,  A_colstarts[n_var] - 1,
00731                 A_colstarts,  0,  n_var);
00732     }
00733     /*  int valuesBegin = 0;
00734         int valuesEnd = A_colstarts[ n_var] - 1;
00735         int startsBegin = 0;
00736         int indexesBegin = 0;
00737         int indexesEnd = A_colstarts[n_var] - 1;
00738 
00739         // if A_vals has only zeros don't generate a linearConstraints section
00740         bool bNumAvalsIsPositive = false;
00741         i = valuesBegin;
00742         while( (i < valuesEnd) && (bNumAvalsIsPositive == false) ){
00743                 if(A_vals[ i] != 0) bNumAvalsIsPositive = true;
00744                 i++;
00745         }
00746         if(bNumAvalsIsPositive == true){
00747                 osinstance->setLinearConstraintCoefficients(nzc,  true,
00748                         A_vals, valuesBegin,  valuesEnd,
00749                         A_rownos,  indexesBegin,  indexesEnd,
00750                         A_colstarts,  startsBegin,  n_var);
00751         }
00752     */
00753 
00754     // Kipp: can AMPL identify QPs???
00755     //osinstance->setQuadraticTerms(numberOfQPTerms, VarOneIdx, VarTwoIdx, Coeff, begin, end);
00756     //loop over each row with a nonlinear term
00757     //
00758     if((nlc + nlo) > 0)
00759     {
00760         OSnLNode* m_treeRoot;
00761         //cout << nlc << endl;
00762         //cout << nlo << endl;
00763         osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nlc + nlo;
00764         osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ nlc + nlo ];
00765         int iNLidx = 0;
00766         std::cout << "WALK THE TREE FOR NONLINEAR CONSTRAINT TERMS:  " << nlc << std::endl;
00767 
00768 
00769         if(nlc > 0)
00770         {
00771             while (iNLidx < nlc)
00772             {
00773                 m_treeRoot = walkTree ((CON_DE + iNLidx)->e);
00774                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx] = new Nl();
00775                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->idx = iNLidx;
00776                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree = new OSExpressionTree();
00777                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree->m_treeRoot = m_treeRoot;
00778                 iNLidx++;
00779                 //std::cout << m_treeRoot->getNonlinearExpressionInXML() << std::endl;
00780             }
00781         }
00782 
00783         std::cout << "WALK THE TREE FOR NONLINEAR OBJECTIVE TERMS  " << nlo  << std::endl;
00784         if(nlo > 0)
00785         {
00786             while ( iNLidx < nlc + nlo)
00787             {
00788                 m_treeRoot = walkTree ((OBJ_DE + iNLidx - nlc)->e);
00789                 //std::cout << "CREATING A NEW NONLINEAR TERM IN THE OBJECTIVE" << std::endl;
00790                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx] = new Nl();
00791                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->idx = -1 - (iNLidx - nlc);
00792                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree = new OSExpressionTree();
00793                 osinstance->instanceData->nonlinearExpressions->nl[ iNLidx]->osExpressionTree->m_treeRoot = m_treeRoot;
00794                 iNLidx++;
00795                 //std::cout << m_treeRoot->getNonlinearExpressionInXML() << std::endl;
00796             }
00797         }
00798         //std::cout << "DONE WALKING THE TREE FOR NONLINEAR OBJECTIVE TERMS" << std::endl;
00799 
00800 
00801     }
00802 //      delete objectiveCoefficients;
00803 //      objectiveCoefficients = NULL;
00804     //
00805     // end loop of nonlinear rows
00806     //
00807 #ifdef AMPLDEBUG
00808     OSiLWriter osilwriter;
00809     std::cout << "WRITE THE INSTANCE" << std::endl;
00810     osilwriter.m_bWhiteSpace = true;
00811     std::cout << osilwriter.writeOSiL( osinstance) << std::endl;
00812     std::cout << "DONE WRITE THE INSTANCE" << std::endl;
00813 
00814     std::cout << osinstance->printModel() << std::endl;
00815 
00816 #endif
00817    
00818     return true;
00819 }

Generated on Thu Nov 10 03:05:49 2011 by  doxygen 1.4.7