Line data Source code
1 : #include "Dynamic/Policy/RandomPolicy.hpp" 2 : 3 : #include <list> 4 : #include <random> 5 : 6 : #include "Dynamic/Env/Connection.hpp" 7 : #include "Dynamic/Env/Env.hpp" 8 : #include "Dynamic/Env/Lane.hpp" 9 : 10 : using namespace std; 11 : using namespace Dynamic; 12 : 13 0 : RandomPolicy::Action::Action( 14 : Env::Connection &connection_, 15 : Env::Lane &lane_ 16 0 : ): 17 0 : Env::Action(connection_, lane_) {} 18 : 19 0 : void RandomPolicy::Action::reward(Reward) { 20 : // Do nothing 21 0 : } 22 : 23 0 : RandomPolicy::RandomPolicy(Vehicle::ID id_, shared_ptr<mt19937> gen_): 24 0 : id(id_), gen(gen_) {} 25 : 26 0 : Env::Lane &RandomPolicy::pickInitialLane( 27 : Vehicle &vehicle, 28 : Env::Env & 29 : ) { 30 0 : auto &sources = vehicle.fromTAZ.sources; 31 : 32 0 : uniform_int_distribution<size_t> sourcesDistribution(0, sources.size() - 1); 33 : 34 0 : auto sourceIt = sources.begin(); 35 0 : advance(sourceIt, sourcesDistribution(*gen)); 36 0 : Env::Edge &source = *sourceIt; 37 : 38 0 : auto &lanes = source.lanes; 39 : 40 0 : uniform_int_distribution<size_t> lanesDistribution(0, lanes.size() - 1); 41 : 42 0 : auto laneIt = lanes.begin(); 43 0 : advance(laneIt, lanesDistribution(*gen)); 44 : 45 0 : Env::Lane &lane = *laneIt; 46 : 47 0 : return lane; 48 : } 49 : 50 0 : shared_ptr<Env::Action> RandomPolicy::pickConnection( 51 : Env::Env &env 52 : ) { 53 0 : Env::Vehicle &vehicle = env.getVehicle(id); 54 : 55 0 : Env::Edge &edge = vehicle.position.lane.edge; 56 : 57 0 : list<reference_wrapper<Env::Connection>> connections = edge.getOutgoingConnections(); 58 : 59 0 : if(connections.size() == 0) { 60 0 : return make_shared<RandomPolicy::Action>(Env::Connection::LEAVE, Env::Lane::INVALID); 61 : } 62 : 63 0 : uniform_int_distribution<size_t> connectionsDistribution(0, connections.size() - 1); 64 : 65 0 : auto it = connections.begin(); 66 0 : advance(it, connectionsDistribution(*gen)); 67 : 68 0 : Env::Connection &connection = *it; 69 : 70 0 : vector<Env::Lane> &lanes = connection.toLane.edge.lanes; 71 : 72 0 : uniform_int_distribution<size_t> lanesDistribution(0, lanes.size() - 1); 73 : 74 0 : auto itLane = lanes.begin(); 75 0 : advance(itLane, lanesDistribution(*gen)); 76 : 77 0 : Env::Lane &lane = *itLane; 78 : 79 0 : return make_shared<RandomPolicy::Action>(connection, lane); 80 : } 81 : 82 0 : RandomPolicy::Factory::Factory(): 83 0 : gen(make_shared<mt19937>(0)) {} 84 : 85 0 : RandomPolicy::Factory::Factory(random_device::result_type seed) { 86 0 : gen = make_shared<mt19937>(seed); 87 0 : } 88 : 89 0 : shared_ptr<Policy> RandomPolicy::Factory::create( 90 : Vehicle::ID id, 91 : Time, 92 : const Env::TAZ &, 93 : const Env::TAZ & 94 : ) { 95 0 : return make_shared<RandomPolicy>(id, gen); 96 : }