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