NetOptim 1.2.8
Loading...
Searching...
No Matches
css_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 <stdexcept>
8#include <tuple>
9#include <valarray>
10
11#include "netoptim_detail.hpp" // import netoptim_detail helpers
12
13namespace netoptim_detail {
14
16 struct timing_edge {
17 double delay = 0.0;
18 char type = 's';
19 };
20
21} // namespace netoptim_detail
22
39template <typename Graph, typename Mapping> class ClockScheduleOracle {
40 using Vec = std::valarray<double>;
41 using Cut = std::pair<Vec, double>;
43
44 Mapping& _dist;
45 NegCycleFinder<Graph> _S;
46
47 public:
51 ClockScheduleOracle(const Graph& gra, Mapping& dist) : _dist{dist}, _S{gra} {}
52
57 auto assess_optim(const Vec& x, double& gamma) -> std::tuple<Cut, bool> {
58 const auto tcp = x[0];
59 const auto beta = x[1];
60 if (tcp < 0.0) {
61 return {Cut{Vec{-1.0, 0.0}, -tcp}, false};
62 }
63 if (const auto fj = tcp - gamma * beta; fj > 0.0) {
64 return {Cut{Vec{1.0, -gamma}, fj}, false};
65 }
66 const auto get_weight = [tcp, beta](const Edge& edge) -> double {
67 return edge.type == 's' ? tcp - edge.delay - beta : edge.delay - beta;
68 };
69 // Howard may yield spurious non-negative cycles (verification is per-edge),
70 // so keep only the most-violated cycle and cut only if it is negative.
71 auto best = std::optional<Cut>{};
72 auto min_weight = 0.0;
73 for (auto&& C : this->_S.howard(this->_dist, get_weight)) {
74 auto weight_sum = 0.0;
75 auto setup = 0.0;
76 for (auto&& edge : C) {
77 weight_sum += get_weight(edge);
78 setup += edge.type == 's' ? 1.0 : 0.0;
79 }
80 if (weight_sum < min_weight) {
81 min_weight = weight_sum;
82 best = Cut{Vec{-setup, static_cast<double>(C.size())}, -weight_sum};
83 }
84 }
85 if (best) {
86 return {std::move(*best), false};
87 }
88 if (beta == 0.0) {
89 throw std::domain_error("ClockScheduleOracle: beta is zero (unbounded ratio)");
90 }
91 gamma = tcp / beta;
92 return {Cut{Vec{1.0, -1.0}, 0.0}, true};
93 }
94
99 auto operator()(const Vec& x, double& gamma) -> std::tuple<Cut, bool> {
100 return assess_optim(x, gamma);
101 }
102};
auto assess_optim(const Vec &x, double &gamma) -> std::tuple< Cut, bool >
Assess optimality at x = (TCP, beta) (cutting-plane interface).
Definition css_oracle.hpp:57
ClockScheduleOracle(const Graph &gra, Mapping &dist)
Construct a new clock-schedule oracle.
Definition css_oracle.hpp:51
auto operator()(const Vec &x, double &gamma) -> std::tuple< Cut, bool >
Function call operator for cutting_plane_optim().
Definition css_oracle.hpp:99
Definition css_oracle.hpp:13
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
Edge for the clock-skew scheduling oracle: delay and type.
Definition css_oracle.hpp:16
char type
's' setup, 'h' hold
Definition css_oracle.hpp:18
double delay
Definition css_oracle.hpp:17