All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
scheduling_jobs_with_deadlines_on_a_single_machine.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Smulewicz
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 //=======================================================================
15 #ifndef PAAL_SCHEDULING_JOBS_WITH_DEADLINES_ON_A_SINGLE_MACHINE_HPP
16 #define PAAL_SCHEDULING_JOBS_WITH_DEADLINES_ON_A_SINGLE_MACHINE_HPP
17 
18 #include "paal/utils/functors.hpp"
21 
22 #include <boost/iterator/counting_iterator.hpp>
23 #include <boost/range/algorithm/sort.hpp>
24 
25 #include <queue>
26 #include <vector>
27 #include <algorithm>
28 #include <utility>
29 
30 namespace paal {
31 namespace greedy {
32 
53 template <class InputIterator, class OutputIterator, class GetTime,
54  class GetDueDate, class GetReleaseDate>
56  const InputIterator first, const InputIterator last, GetTime get_time,
57  GetReleaseDate get_release_date, GetDueDate get_due_date,
58  OutputIterator result) {
59  using Time = puretype(get_time(*first));
60  std::vector<InputIterator> jobs;
61  std::copy(boost::make_counting_iterator(first),
62  boost::make_counting_iterator(last), std::back_inserter(jobs));
63 
64  auto get_due_date_from_iterator =
66  auto due_date_compatator = utils::make_functor_to_comparator(
67  get_due_date_from_iterator, utils::greater{});
68  using QueueType = std::priority_queue<
69  InputIterator, std::vector<InputIterator>, decltype(due_date_compatator)>;
70  QueueType active_jobs_iters(due_date_compatator);
71 
72  auto get_release_date_from_iterator =
73  utils::make_lift_iterator_functor(get_release_date);
74  boost::sort(jobs,
75  utils::make_functor_to_comparator(get_release_date_from_iterator));
76  Time start_idle = Time();
77  Time longest_delay = Time();
78  auto do_job = [&]() {
79  auto job_iter = active_jobs_iters.top();
80  active_jobs_iters.pop();
81  Time start_time = std::max(start_idle, get_release_date(*job_iter));
82  start_idle = start_time + get_time(*job_iter);
83  assign_max(longest_delay, start_idle - get_due_date(*job_iter));
84  *result = std::make_pair(job_iter, start_time);
85  ++result;
86  };
87  for (auto job_iter : jobs) {
88  while (!active_jobs_iters.empty() &&
89  get_release_date(*job_iter) > start_idle)
90  do_job();
91  active_jobs_iters.push(job_iter);
92  }
93  while (!active_jobs_iters.empty()) {
94  do_job();
95  }
96 
97  return longest_delay;
98 }
99 
100 }
101 }
102 
103 #endif // PAAL_SCHEDULING_JOBS_WITH_DEADLINES_ON_A_SINGLE_MACHINE_HPP
auto make_functor_to_comparator(Functor functor, Compare compare=Compare())
make for functor to comparator
Definition: functors.hpp:654
#define puretype(t)
for given expression returns its type with removed const and reference
This file contains set of simple useful functors or functor adapters.
void assign_max(T &t, const T &u)
greater functor
Definition: functors.hpp:478
auto make_lift_iterator_functor(Functor f)
make function for lift_iterator_functor
Definition: functors.hpp:467
auto scheduling_jobs_with_deadlines_on_a_single_machine(const InputIterator first, const InputIterator last, GetTime get_time, GetReleaseDate get_release_date, GetDueDate get_due_date, OutputIterator result)
solve scheduling jobs on identical parallel machines problem and fill start time of all jobs example:...
double Time
[Scheduling Jobs Example]