source: stable/2.0/Cbc/src/CbcBranchBase.cpp @ 905

Last change on this file since 905 was 905, checked in by ladanyi, 13 years ago

include cstdlib before cmath to get things to compile on AIX with xlC (same as changeset 904 in trunk)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 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 <cstdlib>
9#include <cmath>
10#include <cfloat>
11
12#include "OsiSolverInterface.hpp"
13#include "OsiSolverBranch.hpp"
14#include "OsiChooseVariable.hpp"
15#include "CbcModel.hpp"
16#include "CbcMessage.hpp"
17#include "CbcBranchBase.hpp"
18
19
20// Default Constructor
21CbcObject::CbcObject() 
22  : OsiObject(),
23    model_(NULL),
24   id_(-1),
25   preferredWay_(0)
26{
27}
28
29// Constructor from model
30CbcObject::CbcObject(CbcModel * model)
31  : OsiObject(),
32    model_(model),
33    id_(-1),
34    preferredWay_(0)
35{
36}
37
38
39// Destructor
40CbcObject::~CbcObject ()
41{
42}
43
44// Copy constructor
45CbcObject::CbcObject ( const CbcObject & rhs)
46  : OsiObject(rhs)
47{
48  model_ = rhs.model_;
49  id_ = rhs.id_;
50  preferredWay_ = rhs.preferredWay_;
51}
52
53// Assignment operator
54CbcObject & 
55CbcObject::operator=( const CbcObject& rhs)
56{
57  if (this!=&rhs) {
58    OsiObject::operator=(rhs);
59    model_ = rhs.model_;
60    id_ = rhs.id_;
61    preferredWay_ = rhs.preferredWay_;
62  }
63  return *this;
64}
65
66/* Returns floor and ceiling i.e. closest valid points
67 */
68void 
69CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value,
70                        double tolerance) const
71{
72  if (fabs(floor(value+0.5)-value)>tolerance) {
73    floorValue = floor(value);
74  } else {
75    floorValue = floor(value+0.5);
76  }
77  ceilingValue = floorValue+1.0;
78}
79/* Infeasibility of the object
80     
81    This is some measure of the infeasibility of the object. 0.0
82    indicates that the object is satisfied.
83 
84    The preferred branching direction is returned in way,
85 
86    This is used to prepare for strong branching but should also think of
87    case when no strong branching
88 
89    The object may also compute an estimate of cost of going "up" or "down".
90    This will probably be based on pseudo-cost ideas
91
92    This should also set mutable infeasibility_ and whichWay_
93    This is for instant re-use for speed
94*/
95double 
96CbcObject::infeasibility(const OsiSolverInterface * solver,int &preferredWay) const 
97{
98  assert (solver==model_->solver());
99  return infeasibility(preferredWay);
100}
101 
102/* For the variable(s) referenced by the object,
103      look at the current solution and set bounds to match the solution.
104      Returns measure of how much it had to move solution to make feasible
105*/
106double 
107CbcObject::feasibleRegion(OsiSolverInterface * solver) const 
108{
109  assert (solver==model_->solver());
110  CbcObject * fudge = const_cast<CbcObject *>(this);
111  fudge->feasibleRegion();
112  return 0.0;
113}
114/* Infeasibility of the object
115     
116    This is some measure of the infeasibility of the object. 0.0
117    indicates that the object is satisfied.
118 
119    The preferred branching direction is returned in way,
120 
121    This is used to prepare for strong branching but should also think of
122    case when no strong branching
123 
124    The object may also compute an estimate of cost of going "up" or "down".
125    This will probably be based on pseudo-cost ideas
126
127    This should also set mutable infeasibility_ and whichWay_
128    This is for instant re-use for speed
129*/
130double 
131CbcObject::infeasibility(const OsiBranchingInformation * info,
132                         int &preferredWay) const 
133{
134  return infeasibility(preferredWay);
135}
136 
137/* For the variable(s) referenced by the object,
138      look at the current solution and set bounds to match the solution.
139      Returns measure of how much it had to move solution to make feasible
140*/
141double 
142CbcObject::feasibleRegion(OsiSolverInterface * solver,const OsiBranchingInformation * info) const 
143{
144  assert (solver==model_->solver());
145  CbcObject * fudge = const_cast<CbcObject *>(this);
146  fudge->feasibleRegion();
147  return 0.0;
148}
149 
150/* Create a branching object and indicate which way to branch first.
151     
152      The branching object has to know how to create branches (fix
153      variables, etc.)
154*/
155OsiBranchingObject * 
156CbcObject::createBranch(OsiSolverInterface * solver, int way) const 
157{
158  assert (solver==model_->solver());
159  CbcObject * fudge = const_cast<CbcObject *>(this);
160  return fudge->createBranch(way);
161}
162/* Create a branching object and indicate which way to branch first.
163     
164      The branching object has to know how to create branches (fix
165      variables, etc.)
166*/
167OsiBranchingObject * 
168CbcObject::createBranch(OsiSolverInterface * solver,const OsiBranchingInformation * info, int way) const 
169{
170  assert (solver==model_->solver());
171  CbcObject * fudge = const_cast<CbcObject *>(this);
172  return fudge->createBranch(way);
173}
174/* Create an OsiSolverBranch object
175   
176This returns NULL if branch not represented by bound changes
177*/
178OsiSolverBranch * 
179CbcObject::solverBranch() const
180{
181  return NULL;
182}
183/* Pass in information on branch just done and create CbcObjectUpdateData instance.
184   If object does not need data then backward pointer will be NULL.
185   Assumes can get information from solver */
186CbcObjectUpdateData
187CbcObject::createUpdateInformation(const OsiSolverInterface * solver, 
188                                                        const CbcNode * node,
189                                                        const CbcBranchingObject * branchingObject)
190{
191  return CbcObjectUpdateData();
192}
193 
194// Default Constructor
195CbcBranchingObject::CbcBranchingObject()
196  : OsiBranchingObject()
197{
198  model_=NULL;
199  originalCbcObject_=NULL;
200  variable_=-1;
201  way_=0;
202}
203
204// Useful constructor
205CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value)
206  : OsiBranchingObject(model->solver(),value)
207{
208  model_= model;
209  originalCbcObject_=NULL;
210  variable_=variable;
211  way_=way;
212}
213
214// Copy constructor
215CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs)
216  : OsiBranchingObject(rhs)
217{
218  model_=rhs.model_;
219  originalCbcObject_=rhs.originalCbcObject_;
220  variable_=rhs.variable_;
221  way_=rhs.way_;
222  value_=rhs.value_;
223}
224
225// Assignment operator
226CbcBranchingObject & 
227CbcBranchingObject::operator=( const CbcBranchingObject& rhs)
228{
229  if (this != &rhs) {
230    OsiBranchingObject::operator=(rhs);
231    model_=rhs.model_;
232    originalCbcObject_=rhs.originalCbcObject_;
233    variable_=rhs.variable_;
234    way_=rhs.way_;
235  }
236  return *this;
237}
238
239// Destructor
240CbcBranchingObject::~CbcBranchingObject ()
241{
242}
243// Default Constructor
244CbcBranchDecision::CbcBranchDecision ()
245  : object_(NULL),model_(NULL),chooseMethod_(NULL)
246{
247}
248
249// Copy Constructor
250CbcBranchDecision::CbcBranchDecision (const CbcBranchDecision &rhs)
251  : object_(NULL),model_(rhs.model_),chooseMethod_(NULL)
252{
253  if (rhs.chooseMethod_)
254    chooseMethod_ = rhs.chooseMethod_->clone();
255}
256
257CbcBranchDecision::~CbcBranchDecision()
258{
259  delete object_;
260  delete chooseMethod_;
261}
262/* Compare N branching objects. Return index of best
263   and sets way of branching in chosen object.
264   
265   This routine is used only after strong branching.
266   This is reccommended version as it can be more sophisticated
267*/
268
269int
270CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects,
271                               int numberUnsatisfied,
272                               double * changeUp, int * numberInfeasibilitiesUp,
273                               double * changeDown, int * numberInfeasibilitiesDown,
274                               double objectiveValue) 
275{
276  int bestWay=0;
277  int whichObject = -1;
278  if (numberObjects) {
279    initialize(objects[0]->model()); 
280    CbcBranchingObject * bestObject = NULL;
281    for (int i = 0 ; i < numberObjects ; i++) {
282      int betterWay = betterBranch(objects[i],
283                                   bestObject,
284                                   changeUp[i],
285                                   numberInfeasibilitiesUp [i],
286                                   changeDown[i],
287                                   numberInfeasibilitiesDown[i] );
288      if (betterWay) {
289        bestObject = objects[i];
290        bestWay = betterWay;
291        whichObject=i;
292      }
293    }
294    // set way in best
295    if (whichObject>=0) 
296      objects[whichObject]->way(bestWay);
297  }
298  return whichObject;
299}
300// Set (clone) chooseMethod
301void 
302CbcBranchDecision::setChooseMethod(const OsiChooseVariable & method)
303{ 
304  delete chooseMethod_;
305  chooseMethod_ = method.clone();
306}
307// Default constructor
308CbcConsequence::CbcConsequence()
309{
310}
311
312
313// Destructor
314CbcConsequence::~CbcConsequence ()
315{
316}
317
318// Copy constructor
319CbcConsequence::CbcConsequence ( const CbcConsequence & rhs)
320{
321}
322
323// Assignment operator
324CbcConsequence & 
325CbcConsequence::operator=( const CbcConsequence& rhs)
326{
327  if (this!=&rhs) {
328  }
329  return *this;
330}
331// Default constructor
332CbcObjectUpdateData::CbcObjectUpdateData()
333  : object_(NULL),
334    way_(0),
335    objectNumber_(-1),
336    change_(0.0),
337    status_(0),
338    intDecrease_(0),
339    branchingValue_(0.0)
340{
341}
342
343// Useful constructor
344CbcObjectUpdateData::CbcObjectUpdateData (CbcObject * object,
345                                          int way,
346                                          double change,
347                                          int status,
348                                          int intDecrease,
349                                          double branchingValue)
350  : object_(object),
351    way_(way),
352    objectNumber_(-1),
353    change_(change),
354    status_(status),
355    intDecrease_(intDecrease),
356    branchingValue_(branchingValue)
357{
358}
359
360// Destructor
361CbcObjectUpdateData::~CbcObjectUpdateData ()
362{
363}
364
365// Copy constructor
366CbcObjectUpdateData::CbcObjectUpdateData ( const CbcObjectUpdateData & rhs)
367  : object_(rhs.object_),
368    way_(rhs.way_),
369    objectNumber_(rhs.objectNumber_),
370    change_(rhs.change_),
371    status_(rhs.status_),
372    intDecrease_(rhs.intDecrease_),
373    branchingValue_(rhs.branchingValue_)
374{
375}
376
377// Assignment operator
378CbcObjectUpdateData & 
379CbcObjectUpdateData::operator=( const CbcObjectUpdateData& rhs)
380{
381  if (this!=&rhs) {
382    object_ = rhs.object_;
383    way_ = rhs.way_;
384    objectNumber_ = rhs.objectNumber_;
385    change_ = rhs.change_;
386    status_ = rhs.status_;
387    intDecrease_ = rhs.intDecrease_;
388    branchingValue_ = rhs.branchingValue_;
389  }
390  return *this;
391}
392
393 
Note: See TracBrowser for help on using the repository browser.