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 "CbcModel.hpp" |
---|
13 | #include "CbcMessage.hpp" |
---|
14 | #include "CbcBranchBase.hpp" |
---|
15 | |
---|
16 | |
---|
17 | // Default Constructor |
---|
18 | CbcObject::CbcObject() |
---|
19 | :model_(NULL), |
---|
20 | id_(-1), |
---|
21 | priority_(1000) |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | // Constructor from model |
---|
26 | CbcObject::CbcObject(CbcModel * model) |
---|
27 | : |
---|
28 | model_(model), |
---|
29 | id_(-1), |
---|
30 | priority_(1000) |
---|
31 | { |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | // Destructor |
---|
36 | CbcObject::~CbcObject () |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | // Copy constructor |
---|
41 | CbcObject::CbcObject ( const CbcObject & rhs) |
---|
42 | { |
---|
43 | model_ = rhs.model_; |
---|
44 | id_ = rhs.id_; |
---|
45 | priority_ = rhs.priority_; |
---|
46 | } |
---|
47 | |
---|
48 | // Assignment operator |
---|
49 | CbcObject & |
---|
50 | CbcObject::operator=( const CbcObject& rhs) |
---|
51 | { |
---|
52 | if (this!=&rhs) { |
---|
53 | model_ = rhs.model_; |
---|
54 | id_ = rhs.id_; |
---|
55 | priority_ = rhs.priority_; |
---|
56 | } |
---|
57 | return *this; |
---|
58 | } |
---|
59 | |
---|
60 | /* Returns floor and ceiling i.e. closest valid points |
---|
61 | */ |
---|
62 | void |
---|
63 | CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value, |
---|
64 | double tolerance) const |
---|
65 | { |
---|
66 | if (fabs(floor(value+0.5)-value)>tolerance) { |
---|
67 | floorValue = floor(value); |
---|
68 | } else { |
---|
69 | floorValue = floor(value+0.5); |
---|
70 | } |
---|
71 | ceilingValue = floorValue+1.0; |
---|
72 | } |
---|
73 | // Return "up" estimate (default 1.0e-5) |
---|
74 | double |
---|
75 | CbcObject::upEstimate() const |
---|
76 | { |
---|
77 | return 1.0e-5; |
---|
78 | } |
---|
79 | // Return "down" estimate (default 1.0e-5) |
---|
80 | double |
---|
81 | CbcObject::downEstimate() const |
---|
82 | { |
---|
83 | return 1.0e-5; |
---|
84 | } |
---|
85 | // Column number if single column object -1 otherwise |
---|
86 | int |
---|
87 | CbcObject::columnNumber() const |
---|
88 | { |
---|
89 | // Say not as at present only used by heuristics |
---|
90 | return -1; |
---|
91 | } |
---|
92 | // Default Constructor |
---|
93 | CbcBranchingObject::CbcBranchingObject() |
---|
94 | { |
---|
95 | model_=NULL; |
---|
96 | variable_=-1; |
---|
97 | way_=0; |
---|
98 | value_=0.0; |
---|
99 | numberBranchesLeft_=0; |
---|
100 | } |
---|
101 | |
---|
102 | // Useful constructor |
---|
103 | CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value) |
---|
104 | { |
---|
105 | model_= model; |
---|
106 | variable_=variable; |
---|
107 | way_=way; |
---|
108 | value_=value; |
---|
109 | numberBranchesLeft_=2; |
---|
110 | } |
---|
111 | |
---|
112 | // Copy constructor |
---|
113 | CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs) |
---|
114 | { |
---|
115 | model_=rhs.model_; |
---|
116 | variable_=rhs.variable_; |
---|
117 | way_=rhs.way_; |
---|
118 | value_=rhs.value_; |
---|
119 | numberBranchesLeft_=rhs.numberBranchesLeft_; |
---|
120 | } |
---|
121 | |
---|
122 | // Assignment operator |
---|
123 | CbcBranchingObject & |
---|
124 | CbcBranchingObject::operator=( const CbcBranchingObject& rhs) |
---|
125 | { |
---|
126 | if (this != &rhs) { |
---|
127 | model_=rhs.model_; |
---|
128 | variable_=rhs.variable_; |
---|
129 | way_=rhs.way_; |
---|
130 | value_=rhs.value_; |
---|
131 | numberBranchesLeft_=rhs.numberBranchesLeft_; |
---|
132 | } |
---|
133 | return *this; |
---|
134 | } |
---|
135 | |
---|
136 | // Destructor |
---|
137 | CbcBranchingObject::~CbcBranchingObject () |
---|
138 | { |
---|
139 | } |
---|
140 | // Default Constructor |
---|
141 | CbcBranchDecision::CbcBranchDecision () |
---|
142 | { |
---|
143 | } |
---|
144 | |
---|
145 | CbcBranchDecision::~CbcBranchDecision() |
---|
146 | { |
---|
147 | } |
---|
148 | /* Compare N branching objects. Return index of best |
---|
149 | and sets way of branching in chosen object. |
---|
150 | |
---|
151 | This routine is used only after strong branching. |
---|
152 | This is reccommended version as it can be more sophisticated |
---|
153 | */ |
---|
154 | |
---|
155 | int |
---|
156 | CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects, |
---|
157 | int numberUnsatisfied, |
---|
158 | double * changeUp, int * numberInfeasibilitiesUp, |
---|
159 | double * changeDown, int * numberInfeasibilitiesDown, |
---|
160 | double objectiveValue) |
---|
161 | { |
---|
162 | int bestWay=0; |
---|
163 | int whichObject = -1; |
---|
164 | if (numberObjects) { |
---|
165 | initialize(objects[0]->model()); |
---|
166 | CbcBranchingObject * bestObject = NULL; |
---|
167 | for (int i = 0 ; i < numberObjects ; i++) { |
---|
168 | int betterWay = betterBranch(objects[i], |
---|
169 | bestObject, |
---|
170 | changeUp[i], |
---|
171 | numberInfeasibilitiesUp [i], |
---|
172 | changeDown[i], |
---|
173 | numberInfeasibilitiesDown[i] ); |
---|
174 | if (betterWay) { |
---|
175 | bestObject = objects[i]; |
---|
176 | bestWay = betterWay; |
---|
177 | whichObject=i; |
---|
178 | } |
---|
179 | } |
---|
180 | // set way in best |
---|
181 | if (whichObject>=0) |
---|
182 | objects[whichObject]->way(bestWay); |
---|
183 | else |
---|
184 | printf("debug\n"); |
---|
185 | } |
---|
186 | return whichObject; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|