LCOV - code coverage report
Current view: top level - app/include/Dynamic/Policy - QLearner.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 2 0.0 %
Date: 2023-08-17 16:45:52 Functions: 0 0 -

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <functional>
       4             : #include <random>
       5             : 
       6             : #include "Alg/ShortestPath/DijkstraMany.hpp"
       7             : #include "Dynamic/Env/Connection.hpp"
       8             : #include "Dynamic/Env/Env.hpp"
       9             : #include "Dynamic/Env/Lane.hpp"
      10             : #include "Dynamic/Policy/Action.hpp"
      11             : #include "Dynamic/Policy/Policy.hpp"
      12             : #include "Dynamic/SUMOAdapter.hpp"
      13             : #include "Log/ProgressLogger.hpp"
      14             : #include "data/SUMO/Network.hpp"
      15             : #include "utils/reference_wrapper.hpp"
      16             : 
      17             : namespace Dynamic {
      18             : 
      19           0 : class QLearner {
      20             :    protected:
      21             :     typedef Env::Action::Reward Reward;
      22             : 
      23             :    public:
      24             :     // clang-format off
      25             :     class Action {
      26             :         // clang-format on
      27             :        public:
      28             :         Env::Connection& connection;
      29             :         Env::Lane&       lane;
      30             : 
      31             :         Action(Env::Connection& connection, Env::Lane& lane);
      32             :         Action(const Action& action);
      33             : 
      34             :         Action& operator=(const Action& other);
      35             : 
      36             :         bool operator==(const Action& other) const;
      37             :         bool operator!=(const Action& other) const;
      38             : 
      39             :         bool operator<(const Action& other) const;
      40             :     };
      41             : 
      42             :     class State: public std::reference_wrapper<Env::Lane> {
      43             :        public:
      44             :         State(Env::Lane& lane);
      45             : 
      46             :         State apply(Action action) const;
      47             : 
      48             :         std::vector<Action> possibleActions();
      49             :         std::vector<Action> possibleActions() const;
      50             :     };
      51             : 
      52             :     class Logger: public Policy::Logger {
      53             :         friend class QLearner;
      54             :         friend class DoubleQLearner;
      55             : 
      56             :         static const double ALPHA_D;
      57             : 
      58             :         Reward alpha;
      59             : 
      60             :         double D = 0.0, DA = 0.0;
      61             : 
      62             :        public:
      63             :         Logger(Reward alpha);
      64             :         virtual void header(Log::ProgressLogger& logger);
      65             :         virtual void log(Log::ProgressLogger& logger);
      66             :         void         setAlpha(Reward alpha);
      67             :     };
      68             : 
      69             :     class Policy: public Dynamic::Policy {
      70             :         QLearner& qLearner;
      71             : 
      72             :         const Env::Vehicle::ID vehicleID;
      73             : 
      74             :         std::mt19937& gen;
      75             : 
      76             :        public:
      77             :         Policy(QLearner& qLearner, Env::Vehicle::ID vehicleID, std::mt19937& gen);
      78             : 
      79             :         virtual Env::Lane& pickInitialLane(
      80             :             Vehicle&  vehicle,
      81             :             Env::Env& env
      82             :         ) override;
      83             : 
      84             :         virtual std::shared_ptr<Env::Action> pickConnection(
      85             :             Env::Env& env
      86             :         ) override;
      87             : 
      88             :         struct Action: public Env::Action {
      89             :             QLearner& qLearner;
      90             : 
      91             :             Action(Env::Connection& connection, Env::Lane& lane, QLearner& qLearner);
      92             : 
      93             :             virtual void reward(Reward r) override;
      94             :         };
      95             : 
      96             :         struct ActionLeave: public Action {
      97             :            private:
      98             :             Env::Lane& stateLane;
      99             : 
     100             :            public:
     101             :             ActionLeave(Env::Lane& stateLane, QLearner& qLearner);
     102             : 
     103             :             virtual void reward(Reward r) override;
     104             :         };
     105             : 
     106           0 :         class Factory: public Dynamic::Policy::Factory {
     107             :            protected:
     108             :             Env::Env&                   env;
     109             :             const SUMO::NetworkTAZs&    sumo;
     110             :             const Dynamic::SUMOAdapter& adapter;
     111             : 
     112             :             std::mt19937 gen;
     113             : 
     114             :             std::map<Dynamic::Env::Edge::ID, Dynamic::QLearner> qLearners;
     115             : 
     116             :             std::optional<std::reference_wrapper<QLearner::Logger>> policyLogger;
     117             : 
     118             :            public:
     119             :             Factory(
     120             :                 Env::Env&                                               env,
     121             :                 const SUMO::NetworkTAZs&                                sumo,
     122             :                 const Dynamic::SUMOAdapter&                             adapter,
     123             :                 std::random_device::result_type                         seed         = 0,
     124             :                 std::optional<std::reference_wrapper<QLearner::Logger>> policyLogger = std::nullopt
     125             :             );
     126             : 
     127             :             virtual std::shared_ptr<Dynamic::Policy> create(
     128             :                 Vehicle::ID     id,
     129             :                 Time            depart,
     130             :                 const Env::TAZ& fromTAZ,
     131             :                 const Env::TAZ& toTAZ
     132             :             ) override;
     133             : 
     134             :             void dump() const;
     135             :         };
     136             :     };
     137             : 
     138             :    private:
     139             :     Env::Env&                   env;
     140             :     const SUMO::Network&        network;
     141             :     const Dynamic::SUMOAdapter& adapter;
     142             :     const Env::TAZ&             destinationTAZ;
     143             : 
     144             :    public:
     145             :     Alg::ShortestPath::Dijkstra sp;
     146             : 
     147             :    protected:
     148             :     Reward alpha, gamma;
     149             : 
     150             :    private:
     151             :     Reward xi, eta;
     152             :     float  epsilon;
     153             : 
     154             :    protected:
     155             :     // clang-format off
     156             :     typedef std::unordered_map<
     157             :         State,
     158             :         std::map<
     159             :             Action,
     160             :             Reward
     161             :         >,
     162             :         utils::reference_wrapper::hash    <State::type>,
     163             :         utils::reference_wrapper::equal_to<State::type>
     164             :     > QMatrixType;
     165             :     // clang-format on
     166             : 
     167             :    private:
     168             :     /**
     169             :      * TODO: ideas:
     170             :      * - Merge all actions ending at state s into one Q-value (making it
     171             :      *   practically very similar to a shortest-path problem)
     172             :      */
     173             :     mutable QMatrixType QMatrix;
     174             :     // mutable std::unordered_map<
     175             :     //     State,
     176             :     //     Reward,
     177             :     //     utils::reference_wrapper::hash    <State::type>,
     178             :     //     utils::reference_wrapper::equal_to<State::type>
     179             :     // > QMatrix;
     180             : 
     181             :    protected:
     182             :     std::optional<std::reference_wrapper<Logger>> policyLogger;
     183             : 
     184             :     virtual Reward estimateOptimalValue(const State& s) const;
     185             :     virtual Reward estimateOptimalFutureValue(const State& s, const Action& a) const;
     186             : 
     187             :     virtual Action heuristicPolicy(const State& s) const;
     188             :     virtual Reward heuristic(const State& s, const Action& a) const;
     189             : 
     190             :     virtual Reward tabu(const State& s, const Action& a, const Env::Vehicle& vehicle) const;
     191             : 
     192             :     virtual Reward& Qref(const State& s, const Action& a);
     193             :     virtual Reward  Q(const State& s, const Action& a) const;
     194             : 
     195             :     virtual void updateMatrix(const State& s, const Action& a, Reward reward);
     196             : 
     197             :     virtual Reward estimateInitialValue(const State& s, const Action& a) const;
     198             : 
     199             :    public:
     200             :     QLearner(
     201             :         Env::Env&                                               env,
     202             :         const SUMO::Network&                                    network,
     203             :         const Dynamic::SUMOAdapter&                             adapter,
     204             :         const Env::TAZ&                                         destinationTAZ,
     205             :         std::optional<std::reference_wrapper<QLearner::Logger>> policyLogger = std::nullopt,
     206             :         Reward                                                  alpha        = 0.5,
     207             :         Reward                                                  gamma        = 1.0,
     208             :         Reward                                                  xi           = 0.0,
     209             :         Reward                                                  eta          = 1.0,
     210             :         float                                                   epsilon      = 1.0e-3f
     211             :     );
     212             : 
     213             :     void setAlpha(Reward alpha);
     214             :     void setEpsilon(float epsilon);
     215             : 
     216             :     void dump() const;
     217             : };
     218             : }  // namespace Dynamic

Generated by: LCOV version 1.14