CbcFathomDynamicProgramming.hpp 5.37 KB
Newer Older
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
/* $Id$ */
// Copyright (C) 2004, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CbcFathomDynamicProgramming_H
#define CbcFathomDynamicProgramming_H

#include "CbcFathom.hpp"

//#############################################################################
/** FathomDynamicProgramming class.

    The idea is that after some branching the problem will be effectively smaller than
    the original problem and maybe there will be a more specialized technique which can completely
    fathom this branch quickly.

    This is a dynamic programming implementation which is very fast for some
    specialized problems.  It expects small integral rhs, an all integer problem
    and positive integral coefficients. At present it can not do general set covering
    problems just set partitioning.  It can find multiple optima for various rhs
    combinations.

    The main limiting factor is size of state space.  Each 1 rhs doubles the size of the problem.
    2 or 3 rhs quadruples, 4,5,6,7 by 8 etc.
 */

class CbcFathomDynamicProgramming : public CbcFathom {
public:
  // Default Constructor
  CbcFathomDynamicProgramming();

  // Constructor with model - assumed before cuts
  CbcFathomDynamicProgramming(CbcModel &model);
  // Copy constructor
  CbcFathomDynamicProgramming(const CbcFathomDynamicProgramming &rhs);

  virtual ~CbcFathomDynamicProgramming();

  /// update model (This is needed if cliques update matrix etc)
  virtual void setModel(CbcModel *model);

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

  /// Resets stuff if model changes
  virtual void resetModel(CbcModel *model);

  /** returns 0 if no fathoming attempted, 1 fully fathomed ,
        2 incomplete search, 3 incomplete search but treat as complete.
        If solution then newSolution will not be NULL and
        will be freed by CbcModel.  It is expected that the solution is better
        than best so far but CbcModel will double check.

        If returns 3 then of course there is no guarantee of global optimum
    */
  virtual int fathom(double *&newSolution);

  /// Maximum size allowed
  inline int maximumSize() const
  {
    return maximumSizeAllowed_;
  }
  inline void setMaximumSize(int value)
  {
    maximumSizeAllowed_ = value;
  }
  /// Returns type of algorithm and sets up arrays
  int checkPossible(int allowableSize = 0);
  // set algorithm
  inline void setAlgorithm(int value)
  {
    algorithm_ = value;
  }
  /** Tries a column
        returns true if was used in making any changes.
    */
  bool tryColumn(int numberElements, const int *rows,
    const double *coefficients, double cost,
    int upper = COIN_INT_MAX);
  /// Returns cost array
  inline const double *cost() const
  {
    return cost_;
  }
  /// Returns back array
  inline const int *back() const
  {
    return back_;
  }
  /// Gets bit pattern for target result
  inline int target() const
  {
    return target_;
  }
  /// Sets bit pattern for target result
  inline void setTarget(int value)
  {
    target_ = value;
  }

private:
  /// Does deleteions
  void gutsOfDelete();

  /** Adds one attempt of one column of type 0,
        returns true if was used in making any changes
    */
  bool addOneColumn0(int numberElements, const int *rows,
    double cost);
  /** Adds one attempt of one column of type 1,
        returns true if was used in making any changes.
        At present the user has to call it once for each possible value
    */
  bool addOneColumn1(int numberElements, const int *rows,
    const int *coefficients, double cost);
  /** Adds one attempt of one column of type 1,
        returns true if was used in making any changes.
        At present the user has to call it once for each possible value.
        This version is when there are enough 1 rhs to do faster
    */
  bool addOneColumn1A(int numberElements, const int *rows,
    const int *coefficients, double cost);
  /// Gets bit pattern from original column
  int bitPattern(int numberElements, const int *rows,
    const int *coefficients);
  /// Gets bit pattern from original column
  int bitPattern(int numberElements, const int *rows,
    const double *coefficients);
  /// Fills in original column (dense) from bit pattern - returning number nonzero
  int decodeBitPattern(int bitPattern, int *values, int numberRows);

protected:
  /// Size of states (power of 2 unless just one constraint)
  int size_;
  /** Type - 0 coefficients and rhs all 1,
        1 - coefficients > 1 or rhs > 1
    */
  int type_;
  /// Space for states
  double *cost_;
  /// Which state produced this cheapest one
  int *back_;
  /// Some rows may be satisified so we need a lookup
  int *lookup_;
  /// Space for sorted indices
  int *indices_;
  /// Number of active rows
  int numberActive_;
  /// Maximum size allowed
  int maximumSizeAllowed_;
  /// Start bit for each active row
  int *startBit_;
  /// Number bits for each active row
  int *numberBits_;
  /// Effective rhs
  int *rhs_;
  /// Space for sorted coefficients
  int *coefficients_;
  /// Target pattern
  int target_;
  /// Number of Non 1 rhs
  int numberNonOne_;
  /// Current bit pattern
  int bitPattern_;
  /// Current algorithm
  int algorithm_;

private:
  /// Illegal Assignment operator
  CbcFathomDynamicProgramming &operator=(const CbcFathomDynamicProgramming &rhs);
};

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/