NetOptim 1.2.8
Loading...
Searching...
No Matches
optscaling_oracle.hpp
Go to the documentation of this file.
1// -*- coding: utf-8 -*-
2#pragma once
3
4#include <algorithm>
5#include <stdexcept>
6#include <valarray>
7
8#include "netoptim_detail.hpp" // import netoptim_detail helpers
9#include "network_oracle.hpp"
10
30
31namespace netoptim_detail {
32
42 template <typename Graph, typename Fn> class Ratio {
43 public:
44 using Vec = std::valarray<double>;
45
49 Ratio(const Graph& gra, Fn get_cost) : _gra{gra}, _get_cost{std::move(get_cost)} {}
50
52 Ratio(const Ratio&) = default;
53
63 auto eval(const auto& edge, const Vec& x) const -> double {
64 if (x.size() < 2) {
65 throw std::invalid_argument("OptScalingOracle: x must have at least 2 entries");
66 }
67 const auto [aij, aji] = this->_get_cost(edge);
68 return std::min(x[0] - aji, aij - x[1]);
69 }
70
80 auto make_weight_fn(const Vec& x) const {
81 if (x.size() < 2) {
82 throw std::invalid_argument("OptScalingOracle: x must have at least 2 entries");
83 }
84 const auto x0 = x[0];
85 const auto x1 = x[1];
86 auto get_cost = this->_get_cost;
87 return [get_cost, x0, x1](const auto& edge) -> double {
88 const auto [aij, aji] = get_cost(edge);
89 return std::min(x0 - aji, aij - x1);
90 };
91 }
92
101 auto grad(const auto& edge, const Vec& x) const -> Vec {
102 if (x.size() < 2) {
103 throw std::invalid_argument("OptScalingOracle: x must have at least 2 entries");
104 }
105 const auto [aij, aji] = this->_get_cost(edge);
106 return (x[0] - aji < aij - x[1]) ? Vec{1., 0.} : Vec{0., -1.};
107 }
108
109 private:
110 const Graph& _gra;
111 Fn _get_cost;
112 };
113
114} // namespace netoptim_detail
115
133template <typename Graph, typename Mapping, typename Fn> //
136 using Vec = std::valarray<double>;
137 using node_t = typename Graph::key_type;
138 using Cut = std::pair<Vec, double>;
140
142
143 public:
148 OptScalingOracle(const Graph& gra, Mapping& utx, Fn get_cost)
149 : _network(gra, utx, Ratio{gra, get_cost}) {}
150
156 ~OptScalingOracle() = default;
157
193 auto assess_optim(const Vec& x, double& t) -> std::tuple<Cut, bool> {
194 if (x.size() < 2) {
195 throw std::invalid_argument("OptScalingOracle: x must have at least 2 entries");
196 }
197 static_assert(
199 "OptScalingOracle must model the optimality-oracle contract");
200 const auto cut = this->_network.assess_feas(x);
201 if (cut) {
202 return {*cut, false};
203 }
204 auto s = x[0] - x[1];
205 auto fj = s - t;
206 if (fj < 0) {
207 t = s;
208 return {{Vec{1., -1.}, 0.}, true};
209 }
210 return {{Vec{1., -1.}, fj}, false};
211 }
212
217 auto operator()(const Vec& x, double& t) -> std::tuple<Cut, bool> { return assess_optim(x, t); }
218};
Oracle for Parametric Network Problems.
Definition network_oracle.hpp:53
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
OptScalingOracle & operator=(const OptScalingOracle &)=default
auto assess_optim(const Vec &x, double &t) -> std::tuple< Cut, bool >
Assess optimality at point x (cutting plane interface).
Definition optscaling_oracle.hpp:193
OptScalingOracle(OptScalingOracle &&)=default
OptScalingOracle & operator=(OptScalingOracle &&)=default
OptScalingOracle(const OptScalingOracle &)=default
Copy constructor.
OptScalingOracle(const Graph &gra, Mapping &utx, Fn get_cost)
Construct a new optscaling oracle object.
Definition optscaling_oracle.hpp:148
auto operator()(const Vec &x, double &t) -> std::tuple< Cut, bool >
Function call operator for cutting_plane_optim().
Definition optscaling_oracle.hpp:217
~OptScalingOracle()=default
Adapter: evaluates the scaling-ratio constraint for one edge.
Definition optscaling_oracle.hpp:42
auto make_weight_fn(const Vec &x) const
Build a weight callable with the iterate bound once.
Definition optscaling_oracle.hpp:80
auto eval(const auto &edge, const Vec &x) const -> double
Evaluate the constraint function for an edge.
Definition optscaling_oracle.hpp:63
auto grad(const auto &edge, const Vec &x) const -> Vec
Compute the gradient of the constraint function.
Definition optscaling_oracle.hpp:101
Ratio(const Graph &gra, Fn get_cost)
Construct a new Ratio object.
Definition optscaling_oracle.hpp:49
Ratio(const Ratio &)=default
Copy constructor.
std::valarray< double > Vec
Definition optscaling_oracle.hpp:44
Definition netoptim_detail.hpp:23
Optimality oracle: assess x against a best-so-far value g.
Definition netoptim_detail.hpp:112
Definition css_oracle.hpp:13
Internal implementation helpers shared by the netoptim algorithms.
Oracle for Parametric Network Problems.