source: branches/devel/Cbc/src/CbcBranchBase.cpp @ 439

Last change on this file since 439 was 439, checked in by forrest, 13 years ago

towards common use with other solvers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7#include <cassert>
8#include <cmath>
9#include <cfloat>
10
11#include "OsiSolverInterface.hpp"
12#include "OsiSolverBranch.hpp"
13#include "OsiChooseVariable.hpp"
14#include "CbcModel.hpp"
15#include "CbcMessage.hpp"
16#include "CbcBranchBase.hpp"
17
18
19// Default Constructor
20CbcObject::CbcObject() 
21  : OsiObject(),
22    model_(NULL),
23   id_(-1),
24   preferredWay_(0)
25{
26}
27
28// Constructor from model
29CbcObject::CbcObject(CbcModel * model)
30  : OsiObject(),
31    model_(model),
32    id_(-1),
33    preferredWay_(0)
34{
35}
36
37
38// Destructor
39CbcObject::~CbcObject ()
40{
41}
42
43// Copy constructor
44CbcObject::CbcObject ( const CbcObject & rhs)
45  : OsiObject(rhs)
46{
47  model_ = rhs.model_;
48  id_ = rhs.id_;
49  preferredWay_ = rhs.preferredWay_;
50}
51
52// Assignment operator
53CbcObject & 
54CbcObject::operator=( const CbcObject& rhs)
55{
56  if (this!=&rhs) {
57    OsiObject::operator=(rhs);
58    model_ = rhs.model_;
59    id_ = rhs.id_;
60    preferredWay_ = rhs.preferredWay_;
61  }
62  return *this;
63}
64
65/* Returns floor and ceiling i.e. closest valid points
66 */
67void 
68CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value,
69                        double tolerance) const
70{
71  if (fabs(floor(value+0.5)-value)>tolerance) {
72    floorValue = floor(value);
73  } else {
74    floorValue = floor(value+0.5);
75  }
76  ceilingValue = floorValue+1.0;
77}
78/* Infeasibility of the object
79     
80    This is some measure of the infeasibility of the object. 0.0
81    indicates that the object is satisfied.
82 
83    The preferred branching direction is returned in way,
84 
85    This is used to prepare for strong branching but should also think of
86    case when no strong branching
87 
88    The object may also compute an estimate of cost of going "up" or "down".
89    This will probably be based on pseudo-cost ideas
90
91    This should also set mutable infeasibility_ and whichWay_
92    This is for instant re-use for speed
93*/
94double 
95CbcObject::infeasibility(const OsiSolverInterface * solver,int &preferredWay) const 
96{
97  assert (solver==model_->solver());
98  return infeasibility(preferredWay);
99}
100 
101/* For the variable(s) referenced by the object,
102      look at the current solution and set bounds to match the solution.
103      Returns measure of how much it had to move solution to make feasible
104*/
105double 
106CbcObject::feasibleRegion(OsiSolverInterface * solver) const 
107{
108  assert (solver==model_->solver());
109  CbcObject * fudge = const_cast<CbcObject *>(this);
110  fudge->feasibleRegion();
111  return 0.0;
112}
113/* Infeasibility of the object
114     
115    This is some measure of the infeasibility of the object. 0.0
116    indicates that the object is satisfied.
117 
118    The preferred branching direction is returned in way,
119 
120    This is used to prepare for strong branching but should also think of
121    case when no strong branching
122 
123    The object may also compute an estimate of cost of going "up" or "down".
124    This will probably be based on pseudo-cost ideas
125
126    This should also set mutable infeasibility_ and whichWay_
127    This is for instant re-use for speed
128*/
129double 
130CbcObject::infeasibility(const OsiBranchingInformation * info,
131                         int &preferredWay) const 
132{
133  return infeasibility(preferredWay);
134}
135 
136/* For the variable(s) referenced by the object,
137      look at the current solution and set bounds to match the solution.
138      Returns measure of how much it had to move solution to make feasible
139*/
140double 
141CbcObject::feasibleRegion(OsiSolverInterface * solver,const OsiBranchingInformation * info) const 
142{
143  assert (solver==model_->solver());
144  CbcObject * fudge = const_cast<CbcObject *>(this);
145  fudge->feasibleRegion();
146  return 0.0;
147}
148 
149/* Create a branching object and indicate which way to branch first.
150     
151      The branching object has to know how to create branches (fix
152      variables, etc.)
153*/
154OsiBranchingObject * 
155CbcObject::createBranch(OsiSolverInterface * solver, int way) const 
156{
157  assert (solver==model_->solver());
158  CbcObject * fudge = const_cast<CbcObject *>(this);
159  return fudge->createBranch(way);
160}
161/* Create an OsiSolverBranch object
162   
163This returns NULL if branch not represented by bound changes
164*/
165OsiSolverBranch * 
166CbcObject::solverBranch() const
167{
168  return NULL;
169}
170 
171// Default Constructor
172CbcBranchingObject::CbcBranchingObject()
173  : OsiBranchingObject()
174{
175  model_=NULL;
176  originalCbcObject_=NULL;
177  variable_=-1;
178  way_=0;
179}
180
181// Useful constructor
182CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value)
183  : OsiBranchingObject(model->solver(),value)
184{
185  model_= model;
186  originalCbcObject_=NULL;
187  variable_=variable;
188  way_=way;
189}
190
191// Copy constructor
192CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs)
193  : OsiBranchingObject(rhs)
194{
195  model_=rhs.model_;
196  originalCbcObject_=rhs.originalCbcObject_;
197  variable_=rhs.variable_;
198  way_=rhs.way_;
199  value_=rhs.value_;
200}
201
202// Assignment operator
203CbcBranchingObject & 
204CbcBranchingObject::operator=( const CbcBranchingObject& rhs)
205{
206  if (this != &rhs) {
207    OsiBranchingObject::operator=(rhs);
208    model_=rhs.model_;
209    originalCbcObject_=rhs.originalCbcObject_;
210    variable_=rhs.variable_;
211    way_=rhs.way_;
212  }
213  return *this;
214}
215
216// Destructor
217CbcBranchingObject::~CbcBranchingObject ()
218{
219}
220// Default Constructor
221CbcBranchDecision::CbcBranchDecision ()
222  : object_(NULL),model_(NULL),chooseMethod_(NULL)
223{
224}
225
226// Copy Constructor
227CbcBranchDecision::CbcBranchDecision (const CbcBranchDecision &rhs)
228  : object_(NULL),model_(rhs.model_),chooseMethod_(NULL)
229{
230  if (rhs.chooseMethod_)
231    chooseMethod_ = rhs.chooseMethod_->clone();
232}
233
234CbcBranchDecision::~CbcBranchDecision()
235{
236  delete object_;
237  delete chooseMethod_;
238}
239/* Compare N branching objects. Return index of best
240   and sets way of branching in chosen object.
241   
242   This routine is used only after strong branching.
243   This is reccommended version as it can be more sophisticated
244*/
245
246int
247CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects,
248                               int numberUnsatisfied,
249                               double * changeUp, int * numberInfeasibilitiesUp,
250                               double * changeDown, int * numberInfeasibilitiesDown,
251                               double objectiveValue) 
252{
253  int bestWay=0;
254  int whichObject = -1;
255  if (numberObjects) {
256    initialize(objects[0]->model()); 
257    CbcBranchingObject * bestObject = NULL;
258    for (int i = 0 ; i < numberObjects ; i++) {
259      int betterWay = betterBranch(objects[i],
260                                   bestObject,
261                                   changeUp[i],
262                                   numberInfeasibilitiesUp [i],
263                                   changeDown[i],
264                                   numberInfeasibilitiesDown[i] );
265      if (betterWay) {
266        bestObject = objects[i];
267        bestWay = betterWay;
268        whichObject=i;
269      }
270    }
271    // set way in best
272    if (whichObject>=0) 
273      objects[whichObject]->way(bestWay);
274  }
275  return whichObject;
276}
277// Set (clone) chooseMethod
278void 
279CbcBranchDecision::setChooseMethod(const OsiChooseVariable & method)
280{ 
281  delete chooseMethod_;
282  chooseMethod_ = method.clone();
283}
284// Default constructor
285CbcConsequence::CbcConsequence()
286{
287}
288
289
290// Destructor
291CbcConsequence::~CbcConsequence ()
292{
293}
294
295// Copy constructor
296CbcConsequence::CbcConsequence ( const CbcConsequence & rhs)
297{
298}
299
300// Assignment operator
301CbcConsequence & 
302CbcConsequence::operator=( const CbcConsequence& rhs)
303{
304  if (this!=&rhs) {
305  }
306  return *this;
307}
308
309 
Note: See TracBrowser for help on using the repository browser.