source: trunk/Cbc/src/CbcCutGenerator.hpp @ 1211

Last change on this file since 1211 was 1211, checked in by forrest, 10 years ago

changes for heuristic clone

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.8 KB
Line 
1/* $Id: CbcCutGenerator.hpp 1211 2009-08-08 15:59:19Z forrest $ */
2// Copyright (C) 2003, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#ifndef CbcCutGenerator_H
5#define CbcCutGenerator_H
6
7#include "OsiSolverInterface.hpp"
8#include "OsiCuts.hpp"
9#include "CglCutGenerator.hpp"
10
11class CbcModel;
12class OsiRowCut;
13class OsiRowCutDebugger;
14
15//#############################################################################
16
17/** Interface between Cbc and Cut Generation Library.
18
19  \c CbcCutGenerator is intended to provide an intelligent interface between
20  Cbc and the cutting plane algorithms in the CGL. A \c CbcCutGenerator is
21  bound to a \c CglCutGenerator and to an \c CbcModel. It contains parameters
22  which control when and how the \c generateCuts method of the
23  \c CglCutGenerator will be called.
24
25  The builtin decision criteria available to use when deciding whether to
26  generate cuts are limited: every <i>X</i> nodes, when a solution is found,
27  and when a subproblem is found to be infeasible. The idea is that the class
28  will grow more intelligent with time.
29
30  \todo Add a pointer to function member which will allow a client to install
31        their own decision algorithm to decide whether or not to call the CGL
32        \p generateCuts method. Create a default decision method that looks
33        at the builtin criteria.
34
35  \todo It strikes me as not good that generateCuts contains code specific to
36        individual CGL algorithms. Another set of pointer to function members,
37        so that the client can specify the cut generation method as well as
38        pre- and post-generation methods? Taken a bit further, should this
39        class contain a bunch of pointer to function members, one for each
40        of the places where the cut generator might be referenced?
41        Initialization, root node, search tree node, discovery of solution,
42        and termination all come to mind. Initialization and termination would
43        also be useful for instrumenting cbc.
44*/
45
46class CbcCutGenerator  {
47 
48public:
49   
50  /** \name Generate Cuts */
51  //@{
52  /** Generate cuts for the client model.
53
54    Evaluate the state of the client model and decide whether to generate cuts.
55    The generated cuts are inserted into and returned in the collection of cuts
56    \p cs.
57
58    If \p fullScan is >0, the generator is obliged to call the CGL
59    \c generateCuts routine.  Otherwise, it is free to make a local decision.
60    The current implementation uses \c whenCutGenerator_ to decide.
61
62    The routine returns true if reoptimisation is needed (because the state of
63    the solver interface has been modified).
64
65    If node then can find out depth
66  */
67  bool generateCuts( OsiCuts &cs, int fullScan,OsiSolverInterface * solver,
68                     CbcNode * node); 
69  //@}
70
71   
72  /**@name Constructors and destructors */
73  //@{
74  /// Default constructor
75  CbcCutGenerator (); 
76
77  /// Normal constructor
78  CbcCutGenerator(CbcModel * model,CglCutGenerator * generator,
79                  int howOften=1, const char * name=NULL,
80                  bool normal=true, bool atSolution=false, 
81                  bool infeasible=false,int howOftenInsub=-100,
82                  int whatDepth=-1, int whatDepthInSub=-1,int switchOffIfLessThan=0);
83 
84  /// Copy constructor
85  CbcCutGenerator (const CbcCutGenerator &);
86
87  /// Assignment operator
88  CbcCutGenerator & operator=(const CbcCutGenerator& rhs);
89
90  /// Destructor
91  ~CbcCutGenerator ();
92  //@}
93
94  /**@name Gets and sets */
95  //@{
96  /** Set the client model.
97 
98    In addition to setting the client model, refreshModel also calls
99    the \c refreshSolver method of the CglCutGenerator object.
100  */
101  void refreshModel(CbcModel * model);
102
103  /// return name of generator
104  inline const char * cutGeneratorName() const
105  { return generatorName_;}
106
107  /** Set the cut generation interval
108
109    Set the number of nodes evaluated between calls to the Cgl object's
110    \p generateCuts routine.
111
112    If \p value is positive, cuts will always be generated at the specified
113    interval.
114    If \p value is negative, cuts will initially be generated at the specified
115    interval, but Cbc may adjust the value depending on the success of cuts
116    produced by this generator.
117
118    A value of -100 disables the generator, while a value of -99 means
119    just at root.
120  */
121  void setHowOften(int value) ;
122
123  /// Get the cut generation interval.
124  inline int howOften() const
125  { return whenCutGenerator_;}
126  /// Get the cut generation interval.in sub tree
127  inline int howOftenInSub() const
128  { return whenCutGeneratorInSub_;}
129  /// Get level of cut inaccuracy (0 means exact e.g. cliques)
130  inline int inaccuracy() const
131  { return inaccuracy_;}
132  /// Set level of cut inaccuracy (0 means exact e.g. cliques)
133  inline void setInaccuracy(int level)
134  { inaccuracy_=level;}
135
136  /** Set the cut generation depth
137
138    Set the depth criterion for calls to the Cgl object's
139    \p generateCuts routine.  Only active if > 0.
140
141    If whenCutGenerator is positive and this is positive then this overrides. 
142    If whenCutGenerator is -1 then this is used as criterion if any cuts
143    were generated at root node.
144    If whenCutGenerator is anything else this is ignored.
145  */
146  void setWhatDepth(int value) ;
147  /// Set the cut generation depth in sub tree
148  void setWhatDepthInSub(int value) ;
149  /// Get the cut generation depth criterion.
150  inline int whatDepth() const
151  { return depthCutGenerator_;}
152  /// Get the cut generation depth criterion.in sub tree
153  inline int whatDepthInSub() const
154  { return depthCutGeneratorInSub_;}
155
156  /// Get whether the cut generator should be called in the normal place
157  inline bool normal() const
158  { return (switches_&1)!=0;}
159  /// Set whether the cut generator should be called in the normal place
160  inline void setNormal(bool value) 
161  { switches_&=~1;switches_ |= value ? 1 : 0;}
162  /// Get whether the cut generator should be called when a solution is found
163  inline bool atSolution() const
164  { return (switches_&2)!=0;}
165  /// Set whether the cut generator should be called when a solution is found
166  inline void setAtSolution(bool value) 
167  { switches_&=~2;switches_ |= value ? 2 : 0;}
168  /** Get whether the cut generator should be called when the subproblem is
169      found to be infeasible.
170  */
171  inline bool whenInfeasible() const
172  { return (switches_&4)!=0;}
173  /** Set whether the cut generator should be called when the subproblem is
174      found to be infeasible.
175  */
176  inline void setWhenInfeasible(bool value) 
177  { switches_&=~4;switches_ |= value ? 4 : 0;}
178  /// Get whether the cut generator is being timed
179  inline bool timing() const
180  { return (switches_&64)!=0;}
181  /// Set whether the cut generator is being timed
182  inline void setTiming(bool value) 
183  { switches_&=~64;switches_ |= value ? 64 : 0; timeInCutGenerator_=0.0;}
184  /// Return time taken in cut generator
185  inline double timeInCutGenerator() const
186  { return timeInCutGenerator_;}
187  inline void incrementTimeInCutGenerator(double value)
188  { timeInCutGenerator_ += value;}
189  /// Get the \c CglCutGenerator corresponding to this \c CbcCutGenerator.
190  inline CglCutGenerator * generator() const
191  { return generator_;}
192  /// Number times cut generator entered
193  inline int numberTimesEntered() const
194  { return numberTimes_;}
195  inline void setNumberTimesEntered(int value)
196  { numberTimes_ = value;}
197  inline void incrementNumberTimesEntered(int value=1)
198  { numberTimes_ += value;}
199  /// Total number of cuts added
200  inline int numberCutsInTotal() const
201  { return numberCuts_;}
202  inline void setNumberCutsInTotal(int value)
203  { numberCuts_ = value;}
204  inline void incrementNumberCutsInTotal(int value=1)
205  { numberCuts_ += value;}
206  /// Total number of column cuts
207  inline int numberColumnCuts() const
208  { return numberColumnCuts_;}
209  inline void setNumberColumnCuts(int value)
210  { numberColumnCuts_ = value;}
211  inline void incrementNumberColumnCuts(int value=1)
212  { numberColumnCuts_ += value;}
213  /// Total number of cuts active after (at end of n cut passes at each node)
214  inline int numberCutsActive() const
215  { return numberCutsActive_;}
216  inline void setNumberCutsActive(int value)
217  { numberCutsActive_ = value;}
218  inline void incrementNumberCutsActive(int value=1)
219  { numberCutsActive_ += value;}
220  inline void setSwitchOffIfLessThan(int value) 
221  { switchOffIfLessThan_ = value;}
222  inline int switchOffIfLessThan() const
223  { return switchOffIfLessThan_;}
224  /// Say if optimal basis needed
225  inline bool needsOptimalBasis() const
226  { return (switches_&128)!=0;}
227  /// Set if optimal basis needed
228  inline void setNeedsOptimalBasis(bool yesNo)
229  { switches_&=~128;switches_ |= yesNo ? 128 : 0;}
230  /// Whether generator MUST be called again if any cuts (i.e. ignore break from loop)
231  inline bool mustCallAgain() const
232  { return (switches_&8)!=0;}
233  /// Set whether generator MUST be called again if any cuts (i.e. ignore break from loop)
234  inline void setMustCallAgain(bool yesNo)
235  { switches_&=~8;switches_ |= yesNo ? 8 : 0;}
236  /// Whether generator switched off for moment
237  inline bool switchedOff() const
238  { return (switches_&16)!=0;}
239  /// Set whether generator switched off for moment
240  inline void setSwitchedOff(bool yesNo)
241  { switches_&=~16;switches_ |= yesNo ? 16 : 0;}
242  /// Whether last round of cuts did little
243  inline bool ineffectualCuts() const
244  { return (switches_&512)!=0;}
245  /// Set whether last round of cuts did little
246  inline void setIneffectualCuts(bool yesNo)
247  { switches_&=~512;switches_ |= yesNo ? 512 : 0;}
248  /// Number of cuts generated at root
249  inline int numberCutsAtRoot() const
250  { return numberCutsAtRoot_;}
251  inline void setNumberCutsAtRoot(int value)
252  { numberCutsAtRoot_ = value;}
253  /// Number of cuts active at root
254  inline int numberActiveCutsAtRoot() const
255  { return numberActiveCutsAtRoot_;}
256  inline void setNumberActiveCutsAtRoot(int value)
257  { numberActiveCutsAtRoot_ = value;}
258  /// Number of short cuts at root
259  inline int numberShortCutsAtRoot() const
260  { return numberShortCutsAtRoot_;}
261  /// Set model
262  inline void setModel(CbcModel * model)
263  { model_ = model;}
264  //@}
265 
266private:
267  /**@name Private gets and sets */
268  //@{
269  /// Whether global cuts at root
270  inline bool globalCutsAtRoot() const
271  { return (switches_&32)!=0;}
272  /// Set whether global cuts at root
273  inline void setGlobalCutsAtRoot(bool yesNo)
274  { switches_&=~32;switches_ |= yesNo ? 32 : 0;}
275  /// Whether global cuts
276  inline bool globalCuts() const
277  { return (switches_&256)!=0;}
278  /// Set whether global cuts
279  inline void setGlobalCuts(bool yesNo)
280  { switches_&=~256;switches_ |= yesNo ? 256 : 0;}
281  //@}
282  /// Saved cuts
283  OsiCuts savedCuts_;
284  /// Time in cut generator
285  double timeInCutGenerator_;
286  /// The client model
287  CbcModel *model_; 
288
289  // The CglCutGenerator object
290  CglCutGenerator * generator_;
291
292  /// Name of generator
293  char * generatorName_;
294
295  /** Number of nodes between calls to the CglCutGenerator::generateCuts
296     routine.
297  */
298  int whenCutGenerator_;
299  /** Number of nodes between calls to the CglCutGenerator::generateCuts
300     routine in sub tree.
301  */
302  int whenCutGeneratorInSub_;
303  /** If first pass at root produces fewer than this cuts then switch off
304   */
305  int switchOffIfLessThan_;
306
307  /** Depth at which to call the CglCutGenerator::generateCuts
308     routine (If >0 then overrides when and is called if depth%depthCutGenerator==0).
309  */
310  int depthCutGenerator_;
311
312  /** Depth at which to call the CglCutGenerator::generateCuts
313     routine (If >0 then overrides when and is called if depth%depthCutGenerator==0).
314     In sub tree.
315  */
316  int depthCutGeneratorInSub_;
317
318  /// Level of cut inaccuracy (0 means exact e.g. cliques)
319  int inaccuracy_;
320  /// Number times cut generator entered
321  int numberTimes_;
322  /// Total number of cuts added
323  int numberCuts_;
324  /// Total number of column cuts added
325  int numberColumnCuts_;
326  /// Total number of cuts active after (at end of n cut passes at each node)
327  int numberCutsActive_;
328  /// Number of cuts generated at root
329  int numberCutsAtRoot_;
330  /// Number of cuts active at root
331  int numberActiveCutsAtRoot_;
332  /// Number of short cuts at root
333  int numberShortCutsAtRoot_;
334  /// Switches - see gets and sets
335  int switches_;
336};
337/** Abstract cut modifier base class
338
339    In exotic circumstances - cuts may need to be modified
340    a) strengthened - changed
341    b) weakened - changed
342    c) deleted - set to NULL
343    d) unchanged
344*/
345
346class CbcCutModifier {
347public:
348  /// Default Constructor
349  CbcCutModifier ();
350
351  // Copy constructor
352  CbcCutModifier ( const CbcCutModifier &);
353   
354  /// Destructor
355  virtual ~CbcCutModifier();
356
357  /// Assignment
358  CbcCutModifier & operator=(const CbcCutModifier& rhs);
359 /// Clone
360  virtual CbcCutModifier * clone() const = 0;
361
362  /** Returns
363      0 unchanged
364      1 strengthened
365      2 weakened
366      3 deleted
367  */
368  virtual int modify(const OsiSolverInterface * solver, OsiRowCut & cut) =0;
369  /// Create C++ lines to get to current state
370  virtual void generateCpp( FILE * ) {}
371protected:
372 
373};
374
375/** Simple cut modifier base class
376
377    In exotic circumstances - cuts may need to be modified
378    a) strengthened - changed
379    b) weakened - changed
380    c) deleted - set to NULL
381    d) unchanged
382
383    initially get rid of cuts with variables >= k
384    could weaken
385*/
386
387class CbcCutSubsetModifier  : public CbcCutModifier {
388public:
389  /// Default Constructor
390  CbcCutSubsetModifier ();
391
392  /// Useful Constructor
393  CbcCutSubsetModifier (int firstOdd);
394
395  // Copy constructor
396  CbcCutSubsetModifier ( const CbcCutSubsetModifier &);
397   
398  /// Destructor
399  virtual ~CbcCutSubsetModifier();
400
401  /// Assignment
402  CbcCutSubsetModifier & operator=(const CbcCutSubsetModifier& rhs);
403 /// Clone
404  virtual CbcCutModifier * clone() const ;
405
406  /** Returns
407      0 unchanged
408      1 strengthened
409      2 weakened
410      3 deleted
411  */
412  virtual int modify(const OsiSolverInterface * solver, OsiRowCut & cut) ;
413  /// Create C++ lines to get to current state
414  virtual void generateCpp( FILE * ) {}
415protected:
416  /// data
417  /// First odd variable
418  int firstOdd_;
419};
420// How often to do if mostly switched off (A)
421# define SCANCUTS 1000 
422// How often to do if mostly switched off (probing B)
423# define SCANCUTS_PROBING 1000 
424
425#endif
Note: See TracBrowser for help on using the repository browser.