1 | /* $Id: CbcFathomDynamicProgramming.hpp 1573 2011-01-05 01:12:36Z stefan $ */ |
---|
2 | // Copyright (C) 2004, 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 | #ifndef CbcFathomDynamicProgramming_H |
---|
7 | #define CbcFathomDynamicProgramming_H |
---|
8 | |
---|
9 | #include "CbcFathom.hpp" |
---|
10 | |
---|
11 | //############################################################################# |
---|
12 | /** FathomDynamicProgramming class. |
---|
13 | |
---|
14 | The idea is that after some branching the problem will be effectively smaller than |
---|
15 | the original problem and maybe there will be a more specialized technique which can completely |
---|
16 | fathom this branch quickly. |
---|
17 | |
---|
18 | This is a dynamic programming implementation which is very fast for some |
---|
19 | specialized problems. It expects small integral rhs, an all integer problem |
---|
20 | and positive integral coefficients. At present it can not do general set covering |
---|
21 | problems just set partitioning. It can find multiple optima for various rhs |
---|
22 | combinations. |
---|
23 | |
---|
24 | The main limiting factor is size of state space. Each 1 rhs doubles the size of the problem. |
---|
25 | 2 or 3 rhs quadruples, 4,5,6,7 by 8 etc. |
---|
26 | */ |
---|
27 | |
---|
28 | class CbcFathomDynamicProgramming : public CbcFathom { |
---|
29 | public: |
---|
30 | // Default Constructor |
---|
31 | CbcFathomDynamicProgramming (); |
---|
32 | |
---|
33 | // Constructor with model - assumed before cuts |
---|
34 | CbcFathomDynamicProgramming (CbcModel & model); |
---|
35 | // Copy constructor |
---|
36 | CbcFathomDynamicProgramming(const CbcFathomDynamicProgramming & rhs); |
---|
37 | |
---|
38 | virtual ~CbcFathomDynamicProgramming(); |
---|
39 | |
---|
40 | /// update model (This is needed if cliques update matrix etc) |
---|
41 | virtual void setModel(CbcModel * model); |
---|
42 | |
---|
43 | /// Clone |
---|
44 | virtual CbcFathom * clone() const; |
---|
45 | |
---|
46 | /// Resets stuff if model changes |
---|
47 | virtual void resetModel(CbcModel * model); |
---|
48 | |
---|
49 | /** returns 0 if no fathoming attempted, 1 fully fathomed , |
---|
50 | 2 incomplete search, 3 incomplete search but treat as complete. |
---|
51 | If solution then newSolution will not be NULL and |
---|
52 | will be freed by CbcModel. It is expected that the solution is better |
---|
53 | than best so far but CbcModel will double check. |
---|
54 | |
---|
55 | If returns 3 then of course there is no guarantee of global optimum |
---|
56 | */ |
---|
57 | virtual int fathom(double *& newSolution); |
---|
58 | |
---|
59 | /// Maximum size allowed |
---|
60 | inline int maximumSize() const { |
---|
61 | return maximumSizeAllowed_; |
---|
62 | } |
---|
63 | inline void setMaximumSize(int value) { |
---|
64 | maximumSizeAllowed_ = value; |
---|
65 | } |
---|
66 | /// Returns type of algorithm and sets up arrays |
---|
67 | int checkPossible(int allowableSize = 0); |
---|
68 | // set algorithm |
---|
69 | inline void setAlgorithm(int value) { |
---|
70 | algorithm_ = value; |
---|
71 | } |
---|
72 | /** Tries a column |
---|
73 | returns true if was used in making any changes. |
---|
74 | */ |
---|
75 | bool tryColumn(int numberElements, const int * rows, |
---|
76 | const double * coefficients, double cost, |
---|
77 | int upper = COIN_INT_MAX); |
---|
78 | /// Returns cost array |
---|
79 | inline const double * cost() const { |
---|
80 | return cost_; |
---|
81 | } |
---|
82 | /// Returns back array |
---|
83 | inline const int * back() const { |
---|
84 | return back_; |
---|
85 | } |
---|
86 | /// Gets bit pattern for target result |
---|
87 | inline int target() const { |
---|
88 | return target_; |
---|
89 | } |
---|
90 | /// Sets bit pattern for target result |
---|
91 | inline void setTarget(int value) { |
---|
92 | target_ = value; |
---|
93 | } |
---|
94 | private: |
---|
95 | /// Does deleteions |
---|
96 | void gutsOfDelete(); |
---|
97 | |
---|
98 | /** Adds one attempt of one column of type 0, |
---|
99 | returns true if was used in making any changes |
---|
100 | */ |
---|
101 | bool addOneColumn0(int numberElements, const int * rows, |
---|
102 | double cost); |
---|
103 | /** Adds one attempt of one column of type 1, |
---|
104 | returns true if was used in making any changes. |
---|
105 | At present the user has to call it once for each possible value |
---|
106 | */ |
---|
107 | bool addOneColumn1(int numberElements, const int * rows, |
---|
108 | const int * coefficients, double cost); |
---|
109 | /** Adds one attempt of one column of type 1, |
---|
110 | returns true if was used in making any changes. |
---|
111 | At present the user has to call it once for each possible value. |
---|
112 | This version is when there are enough 1 rhs to do faster |
---|
113 | */ |
---|
114 | bool addOneColumn1A(int numberElements, const int * rows, |
---|
115 | const int * coefficients, double cost); |
---|
116 | /// Gets bit pattern from original column |
---|
117 | int bitPattern(int numberElements, const int * rows, |
---|
118 | const int * coefficients); |
---|
119 | /// Gets bit pattern from original column |
---|
120 | int bitPattern(int numberElements, const int * rows, |
---|
121 | const double * coefficients); |
---|
122 | /// Fills in original column (dense) from bit pattern - returning number nonzero |
---|
123 | int decodeBitPattern(int bitPattern, int * values, int numberRows); |
---|
124 | |
---|
125 | protected: |
---|
126 | |
---|
127 | /// Size of states (power of 2 unless just one constraint) |
---|
128 | int size_; |
---|
129 | /** Type - 0 coefficients and rhs all 1, |
---|
130 | 1 - coefficients > 1 or rhs > 1 |
---|
131 | */ |
---|
132 | int type_; |
---|
133 | /// Space for states |
---|
134 | double * cost_; |
---|
135 | /// Which state produced this cheapest one |
---|
136 | int * back_; |
---|
137 | /// Some rows may be satisified so we need a lookup |
---|
138 | int * lookup_; |
---|
139 | /// Space for sorted indices |
---|
140 | int * indices_; |
---|
141 | /// Number of active rows |
---|
142 | int numberActive_; |
---|
143 | /// Maximum size allowed |
---|
144 | int maximumSizeAllowed_; |
---|
145 | /// Start bit for each active row |
---|
146 | int * startBit_; |
---|
147 | /// Number bits for each active row |
---|
148 | int * numberBits_; |
---|
149 | /// Effective rhs |
---|
150 | int * rhs_; |
---|
151 | /// Space for sorted coefficients |
---|
152 | int * coefficients_; |
---|
153 | /// Target pattern |
---|
154 | int target_; |
---|
155 | /// Number of Non 1 rhs |
---|
156 | int numberNonOne_; |
---|
157 | /// Current bit pattern |
---|
158 | int bitPattern_; |
---|
159 | /// Current algorithm |
---|
160 | int algorithm_; |
---|
161 | private: |
---|
162 | |
---|
163 | /// Illegal Assignment operator |
---|
164 | CbcFathomDynamicProgramming & operator=(const CbcFathomDynamicProgramming& rhs); |
---|
165 | |
---|
166 | }; |
---|
167 | |
---|
168 | #endif |
---|
169 | |
---|