| 1 | /* $Id: CoinWarmStartPrimalDual.hpp 1372 2011-01-03 23:31:00Z lou $ */ |
| 2 | // Copyright (C) 2000, International Business Machines |
| 3 | // Corporation and others. All Rights Reserved. |
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 5 | |
| 6 | #ifndef CoinWarmStartPrimalDual_H |
| 7 | #define CoinWarmStartPrimalDual_H |
| 8 | |
| 9 | #include "CoinHelperFunctions.hpp" |
| 10 | #include "CoinWarmStart.hpp" |
| 11 | #include "CoinWarmStartVector.hpp" |
| 12 | |
| 13 | |
| 14 | //############################################################################# |
| 15 | |
| 16 | /** WarmStart information that is only a dual vector */ |
| 17 | |
| 18 | class CoinWarmStartPrimalDual : public virtual CoinWarmStart { |
| 19 | public: |
| 20 | /// return the size of the dual vector |
| 21 | inline int dualSize() const { return dual_.size(); } |
| 22 | /// return a pointer to the array of duals |
| 23 | inline const double * dual() const { return dual_.values(); } |
| 24 | |
| 25 | /// return the size of the primal vector |
| 26 | inline int primalSize() const { return primal_.size(); } |
| 27 | /// return a pointer to the array of primals |
| 28 | inline const double * primal() const { return primal_.values(); } |
| 29 | |
| 30 | /** Assign the primal/dual vectors to be the warmstart information. In this |
| 31 | method the object assumes ownership of the pointers and upon return \c |
| 32 | primal and \c dual will be a NULL pointers. If copying is desirable use |
| 33 | the constructor. |
| 34 | |
| 35 | NOTE: \c primal and \c dual must have been allocated by new double[], |
| 36 | because they will be freed by delete[] upon the desructtion of this |
| 37 | object... |
| 38 | */ |
| 39 | void assign(int primalSize, int dualSize, double*& primal, double *& dual) { |
| 40 | primal_.assignVector(primalSize, primal); |
| 41 | dual_.assignVector(dualSize, dual); |
| 42 | } |
| 43 | |
| 44 | CoinWarmStartPrimalDual() : primal_(), dual_() {} |
| 45 | |
| 46 | CoinWarmStartPrimalDual(int primalSize, int dualSize, |
| 47 | const double* primal, const double * dual) : |
| 48 | primal_(primalSize, primal), dual_(dualSize, dual) {} |
| 49 | |
| 50 | CoinWarmStartPrimalDual(const CoinWarmStartPrimalDual& rhs) : |
| 51 | primal_(rhs.primal_), dual_(rhs.dual_) {} |
| 52 | |
| 53 | CoinWarmStartPrimalDual& operator=(const CoinWarmStartPrimalDual& rhs) { |
| 54 | if (this != &rhs) { |
| 55 | primal_ = rhs.primal_; |
| 56 | dual_ = rhs.dual_; |
| 57 | } |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | /*! \brief Clear the data |
| 62 | |
| 63 | Make it appear as if the warmstart was just created using the default |
| 64 | constructor. |
| 65 | */ |
| 66 | inline void clear() { |
| 67 | primal_.clear(); |
| 68 | dual_.clear(); |
| 69 | } |
| 70 | |
| 71 | inline void swap(CoinWarmStartPrimalDual& rhs) { |
| 72 | if (this != &rhs) { |
| 73 | primal_.swap(rhs.primal_); |
| 74 | dual_.swap(rhs.dual_); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** `Virtual constructor' */ |
| 79 | virtual CoinWarmStart *clone() const override { |
| 80 | return new CoinWarmStartPrimalDual(*this); |
| 81 | } |
| 82 | |
| 83 | virtual ~CoinWarmStartPrimalDual() {} |
| 84 | |
| 85 | /*! \name PrimalDual warm start `diff' methods */ |
| 86 | //@{ |
| 87 | |
| 88 | /*! \brief Generate a `diff' that can convert the warm start passed as a |
| 89 | parameter to the warm start specified by \c this. |
| 90 | |
| 91 | The capabilities are limited: the basis passed as a parameter can be no |
| 92 | larger than the basis pointed to by \c this. |
| 93 | */ |
| 94 | |
| 95 | virtual CoinWarmStartDiff* |
| 96 | generateDiff (const CoinWarmStart *const oldCWS) const override ; |
| 97 | |
| 98 | /*! \brief Apply \p diff to this warm start. |
| 99 | |
| 100 | Update this warm start by applying \p diff. It's assumed that the |
| 101 | allocated capacity of the warm start is sufficiently large. |
| 102 | */ |
| 103 | |
| 104 | virtual void applyDiff (const CoinWarmStartDiff *const cwsdDiff) override ; |
| 105 | |
| 106 | //@} |
| 107 | |
| 108 | #if 0 |
| 109 | protected: |
| 110 | inline const CoinWarmStartVector<double>& primalWarmStartVector() const |
| 111 | { return primal_; } |
| 112 | inline const CoinWarmStartVector<double>& dualWarmStartVector() const |
| 113 | { return dual_; } |
| 114 | #endif |
| 115 | |
| 116 | private: |
| 117 | ///@name Private data members |
| 118 | //@{ |
| 119 | CoinWarmStartVector<double> primal_; |
| 120 | CoinWarmStartVector<double> dual_; |
| 121 | //@} |
| 122 | }; |
| 123 | |
| 124 | //############################################################################# |
| 125 | |
| 126 | /*! \class CoinWarmStartPrimalDualDiff |
| 127 | \brief A `diff' between two CoinWarmStartPrimalDual objects |
| 128 | |
| 129 | This class exists in order to hide from the world the details of calculating |
| 130 | and representing a `diff' between two CoinWarmStartPrimalDual objects. For |
| 131 | convenience, assignment, cloning, and deletion are visible to the world, and |
| 132 | default and copy constructors are made available to derived classes. |
| 133 | Knowledge of the rest of this structure, and of generating and applying |
| 134 | diffs, is restricted to the friend functions |
| 135 | CoinWarmStartPrimalDual::generateDiff() and |
| 136 | CoinWarmStartPrimalDual::applyDiff(). |
| 137 | |
| 138 | The actual data structure is a pair of vectors, #diffNdxs_ and #diffVals_. |
| 139 | |
| 140 | */ |
| 141 | |
| 142 | class CoinWarmStartPrimalDualDiff : public virtual CoinWarmStartDiff |
| 143 | { |
| 144 | friend CoinWarmStartDiff* |
| 145 | CoinWarmStartPrimalDual::generateDiff(const CoinWarmStart *const oldCWS) const; |
| 146 | friend void |
| 147 | CoinWarmStartPrimalDual::applyDiff(const CoinWarmStartDiff *const diff) ; |
| 148 | |
| 149 | public: |
| 150 | |
| 151 | /*! \brief `Virtual constructor'. To be used when retaining polymorphism is |
| 152 | important */ |
| 153 | virtual CoinWarmStartDiff *clone() const override |
| 154 | { |
| 155 | return new CoinWarmStartPrimalDualDiff(*this); |
| 156 | } |
| 157 | |
| 158 | /*! \brief Destructor */ |
| 159 | virtual ~CoinWarmStartPrimalDualDiff() {} |
| 160 | |
| 161 | protected: |
| 162 | |
| 163 | /*! \brief Default constructor |
| 164 | |
| 165 | This is protected (rather than private) so that derived classes can |
| 166 | see it when they make <i>their</i> default constructor protected or |
| 167 | private. |
| 168 | */ |
| 169 | CoinWarmStartPrimalDualDiff () : primalDiff_(), dualDiff_() {} |
| 170 | |
| 171 | /*! \brief Copy constructor |
| 172 | |
| 173 | For convenience when copying objects containing |
| 174 | CoinWarmStartPrimalDualDiff objects. But consider whether you should be |
| 175 | using #clone() to retain polymorphism. |
| 176 | |
| 177 | This is protected (rather than private) so that derived classes can |
| 178 | see it when the make <i>their</i> copy constructor protected or |
| 179 | private. |
| 180 | */ |
| 181 | CoinWarmStartPrimalDualDiff (const CoinWarmStartPrimalDualDiff &rhs) : |
| 182 | primalDiff_(rhs.primalDiff_), dualDiff_(rhs.dualDiff_) {} |
| 183 | |
| 184 | /*! \brief Clear the data |
| 185 | |
| 186 | Make it appear as if the diff was just created using the default |
| 187 | constructor. |
| 188 | */ |
| 189 | inline void clear() { |
| 190 | primalDiff_.clear(); |
| 191 | dualDiff_.clear(); |
| 192 | } |
| 193 | |
| 194 | inline void swap(CoinWarmStartPrimalDualDiff& rhs) { |
| 195 | if (this != &rhs) { |
| 196 | primalDiff_.swap(rhs.primalDiff_); |
| 197 | dualDiff_.swap(rhs.dualDiff_); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | |
| 203 | /*! |
| 204 | \brief These two differences describe the differences in the primal and |
| 205 | in the dual vector. |
| 206 | */ |
| 207 | CoinWarmStartVectorDiff<double> primalDiff_; |
| 208 | CoinWarmStartVectorDiff<double> dualDiff_; |
| 209 | } ; |
| 210 | |
| 211 | #endif |
| 212 | |