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

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

Generated on Thu Mar 31 03:13:08 2011 by  doxygen 1.4.7