summaryrefslogtreecommitdiff
path: root/thirdparty/linux/include/coin1/ClpNonLinearCost.hpp
blob: 100786513b3920f5ef365c98c816c2f1da25fd1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* $Id: ClpNonLinearCost.hpp 1769 2011-07-26 09:31:51Z forrest $ */
// 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;

/** Trivial class to deal with non linear costs

    I don't make any explicit assumptions about convexity but I am
    sure I do make implicit ones.

    One interesting idea for normal LP's will be to allow non-basic
    variables to come into basis as infeasible i.e. if variable at
    lower bound has very large positive reduced cost (when problem
    is infeasible) could it reduce overall problem infeasibility more
    by bringing it into basis below its lower bound.

    Another feature would be to automatically discover when problems
    are convex piecewise linear and re-formulate to use non-linear.
    I did some work on this many years ago on "grade" problems, but
    while it improved primal interior point algorithms were much better
    for that particular problem.
*/
/* 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:

     /**@name Constructors, destructor */
     //@{
     /// Default constructor.
     ClpNonLinearCost();
     /** Constructor from simplex.
         This will just set up wasteful arrays for linear, but
         later may do dual analysis and even finding duplicate columns .
     */
     ClpNonLinearCost(ClpSimplex * model, int method = 1);
     /** Constructor from simplex and list of non-linearities (columns only)
         First lower of each column has to match real lower
         Last lower has to be <= upper (if == then cost ignored)
         This could obviously be changed to make more user friendly
     */
     ClpNonLinearCost(ClpSimplex * model, const int * starts,
                      const double * lower, const double * cost);
     /// Destructor
     ~ClpNonLinearCost();
     // Copy
     ClpNonLinearCost(const ClpNonLinearCost&);
     // Assignment
     ClpNonLinearCost& operator=(const ClpNonLinearCost&);
     //@}


     /**@name Actual work in primal */
     //@{
     /** Changes infeasible costs and computes number and cost of infeas
         Puts all non-basic (non free) variables to bounds
         and all free variables to zero if oldTolerance is non-zero
         - but does not move those <= oldTolerance away*/
     void checkInfeasibilities(double oldTolerance = 0.0);
     /** Changes infeasible costs for each variable
         The indices are row indices and need converting to sequences
     */
     void checkInfeasibilities(int numberInArray, const int * index);
     /** Puts back correct infeasible costs for each variable
         The input indices are row indices and need converting to sequences
         for costs.
         On input array is empty (but indices exist).  On exit just
         changed costs will be stored as normal CoinIndexedVector
     */
     void checkChanged(int numberInArray, CoinIndexedVector * update);
     /** Goes through one bound for each variable.
         If multiplier*work[iRow]>0 goes down, otherwise up.
         The indices are row indices and need converting to sequences
         Temporary offsets may be set
         Rhs entries are increased
     */
     void goThru(int numberInArray, double multiplier,
                 const int * index, const double * work,
                 double * rhs);
     /** Takes off last iteration (i.e. offsets closer to 0)
     */
     void goBack(int numberInArray, const int * index,
                 double * rhs);
     /** Puts back correct infeasible costs for each variable
         The input indices are row indices and need converting to sequences
         for costs.
         At the end of this all temporary offsets are zero
     */
     void goBackAll(const CoinIndexedVector * update);
     /// Temporary zeroing of feasible costs
     void zapCosts();
     /// Refreshes costs always makes row costs zero
     void refreshCosts(const double * columnCosts);
     /// Puts feasible bounds into lower and upper
     void feasibleBounds();
     /// Refresh - assuming regions OK
     void refresh();
     /** Sets bounds and cost for one variable
         Returns change in cost
      May need to be inline for speed */
     double setOne(int sequence, double solutionValue);
     /** Sets bounds and infeasible cost and true cost for one variable
         This is for gub and column generation etc */
     void setOne(int sequence, double solutionValue, double lowerValue, double upperValue,
                 double costValue = 0.0);
     /** Sets bounds and cost for outgoing variable
         may change value
         Returns direction */
     int setOneOutgoing(int sequence, double &solutionValue);
     /// Returns nearest bound
     double nearest(int sequence, double solutionValue);
     /** Returns change in cost - one down if alpha >0.0, up if <0.0
         Value is current - new
      */
     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;
     }
     /// This also updates next bound
     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;
     }
     /// Returns current lower bound
     inline double lower(int sequence) const {
          return lower_[whichRange_[sequence] + offset_[sequence]];
     }
     /// Returns current upper bound
     inline double upper(int sequence) const {
          return lower_[whichRange_[sequence] + offset_[sequence] + 1];
     }
     /// Returns current cost
     inline double cost(int sequence) const {
          return cost_[whichRange_[sequence] + offset_[sequence]];
     }
     //@}


     /**@name Gets and sets */
     //@{
     /// Number of infeasibilities
     inline int numberInfeasibilities() const {
          return numberInfeasibilities_;
     }
     /// Change in cost
     inline double changeInCost() const {
          return changeCost_;
     }
     /// Feasible cost
     inline double feasibleCost() const {
          return feasibleCost_;
     }
     /// Feasible cost with offset and direction (i.e. for reporting)
     double feasibleReportCost() const;
     /// Sum of infeasibilities
     inline double sumInfeasibilities() const {
          return sumInfeasibilities_;
     }
     /// Largest infeasibility
     inline double largestInfeasibility() const {
          return largestInfeasibility_;
     }
     /// Average theta
     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;
     }
     /// See if may want to look both ways
     inline bool lookBothWays() const {
          return bothWays_;
     }
     //@}
     ///@name Private functions to deal with infeasible regions
     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_;
     }
     /// For debug
     void validate();
     //@}

private:
     /**@name Data members */
     //@{
     /// Change in cost because of infeasibilities
     double changeCost_;
     /// Feasible cost
     double feasibleCost_;
     /// Current infeasibility weight
     double infeasibilityWeight_;
     /// Largest infeasibility
     double largestInfeasibility_;
     /// Sum of infeasibilities
     double sumInfeasibilities_;
     /// Average theta - kept here as only for primal
     double averageTheta_;
     /// Number of rows (mainly for checking and copy)
     int numberRows_;
     /// Number of columns (mainly for checking and copy)
     int numberColumns_;
     /// Starts for each entry (columns then rows)
     int * start_;
     /// Range for each entry (columns then rows)
     int * whichRange_;
     /// Temporary range offset for each entry (columns then rows)
     int * offset_;
     /** Lower bound for each range (upper bound is next lower).
         For various reasons there is always an infeasible range
         at bottom - even if lower bound is - infinity */
     double * lower_;
     /// Cost for each range
     double * cost_;
     /// Model
     ClpSimplex * model_;
     // Array to say which regions are infeasible
     unsigned int * infeasible_;
     /// Number of infeasibilities found
     int numberInfeasibilities_;
     // new stuff
     /// Contains status at beginning and current
     unsigned char * status_;
     /// Bound which has been replaced in lower_ or upper_
     double * bound_;
     /// Feasible cost array
     double * cost2_;
     /// Method 1 old, 2 new, 3 both!
     int method_;
     /// If all non-linear costs convex
     bool convex_;
     /// If we should look both ways for djs
     bool bothWays_;
     //@}
};

#endif