1 | // $Id: CbcSOS.hpp 1854 2013-01-28 00:02:55Z stefan $ |
---|
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 | // Edwin 11/9/2009-- carved out of CbcBranchActual |
---|
7 | |
---|
8 | #ifndef CbcSOS_H |
---|
9 | #define CbcSOS_H |
---|
10 | |
---|
11 | /** \brief Branching object for Special Ordered Sets of type 1 and 2. |
---|
12 | |
---|
13 | SOS1 are an ordered set of variables where at most one variable can be |
---|
14 | non-zero. SOS1 are commonly defined with binary variables (interpreted as |
---|
15 | selection between alternatives) but this is not necessary. An SOS1 with |
---|
16 | all binary variables is a special case of a clique (setting any one |
---|
17 | variable to 1 forces all others to 0). |
---|
18 | |
---|
19 | In theory, the implementation makes no assumptions about integrality in |
---|
20 | Type 1 sets. In practice, there are places where the code seems to have been |
---|
21 | written with a binary SOS mindset. Current development of SOS branching |
---|
22 | objects is proceeding in OsiSOS. |
---|
23 | |
---|
24 | SOS2 are an ordered set of variables in which at most two consecutive |
---|
25 | variables can be non-zero and must sum to 1 (interpreted as interpolation |
---|
26 | between two discrete values). By definition the variables are non-integer. |
---|
27 | */ |
---|
28 | |
---|
29 | class CbcSOS : public CbcObject { |
---|
30 | |
---|
31 | public: |
---|
32 | |
---|
33 | // Default Constructor |
---|
34 | CbcSOS (); |
---|
35 | |
---|
36 | /** \brief Constructor with SOS type and member information |
---|
37 | |
---|
38 | Type specifies SOS 1 or 2. Identifier is an arbitrary value. |
---|
39 | |
---|
40 | Which should be an array of variable indices with numberMembers entries. |
---|
41 | Weights can be used to assign arbitrary weights to variables, in the order |
---|
42 | they are specified in which. If no weights are provided, a default array of |
---|
43 | 0, 1, 2, ... is generated. |
---|
44 | */ |
---|
45 | |
---|
46 | CbcSOS (CbcModel * model, int numberMembers, |
---|
47 | const int * which, const double * weights, int identifier, |
---|
48 | int type = 1); |
---|
49 | |
---|
50 | // Copy constructor |
---|
51 | CbcSOS ( const CbcSOS &); |
---|
52 | |
---|
53 | /// Clone |
---|
54 | virtual CbcObject * clone() const; |
---|
55 | |
---|
56 | // Assignment operator |
---|
57 | CbcSOS & operator=( const CbcSOS& rhs); |
---|
58 | |
---|
59 | // Destructor |
---|
60 | virtual ~CbcSOS (); |
---|
61 | |
---|
62 | /// Infeasibility - large is 0.5 |
---|
63 | virtual double infeasibility(const OsiBranchingInformation * info, |
---|
64 | int &preferredWay) const; |
---|
65 | |
---|
66 | using CbcObject::feasibleRegion ; |
---|
67 | /// This looks at solution and sets bounds to contain solution |
---|
68 | virtual void feasibleRegion(); |
---|
69 | |
---|
70 | /// Creates a branching object |
---|
71 | virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ; |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | /** Pass in information on branch just done and create CbcObjectUpdateData instance. |
---|
76 | If object does not need data then backward pointer will be NULL. |
---|
77 | Assumes can get information from solver */ |
---|
78 | virtual CbcObjectUpdateData createUpdateInformation(const OsiSolverInterface * solver, |
---|
79 | const CbcNode * node, |
---|
80 | const CbcBranchingObject * branchingObject); |
---|
81 | /// Update object by CbcObjectUpdateData |
---|
82 | virtual void updateInformation(const CbcObjectUpdateData & data) ; |
---|
83 | using CbcObject::solverBranch ; |
---|
84 | /** Create an OsiSolverBranch object |
---|
85 | |
---|
86 | This returns NULL if branch not represented by bound changes |
---|
87 | */ |
---|
88 | virtual OsiSolverBranch * solverBranch() const; |
---|
89 | /// Redoes data when sequence numbers change |
---|
90 | virtual void redoSequenceEtc(CbcModel * model, int numberColumns, const int * originalColumns); |
---|
91 | |
---|
92 | /// Construct an OsiSOS object |
---|
93 | OsiSOS * osiObject(const OsiSolverInterface * solver) const; |
---|
94 | /// Number of members |
---|
95 | inline int numberMembers() const { |
---|
96 | return numberMembers_; |
---|
97 | } |
---|
98 | |
---|
99 | /// Members (indices in range 0 ... numberColumns-1) |
---|
100 | inline const int * members() const { |
---|
101 | return members_; |
---|
102 | } |
---|
103 | |
---|
104 | /// SOS type |
---|
105 | inline int sosType() const { |
---|
106 | return sosType_; |
---|
107 | } |
---|
108 | /// Down number times |
---|
109 | inline int numberTimesDown() const { |
---|
110 | return numberTimesDown_; |
---|
111 | } |
---|
112 | /// Up number times |
---|
113 | inline int numberTimesUp() const { |
---|
114 | return numberTimesUp_; |
---|
115 | } |
---|
116 | |
---|
117 | /** Array of weights */ |
---|
118 | inline const double * weights() const { |
---|
119 | return weights_; |
---|
120 | } |
---|
121 | |
---|
122 | /// Set number of members |
---|
123 | inline void setNumberMembers(int n) { |
---|
124 | numberMembers_ = n; |
---|
125 | } |
---|
126 | |
---|
127 | /// Members (indices in range 0 ... numberColumns-1) |
---|
128 | inline int * mutableMembers() const { |
---|
129 | return members_; |
---|
130 | } |
---|
131 | |
---|
132 | /** Array of weights */ |
---|
133 | inline double * mutableWeights() const { |
---|
134 | return weights_; |
---|
135 | } |
---|
136 | |
---|
137 | /** \brief Return true if object can take part in normal heuristics |
---|
138 | */ |
---|
139 | virtual bool canDoHeuristics() const { |
---|
140 | return (sosType_ == 1 && integerValued_); |
---|
141 | } |
---|
142 | /// Set whether set is integer valued or not |
---|
143 | inline void setIntegerValued(bool yesNo) { |
---|
144 | integerValued_ = yesNo; |
---|
145 | } |
---|
146 | private: |
---|
147 | /// data |
---|
148 | |
---|
149 | /// Members (indices in range 0 ... numberColumns-1) |
---|
150 | int * members_; |
---|
151 | /** \brief Weights for individual members |
---|
152 | |
---|
153 | Arbitrary weights for members. Can be used to attach meaning to variable |
---|
154 | values independent of objective coefficients. For example, if the SOS set |
---|
155 | comprises binary variables used to choose a facility of a given size, the |
---|
156 | weight could be the corresponding facilty size. Fractional values of the |
---|
157 | SOS variables can then be used to estimate ideal facility size. |
---|
158 | |
---|
159 | Weights cannot be completely arbitrary. From the code, they must be |
---|
160 | differ by at least 1.0e-7 |
---|
161 | */ |
---|
162 | |
---|
163 | double * weights_; |
---|
164 | /// Current pseudo-shadow price estimate down |
---|
165 | mutable double shadowEstimateDown_; |
---|
166 | /// Current pseudo-shadow price estimate up |
---|
167 | mutable double shadowEstimateUp_; |
---|
168 | /// Down pseudo ratio |
---|
169 | double downDynamicPseudoRatio_; |
---|
170 | /// Up pseudo ratio |
---|
171 | double upDynamicPseudoRatio_; |
---|
172 | /// Number of times we have gone down |
---|
173 | int numberTimesDown_; |
---|
174 | /// Number of times we have gone up |
---|
175 | int numberTimesUp_; |
---|
176 | /// Number of members |
---|
177 | int numberMembers_; |
---|
178 | /// SOS type |
---|
179 | int sosType_; |
---|
180 | /// Whether integer valued |
---|
181 | bool integerValued_; |
---|
182 | }; |
---|
183 | |
---|
184 | /** Branching object for Special ordered sets |
---|
185 | |
---|
186 | Variable_ is the set id number (redundant, as the object also holds a |
---|
187 | pointer to the set. |
---|
188 | */ |
---|
189 | class CbcSOSBranchingObject : public CbcBranchingObject { |
---|
190 | |
---|
191 | public: |
---|
192 | |
---|
193 | // Default Constructor |
---|
194 | CbcSOSBranchingObject (); |
---|
195 | |
---|
196 | // Useful constructor |
---|
197 | CbcSOSBranchingObject (CbcModel * model, const CbcSOS * clique, |
---|
198 | int way, |
---|
199 | double separator); |
---|
200 | |
---|
201 | // Copy constructor |
---|
202 | CbcSOSBranchingObject ( const CbcSOSBranchingObject &); |
---|
203 | |
---|
204 | // Assignment operator |
---|
205 | CbcSOSBranchingObject & operator=( const CbcSOSBranchingObject& rhs); |
---|
206 | |
---|
207 | /// Clone |
---|
208 | virtual CbcBranchingObject * clone() const; |
---|
209 | |
---|
210 | // Destructor |
---|
211 | virtual ~CbcSOSBranchingObject (); |
---|
212 | |
---|
213 | using CbcBranchingObject::branch ; |
---|
214 | /// Does next branch and updates state |
---|
215 | virtual double branch(); |
---|
216 | /** Update bounds in solver as in 'branch' and update given bounds. |
---|
217 | branchState is -1 for 'down' +1 for 'up' */ |
---|
218 | virtual void fix(OsiSolverInterface * solver, |
---|
219 | double * lower, double * upper, |
---|
220 | int branchState) const ; |
---|
221 | |
---|
222 | /** Reset every information so that the branching object appears to point to |
---|
223 | the previous child. This method does not need to modify anything in any |
---|
224 | solver. */ |
---|
225 | virtual void previousBranch() { |
---|
226 | CbcBranchingObject::previousBranch(); |
---|
227 | computeNonzeroRange(); |
---|
228 | } |
---|
229 | |
---|
230 | using CbcBranchingObject::print ; |
---|
231 | /** \brief Print something about branch - only if log level high |
---|
232 | */ |
---|
233 | virtual void print(); |
---|
234 | |
---|
235 | /** Return the type (an integer identifier) of \c this */ |
---|
236 | virtual CbcBranchObjType type() const { |
---|
237 | return SoSBranchObj; |
---|
238 | } |
---|
239 | |
---|
240 | /** Compare the original object of \c this with the original object of \c |
---|
241 | brObj. Assumes that there is an ordering of the original objects. |
---|
242 | This method should be invoked only if \c this and brObj are of the same |
---|
243 | type. |
---|
244 | Return negative/0/positive depending on whether \c this is |
---|
245 | smaller/same/larger than the argument. |
---|
246 | */ |
---|
247 | virtual int compareOriginalObject(const CbcBranchingObject* brObj) const; |
---|
248 | |
---|
249 | /** Compare the \c this with \c brObj. \c this and \c brObj must be os the |
---|
250 | same type and must have the same original object, but they may have |
---|
251 | different feasible regions. |
---|
252 | Return the appropriate CbcRangeCompare value (first argument being the |
---|
253 | sub/superset if that's the case). In case of overlap (and if \c |
---|
254 | replaceIfOverlap is true) replace the current branching object with one |
---|
255 | whose feasible region is the overlap. |
---|
256 | */ |
---|
257 | virtual CbcRangeCompare compareBranchingObject |
---|
258 | (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false); |
---|
259 | |
---|
260 | /** Fill out the \c firstNonzero_ and \c lastNonzero_ data members */ |
---|
261 | void computeNonzeroRange(); |
---|
262 | |
---|
263 | private: |
---|
264 | /// data |
---|
265 | const CbcSOS * set_; |
---|
266 | /// separator |
---|
267 | double separator_; |
---|
268 | /** The following two members describe the range in the members_ of the |
---|
269 | original object that whose upper bound is not fixed to 0. This is not |
---|
270 | necessary for Cbc to function correctly, this is there for heuristics so |
---|
271 | that separate branching decisions on the same object can be pooled into |
---|
272 | one branching object. */ |
---|
273 | int firstNonzero_; |
---|
274 | int lastNonzero_; |
---|
275 | }; |
---|
276 | #endif |
---|
277 | |
---|