coin-Bcp
CSP.hpp
Go to the documentation of this file.
1 // Copyright (C) 2005, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #ifndef CSP_H
4 #define CSP_H
5 
6 #include <iostream>
7 #include <vector>
8 #include <map>
9 #include <string>
10 #include <algorithm>
11 #include <numeric>
12 
13 #include <CoinHelperFunctions.hpp>
14 
15 #include "BCP_buffer.hpp"
16 
17 class PATTERN;
18 class CSPROBLEM;
19 
20 struct DELETEOBJECT {
21 template<typename T>
22  void operator() (const T*& ptr){ delete ptr; ptr = NULL; }
23 template<typename T>
24  void operator() (T*& ptr){ delete ptr; ptr = NULL; }
25 };
26 
28 
29 private:
30  int size_;
31  int * indices_;
32  double * elements_;
33 
34 
35 public:
36  // constructor: we ALWAYS copy
37  CSP_packedVector(const int size,
38  const int* indices,
39  const double* elements){
40  size_=size;
41  indices_=new int[size_];
42  elements_=new double[size_];
43  CoinDisjointCopyN(indices, size_, indices_);
44  CoinDisjointCopyN(elements, size_, elements_);
45  }
46 
48 
49  // constructor from a buffer
50 
52  buf.unpack(indices_, size_);
53  buf.unpack(elements_, size_);
54  }
55 
56  void pack(BCP_buffer& buf) const {
57  buf.pack(indices_, size_);
58  buf.pack(elements_, size_);
59  }
60 
61  void unpack(BCP_buffer& buf){
62  delete [] indices_;
63  delete [] elements_;
64  buf.unpack(indices_, size_);
65  buf.unpack(elements_, size_);
66  }
67 
68 
69  const int* getIndices()const {
70  return indices_;
71  }
72 
73  const double* getElements() const {
74  return elements_;
75  }
76 
77  const int getSize() const {
78  return size_;
79  }
80 
81  // disable copy constructor
83  indices_=new int[size_];
84  elements_=new double[size_];
87  }
88 
89 
90 
91 
92 
93 private:
94 
95 
96  // disable assignmnet operator
98 };
99 
100 
101 //#############################################################################
102 // Class CSPROBLEM contains all the data
103 // for an instance of the cutting stock problem
104 
105 class CSPROBLEM {
106 
107 public:
108 
109  inline int getL() const { return l_; }
110  inline const int* getDemand() const { return demand_; }
111  inline const int* getW() const { return w_; }
112  inline int getM() const { return m_; }
113  inline int getS() const { return s_; }
114 
115  inline bool doesCombineExclusionConstraints() const {
117  }
118  inline bool doesAddKnapsackMirConstraints() const {
120  }
121  inline bool doesAddKnifeMirConstraints() const {
122  return addKnifeMirConstraints;
123  }
124 
125  inline void setCombineExclusionConstraints(char yesno) {
127  }
128  inline void setAddKnapsackMirConstraints(char yesno) {
130  }
131  inline void setAddKnifeMirConstraints(char yesno) {
132  addKnifeMirConstraints = yesno;
133  }
134 
135  CSPROBLEM();
136  CSPROBLEM(std::istream &inputStream);
138 
139  void pack(BCP_buffer& buf) const {
140  buf.pack(l_);
141  buf.pack(demand_, m_);
142  buf.pack(w_, m_);
143  buf.pack(s_);
147  }
148 
149  void unpack(BCP_buffer& buf){
150  buf.unpack(l_);
151  buf.unpack(demand_, m_);
152  buf.unpack(w_, m_);
153  buf.unpack(s_);
157  }
158 
159 private:
160  int l_ ; // stock roll length
161  int* demand_; // quanty of demanded widths
162  int* w_; // demanded widths
163  int m_; // number of demanded widths
164  int s_; // number of knives ("stilletto")
165  double start_time_;
166 
170 
171  // upper and lower bounds on patterns (cols)
172  // is handled by BCP variable classes
173 };
174 
175 //#############################################################################
176 
177 class PATTERN {
178 private:
179  // One single, shared, CSPROBLEM for all instances of PATTERN
180  const static CSPROBLEM* csproblem_;
181 
182  // The cost of the pattern, e.g. 1 for our flavor of the CSP.
183  double cost_;
184 
185  // We may want to store ubs in the future
186  // but right now, we'll always calculate
187 
188  // Need to know how many of each width to cut for this pattern
189  // the integer vector A^j*=(a^j*_1,....a^j*_m)
191 
192  // at most how many of this pattern is needed
193  double ub_;
194 
195 public:
196 
197  // CSP_VECTOR's pack method returned a BCP_buffer &. Should this too?
198  void pack(BCP_buffer& buf) const {
199  buf.pack(cost_).pack(ub_);
200  widths_.pack(buf);
201  // no need to "return" because this returns a void
202  }
203 
204  // this constructor is essentially an unpack method for the pattern
206  {
207  // need a routine that unpacks an OsiShallowPackedVector (?)
208  // Q: How do I know how long the size is?
209  // A: It's known by the BCP_buffer method that unpacks a C-style array.
210  // See ~/COIN/Bcp/include/BCP_buffer.hpp
211 
212  // unpack the buffers into these variables
213  buf.unpack(cost_).unpack(ub_);
214  widths_.unpack(buf);
215  }
216 
217  // destructor needs to delete the storage for the ShallowPackedVector
218  virtual ~PATTERN(){
219  }
220 
221  // may or maynot need these...
222  inline double getCost () const { return cost_; }
223  inline double getUb () const { return ub_; }
224 
225  const CSP_packedVector& getWidths() const{
226  return widths_;
227  }
228 
229  // return d_js based on current pis
230  double dj(const double* pi) const;
231 
232  // default constructor
233  PATTERN() : cost_(1), widths_(){
234  }
235 
236 
237  // constructor
238  // Note: nonzero element values are permitted
239  // but shouldn't be allowed.
240  // we are going to assume in the vars-to-cols method that
241  // the widths_ elements are stricly positive.
242  // This is for efficiency.
243  PATTERN(const int numElements, const int* indices, const double * elements,
244  const double ub) :
245  cost_(1), widths_(numElements, indices, elements), ub_(ub) {}
246 
247  // copy construct - private for now.
248  PATTERN(const PATTERN& x) :
249  cost_(x.cost_), widths_(x.widths_), ub_(x.ub_) {}
250 
251 
252 
253 
254 };
255 
256 #endif
257 
int * w_
Definition: CSP.hpp:162
double getUb() const
Definition: CSP.hpp:223
double ub_
Definition: CSP.hpp:193
Definition: CSP.hpp:177
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
const int * getDemand() const
Definition: CSP.hpp:110
void setAddKnapsackMirConstraints(char yesno)
Definition: CSP.hpp:128
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
const int * getW() const
Definition: CSP.hpp:111
int l_
Definition: CSP.hpp:160
CSP_packedVector(const int size, const int *indices, const double *elements)
Definition: CSP.hpp:37
double start_time_
Definition: CSP.hpp:165
void unpack(BCP_buffer &buf)
Definition: CSP.hpp:61
int m_
Definition: CSP.hpp:163
virtual ~PATTERN()
Definition: CSP.hpp:218
const CSP_packedVector & getWidths() const
Definition: CSP.hpp:225
PATTERN(BCP_buffer &buf)
Definition: CSP.hpp:205
const int * getIndices() const
Definition: CSP.hpp:69
CSP_packedVector()
Definition: CSP.hpp:47
PATTERN()
Definition: CSP.hpp:233
CSP_packedVector & operator=(const CSP_packedVector &pv)
void pack(BCP_buffer &buf) const
Definition: CSP.hpp:139
void setCombineExclusionConstraints(char yesno)
Definition: CSP.hpp:125
void setAddKnifeMirConstraints(char yesno)
Definition: CSP.hpp:131
void unpack(BCP_buffer &buf)
Definition: CSP.hpp:149
char addKnifeMirConstraints
Definition: CSP.hpp:169
CSP_packedVector widths_
Definition: CSP.hpp:190
int s_
Definition: CSP.hpp:164
int * demand_
Definition: CSP.hpp:161
double cost_
Definition: CSP.hpp:183
int getS() const
Definition: CSP.hpp:113
double dj(const double *pi) const
CSP_packedVector(BCP_buffer &buf)
Definition: CSP.hpp:51
PATTERN(const PATTERN &x)
Definition: CSP.hpp:248
double * elements_
Definition: CSP.hpp:32
bool doesAddKnifeMirConstraints() const
Definition: CSP.hpp:121
bool doesAddKnapsackMirConstraints() const
Definition: CSP.hpp:118
~CSPROBLEM()
Definition: CSP.hpp:137
int * indices_
Definition: CSP.hpp:31
CSP_packedVector(const CSP_packedVector &pv)
Definition: CSP.hpp:82
const int getSize() const
Definition: CSP.hpp:77
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
int getL() const
Definition: CSP.hpp:109
const double * getElements() const
Definition: CSP.hpp:73
double getCost() const
Definition: CSP.hpp:222
void pack(BCP_buffer &buf) const
Definition: CSP.hpp:56
static const CSPROBLEM * csproblem_
Definition: CSP.hpp:180
PATTERN(const int numElements, const int *indices, const double *elements, const double ub)
Definition: CSP.hpp:243
void operator()(const T *&ptr)
Definition: CSP.hpp:22
void CoinDisjointCopyN(const T *from, const CoinBigIndex size, T *to)
This helper function copies an array to another location.
char combineExclusionConstraints
Definition: CSP.hpp:167
bool doesCombineExclusionConstraints() const
Definition: CSP.hpp:115
char addKnapsackMirConstraints
Definition: CSP.hpp:168
int getM() const
Definition: CSP.hpp:112
void pack(BCP_buffer &buf) const
Definition: CSP.hpp:198