PCB Environment 2
Loading...
Searching...
No Matches
RRRAgent.hpp
1
2#ifndef GYM_PCB_RRRAGENT_H
3#define GYM_PCB_RRRAGENT_H
4
5#include "RL/Action.hpp"
6#include "RL/Agent.hpp"
7#include "RL/Reward.hpp"
8#include "Track.hpp"
9#include <random>
10
11class RRRAgent : public Agent
12{
13public:
14 RRRAgent();
15 ~RRRAgent();
16
17 bool _run() override;
18 PyObject *get_state(PyObject *) override;
19
20 void setMinIterations(uint);
21 void setMaxIterations(uint);
22 void setMaxIterationsStagnant(uint);
23 void setNumTidyIterations(uint);
24 void setCheckStagnationBeforeSuccess(bool);
25 void setHistoryCostDecay(float);
26 void setHistoryCostIncrement(float);
27 void setHistoryCostMax(float);
28 void setRandomizeOrder(bool);
29
30private:
31 uint mMinIterations{1};
32 uint mMaxIterations{256};
33 uint mMaxIterationsStagnant{1};
34 uint mNumTidyIterations{2};
35 bool mCheckStagnationBeforeSuccess{false};
36 bool mRandomizeOrder{true};
37 bool mPostrouteStage{false};
38 float mHistoryCostDecay{1.0f};
39 float mHistoryCostIncrement{0.125f};
40 float mHistoryCostMax{std::numeric_limits<float>::infinity()};
41 std::vector<uint> mConnectionOrder;
42 std::mt19937 RNG{unsigned(std::chrono::system_clock::now().time_since_epoch().count())};
43private:
44 bool init();
45 void initParameters();
46 bool routeHistoryAll();
47 bool rerouteHistoryOneByOne();
48 void unrouteHistoryAll();
49 Action::Result routeProperlyAll();
50 void unrouteAll();
51 Action::Result postroute();
52 Action::Result checkRouting();
53 void saveTracks(std::vector<Track>&);
54 void restoreTracks(std::vector<Track>&);
55 uint mIterationsStagnant;
56 Action::Result mScore;
57 Action::Result mScoreMax;
58 std::vector<Track> mScoreMaxTracks;
59 std::vector<Track> mSavedTracks;
60 bool mErrorState;
61
62 uint rasterize(const Connection&, int8_t value, bool updateHistoryCost) const;
63 void decayHistoryCosts(float);
64
65 void updateSpacings(const Connection&);
66
67 bool routeHistory(Connection&);
68 void unrouteHistory(Connection&);
69 bool rerouteHistory(Connection&);
70 bool route(Connection&);
71 void unroute(Connection&);
72 bool reroute(Connection&);
73 void restoreTrack(uint i, Track&);
74};
75
76inline void RRRAgent::setMinIterations(uint n)
77{
78 mMinIterations = n;
79}
80inline void RRRAgent::setMaxIterations(uint n)
81{
82 mMaxIterations = n;
83}
84inline void RRRAgent::setMaxIterationsStagnant(uint n)
85{
86 mMaxIterationsStagnant = n;
87}
88inline void RRRAgent::setNumTidyIterations(uint n)
89{
90 mNumTidyIterations = n;
91}
92inline void RRRAgent::setCheckStagnationBeforeSuccess(bool b)
93{
94 mCheckStagnationBeforeSuccess = b;
95}
96inline void RRRAgent::setHistoryCostIncrement(float v)
97{
98 if (v < 0.0f)
99 throw std::runtime_error("history cost increment must be positive");
100 mHistoryCostIncrement = v;
101}
102inline void RRRAgent::setHistoryCostDecay(float v)
103{
104 if (v < 0.0f || v > 1.0f)
105 throw std::runtime_error("must have 0 <= history cost decay <= 1");
106 mHistoryCostDecay = v;
107}
108inline void RRRAgent::setHistoryCostMax(float v)
109{
110 if (v < 0.0f)
111 throw std::runtime_error("history cost maximum must be positive");
112 mHistoryCostMax = v;
113}
114inline void RRRAgent::setRandomizeOrder(bool b)
115{
116 mRandomizeOrder = b;
117}
118
119#endif // GYM_PCB_RRRAGENT_H
Definition Connection.hpp:17
Definition Track.hpp:21
Definition Action.hpp:21