Samchon Framework for CPP  1.0.0
PackerMaster.hpp
1 #pragma once
2 #include <samchon/example/interaction/Master.hpp>
3 
4 #include <samchon/example/interaction/ChiefDriver.hpp>
5 #include <samchon/example/interaction/SlaveDriver.hpp>
6 #include <samchon/protocol/Invoke.hpp>
7 
8 #include <samchon/example/packer/Packer.hpp>
9 #include <samchon/library/CombinedPermutationGenerator.hpp>
10 
11 namespace samchon
12 {
13 namespace example
14 {
15 namespace interaction
16 {
17  using namespace std;
18 
19  using namespace library;
20  using namespace protocol;
21 
32  : public Master
33  {
34  private:
35  typedef Master super;
36 
37  protected:
41  shared_ptr<packer::Packer> packer;
42 
43  public:
44  /* ---------------------------------------------------------------------------------
45  CONSTRUCTORS
46  --------------------------------------------------------------------------------- */
51  : super(37300)
52  {
53  packer = make_shared<packer::Packer>();
54  };
55  virtual ~PackerMaster() = default;
56 
57  protected:
58  /* ---------------------------------------------------------------------------------
59  INVOKE MESSATE CHAIN
60  --------------------------------------------------------------------------------- */
61  virtual void optimize(shared_ptr<XML> xml) override
62  {
63  super::optimize(nullptr);
64 
65  packer->construct(xml);
66 
67  CombinedPermutationGenerator caseGen(packer->size(), packer->productSize());
68 
69  sendSegmentData
70  (
71  make_shared<Invoke>
72  (
73  "optimize",
74  packer->toXML()
75  ),
76  caseGen.size()
77  );
78  };
79  virtual void replyOptimization(shared_ptr<XML> xml) override
80  {
81  unique_lock<mutex> uk(mtx);
82 
83  shared_ptr<packer::Packer> packer(new packer::Packer());
84  packer->construct(xml);
85 
86  cout << "An optimization process from a slave system has completed" << endl;
87  cout << "\tOrdinary minimum price: " << this->packer->calcPrice()
88  << ", New Price from the slave: " << packer->calcPrice() << endl;
89 
90  if (this->packer->calcPrice() == 0 || packer->calcPrice() < this->packer->calcPrice())
91  this->packer = packer;
92 
93  if (++optimized < this->size())
94  return;
95 
96  cout << "Parallel optimization has completed." << endl;
97  cout << packer->toString() << endl << endl;
98 
99  chiefDriver->sendData( make_shared<Invoke>("replyOptimization", packer->toXML()) );
100  };
101 
102  /* ---------------------------------------------------------------------------------
103  MAIN
104  --------------------------------------------------------------------------------- */
105  public:
109  static void main()
110  {
111  cout << "----------------------------------------------------------------------------" << endl;
112  cout << " PACKER MASTER" << endl;
113  cout << "----------------------------------------------------------------------------" << endl;
114 
115  PackerMaster master;
116  master.start();
117  };
118  };
119 };
120 };
121 };
virtual void replyOptimization(shared_ptr< XML > xml) override
Handle (replied) optimized value from a slave system.
A master of parallel system solving something.
Definition: Master.hpp:49
shared_ptr< packer::Packer > packer
A packer solver.
virtual void optimize(shared_ptr< XML > xml) override
Optimize something.
A master of parallel system solving packaging problem.