source: trunk/Cbc/src/CbcBranchBase.cpp @ 1121

Last change on this file since 1121 was 1121, checked in by forrest, 10 years ago

compiler warnings, deterministic parallel and stability

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