Changeset 298


Ignore:
Timestamp:
Feb 13, 2010 9:16:56 AM (11 years ago)
Author:
pbelotti
Message:

merge reformulation and checkNLP fixes from trunk

Location:
stable/0.3/Couenne/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable/0.3/Couenne/src/problem/checkNLP.cpp

    r154 r298  
    1 /* $Id$ */
    2 /*
     1/* $Id$
     2 *
    33 * Name:    checkNLP.cpp
    44 * Author:  Pietro Belotti
    55 * Purpose: check NLP feasibility of incumbent integer solution
    66 *
    7  * (C) Carnegie-Mellon University, 2006-09.
     7 * (C) Carnegie-Mellon University, 2006-10.
    88 * This file is licensed under the Common Public License (CPL)
    99 */
     
    1515bool CouenneProblem::checkNLP (const double *solution, double &obj, bool recompute) const {
    1616
    17   const int infeasible = 1;
    18   const int wrong_obj  = 2;
    19 
    20   /*printf ("checking solution: [%.12e] ", obj);
    21   for (int i=0; i<nOrigVars_; i++)
    22     printf ("%.12e ", solution [i]);
    23     printf ("\n");*/
    24 
    25   // pre-check on original variables
    26   for (int i=0; i < nOrigVars_; i++) {
     17  if (Jnlst () -> ProduceOutput (Ipopt::J_ALL, J_PROBLEM)) {
     18
     19    printf ("checking solution: [%.12e] ", obj);
     20
     21    for (int i=0; i<nOrigVars_; i++)
     22      printf ("%.12e ", solution [i]);
     23    printf ("\n");
     24  }
     25
     26  // pre-check on original variables --- this is done after every LP,
     27  // and should be efficient
     28  for (register int i=0; i < nOrigVars_; i++) {
    2729
    2830    CouNumber val = solution [i];
     
    3032    // check (original and auxiliary) variables' integrality
    3133
    32     if ((variables_ [i] -> isInteger ()) &&
    33         (variables_ [i] -> Type () == VAR) &&
    34         (variables_ [i] -> Multiplicity () > 0) &&
     34    exprVar *v = variables_ [i];
     35
     36    if ((v -> Type ()      == VAR) &&
     37        (v -> Multiplicity () > 0) &&
     38        (v -> isInteger ())        &&
    3539        (fabs (val - COUENNE_round (val)) > feas_tolerance_)) {
    3640
     
    4246    }
    4347  }
     48
     49  const int infeasible = 1;
     50  const int wrong_obj  = 2;
    4451
    4552  CouNumber *sol = new CouNumber [nVars ()];
     
    5461  domain_.push (nVars (), sol, domain_.lb (), domain_.ub (), false);
    5562
    56   /*printf ("checknlp: %d vars -------------------\n", domain_.current () -> Dimension ());
    57   for (int i=0; i<domain_.current () -> Dimension (); i++)
    58     printf ("%4d %.12e [%.12e %.12e]\n",
    59     i, domain_.x (i), domain_.lb (i), domain_.ub (i));*/
     63
     64  if (Jnlst () -> ProduceOutput (Ipopt::J_ALL, J_PROBLEM)) {
     65    printf ("checknlp: %d vars -------------------\n", domain_.current () -> Dimension ());
     66    for (int i=0; i<domain_.current () -> Dimension (); i++)
     67      printf ("%4d %.12e [%.12e %.12e]\n",
     68              i, domain_.x (i), domain_.lb (i), domain_.ub (i));
     69  }
    6070
    6171  expression *objBody = Obj (0) -> Body ();
     
    7484      (*(objBody -> Image () ? objBody -> Image () : objBody)) ();
    7585
    76   /*printf ("%.12e %.12e %.12e ------------------------------\n",
    77           realobj, sol [objBody -> Index ()],
    78           (*(objBody -> Image () ? objBody -> Image () : objBody)) ());*/
     86  if (Jnlst () -> ProduceOutput (Ipopt::J_ALL, J_PROBLEM)) {
     87    printf ("%.12e %.12e %.12e ------------------------------\n",
     88            realobj, sol [objBody -> Index ()],
     89            (*(objBody -> Image () ? objBody -> Image () : objBody)) ());
     90  }
    7991
    8092  bool retval = true;
     
    8395
    8496    // check if objective corresponds
    85 
     97   
    8698    if (fabs (realobj - obj) / (1. + fabs (realobj)) > feas_tolerance_) {
    8799
     
    97109      obj = realobj;
    98110
    99     //printf ("recomputed: %.12e\n", obj);
     111    if (Jnlst () -> ProduceOutput (Ipopt::J_ALL, J_PROBLEM))
     112      printf ("recomputed: %.12e\n", obj);
    100113
    101114    for (int i=0; i < nOrigVars_; i++) {
     
    130143        throw infeasible;
    131144      }
    132 
    133       /*if (variables_ [i] -> Type () == AUX) {
    134         printf ("checking aux ");
    135         variables_ [i] -> print (); printf (" := ");
    136         variables_ [i] -> Image () -> print ();
    137         printf (" --- %.12e = %.12e [%.12e]; {",
    138                 (*(variables_ [i])) (),
    139                 (*(variables_ [i] -> Image ())) (),
    140                 (*(variables_ [i])) () -
    141                 (*(variables_ [i] -> Image ())) ());
    142         for (int j=0; j<nVars (); j++)
    143           printf ("%.12e ", (*(variables_ [j])) ());
    144         printf ("}\n");
    145         }*/
     145    }
     146
     147    // check ALL auxs
     148
     149    for (int i=0; i < nVars (); i++) {
     150
     151      if (Jnlst () -> ProduceOutput (Ipopt::J_ALL, J_PROBLEM)) {
     152        if (variables_ [i] -> Type () == AUX) {
     153          printf ("checking aux ");
     154          variables_ [i] -> print (); printf (" := ");
     155          variables_ [i] -> Image () -> print ();
     156          printf (" --- %.12e = %.12e [%.12e]; {",
     157                  (*(variables_ [i])) (),
     158                  (*(variables_ [i] -> Image ())) (),
     159                  (*(variables_ [i])) () -
     160                  (*(variables_ [i] -> Image ())) ());
     161          //for (int j=0; j<nVars (); j++)
     162          //printf ("%.12e ", (*(variables_ [j])) ());
     163          printf ("}\n");
     164        }
     165      }
    146166
    147167      CouNumber delta;
     
    171191        rhs  = (*(c -> Ub   ())) ();
    172192
    173       if ((rhs < COUENNE_INFINITY) &&
    174            (body > rhs + feas_tolerance_ * (1 + CoinMax (fabs (body), fabs (rhs)))) ||
    175           (lhs > -COUENNE_INFINITY) &&
    176           (body < lhs - feas_tolerance_ * (1 + CoinMax (fabs (body), fabs (lhs))))) {
     193      if (((rhs < COUENNE_INFINITY) &&
     194           (body > rhs + feas_tolerance_ * (1 + CoinMax (fabs (body), fabs (rhs))))) ||
     195          ((lhs > -COUENNE_INFINITY) &&
     196           (body < lhs - feas_tolerance_ * (1 + CoinMax (fabs (body), fabs (lhs)))))) {
    177197
    178198        if (Jnlst()->ProduceOutput(Ipopt::J_MOREVECTOR, J_PROBLEM)) {
    179199
    180           Jnlst()->Printf
    181             (Ipopt::J_MOREVECTOR, J_PROBLEM,
    182              "checkNLP: constraint %d violated (lhs=%+e body=%+e rhs=%+e, violation %g): ",
    183              i, lhs, body, rhs, CoinMax (lhs-body, body-rhs));
     200          printf ("checkNLP: constraint %d violated (lhs=%+e body=%+e rhs=%+e, violation %g): ",
     201                  i, lhs, body, rhs, CoinMax (lhs-body, body-rhs));
    184202
    185203          c -> print ();
  • stable/0.3/Couenne/src/problem/problemIO.cpp

    r223 r298  
    1 /* $Id$ */
    2 /*
     1/* $Id$
     2 *
    33 * Name:    problemIO.cpp
    44 * Author:  Pietro Belotti
    55 * Purpose: methods of the class CouenneProblem
    66 *
    7  * (C) Carnegie-Mellon University, 2006.
     7 * (C) Carnegie-Mellon University, 2006-10.
    88 * This file is licensed under the Common Public License (CPL)
    99 */
     
    7171    for (std::vector <expression *>::iterator i = commonexprs_.begin ();
    7272         i != commonexprs_.end (); ++i) {
     73      out << "v["    << i - commonexprs_.begin () << "] := ";
    7374      (*i) -> print (out);
    7475      out << std::endl;
  • stable/0.3/Couenne/src/standardize/constrStandardize.cpp

    r273 r298  
    55 * Purpose: standardization of constraints
    66 *
    7  * (C) Carnegie-Mellon University, 2007-09.
     7 * (C) Carnegie-Mellon University, 2007-10.
    88 * This file is licensed under the Common Public License (CPL)
    99 */
     
    1818//#define DEBUG
    1919
     20// replace a variable
     21void replace (CouenneProblem *p, int wind, int xind);
     22
     23
    2024/// decompose body of constraint through auxiliary variables
    21 
    2225exprAux *CouenneConstraint::standardize (CouenneProblem *p) {
    2326
     
    9699      if (xind >= 0) {
    97100
    98         exprVar
    99           *varStays  = p -> Variables () [xind],
    100           *varLeaves = p -> Variables () [wind];
    101 
    102         // intersect features of the two variables (integrality, bounds)
    103 
    104         varStays -> lb () = varLeaves -> lb () = CoinMax (varStays -> lb (), varLeaves -> lb ());
    105         varStays -> ub () = varLeaves -> ub () = CoinMin (varStays -> ub (), varLeaves -> ub ());
    106 
    107         if (varStays  -> isInteger () ||
    108             varLeaves -> isInteger ()) {
    109 
    110           varStays -> lb () = ceil  (varStays -> lb ());
    111           varStays -> ub () = floor (varStays -> ub ());
    112 
    113           if (varStays -> Type () == AUX)
    114             varStays -> setInteger (true);
    115           else {
    116             //expression *old = varStays; // !!! leak
    117             p -> Variables () [xind] = varStays = new exprIVar (xind, p -> domain ());
    118             p -> auxiliarize (varStays); // replace it everywhere in the problem
    119             //delete old;
    120           }
    121         }
     101        replace (p, wind, xind);
    122102
    123103        p -> auxiliarize (p -> Var (wind), p -> Var (xind));
     
    158138          (*i) -> print (); printf (" := ");
    159139          (*i) -> Image () -> print (); printf ("\n");
     140
     141          // if this is an original variable and is competing with an
     142          // auxiliary variable, or at least this has a lower index,
     143          // we better replace it throughout and eliminate that
     144          // aux. See globallib/st_glmp_fp3 for an example where this
     145          // otherwise would be a bug (x_1 unlinked from x2-x3 and
     146          // leading to unbounded)
     147         
     148          int xind = (*i) -> Index (), iMax, iMin;
     149
     150          if (xind < wind) {
     151            iMax = wind;
     152            iMin = xind;
     153          } else {
     154            iMax = xind;
     155            iMin = wind;
     156          }
     157
     158          replace (p, iMax, iMin);
     159
     160          p -> auxiliarize (p -> Var (iMax), p -> Var (iMin));
     161          p -> Var (iMax) -> zeroMult (); // redundant variable is neutralized
     162          p -> auxiliarize (w);
    160163        }
    161164#endif
     
    172175  return body_ -> standardize (p);
    173176}
     177
     178
     179// Replace a variable ////////////////////////////////
     180void replace (CouenneProblem *p, int wind, int xind) {
     181
     182  exprVar
     183    *varLeaves = p -> Variables () [wind],
     184    *varStays  = p -> Variables () [xind];
     185
     186  // intersect features of the two variables (integrality, bounds)
     187
     188  varStays -> lb () = varLeaves -> lb () = CoinMax (varStays -> lb (), varLeaves -> lb ());
     189  varStays -> ub () = varLeaves -> ub () = CoinMin (varStays -> ub (), varLeaves -> ub ());
     190
     191  if (varStays  -> isInteger () ||
     192      varLeaves -> isInteger ()) {
     193
     194    varStays -> lb () = ceil  (varStays -> lb ());
     195    varStays -> ub () = floor (varStays -> ub ());
     196
     197    if (varStays -> Type () == AUX)
     198      varStays -> setInteger (true);
     199    else {
     200      //expression *old = varStays; // !!! leak
     201      p -> Variables () [xind] = varStays = new exprIVar (xind, p -> domain ());
     202      p -> auxiliarize (varStays); // replace it everywhere in the problem
     203      //delete old;
     204    }
     205  }
     206}
Note: See TracChangeset for help on using the changeset viewer.