NetOptim 1.2.8
Loading...
Searching...
No Matches
network_oracle.hpp
Go to the documentation of this file.
1// -*- coding: utf-8 -*-
2#pragma once
3
4#include <digraphx/neg_cycle.hpp> // import NegCycleFinder
5#include <limits>
6#include <optional>
7#include <type_traits>
8
9#include "netoptim_detail.hpp" // import netoptim_detail helpers
10
32
51template <typename Graph, typename Mapping, typename Fn>
54 using node_t = typename Graph::key_type;
55
56 private:
57 const Graph& _gra;
58 Mapping& _u; // vertex potentials
59 NegCycleFinder<Graph> _S;
60 Fn _h;
61
62 public:
67 NetworkOracle(const Graph& gra, Mapping& utx, Fn h)
68 : _gra{gra}, _u{utx}, _S(gra), _h{std::move(h)} {}
69
71 NetworkOracle(const NetworkOracle&) = default;
72
78 template <typename Num> auto update(const Num& gamma) -> void { this->_h.update(gamma); }
79
118 template <typename Arr>
119 auto assess_feas(const Arr& xval) -> std::optional<std::pair<Arr, double>> {
121 "NetworkOracle must model the feasibility-oracle contract");
122 using Edge = typename netoptim_detail::graph_traits<Graph>::Edge;
123
124 auto get_weight = netoptim_detail::make_get_weight<Fn, Edge>(this->_h, xval);
125
126 auto best = std::optional<std::pair<Arr, double>>{};
127 auto best_ratio = std::numeric_limits<double>::infinity();
128 for (auto&& C : this->_S.howard(this->_u, get_weight)) {
129 if (C.empty()) {
130 continue;
131 }
132 auto weight_sum = 0.0;
133 for (auto&& edge : C) {
134 weight_sum += get_weight(edge);
135 }
136 const auto ratio = weight_sum / static_cast<double>(C.size());
137 if (ratio >= best_ratio) {
138 continue;
139 }
140 best_ratio = ratio;
141 auto grad = [&]() -> Arr {
142 if constexpr (std::is_arithmetic_v<Arr>) {
143 return Arr{};
144 } else {
145 return Arr(xval.size());
146 }
147 }();
148 auto fval = 0.0;
149 for (auto&& edge : C) {
150 fval -= get_weight(edge);
151 grad -= this->_h.grad(edge, xval);
152 }
153 best = std::pair{std::move(grad), fval};
154 }
155 return best;
156 }
157
164 template <typename Arr>
165 auto operator()(const Arr& xvar) -> std::optional<std::pair<Arr, double>> {
166 return this->assess_feas(xvar);
167 }
168};
auto assess_feas(const Arr &xval) -> std::optional< std::pair< Arr, double > >
Assess feasibility and generate cutting plane if needed.
Definition network_oracle.hpp:119
NetworkOracle(const NetworkOracle &)=default
Copy constructor.
auto operator()(const Arr &xvar) -> std::optional< std::pair< Arr, double > >
Function call operator for cutting plane methods.
Definition network_oracle.hpp:165
auto update(const Num &gamma) -> void
Update the oracle with a new parameter value.
Definition network_oracle.hpp:78
NetworkOracle(const Graph &gra, Mapping &utx, Fn h)
Construct a new network oracle object.
Definition network_oracle.hpp:67
Definition netoptim_detail.hpp:23
Feasibility oracle: assess x and produce a cutting plane if infeasible.
Definition netoptim_detail.hpp:100
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
Internal implementation helpers shared by the netoptim algorithms.
std::remove_cv_t< std::remove_reference_t< decltype(_get_val( std::declval< NbrElem >(), std::declval< const Nbrs & >()))> > Edge
Definition netoptim_detail.hpp:36