source: trunk/Cbc/src/CbcSimpleInteger.hpp @ 1422

Last change on this file since 1422 was 1393, checked in by lou, 10 years ago

Mark #if 0 with JJF_ZERO and #if 1 with JJF_ONE as a historical reference
point.

File size: 9.1 KB
Line 
1// Edwin 11/9/2009-- carved out of CbcBranchActual
2
3#ifndef CbcSimpleInteger_H
4#define CbcSimpleInteger_H
5
6/** Simple branching object for an integer variable
7
8  This object can specify a two-way branch on an integer variable. For each
9  arm of the branch, the upper and lower bounds on the variable can be
10  independently specified.
11
12  Variable_ holds the index of the integer variable in the integerVariable_
13  array of the model.
14*/
15
16class CbcIntegerBranchingObject : public CbcBranchingObject {
17
18public:
19
20    /// Default constructor
21    CbcIntegerBranchingObject ();
22
23    /** Create a standard floor/ceiling branch object
24
25      Specifies a simple two-way branch. Let \p value = x*. One arm of the
26      branch will be lb <= x <= floor(x*), the other ceil(x*) <= x <= ub.
27      Specify way = -1 to set the object state to perform the down arm first,
28      way = 1 for the up arm.
29    */
30    CbcIntegerBranchingObject (CbcModel *model, int variable,
31                               int way , double value) ;
32
33    /** Create a degenerate branch object
34
35      Specifies a `one-way branch'. Calling branch() for this object will
36      always result in lowerValue <= x <= upperValue. Used to fix a variable
37      when lowerValue = upperValue.
38    */
39
40    CbcIntegerBranchingObject (CbcModel *model, int variable, int way,
41                               double lowerValue, double upperValue) ;
42
43    /// Copy constructor
44    CbcIntegerBranchingObject ( const CbcIntegerBranchingObject &);
45
46    /// Assignment operator
47    CbcIntegerBranchingObject & operator= (const CbcIntegerBranchingObject& rhs);
48
49    /// Clone
50    virtual CbcBranchingObject * clone() const;
51
52    /// Destructor
53    virtual ~CbcIntegerBranchingObject ();
54
55    /// Does part of constructor
56    void fillPart ( int variable, int way , double value) ;
57    using CbcBranchingObject::branch ;
58    /** \brief Sets the bounds for the variable according to the current arm
59           of the branch and advances the object state to the next arm.
60           Returns change in guessed objective on next branch
61    */
62    virtual double branch();
63    /** Update bounds in solver as in 'branch' and update given bounds.
64        branchState is -1 for 'down' +1 for 'up' */
65    virtual void fix(OsiSolverInterface * solver,
66                     double * lower, double * upper,
67                     int branchState) const ;
68
69#ifdef JJF_ZERO
70    // No need to override. Default works fine.
71    /** Reset every information so that the branching object appears to point to
72        the previous child. This method does not need to modify anything in any
73        solver. */
74    virtual void previousBranch();
75#endif
76
77    using CbcBranchingObject::print ;
78    /** \brief Print something about branch - only if log level high
79    */
80    virtual void print();
81
82    /// Lower and upper bounds for down branch
83    inline const double * downBounds() const {
84        return down_;
85    }
86    /// Lower and upper bounds for up branch
87    inline const double * upBounds() const {
88        return up_;
89    }
90    /// Set lower and upper bounds for down branch
91    inline void setDownBounds(const double bounds[2]) {
92        memcpy(down_, bounds, 2*sizeof(double));
93    }
94    /// Set lower and upper bounds for up branch
95    inline void setUpBounds(const double bounds[2]) {
96        memcpy(up_, bounds, 2*sizeof(double));
97    }
98#ifdef FUNNY_BRANCHING
99    /** Which variable (top bit if upper bound changing,
100        next bit if on down branch */
101    inline const int * variables() const {
102        return variables_;
103    }
104    // New bound
105    inline const double * newBounds() const {
106        return newBounds_;
107    }
108    /// Number of bound changes
109    inline int numberExtraChangedBounds() const {
110        return numberExtraChangedBounds_;
111    }
112    /// Just apply extra bounds to one variable - COIN_DBL_MAX ignore
113    int applyExtraBounds(int iColumn, double lower, double upper, int way) ;
114    /// Deactivate bounds for branching
115    void deactivate();
116    /// Are active bounds for branching
117    inline bool active() const {
118        return (down_[1] != -COIN_DBL_MAX);
119    }
120#endif
121
122    /** Return the type (an integer identifier) of \c this */
123    virtual CbcBranchObjType type() const {
124        return SimpleIntegerBranchObj;
125    }
126
127    /** Compare the \c this with \c brObj. \c this and \c brObj must be os the
128        same type and must have the same original object, but they may have
129        different feasible regions.
130        Return the appropriate CbcRangeCompare value (first argument being the
131        sub/superset if that's the case). In case of overlap (and if \c
132        replaceIfOverlap is true) replace the current branching object with one
133        whose feasible region is the overlap.
134     */
135    virtual CbcRangeCompare compareBranchingObject
136    (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false);
137
138protected:
139    /// Lower [0] and upper [1] bounds for the down arm (way_ = -1)
140    double down_[2];
141    /// Lower [0] and upper [1] bounds for the up arm (way_ = 1)
142    double up_[2];
143#ifdef FUNNY_BRANCHING
144    /** Which variable (top bit if upper bound changing)
145        next bit if changing on down branch only */
146    int * variables_;
147    // New bound
148    double * newBounds_;
149    /// Number of Extra bound changes
150    int numberExtraChangedBounds_;
151#endif
152};
153
154/// Define a single integer class
155
156
157class CbcSimpleInteger : public CbcObject {
158
159public:
160
161    // Default Constructor
162    CbcSimpleInteger ();
163
164    // Useful constructor - passed model and index
165    CbcSimpleInteger (CbcModel * model,  int iColumn, double breakEven = 0.5);
166
167    // Useful constructor - passed model and Osi object
168    CbcSimpleInteger (CbcModel * model,  const OsiSimpleInteger * object);
169
170    // Copy constructor
171    CbcSimpleInteger ( const CbcSimpleInteger &);
172
173    /// Clone
174    virtual CbcObject * clone() const;
175
176    // Assignment operator
177    CbcSimpleInteger & operator=( const CbcSimpleInteger& rhs);
178
179    // Destructor
180    virtual ~CbcSimpleInteger ();
181    /// Construct an OsiSimpleInteger object
182    OsiSimpleInteger * osiObject() const;
183    /// Infeasibility - large is 0.5
184    virtual double infeasibility(const OsiBranchingInformation * info,
185                                 int &preferredWay) const;
186
187    using CbcObject::feasibleRegion ;
188    /** Set bounds to fix the variable at the current (integer) value.
189
190      Given an integer value, set the lower and upper bounds to fix the
191      variable. Returns amount it had to move variable.
192    */
193    virtual double feasibleRegion(OsiSolverInterface * solver, const OsiBranchingInformation * info) const;
194
195    /** Create a branching object and indicate which way to branch first.
196
197        The branching object has to know how to create branches (fix
198        variables, etc.)
199    */
200    virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ;
201    /// Fills in a created branching object
202    void fillCreateBranch(CbcIntegerBranchingObject * branching, const OsiBranchingInformation * info, int way) ;
203
204    using CbcObject::solverBranch ;
205    /** Create an OsiSolverBranch object
206
207        This returns NULL if branch not represented by bound changes
208    */
209    virtual OsiSolverBranch * solverBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info) const;
210
211    /** Set bounds to fix the variable at the current (integer) value.
212
213      Given an integer value, set the lower and upper bounds to fix the
214      variable. The algorithm takes a bit of care in order to compensate for
215      minor numerical inaccuracy.
216    */
217    virtual void feasibleRegion();
218
219    /** Column number if single column object -1 otherwise,
220        so returns >= 0
221        Used by heuristics
222    */
223    virtual int columnNumber() const;
224    /// Set column number
225    inline void setColumnNumber(int value) {
226        columnNumber_ = value;
227    }
228
229    /** Reset variable bounds to their original values.
230
231      Bounds may be tightened, so it may be good to be able to set this info in object.
232     */
233    virtual void resetBounds(const OsiSolverInterface * solver) ;
234    /**  Change column numbers after preprocessing
235     */
236    virtual void resetSequenceEtc(int numberColumns, const int * originalColumns) ;
237    /// Original bounds
238    inline double originalLowerBound() const {
239        return originalLower_;
240    }
241    inline void setOriginalLowerBound(double value) {
242        originalLower_ = value;
243    }
244    inline double originalUpperBound() const {
245        return originalUpper_;
246    }
247    inline void setOriginalUpperBound(double value) {
248        originalUpper_ = value;
249    }
250    /// Breakeven e.g 0.7 -> >= 0.7 go up first
251    inline double breakEven() const {
252        return breakEven_;
253    }
254    /// Set breakeven e.g 0.7 -> >= 0.7 go up first
255    inline void setBreakEven(double value) {
256        breakEven_ = value;
257    }
258
259
260protected:
261    /// data
262
263    /// Original lower bound
264    double originalLower_;
265    /// Original upper bound
266    double originalUpper_;
267    /// Breakeven i.e. >= this preferred is up
268    double breakEven_;
269    /// Column number in model
270    int columnNumber_;
271    /// If -1 down always chosen first, +1 up always, 0 normal
272    int preferredWay_;
273};
274#endif
Note: See TracBrowser for help on using the repository browser.