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

Last change on this file since 2464 was 2464, checked in by unxusr, 10 months ago

.clang-format with proposal for formatting code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: CbcBranchBase.cpp 2464 2019-01-03 19:03:23Z unxusr $ */
2// Copyright (C) 2002, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#if defined(_MSC_VER)
7// Turn off compiler warning about long names
8#pragma warning(disable : 4786)
9#endif
10#include <cassert>
11#include <cstdlib>
12#include <cmath>
13#include <cfloat>
14
15#include "OsiSolverInterface.hpp"
16#include "OsiSolverBranch.hpp"
17#include "OsiChooseVariable.hpp"
18#include "CbcModel.hpp"
19#include "CbcMessage.hpp"
20#include "CbcBranchBase.hpp"
21
22// Default Constructor
23CbcObject::CbcObject()
24  : OsiObject()
25  , model_(NULL)
26  , id_(-1)
27  , position_(-1)
28  , preferredWay_(0)
29{
30}
31
32// Constructor from model
33CbcObject::CbcObject(CbcModel *model)
34  : OsiObject()
35  , model_(model)
36  , id_(-1)
37  , position_(-1)
38  , preferredWay_(0)
39{
40}
41
42// Destructor
43CbcObject::~CbcObject()
44{
45}
46
47// Copy constructor
48CbcObject::CbcObject(const CbcObject &rhs)
49  : OsiObject(rhs)
50{
51  model_ = rhs.model_;
52  id_ = rhs.id_;
53  position_ = rhs.position_;
54  preferredWay_ = rhs.preferredWay_;
55}
56
57// Assignment operator
58CbcObject &
59CbcObject::operator=(const CbcObject &rhs)
60{
61  if (this != &rhs) {
62    OsiObject::operator=(rhs);
63    model_ = rhs.model_;
64    id_ = rhs.id_;
65    position_ = rhs.position_;
66    preferredWay_ = rhs.preferredWay_;
67  }
68  return *this;
69}
70
71/* Returns floor and ceiling i.e. closest valid points
72 */
73void CbcObject::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/* For the variable(s) referenced by the object,
84      look at the current solution and set bounds to match the solution.
85      Returns measure of how much it had to move solution to make feasible
86*/
87double
88CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/) const
89{
90  //assert (solver==model_->solver());
91  CbcObject *fudge = const_cast<CbcObject *>(this);
92  fudge->feasibleRegion();
93  return 0.0;
94}
95
96/* For the variable(s) referenced by the object,
97      look at the current solution and set bounds to match the solution.
98      Returns measure of how much it had to move solution to make feasible
99*/
100double
101CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/,
102  const OsiBranchingInformation * /*info*/) const
103{
104  //assert (solver==model_->solver());
105  CbcObject *fudge = const_cast<CbcObject *>(this);
106  fudge->feasibleRegion();
107  return 0.0;
108}
109/* Create a branching object and indicate which way to branch first.
110
111      The branching object has to know how to create branches (fix
112      variables, etc.)
113*/
114OsiBranchingObject *
115CbcObject::createOsiBranch(OsiSolverInterface *solver,
116  const OsiBranchingInformation *info,
117  int way) const
118{
119  //assert (solver==model_->solver());
120  CbcObject *fudge = const_cast<CbcObject *>(this);
121  return fudge->createBranch(solver, info, way);
122}
123/* Create an OsiSolverBranch object
124
125This returns NULL if branch not represented by bound changes
126*/
127OsiSolverBranch *
128CbcObject::solverBranch() const
129{
130  return NULL;
131}
132/* Pass in information on branch just done and create CbcObjectUpdateData instance.
133   If object does not need data then backward pointer will be NULL.
134   Assumes can get information from solver */
135CbcObjectUpdateData
136CbcObject::createUpdateInformation(const OsiSolverInterface * /*solver*/,
137  const CbcNode * /*node*/,
138  const CbcBranchingObject * /*branchingObject*/)
139{
140  return CbcObjectUpdateData();
141}
142
143// Default Constructor
144CbcBranchingObject::CbcBranchingObject()
145  : OsiBranchingObject()
146{
147  model_ = NULL;
148  originalCbcObject_ = NULL;
149  variable_ = -1;
150  way_ = 0;
151}
152
153// Useful constructor
154CbcBranchingObject::CbcBranchingObject(CbcModel *model, int variable, int way, double value)
155  : OsiBranchingObject(model->solver(), value)
156{
157  model_ = model;
158  originalCbcObject_ = NULL;
159  variable_ = variable;
160  way_ = way;
161}
162
163// Copy constructor
164CbcBranchingObject::CbcBranchingObject(const CbcBranchingObject &rhs)
165  : OsiBranchingObject(rhs)
166{
167  model_ = rhs.model_;
168  originalCbcObject_ = rhs.originalCbcObject_;
169  variable_ = rhs.variable_;
170  way_ = rhs.way_;
171  value_ = rhs.value_;
172}
173
174// Assignment operator
175CbcBranchingObject &
176CbcBranchingObject::operator=(const CbcBranchingObject &rhs)
177{
178  if (this != &rhs) {
179    OsiBranchingObject::operator=(rhs);
180    model_ = rhs.model_;
181    originalCbcObject_ = rhs.originalCbcObject_;
182    variable_ = rhs.variable_;
183    way_ = rhs.way_;
184  }
185  return *this;
186}
187
188// Destructor
189CbcBranchingObject::~CbcBranchingObject()
190{
191}
192// Default Constructor
193CbcBranchDecision::CbcBranchDecision()
194  : object_(NULL)
195  , model_(NULL)
196  , chooseMethod_(NULL)
197{
198}
199
200// Copy Constructor
201CbcBranchDecision::CbcBranchDecision(const CbcBranchDecision &rhs)
202  : object_(NULL)
203  , model_(rhs.model_)
204  , chooseMethod_(NULL)
205{
206  if (rhs.chooseMethod_)
207    chooseMethod_ = rhs.chooseMethod_->clone();
208}
209
210CbcBranchDecision::~CbcBranchDecision()
211{
212  delete object_;
213  delete chooseMethod_;
214}
215/* Compare N branching objects. Return index of best
216   and sets way of branching in chosen object.
217
218   This routine is used only after strong branching.
219   This is reccommended version as it can be more sophisticated
220*/
221
222int CbcBranchDecision::bestBranch(CbcBranchingObject **objects, int numberObjects,
223  int /*numberUnsatisfied*/,
224  double *changeUp, int *numberInfeasibilitiesUp,
225  double *changeDown, int *numberInfeasibilitiesDown,
226  double /*objectiveValue*/)
227{
228  int bestWay = 0;
229  int whichObject = -1;
230  if (numberObjects) {
231    initialize(objects[0]->model());
232    CbcBranchingObject *bestObject = NULL;
233    for (int i = 0; i < numberObjects; i++) {
234      int betterWay = betterBranch(objects[i],
235        bestObject,
236        changeUp[i],
237        numberInfeasibilitiesUp[i],
238        changeDown[i],
239        numberInfeasibilitiesDown[i]);
240      if (betterWay) {
241        bestObject = objects[i];
242        bestWay = betterWay;
243        whichObject = i;
244      }
245    }
246    // set way in best
247    if (whichObject >= 0)
248      objects[whichObject]->way(bestWay);
249  }
250  return whichObject;
251}
252// Set (clone) chooseMethod
253void CbcBranchDecision::setChooseMethod(const OsiChooseVariable &method)
254{
255  delete chooseMethod_;
256  chooseMethod_ = method.clone();
257}
258// Default constructor
259CbcConsequence::CbcConsequence()
260{
261}
262
263// Destructor
264CbcConsequence::~CbcConsequence()
265{
266}
267
268// Copy constructor
269CbcConsequence::CbcConsequence(const CbcConsequence & /*rhs*/)
270{
271}
272
273// Assignment operator
274CbcConsequence &
275CbcConsequence::operator=(const CbcConsequence &rhs)
276{
277  if (this != &rhs) {
278  }
279  return *this;
280}
281// Default constructor
282CbcObjectUpdateData::CbcObjectUpdateData()
283  : object_(NULL)
284  , way_(0)
285  , objectNumber_(-1)
286  , change_(0.0)
287  , status_(0)
288  , intDecrease_(0)
289  , branchingValue_(0.0)
290  , originalObjective_(COIN_DBL_MAX)
291  , cutoff_(COIN_DBL_MAX)
292{
293}
294
295// Useful constructor
296CbcObjectUpdateData::CbcObjectUpdateData(CbcObject *object,
297  int way,
298  double change,
299  int status,
300  int intDecrease,
301  double branchingValue)
302  : object_(object)
303  , way_(way)
304  , objectNumber_(-1)
305  , change_(change)
306  , status_(status)
307  , intDecrease_(intDecrease)
308  , branchingValue_(branchingValue)
309  , originalObjective_(COIN_DBL_MAX)
310  , cutoff_(COIN_DBL_MAX)
311{
312}
313
314// Destructor
315CbcObjectUpdateData::~CbcObjectUpdateData()
316{
317}
318
319// Copy constructor
320CbcObjectUpdateData::CbcObjectUpdateData(const CbcObjectUpdateData &rhs)
321  : object_(rhs.object_)
322  , way_(rhs.way_)
323  , objectNumber_(rhs.objectNumber_)
324  , change_(rhs.change_)
325  , status_(rhs.status_)
326  , intDecrease_(rhs.intDecrease_)
327  , branchingValue_(rhs.branchingValue_)
328  , originalObjective_(rhs.originalObjective_)
329  , cutoff_(rhs.cutoff_)
330{
331}
332
333// Assignment operator
334CbcObjectUpdateData &
335CbcObjectUpdateData::operator=(const CbcObjectUpdateData &rhs)
336{
337  if (this != &rhs) {
338    object_ = rhs.object_;
339    way_ = rhs.way_;
340    objectNumber_ = rhs.objectNumber_;
341    change_ = rhs.change_;
342    status_ = rhs.status_;
343    intDecrease_ = rhs.intDecrease_;
344    branchingValue_ = rhs.branchingValue_;
345    originalObjective_ = rhs.originalObjective_;
346    cutoff_ = rhs.cutoff_;
347  }
348  return *this;
349}
Note: See TracBrowser for help on using the repository browser.