Changeset 514


Ignore:
Timestamp:
Apr 26, 2007 9:21:44 PM (12 years ago)
Author:
pbonami
Message:

Put back forgotten heuristic in BonminSetup?

Location:
trunk/Bonmin/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bonmin/src/Algorithms/BonBabSetupBase.cpp

    r511 r514  
    430430  if(GetRawPtr(options_) == NULL || GetRawPtr(roptions_) == NULL || GetRawPtr(journalist_) == NULL)
    431431    initializeOptionsAndJournalist();
    432   std::cout<<"Reading options...."<<std::endl;
    433432  if(is.good()){
    434433    options_->ReadFromStream(*journalist_, is);
  • trunk/Bonmin/src/Algorithms/BonBonminSetup.cpp

    r511 r514  
    341341    if(algo == B_OA){
    342342      options_->SetNumericValue("oa_dec_time_limit",DBL_MAX, true, true);
    343       options_->SetNumericValue("nlp_solve_frequency", 0, true, true);
     343      options_->SetIntegerValue("nlp_solve_frequency", 0, true, true);
    344344      intParam_[BabLogLevel] = 0;
    345345    }
     
    409409      cutGenerators_.push_back(cg);
    410410    }
     411   
     412    DummyHeuristic * oaHeu = new DummyHeuristic;
     413    oaHeu->setNlp(nonlinearSolver_);
     414    heuristics_.push_back(oaHeu);
    411415}
    412416
    413417Algorithm BonminSetup::getAlgorithm(){
    414   if(algo_ == Dummy)
     418  if(algo_ != Dummy)
    415419    return algo_;
    416420  if(IsValid(options_)){
  • trunk/Bonmin/src/Algorithms/OaGenerators/BonDummyHeuristic.cpp

    r464 r514  
    3030/// Assign an OsiTMINLPInterface
    3131  void
    32   DummyHeuristic::assignInterface(OsiTMINLPInterface * si)
     32  DummyHeuristic::setNlp(OsiTMINLPInterface * si)
    3333  {
    3434    nlp_ = si;
  • trunk/Bonmin/src/Algorithms/OaGenerators/BonDummyHeuristic.hpp

    r464 r514  
    2828        knowsSolution(copy.knowsSolution)
    2929    {}
    30     /// Assign an OsiTMINLPInterface
    31     void assignInterface(OsiTMINLPInterface * si);
     30    /// Set nlp_
     31    void setNlp(OsiTMINLPInterface * si);
    3232    /// heuristic method
    3333    virtual int solution(double &solutionValue, double *betterSolution);
  • trunk/Bonmin/src/Algorithms/OaGenerators/BonOACutGenerator2.cpp

    r481 r514  
    246246     
    247247      milpBound = subMip->lowBound();
    248       std::cout<<"MILP bound "<<milpBound<<std::endl;
     248 //     std::cout<<"MILP bound "<<milpBound<<std::endl;
    249249     
    250250            if(subMip->optimal())
  • trunk/Bonmin/src/Interfaces/Ampl/BonAmplTMINLP.cpp

    r481 r514  
    3333  :
    3434  TMINLP(),
     35  upperBoundingObj_(-1),
    3536  ampl_tnlp_(NULL),
    3637  branch_(),
     
    4849  :
    4950  TMINLP(),
     51  upperBoundingObj_(-1),
    5052  ampl_tnlp_(NULL),
    5153  branch_(),
     
    6668  {
    6769   
    68    
     70    jnlst_ = jnlst;
     71
    6972    if (suffix_handler==NULL)
    7073      suffix_handler_ = suffix_handler = new AmplSuffixHandler();
     
    9194    suffix_handler->AddAvailableSuffix("sstatus", AmplSuffixHandler::Variable_Source, AmplSuffixHandler::Index_Type);
    9295    suffix_handler->AddAvailableSuffix("sstatus", AmplSuffixHandler::Constraint_Source, AmplSuffixHandler::Index_Type);
     96   
     97   
     98    // For objectives
     99    suffix_handler->AddAvailableSuffix("UBObj", AmplSuffixHandler::Objective_Source, AmplSuffixHandler::Index_Type);
     100   
    93101   
    94102    // Perturbation radius
     
    193201  }
    194202 
     203  void
     204  AmplTMINLP::read_obj_suffixes(){
     205    ASL_pfgh* asl = ampl_tnlp_->AmplSolverObject();
     206    DBG_ASSERT(asl);
     207    //Get the values
     208    if(n_obj < 2) return;
     209   
     210    const AmplSuffixHandler * suffix_handler = GetRawPtr(suffix_handler_);
     211   
     212    const Index* UBObj = suffix_handler->GetIntegerSuffixValues("UBObj", AmplSuffixHandler::Objective_Source);
     213    if(UBObj){
     214      //get index of lower bounding objective
     215      int lowerBoundingObj = (UBObj[0] == 1)? 1:0;
     216      // Pass information to Ipopt
     217      ampl_tnlp_->set_active_objective(lowerBoundingObj);
     218     
     219      //get the index of upper bounding objective
     220      for(int i = 0; i < n_obj; i++){
     221        if(UBObj[i]==1){
     222          if(upperBoundingObj_ != -1){
     223            jnlst_->Printf(J_ERROR, J_MAIN,
     224                                   "Too many objectives for upper-bounding");
     225          }
     226          upperBoundingObj_ = i;
     227        }
     228      }
     229    }
     230    else
     231    {
     232      ampl_tnlp_->set_active_objective(0);
     233    }
     234  }
     235 
     236 
    195237  bool AmplTMINLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)
    196238  {
     
    683725  }
    684726 
     727  /** This method to returns the value of an alternative objective function for
     728  upper bounding (if one has been declared by using the prefix UBObj).*/
     729  bool
     730  AmplTMINLP::eval_upper_bound_f(Index n, const Number* x,
     731                                  Number& obj_value){
     732    ASL_pfgh* asl = ampl_tnlp_->AmplSolverObject();
     733    //xknown(x);    // This tells ampl to use a new x
     734    fint nerror = -1;
     735    double retval = objval(upperBoundingObj_, const_cast<double *>(x), &nerror);
     736    if(nerror > 0){
     737      jnlst_->Printf(J_ERROR, J_MAIN,
     738                     "Error in evaluating upper bounding objecting");
     739      throw -1;}
     740    return retval;
     741
     742  }
    685743  /** Return the ampl solver object (ASL*) */
    686744  const ASL_pfgh*
  • trunk/Bonmin/src/Interfaces/Ampl/BonAmplTMINLP.hpp

    r481 r514  
    6868    void read_sos();
    6969
     70    /** Read suffixes on objective functions for upper bounding*/
     71    void read_obj_suffixes();
     72   
     73    /** Default constructor.*/
    7074    AmplTMINLP();
    7175
     
    7680    }
    7781
    78     /** Default destructor */
     82    /** destructor */
    7983    virtual ~AmplTMINLP();
    8084    //@}
     
    189193    /** This methods gives the linear part of the objective function */
    190194    virtual void getLinearPartOfObjective(double * obj);
    191     /** Journlist */
    192     SmartPtr<const Journalist> jnlst_;
    193 
    194     /** Sign of the objective fn (1 for min, -1 for max) */
    195     double obj_sign_;
    196 
    197     /**@name Problem Size Data*/
    198     //@{
    199     Index nz_h_full_; // number of nonzeros in the full_x hessian
    200     /* the rest of the problem size data is available easily through the ampl variables */
    201     //@}
    202 
    203     /**@name Internal copies of data */
    204     //@{
    205     /** A non-const copy of x - this is kept up-to-date in apply_new_x */
    206     Number* non_const_x_;
    207 
    208     /** Solution Vectors */
    209     Number* x_sol_;
    210     Number* z_L_sol_;
    211     Number* z_U_sol_;
    212     Number* g_sol_;
    213     Number* lambda_sol_;
    214     Number obj_sol_;
    215     //@}
    216 
    217     /**@name Flags to track internal state */
    218     //@{
    219     /** true when the objective value has been calculated with the
    220      *  current x, set to false in apply_new_x, and set to true in
    221      *  internal_objval */
    222     bool objval_called_with_current_x_;
    223     /** true when the constraint values have been calculated with the
    224      *  current x, set to false in apply_new_x, and set to true in
    225      *  internal_conval */
    226     bool conval_called_with_current_x_;
    227     //@}
    228 
    229 
    230     /** Make the objective call to ampl */
    231     bool internal_objval(Number& obj_val);
    232 
    233     /** Make the constraint call to ampl*/
    234     bool internal_conval(Index m, Number* g=NULL);
    235 
    236     /** Internal function to update the internal and ampl state if the
    237     x value changes */
    238     void apply_new_x(bool new_x, Index n, const Number* x);
     195
    239196
    240197    /** Method to add the extra option for ampl */
    241198    void fillAmplOptionList(AmplOptionsList* amplOptList);
    242199
     200    /** Do we have an alternate objective for upper bounding?*/
     201    virtual bool hasUpperBoundingObjective(){
     202      return upperBoundingObj_ != -1;}
     203   
     204    /** This method to returns the value of an alternative objective function for
     205      upper bounding (if one has been declared by using the prefix UBObj).*/
     206    virtual bool eval_upper_bound_f(Index n, const Number* x,
     207                                    Number& obj_value);
     208private:
    243209    /**@name Default Compiler Generated Methods
    244210     * (Hidden to avoid implicit creation/calling).
     
    257223    //@}
    258224
     225    /** Index of the objective to use for upper bounding*/
     226    int upperBoundingObj_;
    259227    /** pointer to the internal AmplTNLP */
    260228    AmplTNLP* ampl_tnlp_;
     229    /** Journalist */
     230    SmartPtr<const Journalist> jnlst_;
    261231
    262232    /** Storage of branching priorities information.*/
  • trunk/Bonmin/src/Interfaces/BonOsiTMINLPInterface.cpp

    r508 r514  
    2020#include <sstream>
    2121
     22
    2223#include "Ipopt/BonIpoptSolver.hpp"
    2324#ifdef COIN_HAS_FILTERSQP
     
    2526#endif
    2627
     28
     29#include "OsiBranchingObject.hpp"
    2730using namespace Ipopt;
    2831
     
    21762179  nCallOptimizeTNLP_++;
    21772180  hasBeenOptimized_ = true;
    2178 
     2181 
    21792182  //Options should have been printed if not done already turn off Ipopt output
    21802183  if(!hasPrintedOptions) {
     
    21832186    app_->Options()->SetStringValue("print_user_options","no", false, true);
    21842187  }
    2185 
    2186 
     2188 
     2189 
    21872190#if 1
    21882191  if(optimization_status_ == TNLPSolver::notEnoughFreedom)//Too few degrees of freedom
     2192  {
     2193    std::cout<<"Too few degrees of freedom...."<<std::endl;
     2194    int numrows = getNumRows();
     2195    int numcols = getNumCols();
     2196   
     2197    const double * colLower = getColLower();
     2198    const double * colUpper = getColUpper();
     2199   
     2200   
     2201    const double * rowLower = getRowLower();
     2202    const double * rowUpper = getRowUpper();
     2203   
     2204    int numberFixed = 0;
     2205    for(int i = 0 ; i < numcols ; i++)
    21892206    {
    2190       std::cout<<"Too few degrees of freedom...."<<std::endl;
    2191       int numrows = getNumRows();
    2192       int numcols = getNumCols();
    2193 
    2194       const double * colLower = getColLower();
    2195       const double * colUpper = getColUpper();
    2196 
    2197 
    2198       const double * rowLower = getRowLower();
    2199       const double * rowUpper = getRowUpper();
    2200 
    2201       int numberFixed = 0;
    2202       for(int i = 0 ; i < numcols ; i++)
    2203         {
    2204           if(colUpper[i] - colLower[i] <= INT_BIAS)
     2207      if(colUpper[i] - colLower[i] <= INT_BIAS)
    22052208            {
    22062209              numberFixed++;
    22072210            }
    2208         }
    2209       int numberEqualities = 0;
    2210       for(int i = 0 ; i < numrows ; i++)
    2211         {
    2212           if(rowUpper[i] - rowLower[i] <= INT_BIAS)
     2211    }
     2212    int numberEqualities = 0;
     2213    for(int i = 0 ; i < numrows ; i++)
     2214    {
     2215      if(rowUpper[i] - rowLower[i] <= INT_BIAS)
    22132216            {
    22142217              numberEqualities++;
    22152218            }     
    2216         }
    2217       if(numcols - numberFixed > numberEqualities || numcols < numberEqualities)
    2218       {
    2219         std::string probName;
    2220         getStrParam(OsiProbName, probName);
    2221         throw newUnsolvedError(app_->errorCode(), problem_, probName);
    2222       }
    2223       double * saveColLow = CoinCopyOfArray(getColLower(), getNumCols());
    2224       double * saveColUp = CoinCopyOfArray(getColUpper(), getNumCols());
    2225 
    2226       for(int i = 0; i < numcols && numcols - numberFixed <= numberEqualities ; i++)
    2227         {
    2228           if(colUpper[i] - colLower[i] <= INT_BIAS)
     2219    }
     2220    if(numcols - numberFixed > numberEqualities || numcols < numberEqualities)
     2221    {
     2222      std::string probName;
     2223      getStrParam(OsiProbName, probName);
     2224      throw newUnsolvedError(app_->errorCode(), problem_, probName);
     2225    }
     2226    double * saveColLow = CoinCopyOfArray(getColLower(), getNumCols());
     2227    double * saveColUp = CoinCopyOfArray(getColUpper(), getNumCols());
     2228   
     2229    for(int i = 0; i < numcols && numcols - numberFixed <= numberEqualities ; i++)
     2230    {
     2231      if(colUpper[i] - colLower[i] <= INT_BIAS)
    22292232            {
    22302233              setColLower(i, saveColLow[i]-1e-06);
     
    22322235              numberFixed--;
    22332236            }
    2234         }
    2235       solveAndCheckErrors(warmStarted, throwOnFailure, whereFrom);
    2236       //restore
    2237       for(int i = 0; i < numcols && numcols - numberFixed < numrows ; i++)
    2238         {
    2239           problem_->SetVariableLowerBound(i,saveColLow[i]);
    2240           problem_->SetVariableUpperBound(i,saveColUp[i]);
    2241         }
    2242       delete [] saveColLow;
    2243       delete [] saveColUp;
    2244       return;
    2245     }
     2237    }
     2238    solveAndCheckErrors(warmStarted, throwOnFailure, whereFrom);
     2239    //restore
     2240    for(int i = 0; i < numcols && numcols - numberFixed < numrows ; i++)
     2241    {
     2242      problem_->SetVariableLowerBound(i,saveColLow[i]);
     2243      problem_->SetVariableUpperBound(i,saveColUp[i]);
     2244    }
     2245    delete [] saveColLow;
     2246    delete [] saveColUp;
     2247    return;
     2248  }
    22462249  else
    22472250#endif
    22482251    if(!app_->isRecoverable(optimization_status_))//Solver failed and the error can not be recovered, throw it
     2252    {
     2253      std::string probName;
     2254      getStrParam(OsiProbName, probName);
     2255      throw newUnsolvedError(app_->errorCode(), problem_, probName);
     2256    }
     2257  try{
     2258    totalIterations_ += app_->IterationCount();
     2259  }
     2260  catch(SimpleError &E)
    22492261  {
    2250     std::string probName;
    2251     getStrParam(OsiProbName, probName);
    2252     throw newUnsolvedError(app_->errorCode(), problem_, probName);
    2253   }
    2254   try{
    2255   totalIterations_ += app_->IterationCount();
    2256   }
    2257   catch(SimpleError &E)
     2262    if (throwOnFailure)//something failed throw
    22582263    {
    2259       if (throwOnFailure)//something failed throw
    2260         {
    2261           throw SimpleError("No statistics available from Ipopt",whereFrom);
    2262         }
    2263       else return;
    2264     }
    2265 
     2264      throw SimpleError("No statistics available from Ipopt",whereFrom);
     2265    }
     2266    else {
     2267      return;
     2268    }
     2269  }
     2270  if(problem_->hasUpperBoundingObjective()){//Check if solution is integer and recompute objective value using alternative objective function
     2271    const double * sol = getColSolution();
     2272    bool integerSol = true;
     2273    double intTol = 1e-08;
     2274    if(objects()){
     2275      int nObjects = numberObjects();
     2276      OsiObject ** object = objects();
     2277      for(int i = 0 ; i< nObjects ; i++){
     2278        int dummy;
     2279        if(object[i]->infeasibility(this,dummy) > intTol)
     2280        {
     2281          integerSol=false;
     2282          break;
     2283        }
     2284      }
     2285    }
     2286    else{//Only works for integer constraints
     2287      int numcols = getNumCols();
     2288      for(int i = 0 ; i < numcols ; i++){
     2289        if(isInteger(i) || isBinary(i)){
     2290          if(fabs(sol[i] - floor(sol[i]+0.5)) > intTol){
     2291            integerSol = false;
     2292            break;
     2293          }
     2294        }
     2295      }
     2296    }
     2297    if(integerSol)
     2298      problem_->evaluateUpperBoundingFunction(sol);
     2299  }
    22662300  messageHandler()->message(IPOPT_SUMMARY, messages_)
    2267   <<whereFrom<<optimization_status_<<app_->IterationCount()<<app_->CPUTime()<<CoinMessageEol;
    2268 
     2301    <<whereFrom<<optimization_status_<<app_->IterationCount()<<app_->CPUTime()<<CoinMessageEol;
     2302 
    22692303  if((nCallOptimizeTNLP_ % 20) == 1)
    22702304    messageHandler()->message(LOG_HEAD, messages_)<<CoinMessageEol;
    2271 
    2272 
     2305 
     2306 
    22732307  if (numIterationSuspect_ >= 0 && (getIterationCount()>numIterationSuspect_ || isAbandoned())) {
    22742308    messageHandler()->message(SUSPECT_PROBLEM,
    2275         messages_)<<nCallOptimizeTNLP_<<CoinMessageEol;
     2309                              messages_)<<nCallOptimizeTNLP_<<CoinMessageEol;
    22762310    std::string subProbName;
    22772311    getStrParam(OsiProbName, subProbName);
     
    22812315    problem_->outputDiffs(subProbName, NULL/*getVarNames()*/);
    22822316  }
    2283 
     2317 
    22842318}
    22852319
  • trunk/Bonmin/src/Interfaces/BonTMINLP.hpp

    r481 r514  
    339339      return NULL;
    340340    }
     341
     342    /** Say if has a specific function to compute upper bounds*/
     343    virtual bool hasUpperBoundingObjective(){
     344      return false;}
     345   
     346    /** overload this method to return the value of an alternative objective function for
     347      upper bounding (to use it hasUpperBoundingObjective should return true).*/
     348    virtual bool eval_upper_bound_f(Index n, const Number* x,
     349                                    Number& obj_value){}
    341350  private:
    342351    /**@name Default Compiler Generated Methods
     
    358367    //@}
    359368
    360       private:
    361369  /** resize arrays for linear cuts */
    362370  void resizeLinearCuts(int newNumberCuts, int newNnz);
  • trunk/Bonmin/src/Interfaces/BonTMINLP2TNLP.cpp

    r394 r514  
    699699
    700700
     701  /** Evaluate the upper bounding function at given point and store the result.*/
     702  void
     703  TMINLP2TNLP::evaluateUpperBoundingFunction(const double * x){
     704    tminlp_->eval_upper_bound_f(n_, x, obj_value_);
     705  }
    701706}
    702707// namespace Ipopt
  • trunk/Bonmin/src/Interfaces/BonTMINLP2TNLP.hpp

    r394 r514  
    347347
    348348    //@}
     349     
     350      /** Say if has a specific function to compute upper bounds*/
     351      virtual bool hasUpperBoundingObjective(){
     352        return tminlp_->hasUpperBoundingObjective();}
     353
     354      /** Evaluate the upper bounding function at given point and store the result.*/
     355      void evaluateUpperBoundingFunction(const double * x);
    349356  protected:
    350357    /**@name Default Compiler Generated Methods
Note: See TracChangeset for help on using the changeset viewer.