Samchon Framework for CPP  1.0.0
samchon::example::packer Namespace Reference

A packer with number of wrapper type and products. More...

Classes

class  Instance
 A physical instance. More...
 
class  Packer
 A packer planning the best packaging. More...
 
class  Product
 A product, merchandise. More...
 
class  ProductArray
 An array of Product objects. More...
 
class  Wrapper
 A wrapper can contain products. More...
 
class  WrapperArray
 An array of wrapper with same category (name). More...
 

Detailed Description

A packer with number of wrapper type and products.

Module packer retrieves the solution of packaging by combination permuation and factorial case.

The packer module is built for providing guidance of using entity and case generator module.

example_packer.png

Example Sources

Packer.hpp
1 #pragma once
2 #include <samchon/protocol/SharedEntityArray.hpp>
3 #include <samchon/example/packer/WrapperArray.hpp>
4 
5 #include <samchon/library/CombinedPermutationGenerator.hpp>
6 
7 #include <iostream>
8 
9 namespace samchon
10 {
11 namespace example
12 {
13 namespace packer
14 {
15  using namespace std;
16 
17  using namespace library;
18  using namespace protocol;
19 
36  class Packer
37  : public SharedEntityArray<WrapperArray>
38  {
39  private:
40  typedef SharedEntityArray<WrapperArray> super;
41 
42  protected:
46  shared_ptr<ProductArray> productArray;
47 
48  public:
49  /* ---------------------------------------------------------
50  CONSTRUCTORS
51  --------------------------------------------------------- */
55  Packer()
56  : super()
57  {
58  this->productArray = make_shared<ProductArray>();
59  };
60 
67  Packer(shared_ptr<ProductArray> productArray)
68  : super()
69  {
70  this->productArray = productArray;
71  };
72 
80  Packer(const Packer &packer)
81  : Packer(packer.productArray)
82  {
83  this->reserve(packer.size());
84 
85  for (size_t i = 0; i < packer.size(); i++)
86  this->emplace_back(new WrapperArray(*packer[i]));
87  };
88 
89  virtual void construct(shared_ptr<XML> xml) override
90  {
91  super::construct(xml);
92 
93  productArray->construct(xml->get(productArray->TAG())->at(0));
94  };
95 
96  protected:
97  virtual auto createChild(shared_ptr<XML>) -> WrapperArray* override
98  {
99  return new WrapperArray();
100  };
101 
102  public:
103  /* ---------------------------------------------------------
104  GETTERS
105  --------------------------------------------------------- */
109  auto productSize() const -> size_t
110  {
111  return productArray->size();
112  };
113 
117  auto calcPrice() const -> int
118  {
119  int price = 0;
120  for (size_t i = 0; i < size(); i++)
121  price += at(i)->calcPrice();
122 
123  return price;
124  };
125 
126  /* ---------------------------------------------------------
127  OPERATORS
128  --------------------------------------------------------- */
132  void optimize(size_t start = 0, size_t size = -1)
133  {
134  if(empty() == true || productArray->empty() == true)
135  return;
136 
137  CombinedPermutationGenerator caseGenerator(this->size(), productArray->size());
138  shared_ptr<Packer> minPacker = nullptr;
139 
140  if (size == -1 || start + size > caseGenerator.size())
141  size = caseGenerator.size() - start;
142 
143  for (size_t i = start; i < start + size; i++)
144  {
145  vector<size_t> &row = caseGenerator[i];
146  shared_ptr<Packer> packer(new Packer(*this));
147 
148  bool validity = true;
149 
150  for (size_t j = 0; j < row.size(); j++)
151  {
152  shared_ptr<Product> &product = productArray->at(j);
153  shared_ptr<WrapperArray> &wrapperArray = packer->at( row[j] );
154 
155  if (wrapperArray->tryInsert(product) == false)
156  {
157  validity = false;
158  break;
159  }
160  }
161 
162  if(validity == false)
163  continue;
164 
165  //OPTIMIZE ALL WRAPPERS IN A PACKER
166  for (size_t j = 0; j < packer->size(); j++)
167  packer->at(j)->optimize();
168 
169  if (minPacker == nullptr ||
170  packer->calcPrice() < minPacker->calcPrice())
171  {
172  minPacker = packer;
173  }
174  }
175 
176  //COPY
177  assign(minPacker->begin(), minPacker->end());
178  };
179 
180  /* ---------------------------------------------------------
181  EXPORT
182  --------------------------------------------------------- */
183  virtual auto TAG() const -> string override
184  {
185  return "packer";
186  };
187  virtual auto CHILD_TAG() const -> string override
188  {
189  return "wrapperArray";
190  };
191 
192  virtual auto toXML() const -> shared_ptr<XML> override
193  {
194  shared_ptr<XML> &xml = super::toXML();
195  xml->push_back(productArray->toXML());
196 
197  return xml;
198  };
199 
203  auto toString() const -> string
204  {
205  string str = "$" + to_string(calcPrice()) + "\n";
206  for (size_t i = 0; i < size(); i++)
207  str += at(i)->toString() + "\n";
208 
209  return str;
210  };
211 
212  /* ---------------------------------------------------------
213  MAIN
214  --------------------------------------------------------- */
215  static void main()
216  {
217  // CONSTRUCT PRODUCTS
218  shared_ptr<ProductArray> productArray(new ProductArray());
219  productArray->emplace_back(new Product("Eraser", 500, 10, 70));
220  productArray->emplace_back(new Product("Pencil", 400, 30, 35));
221  /*productArray->emplace_back(new Product("Pencil", 400, 30, 35));
222  productArray->emplace_back(new Product("Pencil", 400, 30, 35));
223  productArray->emplace_back(new Product("Book", 8000, 150, 300));*/
224  productArray->emplace_back(new Product("Book", 8000, 150, 300));
225  productArray->emplace_back(new Product("Drink", 1000, 75, 250));
226  productArray->emplace_back(new Product("Umbrella", 4000, 200, 1000));
227  productArray->emplace_back(new Product("Notebook-PC", 800000, 150, 850));
228  productArray->emplace_back(new Product("Tablet-PC", 600000, 120, 450));
229 
230  // CONSTRUCT PACKER
231  Packer packer(productArray);
232  packer.emplace_back(new WrapperArray("Large", 100, 200, 1000));
233  packer.emplace_back(new WrapperArray("Medium", 70, 150, 500));
234  packer.emplace_back(new WrapperArray("Small", 50, 100, 250));
235 
236  // OPTIMIZE
237  packer.optimize();
238 
239  // PRINT
240  cout << packer.toString();
241  };
242  };
243 };
244 };
245 };
Definition: RWMutex.hpp:4
EntityGroup< std::vector< std::shared_ptr< _Ty >>, _Ty, std::shared_ptr< _Ty > > SharedEntityArray
An EntityGroup with vector container and children capsuled in shared pointers.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
WrapperArray.hpp
1 #pragma once
2 #include <samchon/protocol/SharedEntityArray.hpp>
3 #include <samchon/example/packer/Wrapper.hpp>
4 
5 #include <samchon/library/FactorialGenerator.hpp>
6 
7 #include <iostream>
8 
9 namespace samchon
10 {
11 namespace example
12 {
13 namespace packer
14 {
15  using namespace std;
16 
17  using namespace library;
18  using namespace protocol;
19 
32  class WrapperArray
33  : public SharedEntityArray<Wrapper>
34  {
35  private:
36  typedef SharedEntityArray<Wrapper> super;
37 
38  protected:
42  shared_ptr<ProductArray> reserved;
43 
47  shared_ptr<Wrapper> sample;
48 
49  public:
50  /* ---------------------------------------------------------
51  CONSTRUCTORS
52  --------------------------------------------------------- */
56  WrapperArray()
57  : super()
58  {
59  this->reserved = make_shared<ProductArray>();
60  this->sample = make_shared<Wrapper>();
61  };
62 
71  WrapperArray(const string &name, int price, int volume, int weight)
72  : super()
73  {
74  this->reserved = make_shared<ProductArray>();
75  this->sample = make_shared<Wrapper>(name, price, volume, weight);
76  };
77 
78 
86  WrapperArray(const WrapperArray &wrapperArray)
87  : super()
88  {
89  this->reserved = make_shared<ProductArray>();
90  this->sample = wrapperArray.sample;
91  };
92 
93  virtual ~WrapperArray() = default;
94 
95  virtual void construct(shared_ptr<XML> xml) override
96  {
97  super::construct(xml);
98 
99  sample->construct(xml);
100  };
101 
102  protected:
103  virtual auto createChild(shared_ptr<XML>) -> Wrapper* override
104  {
105  return new Wrapper();
106  };
107 
108  public:
119  auto tryInsert(shared_ptr<Product> product) -> bool
120  {
121  if (product->getVolume() > sample->getVolume() ||
122  product->getWeight() > sample->getWeight())
123  {
124  return false;
125  }
126 
127  reserved->push_back(product);
128  return true;
129  };
130 
131  /* ---------------------------------------------------------
132  OPERATORS
133  --------------------------------------------------------- */
149  void optimize()
150  {
151  if(reserved->empty() == true)
152  return;
153 
154  FactorialGenerator factorial(reserved->size());
155  shared_ptr<WrapperArray> minWrapperArray = nullptr;
156 
157  for (size_t i = 0; i < factorial.size(); i++)
158  {
159  shared_ptr<WrapperArray> wrapperArray(new WrapperArray(*this));
160  vector<size_t> &row = factorial[i];
161 
162  for (size_t j = 0; j < row.size(); j++)
163  {
164  shared_ptr<Product> &product = this->reserved->at(row[j]);
165 
166  if (wrapperArray->empty() == true ||
167  wrapperArray->at(wrapperArray->size() - 1)->tryInsert(product) == false)
168  {
169  Wrapper *wrapper = new Wrapper(*this->sample);
170  wrapper->tryInsert(product);
171 
172  wrapperArray->emplace_back(wrapper);
173  }
174  }
175 
176  //unique_lock<mutex> uk(mtx);
177  if (minWrapperArray == nullptr ||
178  wrapperArray->size() < minWrapperArray->size())
179  {
180  minWrapperArray = wrapperArray;
181  }
182  }
183 
184  assign(minWrapperArray->begin(), minWrapperArray->end());
185  };
186 
192  auto calcPrice() const -> int
193  {
194  return sample->getPrice() * (int)size();
195  };
196 
197  /* ---------------------------------------------------------
198  EXPORT
199  --------------------------------------------------------- */
200  virtual auto TAG() const -> string override
201  {
202  return "wrapperArray";
203  }
204  virtual auto CHILD_TAG() const -> string override
205  {
206  return "wrapper";
207  };
208 
209  virtual auto toXML() const -> shared_ptr<XML> override
210  {
211  shared_ptr<XML> &xml = super::toXML();
212  xml->addAllProperty(sample->toXML());
213 
214  return xml;
215  };
216 
220  auto toString() const -> string
221  {
222  string str = "Category - " + sample->getName() + "\n";
223  for (size_t i = 0; i < size(); i++)
224  str += at(i)->toString() + "\n";
225 
226  return str;
227  };
228  };
229 };
230 };
231 };
Definition: RWMutex.hpp:4
EntityGroup< std::vector< std::shared_ptr< _Ty >>, _Ty, std::shared_ptr< _Ty > > SharedEntityArray
An EntityGroup with vector container and children capsuled in shared pointers.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
Wrapper.hpp
1 #pragma once
2 #include <samchon/example/packer/ProductArray.hpp>
3 #include <samchon/example/packer/Instance.hpp>
4 
5 namespace samchon
6 {
7 namespace example
8 {
9 namespace packer
10 {
11  using namespace std;
12 
13  using namespace library;
14  using namespace protocol;
15 
27  class Wrapper
28  : public ProductArray,
29  public Instance
30  {
31  private:
32  typedef ProductArray super;
33 
34  public:
35  /* ---------------------------------------------------------
36  CONSTRUCTORS
37  --------------------------------------------------------- */
41  Wrapper()
42  : super(),
43  Instance()
44  {
45  };
46 
55  Wrapper(const string &name, int price, int volume, int weight)
56  : super(),
57  Instance(name, price, volume, weight)
58  {
59  };
60 
66  Wrapper(const Wrapper &wrapper)
67  : super(),
68  Instance(wrapper)
69  {
70  };
71 
72  virtual ~Wrapper() = default;
73 
74  virtual void construct(shared_ptr<XML> xml) override
75  {
76  super::construct(xml);
77  Instance::construct(xml);
78  };
79 
80  public:
91  auto tryInsert(shared_ptr<Product> product) -> bool
92  {
93  int volume = 0;
94  int weight = 0;
95 
96  for (size_t i = 0; i < size(); i++)
97  {
98  volume += at(i)->getVolume();
99  weight += at(i)->getWeight();
100  }
101 
102  if (product->getVolume() + volume > this->volume ||
103  product->getWeight() + weight > this->weight)
104  {
105  return false;
106  }
107 
108  push_back(product);
109  return true;
110  };
111 
112  /* ---------------------------------------------------------
113  EXPORTERS
114  --------------------------------------------------------- */
115  virtual auto TAG() const -> string override
116  {
117  return "wrapper";
118  };
119 
120  virtual auto toXML() const -> shared_ptr<XML> override
121  {
122  shared_ptr<XML> &xml = super::toXML();
123  xml->addAllProperty(Instance::toXML());
124 
125  return xml;
126  };
127 
134  virtual auto toString() const -> string override
135  {
136  string str = "\tWrapper " + Instance::toString() + "\n";
137  for (size_t i = 0; i < size(); i++)
138  str += "\t\t" + at(i)->toString()
139  + ((i == size() - 1) ? "" : "\n");
140 
141  return str;
142  };
143  };
144 };
145 };
146 };
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the Entity.
Definition: Instance.hpp:133
Definition: RWMutex.hpp:4
virtual auto toString() const -> string
Return a string represents the Instance.
Definition: Instance.hpp:147
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
Product.hpp
1 #pragma once
2 #include <samchon/example/packer/Instance.hpp>
3 
4 namespace samchon
5 {
6 namespace example
7 {
8 namespace packer
9 {
10  using namespace std;
11 
12  using namespace library;
13  using namespace protocol;
14 
26  class Product
27  : public Instance
28  {
29  private:
30  typedef Instance super;
31 
32  public:
33  /* ---------------------------------------------------------
34  CONSTRUCTOR
35  --------------------------------------------------------- */
39  Product()
40  : super()
41  {
42  };
43 
52  Product(const string &name, int price, int volume, int weight)
53  : super(name, price, volume, weight)
54  {
55  };
56 
57  virtual ~Product() = default;
58 
59  /* ---------------------------------------------------------
60  EXPORT
61  --------------------------------------------------------- */
62  virtual auto TAG() const -> string override
63  {
64  return "product";
65  };
66 
70  virtual auto toString() const -> string override
71  {
72  return "Product " + super::toString();
73  };
74  };
75 };
76 };
77 };
Definition: RWMutex.hpp:4
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
Instance.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 
4 #include <samchon/library/StringUtil.hpp>
5 #include <samchon/library/XML.hpp>
6 
7 namespace samchon
8 {
9 namespace example
10 {
11 namespace packer
12 {
13  using namespace std;
14 
15  using namespace library;
16  using namespace protocol;
17 
29  class Instance
30  : public virtual Entity
31  {
32  private:
33  typedef Entity super;
34 
35  protected:
39  string name;
40 
44  int price;
45 
49  int volume;
50 
54  int weight;
55 
56  public:
57  /* ---------------------------------------------------------
58  CONSTRUCTORS
59  --------------------------------------------------------- */
63  Instance()
64  : super()
65  {
66  };
67 
76  Instance(const string &name, int price, int volume, int weight)
77  : super()
78  {
79  this->name = name;
80  this->price = price;
81  this->volume = volume;
82  this->weight = weight;
83  };
84 
85  virtual ~Instance() = default;
86 
87  virtual void construct(shared_ptr<XML> xml) override
88  {
89  this->name = xml->getProperty("name");
90  this->price = xml->getProperty<int>("price");
91  this->volume = xml->getProperty<int>("volume");
92  this->weight = xml->getProperty<int>("weight");
93  };
94 
95  /* ---------------------------------------------------------
96  GETTERS
97  --------------------------------------------------------- */
101  auto getName() const -> string
102  {
103  return name;
104  };
105 
109  auto getPrice() const -> int
110  {
111  return price;
112  };
113 
117  auto getVolume() const -> int
118  {
119  return volume;
120  };
121 
125  auto getWeight() const -> int
126  {
127  return weight;
128  };
129 
130  /* ---------------------------------------------------------
131  EXPORT
132  --------------------------------------------------------- */
133  virtual auto toXML() const -> shared_ptr<XML> override
134  {
135  shared_ptr<XML> &xml = super::toXML();
136  xml->setProperty("name", name);
137  xml->setProperty("price", price);
138  xml->setProperty("volume", volume);
139  xml->setProperty("weight", weight);
140 
141  return xml;
142  };
143 
147  virtual auto toString() const -> string
148  {
150  (
151  "{1}: ${2}, {3}cm^3, {4}g",
152  name, price, volume, weight
153  );
154  };
155  };
156 };
157 };
158 };
Definition: RWMutex.hpp:4
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:55
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7

Result of the example

example_packer_result.png
See also
protocol::Entity
library::CaseGenerator
Author
Jeongho Nam http://samchon.org