PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/RL/Stats.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
Stats.hpp
1#ifndef GYM_PCB_RL_STATS_H
2#define GYM_PCB_RL_STATS_H
3
4#include "RL/Action.hpp"
5#include <vector>
6
7class Agent;
8
9 struct ResultRecord
10{
11 ResultRecord() { }
12 ResultRecord(const Agent&, const Action::Result&);
13 ResultRecord(const Agent&);
14 Action::Result asResult() const { return Action::Result(RewardSum, Success, true); }
15
16 Reward RewardSum;
17 bool Success;
18 RouterResult Router;
19 uint64_t TimeMicroseconds;
20 uint64_t TimeStep;
21 uint64_t Iteration;
22
23 void reset(bool success, Reward r = std::numeric_limits<Reward>::infinity());
24 std::string str() const;
25 PyObject *getPy() const;
26};
27
28 struct ResultCollection
29{
30 std::vector<ResultRecord> TimeLine;
31 ResultRecord WorstSuccess;
32 ResultRecord Worst;
33 std::map<std::string, float> F32;
34 std::map<std::string, int64_t> I64;
35
36 bool shouldAdd(const Action::Result&) const;
37 void add(ResultRecord&&);
38 void reset();
39 PyObject *getPy() const;
40};
41
42inline void ResultRecord::reset(bool success, Reward r)
43{
44 Success = success;
45 RewardSum = r;
46 Iteration = 0;
47 TimeMicroseconds = 0;
48 TimeStep = 0;
49}
50
51inline void ResultCollection::reset()
52{
53 TimeLine.clear();
54 WorstSuccess.reset(false);
55 Worst.reset(true);
56 F32.clear();
57 I64.clear();
58}
59inline bool ResultCollection::shouldAdd(const Action::Result &res) const
60{
61 return TimeLine.empty() ||
62 (res > TimeLine.back().asResult()) ||
63 (res < Worst.asResult()) ||
64 (res.Success && res < WorstSuccess.asResult());
65}
66inline void ResultCollection::add(ResultRecord &&rr)
67{
68 const auto res = rr.asResult();
69 if (TimeLine.empty() || res > TimeLine.back().asResult())
70 TimeLine.emplace_back(rr);
71 if (rr.Success && res < WorstSuccess.asResult())
72 WorstSuccess = rr;
73 if (res < Worst.asResult())
74 Worst = rr;
75}
76
77#endif // GYM_PCB_RL_STATS_H
Agent
Definition Agent.hpp:31
Action::Result
Definition Action.hpp:21
ResultCollection
Definition Stats.hpp:29
ResultRecord
Definition Stats.hpp:10
RouterResult
Definition Reward.hpp:15