coin-Bcp
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 
11 class ClpSimplex;
12 class 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
41 inline int originalStatus(unsigned char status)
42 {
43  return (status & 15);
44 }
45 inline int currentStatus(unsigned char status)
46 {
47  return (status >> 4);
48 }
49 inline 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 }
54 inline 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 }
59 inline void setInitialStatus(unsigned char &status)
60 {
61  status = static_cast< unsigned char >(CLP_FEASIBLE | (CLP_SAME << 4));
62 }
63 inline 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 
79 public:
80 public:
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);
147  void feasibleBounds();
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 
304  inline int numberInfeasibilities() const
306  {
307  return numberInfeasibilities_;
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  {
329  return largestInfeasibility_;
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  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 
376 private:
379  double changeCost_;
396  int *start_;
400  int *offset_;
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_;
421  int method_;
423  bool convex_;
425  bool bothWays_;
427 };
428 
429 #endif
430 
431 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
432 */
double changeCost_
Change in cost because of infeasibilities.
unsigned char * status_
Contains status at beginning and current.
~ClpNonLinearCost()
Destructor.
bool infeasible(int i) const
For debug.
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.
void checkChanged(int numberInArray, CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
unsigned int * infeasible_
Change in cost because of infeasibilities.
int fullStatus(int sequence) const
Returns full status.
void setInfeasible(int i, bool trueFalse)
For debug.
void checkInfeasibilities(double oldTolerance=0.0)
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
void setAverageTheta(double value)
Number of infeasibilities.
void refreshCosts(const double *columnCosts)
Refreshes costs always makes row costs zero.
double feasibleReportCost() const
Feasible cost with offset and direction (i.e. for reporting)
#define CLP_ABOVE_UPPER
double sumInfeasibilities_
Sum of infeasibilities.
double changeInCost() const
Change in cost.
void zapCosts()
Temporary zeroing of feasible costs.
void setCurrentStatus(unsigned char &status, int value)
double * cost2_
Feasible cost array.
int * start_
Starts for each entry (columns then rows)
void goThru(int numberInArray, double multiplier, const int *index, const double *work, double *rhs)
Goes through one bound for each variable.
#define CLP_SAME
double * lowerRegion(int section) const
Return row or column sections - not as much needed as it once was.
double * bound_
Bound which has been replaced in lower_ or upper_.
double infeasibilityWeight_
Current infeasibility weight.
double changeUpInCost(int sequence) const
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
int numberRows_
Number of rows (mainly for checking and copy)
int * whichRange_
Range for each entry (columns then rows)
double setOne(int sequence, double solutionValue)
Sets bounds and cost for one variable Returns change in cost May need to be inline for speed...
bool lookBothWays() const
See if may want to look both ways.
void refresh()
Refresh - assuming regions OK.
void setOriginalStatus(unsigned char &status, int value)
bool bothWays_
If we should look both ways for djs.
bool changed(int sequence) const
Returns if changed from beginning of iteration.
double * cost_
Cost for each range.
void goBackAll(const CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
Indexed Vector.
double sumInfeasibilities() const
Sum of infeasibilities.
#define CLP_FEASIBLE
#define CLP_METHOD2
ClpSimplex * model_
Model.
unsigned char * statusArray() const
For debug.
#define CLP_METHOD1
int numberInfeasibilities_
Number of infeasibilities found.
double averageTheta() const
Average theta.
This solves LPs using the simplex method.
Definition: ClpSimplex.hpp:106
int numberColumns_
Number of columns (mainly for checking and copy)
double upper(int sequence) const
Returns current upper bound.
double changeDownInCost(int sequence) const
Changes infeasible costs and computes number and cost of infeas Puts all non-basic (non free) variabl...
bool convex_
If all non-linear costs convex.
const double COIN_DBL_MAX
Definition: CoinFinite.hpp:18
void setInitialStatus(unsigned char &status)
double feasibleCost() const
Feasible cost.
int originalStatus(unsigned char status)
void setSameStatus(unsigned char &status)
void goBack(int numberInArray, const int *index, double *rhs)
Takes off last iteration (i.e.
double * upperRegion(int section) const
Return row or column sections - not as much needed as it once was.
#define CLP_BELOW_LOWER
Trivial class to deal with non linear costs.
void setChangeInCost(double value)
Number of infeasibilities.
ClpNonLinearCost()
Default constructor.
int numberInfeasibilities() const
Number of infeasibilities.
int method_
Method 1 old, 2 new, 3 both!
void validate()
For debug.
double averageTheta_
Average theta - kept here as only for primal.
int * offset_
Temporary range offset for each entry (columns then rows)
double lower(int sequence) const
Returns current lower bound.
double feasibleCost_
Feasible cost.
double cost(int sequence) const
Returns current cost.
double nearest(int sequence, double solutionValue)
Returns nearest bound.
int setOneOutgoing(int sequence, double &solutionValue)
Sets bounds and cost for outgoing variable may change value Returns direction.
void setMethod(int value)
Number of infeasibilities.
ClpNonLinearCost & operator=(const ClpNonLinearCost &)
Default constructor.
double changeInCost(int sequence, double alpha, double &rhs)
This also updates next bound.
double * lower_
Lower bound for each range (upper bound is next lower).
void feasibleBounds()
Puts feasible bounds into lower and upper.
int currentStatus(unsigned char status)
double largestInfeasibility() const
Largest infeasibility.
double largestInfeasibility_
Largest infeasibility.