1 | // Copyright (C) 2002, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | #ifndef ClpNonLinearCost_H |
---|
4 | #define ClpNonLinearCost_H |
---|
5 | |
---|
6 | #if defined(_MSC_VER) |
---|
7 | // Turn off compiler warning about long names |
---|
8 | # pragma warning(disable:4786) |
---|
9 | #endif |
---|
10 | |
---|
11 | class ClpSimplex; |
---|
12 | class OsiIndexedVector; |
---|
13 | |
---|
14 | /** Trivial class to deal with non linear costs |
---|
15 | |
---|
16 | I don't make any explicit assumptions about convexity but I am |
---|
17 | sure I do make implicit ones. |
---|
18 | |
---|
19 | One interesting idea for normal LP's will be to allow non-basic |
---|
20 | variables to come into basis as infeasible i.e. if variable at |
---|
21 | lower bound has very large positive reduced cost (when problem |
---|
22 | is infeasible) could it reduce overall problem infeasibility more |
---|
23 | by bringing it into basis below its lower bound. |
---|
24 | |
---|
25 | Another feature would be to automatically discover when problems |
---|
26 | are convex piecewise linear and re-formulate to use non-linear. |
---|
27 | I did some work on this many years ago on "grade" problems, but |
---|
28 | while it improved primal interior point algorithms were much better |
---|
29 | for that particular problem. |
---|
30 | */ |
---|
31 | |
---|
32 | class ClpNonLinearCost { |
---|
33 | |
---|
34 | public: |
---|
35 | |
---|
36 | public: |
---|
37 | |
---|
38 | /**@name Constructors, destructor */ |
---|
39 | //@{ |
---|
40 | /// Default constructor. |
---|
41 | ClpNonLinearCost(); |
---|
42 | /** Constructor from simplex. |
---|
43 | This will just set up wasteful arrays for linear, but |
---|
44 | later may do dual analysis and even finding duplicate columns |
---|
45 | */ |
---|
46 | ClpNonLinearCost(ClpSimplex * model); |
---|
47 | /** Constructor from simplex and list of non-linearities (columns only) |
---|
48 | First lower of each column has to match real lower |
---|
49 | Last lower has to be <= upper (if == then cost ignored) |
---|
50 | This could obviously be changed to make more user friendly |
---|
51 | */ |
---|
52 | ClpNonLinearCost(ClpSimplex * model,const int * starts, |
---|
53 | const double * lower, const double * cost); |
---|
54 | /// Destructor |
---|
55 | ~ClpNonLinearCost(); |
---|
56 | // Copy |
---|
57 | ClpNonLinearCost(const ClpNonLinearCost&); |
---|
58 | // Assignment |
---|
59 | ClpNonLinearCost& operator=(const ClpNonLinearCost&); |
---|
60 | //@} |
---|
61 | |
---|
62 | |
---|
63 | /**@name Actual work in primal */ |
---|
64 | //@{ |
---|
65 | /** Changes infeasible costs and computes number and cost of infeas |
---|
66 | Puts all non-basic (non free) variables to bounds |
---|
67 | and all free variables to zero if toNearest true*/ |
---|
68 | void checkInfeasibilities(bool toNearest=false); |
---|
69 | /** Changes infeasible costs for each variable |
---|
70 | The indices are row indices and need converting to sequences |
---|
71 | */ |
---|
72 | void checkInfeasibilities(int numberInArray, const int * index); |
---|
73 | /** Puts back correct infeasible costs for each variable |
---|
74 | The input indices are row indices and need converting to sequences |
---|
75 | for costs. |
---|
76 | On input array is empty (but indices exist). On exit just |
---|
77 | changed costs will be stored as normal OsiIndexedVector |
---|
78 | */ |
---|
79 | void checkChanged(int numberInArray, OsiIndexedVector * update); |
---|
80 | /** Goes through one bound for each variable. |
---|
81 | If multiplier*work[iRow]>0 goes down, otherwise up. |
---|
82 | The indices are row indices and need converting to sequences |
---|
83 | Temporary offsets may be set |
---|
84 | Rhs entries are increased |
---|
85 | */ |
---|
86 | void goThru(int numberInArray, double multiplier, |
---|
87 | const int * index, const double * work, |
---|
88 | double * rhs); |
---|
89 | /** Takes off last iteration (i.e. offsets closer to 0) |
---|
90 | */ |
---|
91 | void goBack(int numberInArray, const int * index, |
---|
92 | double * rhs); |
---|
93 | /** Puts back correct infeasible costs for each variable |
---|
94 | The input indices are row indices and need converting to sequences |
---|
95 | for costs. |
---|
96 | At the end of this all temporary offsets are zero |
---|
97 | */ |
---|
98 | void goBackAll(const OsiIndexedVector * update); |
---|
99 | /** Sets bounds and cost for one variable |
---|
100 | Returns change in cost |
---|
101 | May need to be inline for speed */ |
---|
102 | double setOne(int sequence, double solutionValue); |
---|
103 | /// Returns nearest bound |
---|
104 | double nearest(int sequence, double solutionValue); |
---|
105 | /** Returns change in cost - one down if alpha >0.0, up if <0.0 |
---|
106 | Value is current - new |
---|
107 | */ |
---|
108 | inline double changeInCost(int sequence, double alpha) const |
---|
109 | { |
---|
110 | int iRange = whichRange_[sequence]+offset_[sequence]; |
---|
111 | if (alpha>0.0) |
---|
112 | return cost_[iRange]-cost_[iRange-1]; |
---|
113 | else |
---|
114 | return cost_[iRange]-cost_[iRange+1]; |
---|
115 | } |
---|
116 | /// Returns current lower bound |
---|
117 | inline double lower(int sequence) const |
---|
118 | { return lower_[whichRange_[sequence]+offset_[sequence]];}; |
---|
119 | /// Returns current upper bound |
---|
120 | inline double upper(int sequence) const |
---|
121 | { return lower_[whichRange_[sequence]+offset_[sequence]+1];}; |
---|
122 | /// Returns current cost |
---|
123 | inline double cost(int sequence) const |
---|
124 | { return cost_[whichRange_[sequence]+offset_[sequence]];}; |
---|
125 | //@} |
---|
126 | |
---|
127 | |
---|
128 | /**@name Gets and sets */ |
---|
129 | //@{ |
---|
130 | /// Number of infeasibilities |
---|
131 | inline int numberInfeasibilities() const |
---|
132 | {return numberInfeasibilities_;}; |
---|
133 | /// Change in cost |
---|
134 | inline double changeInCost() const |
---|
135 | {return changeCost_;}; |
---|
136 | /// Sum of infeasibilities |
---|
137 | inline double sumInfeasibilities() const |
---|
138 | {return sumInfeasibilities_;}; |
---|
139 | /// Largest infeasibility |
---|
140 | inline double largestInfeasibility() const |
---|
141 | {return largestInfeasibility_;}; |
---|
142 | inline void setChangeInCost(double value) |
---|
143 | {changeCost_ = value;}; |
---|
144 | //@} |
---|
145 | |
---|
146 | private: |
---|
147 | /**@name Data members */ |
---|
148 | //@{ |
---|
149 | /// Number of rows (mainly for checking and copy) |
---|
150 | int numberRows_; |
---|
151 | /// Number of columns (mainly for checking and copy) |
---|
152 | int numberColumns_; |
---|
153 | /// Starts for each entry (columns then rows) |
---|
154 | int * start_; |
---|
155 | /// Range for each entry (columns then rows) |
---|
156 | int * whichRange_; |
---|
157 | /// Temporary range offset for each entry (columns then rows) |
---|
158 | int * offset_; |
---|
159 | /** Lower bound for each range (upper bound is next lower). |
---|
160 | For various reasons there is always an infeasible range |
---|
161 | at bottom - even if lower bound is - infinity */ |
---|
162 | double * lower_; |
---|
163 | /// Cost for each range |
---|
164 | double * cost_; |
---|
165 | /// Model |
---|
166 | ClpSimplex * model_; |
---|
167 | /// Number of infeasibilities found |
---|
168 | int numberInfeasibilities_; |
---|
169 | /// Change in cost because of infeasibilities |
---|
170 | double changeCost_; |
---|
171 | /// Largest infeasibility |
---|
172 | double largestInfeasibility_; |
---|
173 | /// Sum of infeasibilities |
---|
174 | double sumInfeasibilities_; |
---|
175 | /// If all non-linear costs convex |
---|
176 | bool convex_; |
---|
177 | //@} |
---|
178 | }; |
---|
179 | |
---|
180 | #endif |
---|