NetOptim 1.2.8
Loading...
Searching...
No Matches
min_cycle_ratio.hpp
Go to the documentation of this file.
1// -*- coding: utf-8 -*-
2#pragma once
3
4#include <algorithm>
5#include <stdexcept>
6#include <utility>
7
8#include "netoptim_detail.hpp" // import netoptim_detail helpers
9#include "parametric.hpp" // import max_parametric
10
29
81template <typename Graph, typename T, typename Fn1, typename Fn2, typename Mapping>
82auto min_cycle_ratio(const Graph& gra, T& r0, Fn1&& get_cost, Fn2&& get_time, Mapping&& dist,
83 size_t max_iters = 1000) {
85 using edge_t = Edge;
86
87 auto calc_ratio = [&](const auto& C) -> T {
88 using cost_T = decltype(get_cost(std::declval<edge_t>()));
89 using time_T = decltype(get_time(std::declval<edge_t>()));
90 auto total_cost = cost_T(0);
91 auto total_time = time_T(0);
92 for (auto&& edge : C) {
93 total_cost += get_cost(edge);
94 total_time += get_time(edge);
95 }
96 if (total_time == time_T(0)) {
97 throw std::domain_error("min_cycle_ratio: cycle has zero total time");
98 }
99 return T(total_cost) / T(total_time);
100 };
101
102 auto calc_weight = [&](const T& r, const edge_t& edge) -> T {
103 return get_cost(edge) - r * T(get_time(edge));
104 };
105
106 return max_parametric(gra, r0, std::move(calc_weight), std::move(calc_ratio),
107 std::forward<Mapping>(dist), max_iters);
108}
auto min_cycle_ratio(const Graph &gra, T &r0, Fn1 &&get_cost, Fn2 &&get_time, Mapping &&dist, size_t max_iters=1000)
Solve the minimum cost-to-time cycle ratio problem.
Definition min_cycle_ratio.hpp:82
Internal implementation helpers shared by the netoptim algorithms.
Maximum parametric problem solver for network optimization.
auto max_parametric(const Graph &gra, T &r_opt, Fn1 &&distrance, Fn2 &&zero_cancel, Mapping &&dist, size_t max_iters=1000)
Solve the maximum parametric problem.
Definition parametric.hpp:74
std::remove_cv_t< std::remove_reference_t< decltype(_get_val( std::declval< NbrElem >(), std::declval< const Nbrs & >()))> > Edge
Definition netoptim_detail.hpp:36