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

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

Generated on Mon May 3 03:05:21 2010 by  doxygen 1.4.7