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

Last change on this file since 1422 was 1286, checked in by EdwinStraver, 10 years ago

Changed formatting using AStyle -A4 -p

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/* $Id: CbcBranchBase.cpp 1286 2009-11-09 23:33:07Z tkr $ */
2// Copyright (C) 2002, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#if defined(_MSC_VER)
5// Turn off compiler warning about long names
6#  pragma warning(disable:4786)
7#endif
8#include <cassert>
9#include <cstdlib>
10#include <cmath>
11#include <cfloat>
12
13#include "OsiSolverInterface.hpp"
14#include "OsiSolverBranch.hpp"
15#include "OsiChooseVariable.hpp"
16#include "CbcModel.hpp"
17#include "CbcMessage.hpp"
18#include "CbcBranchBase.hpp"
19
20
21// Default Constructor
22CbcObject::CbcObject()
23        : OsiObject(),
24        model_(NULL),
25        id_(-1),
26        position_(-1),
27        preferredWay_(0)
28{
29}
30
31// Constructor from model
32CbcObject::CbcObject(CbcModel * model)
33        : OsiObject(),
34        model_(model),
35        id_(-1),
36        position_(-1),
37        preferredWay_(0)
38{
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
74CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value,
75                        double tolerance) const
76{
77    if (fabs(floor(value + 0.5) - value) > tolerance) {
78        floorValue = floor(value);
79    } else {
80        floorValue = floor(value + 0.5);
81    }
82    ceilingValue = floorValue + 1.0;
83}
84/* For the variable(s) referenced by the object,
85      look at the current solution and set bounds to match the solution.
86      Returns measure of how much it had to move solution to make feasible
87*/
88double
89CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/) const
90{
91    //assert (solver==model_->solver());
92    CbcObject * fudge = const_cast<CbcObject *>(this);
93    fudge->feasibleRegion();
94    return 0.0;
95}
96
97/* For the variable(s) referenced by the object,
98      look at the current solution and set bounds to match the solution.
99      Returns measure of how much it had to move solution to make feasible
100*/
101double
102CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/,
103                          const OsiBranchingInformation * /*info*/) const
104{
105    //assert (solver==model_->solver());
106    CbcObject * fudge = const_cast<CbcObject *>(this);
107    fudge->feasibleRegion();
108    return 0.0;
109}
110/* Create a branching object and indicate which way to branch first.
111
112      The branching object has to know how to create branches (fix
113      variables, etc.)
114*/
115OsiBranchingObject *
116CbcObject::createOsiBranch(OsiSolverInterface * solver,
117                           const OsiBranchingInformation * info,
118                           int way) const
119{
120    //assert (solver==model_->solver());
121    CbcObject * fudge = const_cast<CbcObject *>(this);
122    return fudge->createBranch(solver, info, way);
123}
124/* Create an OsiSolverBranch object
125
126This returns NULL if branch not represented by bound changes
127*/
128OsiSolverBranch *
129CbcObject::solverBranch() const
130{
131    return NULL;
132}
133/* Pass in information on branch just done and create CbcObjectUpdateData instance.
134   If object does not need data then backward pointer will be NULL.
135   Assumes can get information from solver */
136CbcObjectUpdateData
137CbcObject::createUpdateInformation(const OsiSolverInterface * /*solver*/,
138                                   const CbcNode * /*node*/,
139                                   const CbcBranchingObject * /*branchingObject*/)
140{
141    return CbcObjectUpdateData();
142}
143
144// Default Constructor
145CbcBranchingObject::CbcBranchingObject()
146        : OsiBranchingObject()
147{
148    model_ = NULL;
149    originalCbcObject_ = NULL;
150    variable_ = -1;
151    way_ = 0;
152}
153
154// Useful constructor
155CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value)
156        : OsiBranchingObject(model->solver(), value)
157{
158    model_ = model;
159    originalCbcObject_ = NULL;
160    variable_ = variable;
161    way_ = way;
162}
163
164// Copy constructor
165CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs)
166        : OsiBranchingObject(rhs)
167{
168    model_ = rhs.model_;
169    originalCbcObject_ = rhs.originalCbcObject_;
170    variable_ = rhs.variable_;
171    way_ = rhs.way_;
172    value_ = rhs.value_;
173}
174
175// Assignment operator
176CbcBranchingObject &
177CbcBranchingObject::operator=( const CbcBranchingObject & rhs)
178{
179    if (this != &rhs) {
180        OsiBranchingObject::operator=(rhs);
181        model_ = rhs.model_;
182        originalCbcObject_ = rhs.originalCbcObject_;
183        variable_ = rhs.variable_;
184        way_ = rhs.way_;
185    }
186    return *this;
187}
188
189// Destructor
190CbcBranchingObject::~CbcBranchingObject ()
191{
192}
193// Default Constructor
194CbcBranchDecision::CbcBranchDecision ()
195        : object_(NULL), model_(NULL), chooseMethod_(NULL)
196{
197}
198
199// Copy Constructor
200CbcBranchDecision::CbcBranchDecision (const CbcBranchDecision &rhs)
201        : object_(NULL), model_(rhs.model_), chooseMethod_(NULL)
202{
203    if (rhs.chooseMethod_)
204        chooseMethod_ = rhs.chooseMethod_->clone();
205}
206
207CbcBranchDecision::~CbcBranchDecision()
208{
209    delete object_;
210    delete chooseMethod_;
211}
212/* Compare N branching objects. Return index of best
213   and sets way of branching in chosen object.
214
215   This routine is used only after strong branching.
216   This is reccommended version as it can be more sophisticated
217*/
218
219int
220CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects,
221                               int /*numberUnsatisfied*/,
222                               double * changeUp, int * numberInfeasibilitiesUp,
223                               double * changeDown, int * numberInfeasibilitiesDown,
224                               double /*objectiveValue*/)
225{
226    int bestWay = 0;
227    int whichObject = -1;
228    if (numberObjects) {
229        initialize(objects[0]->model());
230        CbcBranchingObject * bestObject = NULL;
231        for (int i = 0 ; i < numberObjects ; i++) {
232            int betterWay = betterBranch(objects[i],
233                                         bestObject,
234                                         changeUp[i],
235                                         numberInfeasibilitiesUp [i],
236                                         changeDown[i],
237                                         numberInfeasibilitiesDown[i] );
238            if (betterWay) {
239                bestObject = objects[i];
240                bestWay = betterWay;
241                whichObject = i;
242            }
243        }
244        // set way in best
245        if (whichObject >= 0)
246            objects[whichObject]->way(bestWay);
247    }
248    return whichObject;
249}
250// Set (clone) chooseMethod
251void
252CbcBranchDecision::setChooseMethod(const OsiChooseVariable & method)
253{
254    delete chooseMethod_;
255    chooseMethod_ = method.clone();
256}
257// Default constructor
258CbcConsequence::CbcConsequence()
259{
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}
350
351
Note: See TracBrowser for help on using the repository browser.