NetOptim 1.2.8
Loading...
Searching...
No Matches
netoptim_detail.hpp
Go to the documentation of this file.
1// -*- coding: utf-8 -*-
2#pragma once
3
13
14#include <cstddef>
15#include <digraphx/neg_cycle.hpp> // import NegCycleFinder, _get_key / _get_val
16#include <type_traits>
17#include <utility>
18#include <vector>
19
20namespace netoptim_detail {
21
22 template <typename T>
23 concept HasKeyType = requires { typename T::key_type; };
24
31 template <typename Graph> struct graph_traits {
32 using Elem = decltype(*std::declval<const Graph&>().begin());
33 using Nbrs = std::remove_cv_t<std::remove_reference_t<decltype(_get_val(
34 std::declval<Elem>(), std::declval<const Graph&>()))>>;
35 using NbrElem = decltype(*std::declval<const Nbrs&>().begin());
36 using Edge = std::remove_cv_t<std::remove_reference_t<decltype(_get_val(
37 std::declval<NbrElem>(), std::declval<const Nbrs&>()))>>;
38 using Cycle = std::vector<Edge>;
39 };
40
61 template <typename Graph, typename T, typename Fn1, typename Fn2, typename Mapping>
62 auto parametric_search(const Graph& gra, T& r_opt, Fn1&& distrance, Fn2&& zero_cancel,
63 Mapping&& dist, size_t max_iters) {
64 using Edge = typename graph_traits<Graph>::Edge;
65 using Cycle = std::vector<Edge>;
66
67 auto get_weight = [&distrance, &r_opt](const Edge& edge) -> T {
68 return static_cast<T>(distrance(r_opt, edge));
69 };
70
71 auto ncf = NegCycleFinder<Graph>(gra);
72 auto r_min = r_opt;
73 auto c_min = Cycle{};
74 auto c_opt = Cycle{};
75
76 for (auto niter = 0U; niter != max_iters; ++niter) {
77 for (auto&& ci : ncf.howard(dist, get_weight)) {
78 auto ri = static_cast<T>(zero_cancel(ci));
79 if (r_min > ri) {
80 r_min = ri;
81 c_min = std::move(ci);
82 }
83 }
84 if (r_min >= r_opt) break;
85 c_opt = std::move(c_min);
86 r_opt = r_min;
87 }
88 return c_opt;
89 }
90
91 // --- Strategy contract for the cutting-plane oracles -------------------
92
99 template <typename O, typename A>
100 concept OracleFeas = requires(O& o, const A& x) {
101 { o.assess_feas(x) };
102 };
103
111 template <typename O, typename A, typename N>
112 concept OracleOptim = requires(O& o, const A& x, N& g) {
113 { o.assess_optim(x, g) };
114 };
115
126 template <typename Fn, typename Arr>
127 concept HasMakeWeightFn = requires(Fn& fn, const Arr& x) {
128 { fn.make_weight_fn(x) };
129 };
130
144 template <typename Fn, typename Edge, typename Arr>
145 auto make_get_weight(Fn& fn, const Arr& xval) {
146 if constexpr (HasMakeWeightFn<Fn, Arr>) {
147 return fn.make_weight_fn(xval);
148 } else {
149 return [&fn, &xval](const Edge& edge) -> double { return fn.eval(edge, xval); };
150 }
151 }
152
153} // namespace netoptim_detail
Definition netoptim_detail.hpp:23
Detect an optional make_weight_fn(x) fast path on an edge oracle.
Definition netoptim_detail.hpp:127
Feasibility oracle: assess x and produce a cutting plane if infeasible.
Definition netoptim_detail.hpp:100
Optimality oracle: assess x against a best-so-far value g.
Definition netoptim_detail.hpp:112
Definition css_oracle.hpp:13
auto parametric_search(const Graph &gra, T &r_opt, Fn1 &&distrance, Fn2 &&zero_cancel, Mapping &&dist, size_t max_iters)
Template Method: parametric max-problem search skeleton.
Definition netoptim_detail.hpp:62
auto make_get_weight(Fn &fn, const Arr &xval)
Build the per-edge weight callable used by Howard's method.
Definition netoptim_detail.hpp:145
Type trait deriving Node / Edge / Cycle from any supported graph container.
Definition netoptim_detail.hpp:31
decltype(*std::declval< const Nbrs & >().begin()) NbrElem
Definition netoptim_detail.hpp:35
std::remove_cv_t< std::remove_reference_t< decltype(_get_val( std::declval< Elem >(), std::declval< const Graph & >()))> > Nbrs
Definition netoptim_detail.hpp:33
std::remove_cv_t< std::remove_reference_t< decltype(_get_val( std::declval< NbrElem >(), std::declval< const Nbrs & >()))> > Edge
Definition netoptim_detail.hpp:36
std::vector< Edge > Cycle
Definition netoptim_detail.hpp:38
decltype(*std::declval< const Graph & >().begin()) Elem
Definition netoptim_detail.hpp:32