/home/coin/SVN-release/OS-2.1.0/Couenne/src/util/drawCuts.cpp

Go to the documentation of this file.
00001 /* $Id: drawCuts.cpp 154 2009-06-16 18:52:53Z pbelotti $ */
00002 /*
00003  * Name:    drawCuts.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: print function and cut information to be later displayed
00006  *          through PlotUtils' graph
00007  *
00008  * (C) Carnegie-Mellon University, 2006. 
00009  * This file is licensed under the Common Public License (CPL)
00010  */
00011 
00012 #include "CouenneCutGenerator.hpp"
00013 #include "CouenneProblem.hpp"
00014 #include "exprAux.hpp"
00015 
00016 
00017 // [cool!] print graph-readable output for displaying inequalities
00018 // on a Cartesian plane
00019 
00020 void draw_cuts (OsiCuts &cs, const CouenneCutGenerator *cg, int j, expression *w, expression *img) {
00021 
00022   static bool first_draw = true;
00023   static CouNumber maxY = -COUENNE_INFINITY,
00024                    minY =  COUENNE_INFINITY;
00025 
00026   if ((img -> code () == COU_EXPRSIN) || 
00027       (img -> code () == COU_EXPRPOW) || 
00028       (img -> code () == COU_EXPREXP) || 
00029       (img -> code () == COU_EXPRLOG) || 
00030       (img -> code () == COU_EXPRCOS)) {
00031 
00032     //    fprintf (stderr, " ==> "); w -> print (std::cerr); fprintf (stderr, "\n");
00033 
00034     expression *lbe, *ube;
00035 
00036     expression *indep = img -> Argument ();
00037 
00038     if (!indep) 
00039       indep = img -> ArgList () [0];//getFixVar ();
00040 
00041     int xi = indep -> Index ();
00042 
00043     //    fprintf (stderr, "looking into w_%d = f (x_%d)\n", 
00044     //       w -> Index (), xi);
00045 
00046     indep -> getBounds (lbe, ube);
00047 
00048     CouNumber lb = (*lbe) (),
00049       ub = (*ube) ();
00050 
00051     delete lbe;
00052     delete ube;
00053 
00054     if (xi >= 0) {
00055 
00056       CouNumber curx = cg -> Problem () -> X (xi);
00057 
00058 #define N_STEPS 100
00059 
00060       // plot function
00061 
00062       if (first_draw) {
00063 
00064         first_draw = false;
00065 
00066         for (CouNumber x = lb; 
00067              x <= ub + COUENNE_EPS; 
00068              x += ((ub - lb) / N_STEPS)) {
00069 
00070           cg -> Problem () -> X () [xi] = x;
00071           
00072           CouNumber y = (*img) ();
00073 
00074           if (y > maxY) maxY = y;
00075           if (y < minY) minY = y;
00076 
00077           fprintf (stderr, "%.12e %.12e\n", x, y);
00078         }
00079 
00080         maxY += (maxY-minY) / 20;
00081         minY -= (maxY-minY) / 20;
00082       }
00083         
00084       lb -= (ub-lb) / 20;
00085       ub += (ub-lb) / 20;
00086 
00087       // plot lines defining constraint (only for cuts involving at
00088       // most two variables (that is, w is a unary function)
00089       for (int jj=j; jj < cs.sizeRowCuts (); jj++) {
00090 
00091         CouNumber lb0 = lb, 
00092           ub0 = ub;
00093 
00094         const double *el  = cs.rowCutPtr (jj) -> row (). getElements ();
00095         double  rhs = cs.rowCutPtr (jj) -> rhs ();
00096 
00097         if (fabs (el [1]) > COUENNE_EPS) {
00098           lb0 = CoinMax (lb, CoinMin ((rhs - el[0] * minY) / el [1], (rhs - el[0] * maxY) / el [1]));
00099           ub0 = CoinMin (ub, CoinMax ((rhs - el[0] * minY) / el [1], (rhs - el[0] * maxY) / el [1]));
00100         }
00101 
00102         fprintf (stderr, "#m=2,S=%d\n", (cs.rowCutPtr (jj) -> sense () == 'L') ? 10:11);
00103 
00104         fprintf (stderr, "%.12e %.12e\n", lb0, (rhs - el [1] * lb0) / el [0]);
00105         fprintf (stderr, "%.12e %.12e\n", ub0, (rhs - el [1] * ub0) / el [0]);
00106       }
00107 
00108       cg -> Problem () -> X () [xi] = curx;
00109       exit(0);
00110     }
00111   }
00112 }

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