RobotSetup.cpp
Go to the documentation of this file.
1 // (C) Copyright CNRS and others 2010
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, Université de la Méditérannée
7 //
8 // Date : 05/22/2010
9 
10 #include <climits>
11 
12 #include "BonminConfig.h"
14 
15 #include "RobotSetup.hpp"
16 #include "BonNWayObject.hpp"
17 #include "BonNWayChoose.hpp"
18 
19 
20 
21 namespace Bonmin
22 {
23  RobotSetup::RobotSetup(const CoinMessageHandler * handler):BonminSetup(handler)
24  {}
25 
27  {}
28 
30  OsiTMINLPInterface &nlp):
31  BonminSetup(other, nlp)
32  {
33  }
34 
36  OsiTMINLPInterface &nlp,
37  const std::string &prefix):
38  BonminSetup(other, nlp, prefix)
39  {
41  }
42 
44  {
47 
48 
49  roptions->AddLowerBoundedIntegerOption("branch_on_frac_only",
50  "Starting at given depth branch on the subset of fractional variables (and set the last branch that one of them is 1)",
51  0,INT_MAX,"");
52 
53  roptions->AddStringOption2("do_a_quick_one",
54  "Do we try our luck?",
55  "no",
56  "no", "Don't (of course).",
57  "yes", "Be crazy",
58  "");
59 
60  }
61 
63  void
65  {
67  }
68 
70  void
71  RobotSetup::initialize(Ipopt::SmartPtr<TMINLP> tminlp, bool createContinuousSolver /*= false*/)
72  {
73  BonminSetup::initialize(tminlp,createContinuousSolver);
75  }
76 
78  void
79  RobotSetup::initialize(const OsiTMINLPInterface &nlpSi, bool createContinuousSolver /*= false*/)
80  {
81  BonminSetup::initialize(nlpSi,createContinuousSolver);
83  }
84 
86  {
88 
89  delete branchingMethod_;
90 
91  continuousSolver_->deleteObjects();
92  continuousSolver_->findIntegersAndSOS(false);
93  setPriorities();
94  addNWays();
95  Ipopt::SmartPtr<StrongBranchingSolver> strong_solver = NULL;
96  nonlinearSolver_->SetStrongBrachingSolver(strong_solver);
97  BonNWayChoose* chooseVariable = new BonNWayChoose(*this,
99  branchingMethod_ = chooseVariable;
100  branchingMethod_->setNumberStrong(intParam_[NumberStrong]);
101 
102  }
103 
104  void
106  {
107 
108  int do_quick;
109  options()->GetEnumValue("do_a_quick_one", do_quick, prefix());
110  int depth_frac;
111  options()->GetIntegerValue("branch_on_frac_only", depth_frac, prefix());
112 
113  // pass user set Sos constraints (code inspired from CoinSolve.cpp)
114  const TMINLP::SosInfo * sos = nonlinearSolver()->model()->sosConstraints();
115  if (!getIntParameter(BabSetupBase::DisableSos) && sos && sos->num > 0) //we have some sos constraints
116  {
117  const int & numSos = sos->num;
118  OsiObject ** objects = new OsiObject*[numSos];
119  const int * starts = sos->starts;
120  const int * indices = sos->indices;
121  //const char * types = sos->types;
122  const double * weights = sos->weights;
123  bool hasPriorities = false;
124  const int * varPriorities = nonlinearSolver()->getPriorities();
125  int numberObjects = nonlinearSolver()->numberObjects();
126  if (varPriorities)
127  {
128  for (int i = 0 ; i < numberObjects ; i++) {
129  if (varPriorities[i]) {
130  hasPriorities = true;
131  break;
132  }
133  }
134  }
135  const int * sosPriorities = sos->priorities;
136  if (sosPriorities)
137  {
138  for (int i = 0 ; i < numSos ; i++) {
139  if (sosPriorities[i]) {
140  hasPriorities = true;
141  break;
142  }
143  }
144  }
145 
146  std::vector<std::list<int> > groups(numSos + 1);
147 
148  for (int i = 0 ; i < numSos ; i++)
149  {
150  int start = starts[i];
151  int length = starts[i + 1] - start;
152  for(int j = 0 ; j < length ; j++){
153  groups[(size_t) weights[j]].push_back(indices[start+j]);
154  }
155  }
156 
157  for (int i = 0 ; i < numSos ; i++)
158  {
159  int start = starts[i];
160  int length = starts[i + 1] - start;
161  BonNWayObject * nway = new BonNWayObject(length, &indices[start],i);
162  nway->setPriority(1);
163  for(int j = 0 ; j < length ; j++){//Setup consequences
164  n_way_consequences cons;
165  std::vector<int>& ids = cons.indices;
166  int idx = (int) weights[j];
167  const std::list<int> &to_add = groups[idx];
168  for(std::list<int>::const_iterator k = to_add.begin() ;
169  k != to_add.end() ; k++){
170  if(*k != indices[start+j]) ids.push_back(*k);
171  }
172  nway->setConsequence(j, cons);
173  }
174  objects[i] = nway;
175 
176  if(do_quick)
177  nway->make_quick();
178  nway->set_only_frac_branches(depth_frac);
179  if (hasPriorities && sosPriorities && sosPriorities[i]) {
180  objects[i]->setPriority(sosPriorities[i]);
181  }
182  }
183  nonlinearSolver()->addObjects(numSos, objects);
184  for (int i = 0 ; i < numSos ; i++)
185  delete objects[i];
186  delete [] objects;
187  }
188  }
189 }/* end namespace Bonmin*/
190 
void set_only_frac_branches(int depth)
int getIntParameter(const IntParameter &p) const
Return value of integer parameter.
virtual void registerOptions()
Register all the options for this algorithm instance.
Definition: RobotSetup.cpp:64
const vector< OsiObject * > & objects() const
Access to extra objects.
int intParam_[NumberIntParam]
storage of integer parameters.
void initializeRobot()
Initialize a branch-and-with robot nway.
Definition: RobotSetup.cpp:85
algorithm setup.
Definition: RobotSetup.hpp:18
int num
Number of SOS constraints.
Definition: BonTMINLP.hpp:75
This is class provides an Osi interface for a Mixed Integer Linear Program expressed as a TMINLP (so ...
virtual const SosInfo * sosConstraints() const =0
This class chooses a variable to branch on.
Number of candidates for strong branching.
Class to store sos constraints for model.
Definition: BonTMINLP.hpp:72
static void registerOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register class options.
void setPriorities()
Set the priorities into OsiTMINLPInterface when needed.
OsiTMINLPInterface * nonlinearSolver_
Storage of the non-linear solver used.
static char * j
Definition: OSdtoa.cpp:3622
const int * getPriorities() const
Get priorities on integer variables.
RobotSetup(const CoinMessageHandler *handler=NULL)
Default constructor.
Definition: RobotSetup.cpp:23
OsiChooseVariable * branchingMethod_
Branching method.
int * starts
For 0 &lt;= i &lt; nums, start[i] gives the indice of indices and weights arrays at which the description o...
Definition: BonTMINLP.hpp:86
static void registerAllOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register all bonmin type executable options.
Definition: RobotSetup.cpp:43
void setConsequence(int iMember, const n_way_consequences &consequence)
Set up a consequence for a single member.
static void registerAllOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register all bonmin type executable options.
void fint fint * k
int * priorities
priorities of sos constraints.
Definition: BonTMINLP.hpp:79
static int
Definition: OSdtoa.cpp:2173
Consider or not SOS constraints.
int * indices
indices of elements belonging to the SOS.
Definition: BonTMINLP.hpp:88
void addNWays()
Add nway objects.
Definition: RobotSetup.cpp:105
Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions_
Registered Options.
OsiTMINLPInterface * nonlinearSolver()
Pointer to the non-linear solver used.
void initialize(Ipopt::SmartPtr< TMINLP > tminlp, bool createContinuousSolver=true)
Initialize, read options and create appropriate bonmin setup.
Definition: RobotSetup.cpp:71
Ipopt::SmartPtr< Ipopt::OptionsList > options()
Acces list of Options.
std::vector< int > indices
static char prefix[100]
Definition: BM_lp.cpp:26
OsiSolverInterface * continuousSolver_
Storage of continuous solver.
const char * prefix() const
Get prefix to use for options.
double * weights
weights of the elements of the SOS.
Definition: BonTMINLP.hpp:90
void initialize(Ipopt::SmartPtr< TMINLP > tminlp, bool createContinuousSolver=true)
Initialize, read options and create appropriate bonmin setup.