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

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

Generated on Tue Mar 30 03:04:40 2010 by  doxygen 1.4.7