All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
scheduling_jobs_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
18 #include "paal/utils/functors.hpp"
19 
20 #include <iostream>
21 #include <unordered_map>
22 #include <utility>
23 
24 typedef double Time;
25 typedef std::pair<Time, char> Job;
26 typedef int Machine;
27 
28 template <class Result> void print_result(const Result &result) {
29  std::unordered_map<Machine, Time> machineTime;
30  for (auto const &machineJobpair : result) {
31  Machine machine = *machineJobpair.first;
32  Job job = *machineJobpair.second;
33  machineTime[machine] += job.first / machine;
34  std::cout << "On machine: " << machine << " do job: " << job.second
35  << std::endl;
36  }
37  Time max_time = 0;
38  for (auto const &it : machineTime) {
39  max_time = std::max(max_time, it.second);
40  }
41  std::cout << "Solution: " << max_time << std::endl;
42 }
43 
44 int main() {
45  auto returnJobLoadFunctor = [](Job job) { return job.first; };
46 
47  std::vector<Machine> machines = { 1, 2, 3 };
48  std::vector<Job> jobs = { { 2.1, 'a' }, { 3.1, 'b' }, { 4.1, 'c' },
49  { 5.1, 'd' }, { 6.1, 'e' }, { 7.1, 'f' },
50  { 8.1, 'g' } };
51 
52  std::vector<std::pair<decltype(machines) ::iterator,
53  decltype(jobs) ::iterator>> deterministicResult,
54  randomizedResult;
55 
56  std::cout << "Deterministic schedule:" << std::endl;
58  machines.begin(), machines.end(), jobs.begin(), jobs.end(),
59  back_inserter(deterministicResult), paal::utils::identity_functor(),
60  returnJobLoadFunctor);
61  print_result(deterministicResult);
62 
63  std::cout << "Randomized schedule:" << std::endl;
64  paal::greedy::schedule_randomized(
65  machines.begin(), machines.end(), jobs.begin(), jobs.end(),
66  back_inserter(randomizedResult), paal::utils::identity_functor(),
67  returnJobLoadFunctor);
68  print_result(randomizedResult);
69 
70  return 0;
71 }
This file contains set of simple useful functors or functor adapters.
functor returns its argument
Definition: functors.hpp:128
double Time
[Scheduling Jobs Example]
void schedule_deterministic(const MachineIterator mfirst, const MachineIterator mlast, const JobIterator jfirst, const JobIterator jlast, OutputIterator result, GetSpeed get_speed, GetLoad get_load)
detail