Samchon Framework for CPP  1.0.0
PackerRequest.hpp
1 #pragma once
2 
3 #include <iostream>
4 #include <samchon/examples/interaction/base/MasterBase.hpp>
5 #include <samchon/examples/packer/Packer.hpp>
6 
7 namespace samchon
8 {
9 namespace examples
10 {
11 namespace interaction
12 {
13  class PackerRequest
14  {
15  private:
16  base::MasterBase *master;
17 
18  std::shared_ptr<packer::Packer> best_packer;
19  size_t requested_size;
20  size_t completed_count;
21 
22  public:
23  PackerRequest(base::MasterBase *master)
24  {
25  this->master = master;
26  };
27  void init(size_t size)
28  {
29  best_packer = nullptr;
30  this->completed_count = 0;
31  this->requested_size = size;
32  };
33 
34  void optimize(std::shared_ptr<library::XML> xml)
35  {
36  std::shared_ptr<packer::Packer> packer(new packer::Packer());
37  packer->construct(xml);
38 
39  auto invoke = std::make_shared<protocol::Invoke>("optimizePacker", packer->toXML());
40  size_t size = library::CombinedPermutationGenerator(packer->size(), packer->productSize()).size();
41 
42  std::cout << "Start Packer optimization: #" << size << std::endl;
43  init(size);
44  };
45  void replyOptimization(std::shared_ptr<library::XML> xml)
46  {
47  std::shared_ptr<packer::Packer> packer(new packer::Packer());
48  packer->construct(xml);
49 
50  std::cout << "A slave has finished his optimization: $" << packer->calcPrice() << std::endl;
51 
52  // IF CURRENT TRAVEL IS SHORTER, MAKE IT THE BEST
53  if (best_packer == nullptr || best_packer->calcPrice() == 0 ||
54  packer->calcPrice() < best_packer->calcPrice())
55  {
56  std::cout << "The slave renewed the best solution" << std::endl;
57  best_packer = packer;
58  }
59 
60  // IF ALL TASKS ARE DONE, REPORT TO THE CHIEF
61  if (++completed_count == requested_size)
62  master->getParent()->sendData
63  (
64  std::make_shared<protocol::Invoke>("replyPackerOptimization", best_packer->toXML())
65  );
66  };
67  };
68 };
69 };
70 };