/home/coin/SVN-release/OS-2.4.0/Couenne/src/readnl/nl2e.cpp

Go to the documentation of this file.
00001 /* $Id: nl2e.cpp 603 2011-06-03 10:48:19Z pbelotti $
00002  *
00003  * Name:    nl2e.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: converts a nl expression into a Couenne expression
00006  *
00007  * (C) Carnegie-Mellon University, 2006-09.
00008  * This file is licensed under the Eclipse Public License (EPL)
00009  */
00010 
00011 #include "CouenneTypes.hpp"
00012 
00013 #include "CouenneExprVar.hpp"
00014 #include "CouenneExprAbs.hpp"
00015 #include "CouenneExprSum.hpp"
00016 #include "CouenneExprSub.hpp"
00017 #include "CouenneExprMul.hpp"
00018 #include "CouenneExprDiv.hpp"
00019 #include "CouenneExprInv.hpp"
00020 #include "CouenneExprSin.hpp"
00021 #include "CouenneExprPow.hpp"
00022 #include "CouenneExprClone.hpp"
00023 #include "CouenneExprLog.hpp"
00024 #include "CouenneExprOpp.hpp"
00025 #include "CouenneExprCos.hpp"
00026 #include "CouenneExprExp.hpp"
00027 
00028 #include "asl.h"
00029 #include "nlp.h"
00030 #include "opcode.hd"
00031 
00032 using namespace Couenne;
00033 
00034 // get ASL op. code relative to function pointer passed as parameter 
00035 size_t getOperator (efunc *);
00036 
00037 
00038 // warning for non-implemented functions -- return 0 constant expression
00039 //expression *notimpl (const std::string &fname) {
00040 void notimpl (const std::string &fname) {
00041   std::cerr << "*** Error: " << fname << " not implemented" << std::endl;
00042   exit (-1);
00043 }
00044 
00045 
00046 // converts an AMPL expression (sub)tree into an expression* (sub)tree
00047 expression *CouenneProblem::nl2e (expr *e, const ASL *asl) {
00048 
00049   switch (getOperator (e -> op)) {
00050 
00051   case OPPLUS:  return new exprSum (nl2e (e -> L.e, asl), nl2e (e -> R.e, asl));
00052   case OPMINUS: return new exprSub (nl2e (e -> L.e, asl), nl2e (e -> R.e, asl));
00053   case OPMULT:  return new exprMul (nl2e (e -> L.e, asl), nl2e (e -> R.e, asl));
00054   case OPDIV:   return new exprDiv (nl2e (e -> L.e, asl), nl2e (e -> R.e, asl));
00055     //case OPREM:   notimpl ("remainder");
00056   case OPPOW:   return new exprPow (nl2e (e -> L.e, asl), nl2e (e -> R.e, asl));
00057     //case OPLESS:  notimpl ("less");
00058     //case MINLIST: notimpl ("min");
00059     //case MAXLIST: notimpl ("max");
00060     //case FLOOR:   notimpl ("floor");
00061     //case CEIL:    notimpl ("ceil");
00062   case ABS:     return new exprAbs (nl2e (e -> L.e, asl));
00063   case OPUMINUS:return new exprOpp (nl2e (e -> L.e, asl));
00064     //          return new exprOpp (nl2e (e -> L.e -> L.e, asl));
00065     //case OPIFnl:  { notimpl ("ifnl");
00066 
00067     // see ASL/solvers/rops.c, IfNL
00068     //}
00069 
00070   case OP_tanh: return new exprDiv 
00071       (new exprSub (new exprExp (nl2e (e -> L.e, asl)),
00072                     new exprExp (new exprOpp (nl2e (e->L.e, asl)))),
00073        new exprSum (new exprExp (nl2e (e -> L.e, asl)),
00074                     new exprExp (new exprOpp (nl2e (e->L.e, asl)))));
00075 
00076   case OP_tan: {
00077     expression *arg;
00078     arg = nl2e (e -> L.e, asl);
00079     return new exprDiv (new exprSin (arg), new exprCos (new exprClone (arg)));
00080   }
00081   case OP_sqrt:    return new exprPow (nl2e (e -> L.e, asl), new exprConst (0.5));
00082   case OP_sinh:    return new exprMul (new exprConst (0.5),
00083                                        new exprSub (new exprExp (nl2e (e -> L.e, asl)),
00084                                                     new exprExp (new exprOpp (nl2e (e->L.e, asl)))));
00085   case OP_sin:     return new exprSin (nl2e (e -> L.e, asl));
00086   case OP_log10:   return new exprMul (new exprConst (1.0 / log (10.0)), 
00087                                        new exprLog (nl2e (e -> L.e, asl)));
00088   case OP_log:     return new exprLog (nl2e (e -> L.e, asl));
00089   case OP_exp:     return new exprExp (nl2e (e -> L.e, asl));
00090   case OP_cosh:    return new exprMul (new exprConst (0.5),
00091                                        new exprSum (new exprExp (nl2e (e -> L.e, asl)),
00092                                                     new exprExp (new exprOpp (nl2e (e->L.e, asl)))));
00093 
00094   case OP_cos:   return new exprCos (nl2e (e -> L.e, asl));
00095     //case OP_atanh: notimpl ("atanh");
00096     //case OP_atan2: notimpl ("atan2");
00097     //case OP_atan:  notimpl ("atan");
00098     //case OP_asinh: notimpl ("asinh");
00099     //case OP_asin:  notimpl ("asin");
00100     //case OP_acosh: notimpl ("acosh");
00101     //case OP_acos:  notimpl ("acos");
00102 
00103   case OPSUMLIST: {
00104     int i=0;
00105     expression **al = new expression * [(e->R.ep - e->L.ep)];
00106     for (expr **ep = e->L.ep; ep < e->R.ep; ep++)
00107       al [i++] = nl2e (*ep, asl);
00108     return new exprSum (al, i);
00109   }
00110     //case OPintDIV: notimpl ("intdiv");
00111     //case OPprecision: notimpl ("precision");
00112     //case OPround:  notimpl ("round");
00113     //case OPtrunc:  notimpl ("trunc");
00114 
00115   case OP1POW: return new exprPow (nl2e (e -> L.e, asl), 
00116                                    new exprConst (((expr_n *)e->R.e)->v));
00117   case OP2POW: return new exprPow (nl2e (e -> L.e, asl), 
00118                                    new exprConst (2.));
00119   case OPCPOW: return new exprPow (new exprConst (((expr_n *)e->L.e)->v),
00120                                    nl2e (e -> R.e, asl));
00121     //case OPFUNCALL: notimpl ("function call");
00122   case OPNUM:     return new exprConst (((expr_n *)e)->v);
00123     //case OPPLTERM:  notimpl ("plterm");
00124     //case OPIFSYM:   notimpl ("ifsym");
00125     //case OPHOL:     notimpl ("hol");
00126   case OPVARVAL:  {
00127 
00128     int j = ((expr_v *) e) -> a;
00129 
00130     if (j >= nOrigVars_) // common expression
00131       // use base pointer otherwise the .a field returns an awkward, out-of-bound index
00132       // TODO: fix! In itointqor.nl should return v51=y44 but returns v52=y44
00133       //                                          v??=y39 but returns v79=y39
00134       j = ((expr_v *) e) - ((const ASL_fg *) asl) -> I.var_e_; 
00135 
00136     if (j >= nOrigVars_ + ndefined_) {
00137       printf ("error: unknown variable x_%d\n", j);
00138       //return new exprClone (variables_ [0]);
00139       exit (-1);
00140     }
00141 
00142     return new exprClone (variables_ [j]);
00143   }
00144 
00145   default: 
00146     printf ("ERROR: unknown operator (address %p), aborting.\n", Intcast (e -> op));
00147     exit (-1);
00148     //return new exprConst (0);
00149   }
00150 
00151   return new exprConst (0.);
00152 }

Generated on Thu Sep 22 03:05:59 2011 by  doxygen 1.4.7