- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

ClpNonLinearCost.hpp
Go to the documentation of this file.
1 /* $Id: ClpNonLinearCost.hpp 1769 2011-07-26 09:31:51Z forrest $ */
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 #ifndef ClpNonLinearCost_H
7 #define ClpNonLinearCost_H
8
9
10 #include "CoinPragma.hpp"
11
12 class ClpSimplex;
13 class CoinIndexedVector;
14
32 /* status has original status and current status
33 0 - below lower so stored is upper
34 1 - in range
35 2 - above upper so stored is lower
36 4 - (for current) - same as original
37 */
38 #define CLP_BELOW_LOWER 0
39 #define CLP_FEASIBLE 1
40 #define CLP_ABOVE_UPPER 2
41 #define CLP_SAME 4
42 inline int originalStatus(unsigned char status)
43 {
44 return (status & 15);
45 }
46 inline int currentStatus(unsigned char status)
47 {
48 return (status >> 4);
49 }
50 inline void setOriginalStatus(unsigned char & status, int value)
51 {
52 status = static_cast<unsigned char>(status & ~15);
53 status = static_cast<unsigned char>(status | value);
54 }
55 inline void setCurrentStatus(unsigned char &status, int value)
56 {
57 status = static_cast<unsigned char>(status & ~(15 << 4));
58 status = static_cast<unsigned char>(status | (value << 4));
59 }
60 inline void setInitialStatus(unsigned char &status)
61 {
62 status = static_cast<unsigned char>(CLP_FEASIBLE | (CLP_SAME << 4));
63 }
64 inline void setSameStatus(unsigned char &status)
65 {
66 status = static_cast<unsigned char>(status & ~(15 << 4));
67 status = static_cast<unsigned char>(status | (CLP_SAME << 4));
68 }
69 // Use second version to get more speed
70 //#define FAST_CLPNON
71 #ifndef FAST_CLPNON
72 #define CLP_METHOD1 ((method_&1)!=0)
73 #define CLP_METHOD2 ((method_&2)!=0)
74 #else
75 #define CLP_METHOD1 (false)
76 #define CLP_METHOD2 (true)
77 #endif
78 class ClpNonLinearCost {
79
80 public:
81
82 public:
83
86 ClpNonLinearCost();
92 ClpNonLinearCost(ClpSimplex * model, int method = 1);
98 ClpNonLinearCost(ClpSimplex * model, const int * starts,
99 const double * lower, const double * cost);
101 ~ClpNonLinearCost();
102 // Copy
103 ClpNonLinearCost(const ClpNonLinearCost&);
104 // Assignment
105 ClpNonLinearCost& operator=(const ClpNonLinearCost&);
107
108
115 void checkInfeasibilities(double oldTolerance = 0.0);
119 void checkInfeasibilities(int numberInArray, const int * index);
126 void checkChanged(int numberInArray, CoinIndexedVector * update);
133 void goThru(int numberInArray, double multiplier,
134 const int * index, const double * work,
135 double * rhs);
138 void goBack(int numberInArray, const int * index,
139 double * rhs);
145 void goBackAll(const CoinIndexedVector * update);
147 void zapCosts();
149 void refreshCosts(const double * columnCosts);
151 void feasibleBounds();
153 void refresh();
157 double setOne(int sequence, double solutionValue);
160 void setOne(int sequence, double solutionValue, double lowerValue, double upperValue,
161 double costValue = 0.0);
165 int setOneOutgoing(int sequence, double &solutionValue);
167 double nearest(int sequence, double solutionValue);
171 inline double changeInCost(int sequence, double alpha) const {
172 double returnValue = 0.0;
173 if (CLP_METHOD1) {
174 int iRange = whichRange_[sequence] + offset_[sequence];
175 if (alpha > 0.0)
176 returnValue = cost_[iRange] - cost_[iRange-1];
177 else
178 returnValue = cost_[iRange] - cost_[iRange+1];
179 }
180 if (CLP_METHOD2) {
181 returnValue = (alpha > 0.0) ? infeasibilityWeight_ : -infeasibilityWeight_;
182 }
183 return returnValue;
184 }
185 inline double changeUpInCost(int sequence) const {
186 double returnValue = 0.0;
187 if (CLP_METHOD1) {
188 int iRange = whichRange_[sequence] + offset_[sequence];
189 if (iRange + 1 != start_[sequence+1] && !infeasible(iRange + 1))
190 returnValue = cost_[iRange] - cost_[iRange+1];
191 else
192 returnValue = -1.0e100;
193 }
194 if (CLP_METHOD2) {
195 returnValue = -infeasibilityWeight_;
196 }
197 return returnValue;
198 }
199 inline double changeDownInCost(int sequence) const {
200 double returnValue = 0.0;
201 if (CLP_METHOD1) {
202 int iRange = whichRange_[sequence] + offset_[sequence];
203 if (iRange != start_[sequence] && !infeasible(iRange - 1))
204 returnValue = cost_[iRange] - cost_[iRange-1];
205 else
206 returnValue = 1.0e100;
207 }
208 if (CLP_METHOD2) {
209 returnValue = infeasibilityWeight_;
210 }
211 return returnValue;
212 }
214 inline double changeInCost(int sequence, double alpha, double &rhs) {
215 double returnValue = 0.0;
216 #ifdef NONLIN_DEBUG
217 double saveRhs = rhs;
218 #endif
219 if (CLP_METHOD1) {
220 int iRange = whichRange_[sequence] + offset_[sequence];
221 if (alpha > 0.0) {
222 assert(iRange - 1 >= start_[sequence]);
223 offset_[sequence]--;
224 rhs += lower_[iRange] - lower_[iRange-1];
225 returnValue = alpha * (cost_[iRange] - cost_[iRange-1]);
226 } else {
227 assert(iRange + 1 < start_[sequence+1] - 1);
228 offset_[sequence]++;
229 rhs += lower_[iRange+2] - lower_[iRange+1];
230 returnValue = alpha * (cost_[iRange] - cost_[iRange+1]);
231 }
232 }
233 if (CLP_METHOD2) {
234 #ifdef NONLIN_DEBUG
235 double saveRhs1 = rhs;
236 rhs = saveRhs;
237 #endif
238 unsigned char iStatus = status_[sequence];
239 int iWhere = currentStatus(iStatus);
240 if (iWhere == CLP_SAME)
241 iWhere = originalStatus(iStatus);
242 // rhs always increases
243 if (iWhere == CLP_FEASIBLE) {
244 if (alpha > 0.0) {
245 // going below
246 iWhere = CLP_BELOW_LOWER;
247 rhs = COIN_DBL_MAX;
248 } else {
249 // going above
250 iWhere = CLP_ABOVE_UPPER;
251 rhs = COIN_DBL_MAX;
252 }
253 } else if (iWhere == CLP_BELOW_LOWER) {
254 assert (alpha < 0);
255 // going feasible
256 iWhere = CLP_FEASIBLE;
257 rhs += bound_[sequence] - model_->upperRegion()[sequence];
258 } else {
259 assert (iWhere == CLP_ABOVE_UPPER);
260 // going feasible
261 iWhere = CLP_FEASIBLE;
262 rhs += model_->lowerRegion()[sequence] - bound_[sequence];
263 }
264 setCurrentStatus(status_[sequence], iWhere);
265 #ifdef NONLIN_DEBUG
266 assert(saveRhs1 == rhs);
267 #endif
268 returnValue = fabs(alpha) * infeasibilityWeight_;
269 }
270 return returnValue;
271 }
273 inline double lower(int sequence) const {
274 return lower_[whichRange_[sequence] + offset_[sequence]];
275 }
277 inline double upper(int sequence) const {
278 return lower_[whichRange_[sequence] + offset_[sequence] + 1];
279 }
281 inline double cost(int sequence) const {
282 return cost_[whichRange_[sequence] + offset_[sequence]];
283 }
285
286
289 inline int numberInfeasibilities() const {
291 return numberInfeasibilities_;
292 }
294 inline double changeInCost() const {
295 return changeCost_;
296 }
298 inline double feasibleCost() const {
299 return feasibleCost_;
300 }
302 double feasibleReportCost() const;
304 inline double sumInfeasibilities() const {
305 return sumInfeasibilities_;
306 }
308 inline double largestInfeasibility() const {
309 return largestInfeasibility_;
310 }
312 inline double averageTheta() const {
313 return averageTheta_;
314 }
315 inline void setAverageTheta(double value) {
316 averageTheta_ = value;
317 }
318 inline void setChangeInCost(double value) {
319 changeCost_ = value;
320 }
321 inline void setMethod(int value) {
322 method_ = value;
323 }
325 inline bool lookBothWays() const {
326 return bothWays_;
327 }
329 inline bool infeasible(int i) const {
331 return ((infeasible_[i>>5] >> (i & 31)) & 1) != 0;
332 }
333 inline void setInfeasible(int i, bool trueFalse) {
334 unsigned int & value = infeasible_[i>>5];
335 int bit = i & 31;
336 if (trueFalse)
337 value |= (1 << bit);
338 else
339 value &= ~(1 << bit);
340 }
341 inline unsigned char * statusArray() const {
342 return status_;
343 }
345 void validate();
347
348 private:
351 double changeCost_;
354 double feasibleCost_;
356 double infeasibilityWeight_;
358 double largestInfeasibility_;
360 double sumInfeasibilities_;
362 double averageTheta_;
364 int numberRows_;
366 int numberColumns_;
368 int * start_;
370 int * whichRange_;
372 int * offset_;
376 double * lower_;
378 double * cost_;
380 ClpSimplex * model_;
381 // Array to say which regions are infeasible
382 unsigned int * infeasible_;
384 int numberInfeasibilities_;
385 // new stuff
387 unsigned char * status_;
389 double * bound_;
391 double * cost2_;
393 int method_;
395 bool convex_;
397 bool bothWays_;
399 };
400
401 #endif
ClpNonLinearCost::changeCost_
double changeCost_
Change in cost because of infeasibilities.
Definition: ClpNonLinearCost.hpp:352
ClpNonLinearCost::status_
unsigned char * status_
Contains status at beginning and current.
Definition: ClpNonLinearCost.hpp:387
ClpNonLinearCost::~ClpNonLinearCost
~ClpNonLinearCost()
Destructor.
ClpNonLinearCost::infeasible
bool infeasible(int i) const
For debug.
Definition: ClpNonLinearCost.hpp:330
ClpNonLinearCost::changeInCost
double changeInCost(int sequence, double alpha) const
Returns change in cost - one down if alpha &gt;0.0, up if &lt;0.0 Value is current - new.
Definition: ClpNonLinearCost.hpp:171
ClpNonLinearCost::checkChanged
void checkChanged(int numberInArray, CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
ClpNonLinearCost::infeasible_
unsigned int * infeasible_
Change in cost because of infeasibilities.
Definition: ClpNonLinearCost.hpp:382
ClpNonLinearCost::setInfeasible
void setInfeasible(int i, bool trueFalse)
For debug.
Definition: ClpNonLinearCost.hpp:333
ClpNonLinearCost::checkInfeasibilities
void checkInfeasibilities(double oldTolerance=0.0)
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
ClpNonLinearCost::setAverageTheta
void setAverageTheta(double value)
Number of infeasibilities.
Definition: ClpNonLinearCost.hpp:315
ClpNonLinearCost
Definition: ClpNonLinearCost.hpp:78
ClpNonLinearCost::refreshCosts
void refreshCosts(const double *columnCosts)
Refreshes costs always makes row costs zero.
ClpNonLinearCost::feasibleReportCost
double feasibleReportCost() const
Feasible cost with offset and direction (i.e. for reporting)
CLP_ABOVE_UPPER
#define CLP_ABOVE_UPPER
Definition: ClpNonLinearCost.hpp:40
ClpNonLinearCost::sumInfeasibilities_
double sumInfeasibilities_
Sum of infeasibilities.
Definition: ClpNonLinearCost.hpp:360
ClpNonLinearCost::changeInCost
double changeInCost() const
Change in cost.
Definition: ClpNonLinearCost.hpp:294
ClpNonLinearCost::zapCosts
void zapCosts()
Temporary zeroing of feasible costs.
setCurrentStatus
void setCurrentStatus(unsigned char &status, int value)
Definition: AbcNonLinearCost.hpp:57
ClpNonLinearCost::cost2_
double * cost2_
Feasible cost array.
Definition: ClpNonLinearCost.hpp:391
ClpNonLinearCost::start_
int * start_
Starts for each entry (columns then rows)
Definition: ClpNonLinearCost.hpp:368
ClpNonLinearCost::goThru
void goThru(int numberInArray, double multiplier, const int *index, const double *work, double *rhs)
Goes through one bound for each variable.
CLP_SAME
#define CLP_SAME
Definition: ClpNonLinearCost.hpp:41
ClpSimplex::lowerRegion
double * lowerRegion(int section) const
Return row or column sections - not as much needed as it once was.
Definition: ClpSimplex.hpp:1026
ClpNonLinearCost::bound_
double * bound_
Bound which has been replaced in lower_ or upper_.
Definition: ClpNonLinearCost.hpp:389
ClpNonLinearCost::infeasibilityWeight_
double infeasibilityWeight_
Current infeasibility weight.
Definition: ClpNonLinearCost.hpp:356
ClpNonLinearCost::changeUpInCost
double changeUpInCost(int sequence) const
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
Definition: ClpNonLinearCost.hpp:185
ClpNonLinearCost::numberRows_
int numberRows_
Number of rows (mainly for checking and copy)
Definition: ClpNonLinearCost.hpp:364
ClpNonLinearCost::whichRange_
int * whichRange_
Range for each entry (columns then rows)
Definition: ClpNonLinearCost.hpp:370
ClpNonLinearCost::setOne
double setOne(int sequence, double solutionValue)
Sets bounds and cost for one variable Returns change in cost May need to be inline for speed...
ClpNonLinearCost::lookBothWays
bool lookBothWays() const
See if may want to look both ways.
Definition: ClpNonLinearCost.hpp:325
ClpNonLinearCost::refresh
void refresh()
Refresh - assuming regions OK.
setOriginalStatus
void setOriginalStatus(unsigned char &status, int value)
Definition: AbcNonLinearCost.hpp:52
ClpNonLinearCost::bothWays_
bool bothWays_
If we should look both ways for djs.
Definition: ClpNonLinearCost.hpp:397
ClpNonLinearCost::cost_
double * cost_
Cost for each range.
Definition: ClpNonLinearCost.hpp:378
ClpNonLinearCost::goBackAll
void goBackAll(const CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
CoinIndexedVector
Indexed Vector.
Definition: CoinIndexedVector.hpp:104
ClpNonLinearCost::sumInfeasibilities
double sumInfeasibilities() const
Sum of infeasibilities.
Definition: ClpNonLinearCost.hpp:304
CLP_FEASIBLE
#define CLP_FEASIBLE
Definition: ClpNonLinearCost.hpp:39
CLP_METHOD2
#define CLP_METHOD2
Definition: ClpNonLinearCost.hpp:73
ClpNonLinearCost::model_
ClpSimplex * model_
Model.
Definition: ClpNonLinearCost.hpp:380
ClpNonLinearCost::statusArray
unsigned char * statusArray() const
For debug.
Definition: ClpNonLinearCost.hpp:341
CLP_METHOD1
#define CLP_METHOD1
Definition: ClpNonLinearCost.hpp:72
ClpNonLinearCost::numberInfeasibilities_
int numberInfeasibilities_
Number of infeasibilities found.
Definition: ClpNonLinearCost.hpp:384
ClpNonLinearCost::averageTheta
double averageTheta() const
Average theta.
Definition: ClpNonLinearCost.hpp:312
ClpSimplex
This solves LPs using the simplex method.
Definition: ClpSimplex.hpp:70
ClpNonLinearCost::numberColumns_
int numberColumns_
Number of columns (mainly for checking and copy)
Definition: ClpNonLinearCost.hpp:366
ClpNonLinearCost::upper
double upper(int sequence) const
Returns current upper bound.
Definition: ClpNonLinearCost.hpp:277
ClpNonLinearCost::changeDownInCost
double changeDownInCost(int sequence) const
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
Definition: ClpNonLinearCost.hpp:199
ClpNonLinearCost::convex_
bool convex_
If all non-linear costs convex.
Definition: ClpNonLinearCost.hpp:395
COIN_DBL_MAX
const double COIN_DBL_MAX
Definition: CoinFinite.hpp:18
setInitialStatus
void setInitialStatus(unsigned char &status)
Definition: AbcNonLinearCost.hpp:62
ClpNonLinearCost::feasibleCost
double feasibleCost() const
Feasible cost.
Definition: ClpNonLinearCost.hpp:298
originalStatus
int originalStatus(unsigned char status)
Definition: AbcNonLinearCost.hpp:44
setSameStatus
void setSameStatus(unsigned char &status)
Definition: AbcNonLinearCost.hpp:66
ClpNonLinearCost::goBack
void goBack(int numberInArray, const int *index, double *rhs)
Takes off last iteration (i.e.
ClpSimplex::upperRegion
double * upperRegion(int section) const
Return row or column sections - not as much needed as it once was.
Definition: ClpSimplex.hpp:1030
CoinPragma.hpp
CLP_BELOW_LOWER
#define CLP_BELOW_LOWER
Trivial class to deal with non linear costs.
Definition: ClpNonLinearCost.hpp:38
ClpNonLinearCost::setChangeInCost
void setChangeInCost(double value)
Number of infeasibilities.
Definition: ClpNonLinearCost.hpp:318
ClpNonLinearCost::ClpNonLinearCost
ClpNonLinearCost()
Default constructor.
ClpNonLinearCost::numberInfeasibilities
int numberInfeasibilities() const
Number of infeasibilities.
Definition: ClpNonLinearCost.hpp:290
ClpNonLinearCost::method_
int method_
Method 1 old, 2 new, 3 both!
Definition: ClpNonLinearCost.hpp:393
ClpNonLinearCost::validate
void validate()
For debug.
ClpNonLinearCost::averageTheta_
double averageTheta_
Average theta - kept here as only for primal.
Definition: ClpNonLinearCost.hpp:362
ClpNonLinearCost::offset_
int * offset_
Temporary range offset for each entry (columns then rows)
Definition: ClpNonLinearCost.hpp:372
ClpNonLinearCost::lower
double lower(int sequence) const
Returns current lower bound.
Definition: ClpNonLinearCost.hpp:273
ClpNonLinearCost::feasibleCost_
double feasibleCost_
Feasible cost.
Definition: ClpNonLinearCost.hpp:354
ClpNonLinearCost::cost
double cost(int sequence) const
Returns current cost.
Definition: ClpNonLinearCost.hpp:281
ClpNonLinearCost::nearest
double nearest(int sequence, double solutionValue)
Returns nearest bound.
ClpNonLinearCost::setOneOutgoing
int setOneOutgoing(int sequence, double &solutionValue)
Sets bounds and cost for outgoing variable may change value Returns direction.
ClpNonLinearCost::setMethod
void setMethod(int value)
Number of infeasibilities.
Definition: ClpNonLinearCost.hpp:321
ClpNonLinearCost::operator=
ClpNonLinearCost & operator=(const ClpNonLinearCost &)
Default constructor.
ClpNonLinearCost::changeInCost
double changeInCost(int sequence, double alpha, double &rhs)
This also updates next bound.
Definition: ClpNonLinearCost.hpp:214
ClpNonLinearCost::lower_
double * lower_
Lower bound for each range (upper bound is next lower).
Definition: ClpNonLinearCost.hpp:376
ClpNonLinearCost::feasibleBounds
void feasibleBounds()
Puts feasible bounds into lower and upper.
currentStatus
int currentStatus(unsigned char status)
Definition: AbcNonLinearCost.hpp:48
ClpNonLinearCost::largestInfeasibility
double largestInfeasibility() const
Largest infeasibility.
Definition: ClpNonLinearCost.hpp:308
ClpNonLinearCost::largestInfeasibility_
double largestInfeasibility_
Largest infeasibility.
Definition: ClpNonLinearCost.hpp:358