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

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

Generated on Sun Mar 20 03:06:29 2011 by  doxygen 1.4.7