File: OsiChooseVariable.hpp

package info (click to toggle)
coinor-osi 0.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 10,616 kB
  • ctags: 3,542
  • sloc: cpp: 61,873; sh: 9,955; makefile: 808; ansic: 45
file content (532 lines) | stat: -rw-r--r-- 19,207 bytes parent folder | download | duplicates (2)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright (C) 2006, International Business Machines
// Corporation and others.  All Rights Reserved.
#ifndef OsiChooseVariable_H
#define OsiChooseVariable_H

#include <string>
#include <vector>

#include "CoinWarmStartBasis.hpp"
#include "OsiBranchingObject.hpp"

class OsiSolverInterface;
class OsiHotInfo;

/** This class chooses a variable to branch on

    The base class just chooses the variable and direction without strong branching but it 
    has information which would normally be used by strong branching e.g. to re-enter
    having fixed a variable but using same candidates for strong branching.

    The flow is :
    a) initialize the process.  This decides on strong branching list
       and stores indices of all infeasible objects  
    b) do strong branching on list.  If list is empty then just
       choose one candidate and return without strong branching.  If not empty then
       go through list and return best.  However we may find that the node is infeasible
       or that we can fix a variable.  If so we return and it is up to user to call
       again (after fixing a variable).
*/

class OsiChooseVariable  {
 
public:
    
  /// Default Constructor 
  OsiChooseVariable ();

  /// Constructor from solver (so we can set up arrays etc)
  OsiChooseVariable (const OsiSolverInterface * solver);

  /// Copy constructor 
  OsiChooseVariable (const OsiChooseVariable &);
   
  /// Assignment operator 
  OsiChooseVariable & operator= (const OsiChooseVariable& rhs);

  /// Clone
  virtual OsiChooseVariable * clone() const;

  /// Destructor 
  virtual ~OsiChooseVariable ();

  /** Sets up strong list and clears all if initialize is true.
      Returns number of infeasibilities. 
      If returns -1 then has worked out node is infeasible!
  */
  virtual int setupList ( OsiBranchingInformation *info, bool initialize);
  /** Choose a variable
      Returns - 
     -1 Node is infeasible
     0  Normal termination - we have a candidate
     1  All looks satisfied - no candidate
     2  We can change the bound on a variable - but we also have a strong branching candidate
     3  We can change the bound on a variable - but we have a non-strong branching candidate
     4  We can change the bound on a variable - no other candidates
     We can pick up branch from bestObjectIndex() and bestWhichWay()
     We can pick up a forced branch (can change bound) from firstForcedObjectIndex() and firstForcedWhichWay()
     If we have a solution then we can pick up from goodObjectiveValue() and goodSolution()
     If fixVariables is true then 2,3,4 are all really same as problem changed
  */
  virtual int chooseVariable( OsiSolverInterface * solver, OsiBranchingInformation *info, bool fixVariables);
  /// Returns true if solution looks feasible against given objects
  virtual bool feasibleSolution(const OsiBranchingInformation * info,
			const double * solution,
			int numberObjects,
			const OsiObject ** objects);
  /// Saves a good solution
  void saveSolution(const OsiSolverInterface * solver);
  /// Clears out good solution after use
  void clearGoodSolution();
  /// Given a candidate fill in useful information e.g. estimates
  virtual void updateInformation( const OsiBranchingInformation *info,
				  int branch, OsiHotInfo * hotInfo);
#if 1
  /// Given a branch fill in useful information e.g. estimates
  virtual void updateInformation( int whichObject, int branch, 
				  double changeInObjective, double changeInValue,
				  int status);
#endif
  /// Objective value for feasible solution
  inline double goodObjectiveValue() const
  { return goodObjectiveValue_;}
  /// Estimate of up change or change on chosen if n-way
  inline double upChange() const
  { return upChange_;}
  /// Estimate of down change or max change on other possibilities if n-way
  inline double downChange() const
  { return downChange_;}
  /// Good solution - deleted by finalize
  inline const double * goodSolution() const
  { return goodSolution_;}
  /// Index of chosen object
  inline int bestObjectIndex() const
  { return bestObjectIndex_;}
  /// Set index of chosen object
  inline void setBestObjectIndex(int value)
  { bestObjectIndex_ = value;}
  /// Preferred way of chosen object
  inline int bestWhichWay() const
  { return bestWhichWay_;}
  /// Set preferred way of chosen object
  inline void setBestWhichWay(int value)
  { bestWhichWay_ = value;}
  /// Index of forced object
  inline int firstForcedObjectIndex() const
  { return firstForcedObjectIndex_;}
  /// Set index of forced object
  inline void setFirstForcedObjectIndex(int value)
  { firstForcedObjectIndex_ = value;}
  /// Preferred way of forced object
  inline int firstForcedWhichWay() const
  { return firstForcedWhichWay_;}
  /// Set preferred way of forced object
  inline void setFirstForcedWhichWay(int value)
  { firstForcedWhichWay_ = value;}
  /// Get the number of objects unsatisfied at this node - accurate on first pass
  inline int numberUnsatisfied() const
  {return numberUnsatisfied_;}
  /// Number of objects to choose for strong branching
  inline int numberStrong() const
  { return numberStrong_;}
  /// Set number of objects to choose for strong branching
  inline void setNumberStrong(int value)
  { numberStrong_ = value;}
  /// Number left on strong list
  inline int numberOnList() const
  { return numberOnList_;}
  /// Number of strong branches actually done 
  inline int numberStrongDone() const
  { return numberStrongDone_;}
  /// Number of strong iterations actually done 
  inline int numberStrongIterations() const
  { return numberStrongIterations_;}
  /// Number of strong branches which changed bounds 
  inline int numberStrongFixed() const
  { return numberStrongFixed_;}
  /// List of candidates
  inline const int * candidates() const
  { return list_;}
  /// Trust results from strong branching for changing bounds
  inline bool trustStrongForBound() const
  { return trustStrongForBound_;}
  /// Set trust results from strong branching for changing bounds
  inline void setTrustStrongForBound(bool yesNo)
  { trustStrongForBound_ = yesNo;}
  /// Trust results from strong branching for valid solution
  inline bool trustStrongForSolution() const
  { return trustStrongForSolution_;}
  /// Set trust results from strong branching for valid solution
  inline void setTrustStrongForSolution(bool yesNo)
  { trustStrongForSolution_ = yesNo;}
  /// Set solver and redo arrays
  void setSolver (const OsiSolverInterface * solver);
  /** Return status - 
     -1 Node is infeasible
     0  Normal termination - we have a candidate
     1  All looks satisfied - no candidate
     2  We can change the bound on a variable - but we also have a strong branching candidate
     3  We can change the bound on a variable - but we have a non-strong branching candidate
     4  We can change the bound on a variable - no other candidates
     We can pick up branch from bestObjectIndex() and bestWhichWay()
     We can pick up a forced branch (can change bound) from firstForcedObjectIndex() and firstForcedWhichWay()
     If we have a solution then we can pick up from goodObjectiveValue() and goodSolution()
  */
  inline int status() const
  { return status_;}
  inline void setStatus(int value)
  { status_ = value;}


protected:
  // Data
  /// Objective value for feasible solution
  double goodObjectiveValue_;
  /// Estimate of up change or change on chosen if n-way
  double upChange_;
  /// Estimate of down change or max change on other possibilities if n-way
  double downChange_;
  /// Good solution - deleted by finalize
  double * goodSolution_;
  /// List of candidates
  int * list_;
  /// Useful array (for sorting etc)
  double * useful_;
  /// Pointer to solver
  const OsiSolverInterface * solver_;
  /* Status -
     -1 Node is infeasible
     0  Normal termination - we have a candidate
     1  All looks satisfied - no candidate
     2  We can change the bound on a variable - but we also have a strong branching candidate
     3  We can change the bound on a variable - but we have a non-strong branching candidate
     4  We can change the bound on a variable - no other candidates
  */
  int status_;
  /// Index of chosen object
  int bestObjectIndex_;
  /// Preferred way of chosen object
  int bestWhichWay_;
  /// Index of forced object
  int firstForcedObjectIndex_;
  /// Preferred way of forced object
  int firstForcedWhichWay_;
  /// The number of objects unsatisfied at this node.
  int numberUnsatisfied_;
  /// Number of objects to choose for strong branching
  int numberStrong_;
  /// Number left on strong list
  int numberOnList_;
  /// Number of strong branches actually done 
  int numberStrongDone_;
  /// Number of strong iterations actually done 
  int numberStrongIterations_;
  /// Number of bound changes due to strong branching
  int numberStrongFixed_;
  /// List of unsatisfied objects - first numberOnList_ for strong branching
  /// Trust results from strong branching for changing bounds
  bool trustStrongForBound_;
  /// Trust results from strong branching for valid solution
  bool trustStrongForSolution_;
};

/** This class is the placeholder for the pseudocosts used by OsiChooseStrong.
    It can also be used by any other pseudocost based strong branching
    algorithm.
*/

class OsiPseudoCosts {
protected:
   // Data
  /// Total of all changes up
  double * upTotalChange_;
  /// Total of all changes down
  double * downTotalChange_;
  /// Number of times up
  int * upNumber_;
  /// Number of times down
  int * downNumber_;
  /// Number of objects (could be found from solver)
  int numberObjects_;
  /// Number before we trust
  int numberBeforeTrusted_;

private:
  void gutsOfDelete();
  void gutsOfCopy(const OsiPseudoCosts& rhs);

public:
  OsiPseudoCosts();
  virtual ~OsiPseudoCosts();
  OsiPseudoCosts(const OsiPseudoCosts& rhs);
  OsiPseudoCosts& operator=(const OsiPseudoCosts& rhs);

  /// Number of times before trusted
  inline int numberBeforeTrusted() const
  { return numberBeforeTrusted_; }
  /// Set number of times before trusted
  inline void setNumberBeforeTrusted(int value)
  { numberBeforeTrusted_ = value; }
  /// Initialize the pseudocosts with n entries
  void initialize(int n);
  /// Give the number of objects for which pseudo costs are stored
  inline int numberObjects() const
  { return numberObjects_; }

  /** @name Accessor methods to pseudo costs data */
  //@{
  inline double* upTotalChange()               { return upTotalChange_; }
  inline const double* upTotalChange() const   { return upTotalChange_; }

  inline double* downTotalChange()             { return downTotalChange_; }
  inline const double* downTotalChange() const { return downTotalChange_; }

  inline int* upNumber()                       { return upNumber_; }
  inline const int* upNumber() const           { return upNumber_; }

  inline int* downNumber()                     { return downNumber_; }
  inline const int* downNumber() const         { return downNumber_; }
  //@}

  /// Given a candidate fill in useful information e.g. estimates
  virtual void updateInformation(const OsiBranchingInformation *info,
				  int branch, OsiHotInfo * hotInfo);
#if 1 
  /// Given a branch fill in useful information e.g. estimates
  virtual void updateInformation( int whichObject, int branch, 
				  double changeInObjective, double changeInValue,
				  int status);
#endif
};

/** This class chooses a variable to branch on

    This chooses the variable and direction with reliability strong branching.

    The flow is :
    a) initialize the process.  This decides on strong branching list
       and stores indices of all infeasible objects  
    b) do strong branching on list.  If list is empty then just
       choose one candidate and return without strong branching.  If not empty then
       go through list and return best.  However we may find that the node is infeasible
       or that we can fix a variable.  If so we return and it is up to user to call
       again (after fixing a variable).
*/

class OsiChooseStrong  : public OsiChooseVariable {
 
public:
    
  /// Default Constructor 
  OsiChooseStrong ();

  /// Constructor from solver (so we can set up arrays etc)
  OsiChooseStrong (const OsiSolverInterface * solver);

  /// Copy constructor 
  OsiChooseStrong (const OsiChooseStrong &);
   
  /// Assignment operator 
  OsiChooseStrong & operator= (const OsiChooseStrong& rhs);

  /// Clone
  virtual OsiChooseVariable * clone() const;

  /// Destructor 
  virtual ~OsiChooseStrong ();

  /** Sets up strong list and clears all if initialize is true.
      Returns number of infeasibilities. 
      If returns -1 then has worked out node is infeasible!
  */
  virtual int setupList ( OsiBranchingInformation *info, bool initialize);
  /** Choose a variable
      Returns - 
     -1 Node is infeasible
     0  Normal termination - we have a candidate
     1  All looks satisfied - no candidate
     2  We can change the bound on a variable - but we also have a strong branching candidate
     3  We can change the bound on a variable - but we have a non-strong branching candidate
     4  We can change the bound on a variable - no other candidates
     We can pick up branch from bestObjectIndex() and bestWhichWay()
     We can pick up a forced branch (can change bound) from firstForcedObjectIndex() and firstForcedWhichWay()
     If we have a solution then we can pick up from goodObjectiveValue() and goodSolution()
     If fixVariables is true then 2,3,4 are all really same as problem changed
  */
  virtual int chooseVariable( OsiSolverInterface * solver, OsiBranchingInformation *info, bool fixVariables);

  /** Pseudo Shadow Price mode
      0 - off
      1 - use if no strong info
      2 - use if strong not trusted
      3 - use even if trusted
  */
  inline int shadowPriceMode() const
  { return shadowPriceMode_;}
  /// Set Shadow price mode
  inline void setShadowPriceMode(int value)
  { shadowPriceMode_ = value;}

  /** Accessor method to pseudo cost object*/
  const OsiPseudoCosts& pseudoCosts() const
  { return pseudoCosts_; }

  /** Accessor method to pseudo cost object*/
  OsiPseudoCosts& pseudoCosts()
  { return pseudoCosts_; }

  /** A feww pass-through methods to access members of pseudoCosts_ as if they
      were members of OsiChooseStrong object */
  inline int numberBeforeTrusted() const {
    return pseudoCosts_.numberBeforeTrusted(); }
  inline void setNumberBeforeTrusted(int value) {
    pseudoCosts_.setNumberBeforeTrusted(value); }
  inline int numberObjects() const {
    return pseudoCosts_.numberObjects(); }

protected:

  /**  This is a utility function which does strong branching on
       a list of objects and stores the results in OsiHotInfo.objects.
       On entry the object sequence is stored in the OsiHotInfo object
       and maybe more.
       It returns -
       -1 - one branch was infeasible both ways
       0 - all inspected - nothing can be fixed
       1 - all inspected - some can be fixed (returnCriterion==0)
       2 - may be returning early - one can be fixed (last one done) (returnCriterion==1) 
       3 - returning because max time
       
  */
  int doStrongBranching( OsiSolverInterface * solver, 
			 OsiBranchingInformation *info,
			 int numberToDo, int returnCriterion);

  /** Clear out the results array */
  void resetResults(int num);

protected:
  /** Pseudo Shadow Price mode
      0 - off
      1 - use and multiply by strong info
      2 - use 
  */
  int shadowPriceMode_;

  /** The pseudo costs for the chooser */
  OsiPseudoCosts pseudoCosts_;

  /** The results of the strong branching done on the candidates where the
      pseudocosts were not sufficient */
  OsiHotInfo* results_;
  /** The number of OsiHotInfo objetcs that contain information */
  int numResults_;
};

/** This class contains the result of strong branching on a variable
    When created it stores enough information for strong branching
*/

class OsiHotInfo  {
 
public:
    
  /// Default Constructor 
  OsiHotInfo ();

  /// Constructor from useful information
  OsiHotInfo ( OsiSolverInterface * solver, 
	       const OsiBranchingInformation *info,
	       const OsiObject * const * objects,
	       int whichObject);

  /// Copy constructor 
  OsiHotInfo (const OsiHotInfo &);
   
  /// Assignment operator 
  OsiHotInfo & operator= (const OsiHotInfo& rhs);

  /// Clone
  virtual OsiHotInfo * clone() const;

  /// Destructor 
  virtual ~OsiHotInfo ();

  /** Fill in useful information after strong branch.
      Return status
  */
  int updateInformation( const OsiSolverInterface * solver, const OsiBranchingInformation * info,
			 OsiChooseVariable * choose);
  /// Original objective value
  inline double originalObjectiveValue() const
  { return originalObjectiveValue_;}
  /// Up change  - invalid if n-way
  inline double upChange() const
  { assert (branchingObject_->numberBranches()==2); return changes_[1];}
  /// Down change  - invalid if n-way
  inline double downChange() const
  { assert (branchingObject_->numberBranches()==2); return changes_[0];}
  /// Set up change  - invalid if n-way
  inline void setUpChange(double value)
  { assert (branchingObject_->numberBranches()==2); changes_[1] = value;}
  /// Set down change  - invalid if n-way
  inline void setDownChange(double value)
  { assert (branchingObject_->numberBranches()==2); changes_[0] = value;}
  /// Change on way k
  inline double change(int k) const
  { return changes_[k];}

  /// Up iteration count  - invalid if n-way
  inline int upIterationCount() const
  { assert (branchingObject_->numberBranches()==2); return iterationCounts_[1];}
  /// Down iteration count  - invalid if n-way
  inline int downIterationCount() const
  { assert (branchingObject_->numberBranches()==2); return iterationCounts_[0];}
  /// Iteration count on way k
  inline int iterationCount(int k) const
  { return iterationCounts_[k];}

  /// Up status  - invalid if n-way
  inline int upStatus() const
  { assert (branchingObject_->numberBranches()==2); return statuses_[1];}
  /// Down status  - invalid if n-way
  inline int downStatus() const
  { assert (branchingObject_->numberBranches()==2); return statuses_[0];}
  /// Set up status  - invalid if n-way
  inline void setUpStatus(int value)
  { assert (branchingObject_->numberBranches()==2); statuses_[1] = value;}
  /// Set down status  - invalid if n-way
  inline void setDownStatus(int value)
  { assert (branchingObject_->numberBranches()==2); statuses_[0] = value;}
  /// Status on way k
  inline int status(int k) const
  { return statuses_[k];}
  /// Branching object
  inline OsiBranchingObject * branchingObject() const
  { return branchingObject_;}
  inline int whichObject() const
  { return whichObject_;}

protected:
  // Data
  /// Original objective value
  double originalObjectiveValue_;
    /// Objective changes
  double * changes_;
  /// Iteration counts
  int * iterationCounts_;
  /** Status
      -1 - not done
      0 - feasible and finished
      1 -  infeasible
      2 - not finished
  */
  int * statuses_;
  /// Branching object
  OsiBranchingObject * branchingObject_;
  /// Which object on list
  int whichObject_;
};


#endif