Loading...
Searching...
No Matches
ClpNonLinearCost.hpp
Go to the documentation of this file.
/* $Id$ */
// Copyright (C) 2002, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#ifndef ClpNonLinearCost_H
#define ClpNonLinearCost_H
#include "CoinPragma.hpp"
class ClpSimplex;
class CoinIndexedVector;
/* status has original status and current status
0 - below lower so stored is upper
1 - in range
2 - above upper so stored is lower
4 - (for current) - same as original
*/
#define CLP_BELOW_LOWER 0
#define CLP_FEASIBLE 1
#define CLP_ABOVE_UPPER 2
#define CLP_SAME 4
inline int originalStatus(unsigned char status)
{
return (status & 15);
}
inline int currentStatus(unsigned char status)
{
return (status >> 4);
}
inline void setOriginalStatus(unsigned char &status, int value)
{
status = static_cast< unsigned char >(status & ~15);
status = static_cast< unsigned char >(status | value);
}
inline void setCurrentStatus(unsigned char &status, int value)
{
status = static_cast< unsigned char >(status & ~(15 << 4));
status = static_cast< unsigned char >(status | (value << 4));
}
inline void setInitialStatus(unsigned char &status)
{
status = static_cast< unsigned char >(CLP_FEASIBLE | (CLP_SAME << 4));
}
inline void setSameStatus(unsigned char &status)
{
status = static_cast< unsigned char >(status & ~(15 << 4));
status = static_cast< unsigned char >(status | (CLP_SAME << 4));
}
// Use second version to get more speed
//#define FAST_CLPNON
#ifndef FAST_CLPNON
#define CLP_METHOD1 ((method_ & 1) != 0)
#define CLP_METHOD2 ((method_ & 2) != 0)
#else
#define CLP_METHOD1 (false)
#define CLP_METHOD2 (true)
#endif
class ClpNonLinearCost {
public:
public:
ClpNonLinearCost();
ClpNonLinearCost(ClpSimplex *model, int method = 1);
ClpNonLinearCost(ClpSimplex *model, const int *starts,
const double *lower, const double *cost);
~ClpNonLinearCost();
// Copy
ClpNonLinearCost(const ClpNonLinearCost &);
// Assignment
ClpNonLinearCost &operator=(const ClpNonLinearCost &);
void checkInfeasibilities(double oldTolerance = 0.0);
void checkInfeasibilities(int numberInArray, const int *index);
void checkChanged(int numberInArray, CoinIndexedVector *update);
void goThru(int numberInArray, double multiplier,
const int *index, const double *work,
double *rhs);
void goBack(int numberInArray, const int *index,
double *rhs);
void goBackAll(const CoinIndexedVector *update);
void zapCosts();
void refreshCosts(const double *columnCosts);
void feasibleBounds();
void refresh();
void refresh(int iSequence);
double setOne(int sequence, double solutionValue);
void setOne(int sequence, double solutionValue, double lowerValue, double upperValue,
double costValue = 0.0);
int setOneOutgoing(int sequence, double &solutionValue);
double nearest(int sequence, double solutionValue);
inline double changeInCost(int sequence, double alpha) const
{
double returnValue = 0.0;
if (CLP_METHOD1) {
int iRange = whichRange_[sequence] + offset_[sequence];
if (alpha > 0.0)
returnValue = cost_[iRange] - cost_[iRange - 1];
else
returnValue = cost_[iRange] - cost_[iRange + 1];
}
if (CLP_METHOD2) {
returnValue = (alpha > 0.0) ? infeasibilityWeight_ : -infeasibilityWeight_;
}
return returnValue;
}
inline double changeUpInCost(int sequence) const
{
double returnValue = 0.0;
if (CLP_METHOD1) {
int iRange = whichRange_[sequence] + offset_[sequence];
if (iRange + 1 != start_[sequence + 1] && !infeasible(iRange + 1))
returnValue = cost_[iRange] - cost_[iRange + 1];
else
returnValue = -1.0e100;
}
if (CLP_METHOD2) {
returnValue = -infeasibilityWeight_;
}
return returnValue;
}
inline double changeDownInCost(int sequence) const
{
double returnValue = 0.0;
if (CLP_METHOD1) {
int iRange = whichRange_[sequence] + offset_[sequence];
if (iRange != start_[sequence] && !infeasible(iRange - 1))
returnValue = cost_[iRange] - cost_[iRange - 1];
else
returnValue = 1.0e100;
}
if (CLP_METHOD2) {
returnValue = infeasibilityWeight_;
}
return returnValue;
}
inline double changeInCost(int sequence, double alpha, double &rhs)
{
double returnValue = 0.0;
#ifdef NONLIN_DEBUG
double saveRhs = rhs;
#endif
if (CLP_METHOD1) {
int iRange = whichRange_[sequence] + offset_[sequence];
if (alpha > 0.0) {
assert(iRange - 1 >= start_[sequence]);
offset_[sequence]--;
rhs += lower_[iRange] - lower_[iRange - 1];
returnValue = alpha * (cost_[iRange] - cost_[iRange - 1]);
} else {
assert(iRange + 1 < start_[sequence + 1] - 1);
offset_[sequence]++;
rhs += lower_[iRange + 2] - lower_[iRange + 1];
returnValue = alpha * (cost_[iRange] - cost_[iRange + 1]);
}
}
if (CLP_METHOD2) {
#ifdef NONLIN_DEBUG
double saveRhs1 = rhs;
rhs = saveRhs;
#endif
unsigned char iStatus = status_[sequence];
int iWhere = currentStatus(iStatus);
if (iWhere == CLP_SAME)
iWhere = originalStatus(iStatus);
// rhs always increases
if (iWhere == CLP_FEASIBLE) {
if (alpha > 0.0) {
// going below
iWhere = CLP_BELOW_LOWER;
rhs = COIN_DBL_MAX;
} else {
// going above
iWhere = CLP_ABOVE_UPPER;
rhs = COIN_DBL_MAX;
}
} else if (iWhere == CLP_BELOW_LOWER) {
assert(alpha < 0);
// going feasible
iWhere = CLP_FEASIBLE;
rhs += bound_[sequence] - model_->upperRegion()[sequence];
} else {
assert(iWhere == CLP_ABOVE_UPPER);
// going feasible
iWhere = CLP_FEASIBLE;
rhs += model_->lowerRegion()[sequence] - bound_[sequence];
}
setCurrentStatus(status_[sequence], iWhere);
#ifdef NONLIN_DEBUG
assert(saveRhs1 == rhs);
#endif
returnValue = fabs(alpha) * infeasibilityWeight_;
}
return returnValue;
}
inline double lower(int sequence) const
{
return lower_[whichRange_[sequence] + offset_[sequence]];
}
inline double upper(int sequence) const
{
return lower_[whichRange_[sequence] + offset_[sequence] + 1];
}
inline double cost(int sequence) const
{
return cost_[whichRange_[sequence] + offset_[sequence]];
}
inline int fullStatus(int sequence) const
{
return status_[sequence];
}
inline bool changed(int sequence) const
{
return (status_[sequence] & 64) == 0;
}
inline int numberInfeasibilities() const
{
return numberInfeasibilities_;
}
inline double changeInCost() const
{
return changeCost_;
}
inline double feasibleCost() const
{
return feasibleCost_;
}
double feasibleReportCost() const;
inline double sumInfeasibilities() const
{
return sumInfeasibilities_;
}
inline double largestInfeasibility() const
{
return largestInfeasibility_;
}
inline double averageTheta() const
{
return averageTheta_;
}
inline void setAverageTheta(double value)
{
averageTheta_ = value;
}
inline void setChangeInCost(double value)
{
changeCost_ = value;
}
inline void setMethod(int value)
{
method_ = value;
}
inline bool lookBothWays() const
{
return bothWays_;
}
inline bool infeasible(int i) const
{
return ((infeasible_[i >> 5] >> (i & 31)) & 1) != 0;
}
inline void setInfeasible(int i, bool trueFalse)
{
unsigned int &value = infeasible_[i >> 5];
int bit = i & 31;
if (trueFalse)
value |= (1 << bit);
else
value &= ~(1 << bit);
}
inline unsigned char *statusArray() const
{
return status_;
}
void validate();
private:
double changeCost_;
double feasibleCost_;
double infeasibilityWeight_;
double largestInfeasibility_;
double sumInfeasibilities_;
double averageTheta_;
int numberRows_;
int numberColumns_;
int *start_;
int *whichRange_;
int *offset_;
double *lower_;
double *cost_;
ClpSimplex *model_;
// Array to say which regions are infeasible
unsigned int *infeasible_;
int numberInfeasibilities_;
// new stuff
unsigned char *status_;
double *bound_;
double *cost2_;
int method_;
bool convex_;
bool bothWays_;
};
#endif
/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/
CLP_FEASIBLE
#define CLP_FEASIBLE
Definition AbcNonLinearCost.hpp:39
CLP_SAME
#define CLP_SAME
Definition AbcNonLinearCost.hpp:41
setOriginalStatus
void setOriginalStatus(unsigned char &status, int value)
Definition ClpNonLinearCost.hpp:49
CLP_BELOW_LOWER
#define CLP_BELOW_LOWER
Trivial class to deal with non linear costs.
Definition ClpNonLinearCost.hpp:37
setCurrentStatus
void setCurrentStatus(unsigned char &status, int value)
Definition ClpNonLinearCost.hpp:54
currentStatus
int currentStatus(unsigned char status)
Definition ClpNonLinearCost.hpp:45
CLP_FEASIBLE
#define CLP_FEASIBLE
Definition ClpNonLinearCost.hpp:38
CLP_SAME
#define CLP_SAME
Definition ClpNonLinearCost.hpp:40
setSameStatus
void setSameStatus(unsigned char &status)
Definition ClpNonLinearCost.hpp:63
setInitialStatus
void setInitialStatus(unsigned char &status)
Definition ClpNonLinearCost.hpp:59
CLP_ABOVE_UPPER
#define CLP_ABOVE_UPPER
Definition ClpNonLinearCost.hpp:39
CLP_METHOD2
#define CLP_METHOD2
Definition ClpNonLinearCost.hpp:72
CLP_METHOD1
#define CLP_METHOD1
Definition ClpNonLinearCost.hpp:71
originalStatus
int originalStatus(unsigned char status)
Definition ClpNonLinearCost.hpp:41
ClpNonLinearCost::convex_
bool convex_
If all non-linear costs convex.
Definition ClpNonLinearCost.hpp:423
ClpNonLinearCost::cost_
double * cost_
Cost for each range.
Definition ClpNonLinearCost.hpp:406
ClpNonLinearCost::changeCost_
double changeCost_
Change in cost because of infeasibilities.
Definition ClpNonLinearCost.hpp:380
ClpNonLinearCost::ClpNonLinearCost
ClpNonLinearCost()
Default constructor.
ClpNonLinearCost::refreshCosts
void refreshCosts(const double *columnCosts)
Refreshes costs always makes row costs zero.
ClpNonLinearCost::lower
double lower(int sequence) const
Returns current lower bound.
Definition ClpNonLinearCost.hpp:275
ClpNonLinearCost::averageTheta_
double averageTheta_
Average theta - kept here as only for primal.
Definition ClpNonLinearCost.hpp:390
ClpNonLinearCost::changeUpInCost
double changeUpInCost(int sequence) const
Definition ClpNonLinearCost.hpp:184
ClpNonLinearCost::bound_
double * bound_
Bound which has been replaced in lower_ or upper_.
Definition ClpNonLinearCost.hpp:417
ClpNonLinearCost::sumInfeasibilities
double sumInfeasibilities() const
Sum of infeasibilities.
Definition ClpNonLinearCost.hpp:322
ClpNonLinearCost::nearest
double nearest(int sequence, double solutionValue)
Returns nearest bound.
ClpNonLinearCost::sumInfeasibilities_
double sumInfeasibilities_
Sum of infeasibilities.
Definition ClpNonLinearCost.hpp:388
ClpNonLinearCost::infeasible
bool infeasible(int i) const
Definition ClpNonLinearCost.hpp:355
ClpNonLinearCost::fullStatus
int fullStatus(int sequence) const
Returns full status.
Definition ClpNonLinearCost.hpp:290
ClpNonLinearCost::operator=
ClpNonLinearCost & operator=(const ClpNonLinearCost &)
ClpNonLinearCost::ClpNonLinearCost
ClpNonLinearCost(ClpSimplex *model, int method=1)
Constructor from simplex.
ClpNonLinearCost::setChangeInCost
void setChangeInCost(double value)
Definition ClpNonLinearCost.hpp:340
ClpNonLinearCost::changed
bool changed(int sequence) const
Returns if changed from beginning of iteration.
Definition ClpNonLinearCost.hpp:295
ClpNonLinearCost::feasibleCost
double feasibleCost() const
Feasible cost.
Definition ClpNonLinearCost.hpp:315
ClpNonLinearCost::changeInCost
double changeInCost(int sequence, double alpha, double &rhs)
This also updates next bound.
Definition ClpNonLinearCost.hpp:215
ClpNonLinearCost::largestInfeasibility_
double largestInfeasibility_
Largest infeasibility.
Definition ClpNonLinearCost.hpp:386
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::feasibleReportCost
double feasibleReportCost() const
Feasible cost with offset and direction (i.e. for reporting).
ClpNonLinearCost::bothWays_
bool bothWays_
If we should look both ways for djs.
Definition ClpNonLinearCost.hpp:425
ClpNonLinearCost::cost
double cost(int sequence) const
Returns current cost.
Definition ClpNonLinearCost.hpp:285
ClpNonLinearCost::ClpNonLinearCost
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...
ClpNonLinearCost::lookBothWays
bool lookBothWays() const
See if may want to look both ways.
Definition ClpNonLinearCost.hpp:349
ClpNonLinearCost::setInfeasible
void setInfeasible(int i, bool trueFalse)
Definition ClpNonLinearCost.hpp:359
ClpNonLinearCost::goBackAll
void goBackAll(const CoinIndexedVector *update)
Puts back correct infeasible costs for each variable The input indices are row indices and need conve...
ClpNonLinearCost::status_
unsigned char * status_
Contains status at beginning and current.
Definition ClpNonLinearCost.hpp:415
ClpNonLinearCost::ClpNonLinearCost
ClpNonLinearCost(const ClpNonLinearCost &)
ClpNonLinearCost::lower_
double * lower_
Lower bound for each range (upper bound is next lower).
Definition ClpNonLinearCost.hpp:404
ClpNonLinearCost::setAverageTheta
void setAverageTheta(double value)
Definition ClpNonLinearCost.hpp:336
ClpNonLinearCost::feasibleCost_
double feasibleCost_
Feasible cost.
Definition ClpNonLinearCost.hpp:382
ClpNonLinearCost::method_
int method_
Method 1 old, 2 new, 3 both!
Definition ClpNonLinearCost.hpp:421
ClpNonLinearCost::statusArray
unsigned char * statusArray() const
Definition ClpNonLinearCost.hpp:368
ClpNonLinearCost::setMethod
void setMethod(int value)
Definition ClpNonLinearCost.hpp:344
ClpNonLinearCost::numberInfeasibilities_
int numberInfeasibilities_
Number of infeasibilities found.
Definition ClpNonLinearCost.hpp:412
ClpNonLinearCost::numberRows_
int numberRows_
Number of rows (mainly for checking and copy).
Definition ClpNonLinearCost.hpp:392
ClpNonLinearCost::numberInfeasibilities
int numberInfeasibilities() const
Number of infeasibilities.
Definition ClpNonLinearCost.hpp:305
ClpNonLinearCost::setOneOutgoing
int setOneOutgoing(int sequence, double &solutionValue)
Sets bounds and cost for outgoing variable may change value Returns direction.
ClpNonLinearCost::start_
int * start_
Starts for each entry (columns then rows).
Definition ClpNonLinearCost.hpp:396
ClpNonLinearCost::offset_
int * offset_
Temporary range offset for each entry (columns then rows).
Definition ClpNonLinearCost.hpp:400
ClpNonLinearCost::setOne
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 ...
ClpNonLinearCost::goBack
void goBack(int numberInArray, const int *index, double *rhs)
Takes off last iteration (i.e.
ClpNonLinearCost::cost2_
double * cost2_
Feasible cost array.
Definition ClpNonLinearCost.hpp:419
ClpNonLinearCost::whichRange_
int * whichRange_
Range for each entry (columns then rows).
Definition ClpNonLinearCost.hpp:398
ClpNonLinearCost::numberColumns_
int numberColumns_
Number of columns (mainly for checking and copy).
Definition ClpNonLinearCost.hpp:394
ClpNonLinearCost::goThru
void goThru(int numberInArray, double multiplier, const int *index, const double *work, double *rhs)
Goes through one bound for each variable.
ClpNonLinearCost::checkInfeasibilities
void checkInfeasibilities(int numberInArray, const int *index)
Changes infeasible costs for each variable The indices are row indices and need converting to sequenc...
ClpNonLinearCost::refresh
void refresh()
Refresh - assuming regions OK.
ClpNonLinearCost::refresh
void refresh(int iSequence)
Refresh one- assuming regions OK.
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::zapCosts
void zapCosts()
Temporary zeroing of feasible costs.
ClpNonLinearCost::largestInfeasibility
double largestInfeasibility() const
Largest infeasibility.
Definition ClpNonLinearCost.hpp:327
ClpNonLinearCost::averageTheta
double averageTheta() const
Average theta.
Definition ClpNonLinearCost.hpp:332
ClpNonLinearCost::infeasible_
unsigned int * infeasible_
Definition ClpNonLinearCost.hpp:410
ClpNonLinearCost::changeDownInCost
double changeDownInCost(int sequence) const
Definition ClpNonLinearCost.hpp:199
ClpNonLinearCost::changeInCost
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.
Definition ClpNonLinearCost.hpp:169
ClpNonLinearCost::upper
double upper(int sequence) const
Returns current upper bound.
Definition ClpNonLinearCost.hpp:280
ClpNonLinearCost::validate
void validate()
For debug.
ClpNonLinearCost::changeInCost
double changeInCost() const
Change in cost.
Definition ClpNonLinearCost.hpp:310
ClpNonLinearCost::~ClpNonLinearCost
~ClpNonLinearCost()
Destructor.
ClpNonLinearCost::model_
ClpSimplex * model_
Model.
Definition ClpNonLinearCost.hpp:408
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::infeasibilityWeight_
double infeasibilityWeight_
Current infeasibility weight.
Definition ClpNonLinearCost.hpp:384
ClpNonLinearCost::feasibleBounds
void feasibleBounds()
Puts feasible bounds into lower and upper.
ClpSimplex
This solves LPs using the simplex method.
Definition ClpSimplex.hpp:106