Clp 1.17.5
Loading...
Searching...
No Matches
ClpNonLinearCost.hpp
Go to the documentation of this file.
1/* $Id: ClpNonLinearCost.hpp 2385 2019-01-06 19:43:06Z unxusr $ */
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#include "CoinPragma.hpp"
10
11class ClpSimplex;
12class CoinIndexedVector;
13
31/* status has original status and current status
32 0 - below lower so stored is upper
33 1 - in range
34 2 - above upper so stored is lower
35 4 - (for current) - same as original
36*/
37#define CLP_BELOW_LOWER 0
38#define CLP_FEASIBLE 1
39#define CLP_ABOVE_UPPER 2
40#define CLP_SAME 4
41inline int originalStatus(unsigned char status)
42{
43 return (status & 15);
44}
45inline int currentStatus(unsigned char status)
46{
47 return (status >> 4);
48}
49inline void setOriginalStatus(unsigned char &status, int value)
50{
51 status = static_cast< unsigned char >(status & ~15);
52 status = static_cast< unsigned char >(status | value);
53}
54inline void setCurrentStatus(unsigned char &status, int value)
55{
56 status = static_cast< unsigned char >(status & ~(15 << 4));
57 status = static_cast< unsigned char >(status | (value << 4));
58}
59inline void setInitialStatus(unsigned char &status)
60{
61 status = static_cast< unsigned char >(CLP_FEASIBLE | (CLP_SAME << 4));
62}
63inline void setSameStatus(unsigned char &status)
64{
65 status = static_cast< unsigned char >(status & ~(15 << 4));
66 status = static_cast< unsigned char >(status | (CLP_SAME << 4));
67}
68// Use second version to get more speed
69//#define FAST_CLPNON
70#ifndef FAST_CLPNON
71#define CLP_METHOD1 ((method_ & 1) != 0)
72#define CLP_METHOD2 ((method_ & 2) != 0)
73#else
74#define CLP_METHOD1 (false)
75#define CLP_METHOD2 (true)
76#endif
78
79public:
80public:
89 ClpNonLinearCost(ClpSimplex *model, int method = 1);
95 ClpNonLinearCost(ClpSimplex *model, const int *starts,
96 const double *lower, const double *cost);
99 // Copy
101 // Assignment
104
111 void checkInfeasibilities(double oldTolerance = 0.0);
115 void checkInfeasibilities(int numberInArray, const int *index);
122 void checkChanged(int numberInArray, CoinIndexedVector *update);
129 void goThru(int numberInArray, double multiplier,
130 const int *index, const double *work,
131 double *rhs);
134 void goBack(int numberInArray, const int *index,
135 double *rhs);
141 void goBackAll(const CoinIndexedVector *update);
143 void zapCosts();
145 void refreshCosts(const double *columnCosts);
149 void refresh();
151 void refresh(int iSequence);
155 double setOne(int sequence, double solutionValue);
158 void setOne(int sequence, double solutionValue, double lowerValue, double upperValue,
159 double costValue = 0.0);
163 int setOneOutgoing(int sequence, double &solutionValue);
165 double nearest(int sequence, double solutionValue);
169 inline double changeInCost(int sequence, double alpha) const
170 {
171 double returnValue = 0.0;
172 if (CLP_METHOD1) {
173 int iRange = whichRange_[sequence] + offset_[sequence];
174 if (alpha > 0.0)
175 returnValue = cost_[iRange] - cost_[iRange - 1];
176 else
177 returnValue = cost_[iRange] - cost_[iRange + 1];
178 }
179 if (CLP_METHOD2) {
180 returnValue = (alpha > 0.0) ? infeasibilityWeight_ : -infeasibilityWeight_;
181 }
182 return returnValue;
183 }
184 inline double changeUpInCost(int sequence) const
185 {
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 {
201 double returnValue = 0.0;
202 if (CLP_METHOD1) {
203 int iRange = whichRange_[sequence] + offset_[sequence];
204 if (iRange != start_[sequence] && !infeasible(iRange - 1))
205 returnValue = cost_[iRange] - cost_[iRange - 1];
206 else
207 returnValue = 1.0e100;
208 }
209 if (CLP_METHOD2) {
210 returnValue = infeasibilityWeight_;
211 }
212 return returnValue;
213 }
215 inline double changeInCost(int sequence, double alpha, double &rhs)
216 {
217 double returnValue = 0.0;
218#ifdef NONLIN_DEBUG
219 double saveRhs = rhs;
220#endif
221 if (CLP_METHOD1) {
222 int iRange = whichRange_[sequence] + offset_[sequence];
223 if (alpha > 0.0) {
224 assert(iRange - 1 >= start_[sequence]);
225 offset_[sequence]--;
226 rhs += lower_[iRange] - lower_[iRange - 1];
227 returnValue = alpha * (cost_[iRange] - cost_[iRange - 1]);
228 } else {
229 assert(iRange + 1 < start_[sequence + 1] - 1);
230 offset_[sequence]++;
231 rhs += lower_[iRange + 2] - lower_[iRange + 1];
232 returnValue = alpha * (cost_[iRange] - cost_[iRange + 1]);
233 }
234 }
235 if (CLP_METHOD2) {
236#ifdef NONLIN_DEBUG
237 double saveRhs1 = rhs;
238 rhs = saveRhs;
239#endif
240 unsigned char iStatus = status_[sequence];
241 int iWhere = currentStatus(iStatus);
242 if (iWhere == CLP_SAME)
243 iWhere = originalStatus(iStatus);
244 // rhs always increases
245 if (iWhere == CLP_FEASIBLE) {
246 if (alpha > 0.0) {
247 // going below
248 iWhere = CLP_BELOW_LOWER;
249 rhs = COIN_DBL_MAX;
250 } else {
251 // going above
252 iWhere = CLP_ABOVE_UPPER;
253 rhs = COIN_DBL_MAX;
254 }
255 } else if (iWhere == CLP_BELOW_LOWER) {
256 assert(alpha < 0);
257 // going feasible
258 iWhere = CLP_FEASIBLE;
259 rhs += bound_[sequence] - model_->upperRegion()[sequence];
260 } else {
261 assert(iWhere == CLP_ABOVE_UPPER);
262 // going feasible
263 iWhere = CLP_FEASIBLE;
264 rhs += model_->lowerRegion()[sequence] - bound_[sequence];
265 }
266 setCurrentStatus(status_[sequence], iWhere);
267#ifdef NONLIN_DEBUG
268 assert(saveRhs1 == rhs);
269#endif
270 returnValue = fabs(alpha) * infeasibilityWeight_;
271 }
272 return returnValue;
273 }
275 inline double lower(int sequence) const
276 {
277 return lower_[whichRange_[sequence] + offset_[sequence]];
278 }
280 inline double upper(int sequence) const
281 {
282 return lower_[whichRange_[sequence] + offset_[sequence] + 1];
283 }
285 inline double cost(int sequence) const
286 {
287 return cost_[whichRange_[sequence] + offset_[sequence]];
288 }
290 inline int fullStatus(int sequence) const
291 {
292 return status_[sequence];
293 }
295 inline bool changed(int sequence) const
296 {
297 return (status_[sequence] & 64) == 0;
298 }
299
301
305 inline int numberInfeasibilities() const
306 {
308 }
310 inline double changeInCost() const
311 {
312 return changeCost_;
313 }
315 inline double feasibleCost() const
316 {
317 return feasibleCost_;
318 }
320 double feasibleReportCost() const;
322 inline double sumInfeasibilities() const
323 {
324 return sumInfeasibilities_;
325 }
327 inline double largestInfeasibility() const
328 {
330 }
332 inline double averageTheta() const
333 {
334 return averageTheta_;
335 }
336 inline void setAverageTheta(double value)
337 {
338 averageTheta_ = value;
339 }
340 inline void setChangeInCost(double value)
341 {
342 changeCost_ = value;
343 }
344 inline void setMethod(int value)
345 {
346 method_ = value;
347 }
349 inline bool lookBothWays() const
350 {
351 return bothWays_;
352 }
354
355 inline bool infeasible(int i) const
356 {
357 return ((infeasible_[i >> 5] >> (i & 31)) & 1) != 0;
358 }
359 inline void setInfeasible(int i, bool trueFalse)
360 {
361 unsigned int &value = infeasible_[i >> 5];
362 int bit = i & 31;
363 if (trueFalse)
364 value |= (1 << bit);
365 else
366 value &= ~(1 << bit);
367 }
368 inline unsigned char *statusArray() const
369 {
370 return status_;
371 }
373 void validate();
375
376private:
396 int *start_;
404 double *lower_;
406 double *cost_;
409 // Array to say which regions are infeasible
410 unsigned int *infeasible_;
413 // new stuff
415 unsigned char *status_;
417 double *bound_;
419 double *cost2_;
427};
428
429#endif
430
431/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
432*/
void setOriginalStatus(unsigned char &status, int value)
#define CLP_BELOW_LOWER
Trivial class to deal with non linear costs.
void setCurrentStatus(unsigned char &status, int value)
int currentStatus(unsigned char status)
#define CLP_FEASIBLE
#define CLP_SAME
void setSameStatus(unsigned char &status)
void setInitialStatus(unsigned char &status)
#define CLP_ABOVE_UPPER
#define CLP_METHOD2
#define CLP_METHOD1
int originalStatus(unsigned char status)
bool convex_
If all non-linear costs convex.
double * cost_
Cost for each range.
double changeCost_
Change in cost because of infeasibilities.
ClpNonLinearCost()
Default constructor.
void refreshCosts(const double *columnCosts)
Refreshes costs always makes row costs zero.
double lower(int sequence) const
Returns current lower bound.
double averageTheta_
Average theta - kept here as only for primal.
double changeUpInCost(int sequence) const
double * bound_
Bound which has been replaced in lower_ or upper_.
double sumInfeasibilities() const
Sum of infeasibilities.
double nearest(int sequence, double solutionValue)
Returns nearest bound.
double sumInfeasibilities_
Sum of infeasibilities.
bool infeasible(int i) const
int fullStatus(int sequence) const
Returns full status.
ClpNonLinearCost & operator=(const ClpNonLinearCost &)
ClpNonLinearCost(ClpSimplex *model, int method=1)
Constructor from simplex.
void setChangeInCost(double value)
bool changed(int sequence) const
Returns if changed from beginning of iteration.
double feasibleCost() const
Feasible cost.
double changeInCost(int sequence, double alpha, double &rhs)
This also updates next bound.
double largestInfeasibility_
Largest infeasibility.
void checkInfeasibilities(double oldTolerance=0.0)
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
double feasibleReportCost() const
Feasible cost with offset and direction (i.e. for reporting)
bool bothWays_
If we should look both ways for djs.
double cost(int sequence) const
Returns current cost.
ClpNonLinearCost(ClpSimplex *model, const int *starts, const double *lower, const double *cost)
Constructor from simplex and list of non-linearities (columns only) First lower of each column has to...
bool lookBothWays() const
See if may want to look both ways.
void setInfeasible(int i, bool trueFalse)
void goBackAll(const CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
unsigned char * status_
Contains status at beginning and current.
ClpNonLinearCost(const ClpNonLinearCost &)
double * lower_
Lower bound for each range (upper bound is next lower).
void setAverageTheta(double value)
double feasibleCost_
Feasible cost.
int method_
Method 1 old, 2 new, 3 both!
unsigned char * statusArray() const
void setMethod(int value)
int numberInfeasibilities_
Number of infeasibilities found.
int numberRows_
Number of rows (mainly for checking and copy)
int numberInfeasibilities() const
Number of infeasibilities.
int setOneOutgoing(int sequence, double &solutionValue)
Sets bounds and cost for outgoing variable may change value Returns direction.
int * start_
Starts for each entry (columns then rows)
int * offset_
Temporary range offset for each entry (columns then rows)
void setOne(int sequence, double solutionValue, double lowerValue, double upperValue, double costValue=0.0)
Sets bounds and infeasible cost and true cost for one variable This is for gub and column generation ...
void goBack(int numberInArray, const int *index, double *rhs)
Takes off last iteration (i.e.
double * cost2_
Feasible cost array.
int * whichRange_
Range for each entry (columns then rows)
int numberColumns_
Number of columns (mainly for checking and copy)
void goThru(int numberInArray, double multiplier, const int *index, const double *work, double *rhs)
Goes through one bound for each variable.
void checkInfeasibilities(int numberInArray, const int *index)
Changes infeasible costs for each variable The indices are row indices and need converting to sequenc...
void refresh()
Refresh - assuming regions OK.
void refresh(int iSequence)
Refresh one- assuming regions OK.
void checkChanged(int numberInArray, CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
void zapCosts()
Temporary zeroing of feasible costs.
double largestInfeasibility() const
Largest infeasibility.
double averageTheta() const
Average theta.
unsigned int * infeasible_
double changeDownInCost(int sequence) const
double changeInCost(int sequence, double alpha) const
Returns change in cost - one down if alpha >0.0, up if <0.0 Value is current - new.
double upper(int sequence) const
Returns current upper bound.
void validate()
For debug.
double changeInCost() const
Change in cost.
~ClpNonLinearCost()
Destructor.
ClpSimplex * model_
Model.
double setOne(int sequence, double solutionValue)
Sets bounds and cost for one variable Returns change in cost May need to be inline for speed.
double infeasibilityWeight_
Current infeasibility weight.
void feasibleBounds()
Puts feasible bounds into lower and upper.
This solves LPs using the simplex method.
double * lowerRegion(int section) const
double * upperRegion(int section) const