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

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

Generated on Mon Aug 3 03:02:23 2009 by  doxygen 1.4.7