source: branches/0.2a/Bonmin/experimental/NotConvex/BonCouenneSetup.cpp @ 528

Last change on this file since 528 was 528, checked in by pbonami, 12 years ago

Test a 0.2a branch

File size: 7.7 KB
Line 
1// (C) Copyright International Business Machines Corporation 2007
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// Authors :
6// Pierre Bonami, International Business Machines Corporation
7//
8// Date : 04/18/2007
9
10#include "BonCouenneSetup.hpp"
11#include "BonNlpHeuristic.hpp"
12#include "BonCouenneInterface.hpp"
13
14#include "CouenneObject.hpp"
15#include "CouenneChooseVariable.hpp"
16#include "BonAuxInfos.hpp"
17#include "BonCbcNode.hpp"
18
19
20#include "asl.h"
21#include "getstub.h"
22
23namespace Bonmin{
24 
25  SmartAsl::~SmartAsl(){
26    //Code from Ipopt::AmplTNLP to free asl
27    if(asl != NULL){
28        if (X0) {
29          delete [] X0;
30          X0 = NULL;
31        }
32        if (havex0) {
33          delete [] havex0;
34          havex0 = NULL;
35        }
36        if (pi0) {
37          delete [] pi0;
38          pi0 = NULL;
39        }
40        if (havepi0) {
41          delete [] havepi0;
42          havepi0 = NULL;
43        }
44        ASL* asl_to_free = (ASL*)asl;
45        ASL_free(&asl_to_free);
46        asl = NULL;
47      }
48      ASL_free(&asl);
49  }
50 
51  CouenneSetup::~CouenneSetup(){
52  }
53 
54  void CouenneSetup::InitializeBonmin(char **& argv){
55    /* Get the basic options. */
56    readOptionsFile();
57   
58   
59    /** Change default value for failure behavior so that code doesn't crash when Ipopt does not solve a sub-problem.*/
60    options_->SetStringValue("nlp_failure_behavior","fathom","bonmin.");
61
62    gatherParametersValues(options_);
63   
64    continuousSolver_ = new OsiClpSolverInterface;
65    CouenneInterface * ci = new CouenneInterface;
66    nonlinearSolver_ = ci;
67    /* Read the model in various places. */
68    ci->readAmplNlFile(argv,roptions(),options(),journalist());
69    aslfg_ = new SmartAsl;
70    aslfg_->asl = readASLfg (argv);
71   
72   
73    /* Initialize Couenne cut generator.*/
74    int ivalue, num_points;
75    options()->GetEnumValue("convexification_type", ivalue,"bonmin.");
76    enum conv_type convtype((enum conv_type) ivalue);
77    options()->GetIntegerValue("convexification_points",num_points,"bonmin.");
78   
79    CouenneCutGenerator * couenneCg = new CouenneCutGenerator(ci, aslfg_->asl, true, convtype,num_points);
80    CouenneProblem * couenneProb = couenneCg -> Problem();
81
82    Bonmin::BabInfo * extraStuff = new Bonmin::BabInfo(0);
83   
84    // as per instructions by John Forrest, to get changed bounds
85    extraStuff -> setExtraCharacteristics (extraStuff -> extraCharacteristics () | 2);
86   
87    continuousSolver_ -> setAuxiliaryInfo (extraStuff);
88    delete extraStuff;
89    extraStuff = dynamic_cast<Bonmin::BabInfo *>(continuousSolver_ -> getAuxiliaryInfo());
90    /* Setup log level*/
91    int lpLogLevel;
92    options()->GetIntegerValue("lp_log_level",lpLogLevel,"bonmin.");
93    continuousSolver_->messageHandler()->setLogLevel(lpLogLevel);
94    ci->extractLinearRelaxation(*continuousSolver_, *couenneCg);
95   
96    if(extraStuff->infeasibleNode()){
97      std::cout<<"Initial linear relaxation constructed by Couenne is infeasible, quit"<<std::endl;
98      return;
99    }
100 
101   
102    continuousSolver_->findIntegersAndSOS(false);
103    {
104    int numberIntegerObjects = continuousSolver_->numberObjects() > 0;
105      int numAuxs = couenneProb->nAuxs();
106      OsiObject ** objects = new OsiObject*[numAuxs];
107      int nobj = 0;
108     
109      for (int i = 0 ; i < numAuxs; i++) // for each aux variable
110       
111        // if this variable is associated with a nonlinear function
112        //      if (couenneProb -> Aux (i) -> Image () -> Linearity () > LINEAR)
113      {
114        /*
115         printf ("creating CouenneObject for ");
116         
117         couenneProb -> Aux (i) ->             print (std::cout); printf (" := ");
118         couenneProb -> Aux (i) -> Image () -> print (std::cout); printf ("\n");
119         */
120        // then we may have to branch on it
121        objects [nobj] = new CouenneObject (couenneProb -> Aux (i));
122        objects [nobj++] -> setPriority (1);
123      }
124     
125      continuousSolver_ -> addObjects (nobj, objects);
126      for(int i = 0 ; i < nobj ; i++){
127        delete objects[i];
128      }
129      delete [] objects;
130    }
131   
132    //Setup Convexifier generators
133   
134   
135   
136   
137    int numGen = 0;
138    int freq;
139    options()->GetIntegerValue("convexification_cuts",freq,"couenne.");
140    if (freq != 0) {
141      CuttingMethod cg;
142      cg.frequency = freq;
143      cg.cgl = couenneCg;
144      cg.id = "Couenne convexifier cuts";
145      cutGenerators().push_back(cg);
146    }
147
148    /*Setup heuristic to solve nlp problems.*/
149    int doNlpHeurisitic = 0;
150    options()->GetEnumValue("local_optimization_heuristic", doNlpHeurisitic, "couenne.");
151    if(doNlpHeurisitic)
152    {
153      int numSolve;
154      options()->GetIntegerValue("log_num_local_optimization_per_level",numSolve,"couenne.");
155      NlpSolveHeuristic * nlpHeuristic = new NlpSolveHeuristic;
156      nlpHeuristic->setNlp(*ci,false);
157      nlpHeuristic->setCouenneProblem(couenneProb);
158      nlpHeuristic->setMaxNlpInf(1e10);
159      nlpHeuristic->setNumberSolvePerLevel(numSolve);
160      heuristics_.push_back(nlpHeuristic);
161    }
162   
163    branchingMethod_ = new CouenneChooseVariable(continuousSolver_, 
164                                  const_cast<CouenneProblem *> (couenneProb));
165
166   
167}
168 
169void CouenneSetup::registerOptions(){
170  registerAllOptions(roptions());
171}
172
173
174void
175  CouenneSetup::registerAllOptions(Ipopt::SmartPtr<Ipopt::RegisteredOptions> roptions){
176    BabSetupBase::registerAllOptions(roptions);
177    BonCbcFullNodeInfo::registerOptions(roptions);
178
179    roptions->SetRegisteringCategory("Couenne options");
180   
181    roptions->AddLowerBoundedIntegerOption("convexification_cuts",
182                                           "Specify the frequency (in terms of nodes) at which couenne ecp cuts are generated.",
183                                           0,1,
184                                           "A frequency of 0 amounts to to never solve the NLP relaxation.");
185   
186    roptions->AddStringOption2("local_optimization_heuristic",
187                               "Do we search for local solutions of NLP's",
188                               "yes",
189                               "no","",
190                               "yes","");
191   
192    roptions->AddLowerBoundedIntegerOption("log_num_local_optimization_per_level",
193                               "Specify the logarithm of the number of local optimization to perform on average for each level of given depth of the tree.",
194                               -1,-1,"Solve as many nlp's at the nodes for each level of the tree. Nodes are randomly selected. If for a"
195                                           "given level there are less nodes than this number nlp are solved for every nodes."
196                                           "For example if parameter is 8, nlp's are solved for all node untill level 8, then for half the node at level 9, 1/4 at level 10...."
197                                           "Value -1 specify to perform at all nodes."
198                                           );
199   
200   
201    roptions->AddStringOption3("convexification_type",
202                               "Deterimnes in which point the linear over/under-estimator are generated",
203                               "current-point-only",
204                               "current-point-only","Only at current optimum of relaxation",
205                               "uniform-grid","Points chosen in a unform grid between the bounds of the problem",
206                               "around-current-point","At points around current optimum of relaxation");
207   
208    roptions->AddLowerBoundedIntegerOption("convexification_points",
209                                           "Specify the number of points at which to convexify when convexification type"
210                                           "is uniform-grid or arround-current-point.",
211                                           0,1,
212                                           "");
213   
214   
215  }
216
217}
218
Note: See TracBrowser for help on using the repository browser.