GEGELATI

Loading...
Searching...
No Matches
learningAgent.h
1
39#ifndef LEARNING_AGENT_H
40#define LEARNING_AGENT_H
41
42#include <map>
43#include <queue>
44
45#include "archive.h"
46#include "environment.h"
47#include "instructions/set.h"
48#include "log/laLogger.h"
49#include "mutator/mutationParameters.h"
50#include "tpg/tpgExecutionEngine.h"
51#include "tpg/tpgGraph.h"
52
53#include "learn/evaluationResult.h"
54#include "learn/job.h"
55#include "learn/learningEnvironment.h"
56#include "learn/learningParameters.h"
57 namespace Learn {
58
63 class LearningAgent
64 {
65 protected:
67 LearningEnvironment& learningEnvironment;
68
70 Environment env;
71
73 Archive archive;
74
76 LearningParameters params;
77
79 std::shared_ptr<TPG::TPGGraph> tpg;
80
83 std::pair<const TPG::TPGVertex*, std::shared_ptr<EvaluationResult>>
84 bestRoot{nullptr, nullptr};
85
99 std::map<const TPG::TPGVertex*, std::shared_ptr<EvaluationResult>>
100 resultsPerRoot;
101
103 Mutator::RNG rng;
104
106 uint64_t maxNbThreads = 1;
107
115 std::vector<std::reference_wrapper<Log::LALogger>> loggers;
116
119 double bestScoreLastGen = 0.0;
120
121 public:
132 LearningAgent(LearningEnvironment& le, const Instructions::Set& iSet,
133 const LearningParameters& p,
134 const TPG::TPGFactory& factory = TPG::TPGFactory())
135 : learningEnvironment{le},
136 env(iSet, p, le.getDataSources(),
137 (le.isDiscrete()) ? 0 : le.getNbActions()),
138 tpg(factory.createTPGGraph(env)), params{p},
139 archive(p.archiveSize, p.archivingProbability){};
140
142 virtual ~LearningAgent() = default;
143
149 std::shared_ptr<TPG::TPGGraph> getTPGGraph();
150
156 const Archive& getArchive() const;
157
163 const Environment& getEnvironment() const;
164
170 Mutator::RNG& getRNG();
171
182 void addLogger(Log::LALogger& logger);
183
212 virtual std::shared_ptr<EvaluationResult> evaluateJob(
213 TPG::TPGExecutionEngine& tee, const Job& job,
214 uint64_t generationNumber, LearningMode mode,
215 LearningEnvironment& le) const;
216
232 bool isRootEvalSkipped(
233 const TPG::TPGVertex& root,
234 std::shared_ptr<Learn::EvaluationResult>& previousResult) const;
235
248 virtual std::multimap<std::shared_ptr<EvaluationResult>,
249 const TPG::TPGVertex*>
250 evaluateAllRoots(uint64_t generationNumber, LearningMode mode);
251
269 virtual std::shared_ptr<EvaluationResult> evaluateOneRoot(
270 uint64_t generationNumber, LearningMode mode,
271 const TPG::TPGVertex* root);
272
284 virtual void trainOneGeneration(uint64_t generationNumber);
285
303 virtual void decimateWithTournament(
304 std::multimap<std::shared_ptr<EvaluationResult>,
305 const TPG::TPGVertex*>& results);
306
320 virtual void decimateWorstRoots(
321 std::multimap<std::shared_ptr<EvaluationResult>,
322 const TPG::TPGVertex*>& results);
323
338 virtual uint64_t train(volatile bool& altTraining,
339 bool printProgressBar);
340
361 virtual void updateEvaluationRecords(
362 const std::multimap<std::shared_ptr<EvaluationResult>,
363 const TPG::TPGVertex*>& results);
364
373 virtual void forgetPreviousResults();
374
381 virtual void updateBestScoreLastGen(
382 std::multimap<std::shared_ptr<Learn::EvaluationResult>,
383 const TPG::TPGVertex*>& results);
384
390 virtual double getBestScoreLastGen() const;
391
400 virtual const std::pair<const TPG::TPGVertex*,
401 std::shared_ptr<EvaluationResult>>&
402 getBestRoot() const;
403
410 virtual void keepBestPolicy();
411
427 virtual std::shared_ptr<Learn::Job> makeJob(
428 const TPG::TPGVertex* vertex, Learn::LearningMode mode, int idx = 0,
429 TPG::TPGGraph* tpgGraph = nullptr);
430
442 virtual std::queue<std::shared_ptr<Learn::Job>> makeJobs(
443 Learn::LearningMode mode, TPG::TPGGraph* tpgGraph = nullptr);
444
454 virtual void init(uint64_t seed = 0);
455 };
456}; // namespace Learn
457
458#endif
Archive
Definition archive.h:80
Environment
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
Instructions::Set
Class for storing a set of Instruction.
Definition set.h:55
Learn::Job
This class embeds roots for the simulations.
Definition job.h:53
Learn::LearningAgent
Class used to control the learning steps of a TPGGraph within a given LearningEnvironment.
Definition learningAgent.h:64
Learn::LearningAgent::addLogger
void addLogger(Log::LALogger &logger)
Adds a LALogger to the loggers vector.
Definition learningAgent.cpp:88
Learn::LearningAgent::getTPGGraph
std::shared_ptr< TPG::TPGGraph > getTPGGraph()
Getter for the TPGGraph built by the LearningAgent.
Definition learningAgent.cpp:51
Learn::LearningAgent::maxNbThreads
uint64_t maxNbThreads
Control the maximum number of threads when running in parallel.
Definition learningAgent.h:106
Learn::LearningAgent::getArchive
const Archive & getArchive() const
Getter for the Archive filled by the LearningAgent.
Definition learningAgent.cpp:56
Learn::LearningAgent::evaluateAllRoots
virtual std::multimap< std::shared_ptr< EvaluationResult >, const TPG::TPGVertex * > evaluateAllRoots(uint64_t generationNumber, LearningMode mode)
Evaluate all root TPGVertex of the TPGGraph.
Definition learningAgent.cpp:184
Learn::LearningAgent::params
LearningParameters params
Parameters for the learning process.
Definition learningAgent.h:76
Learn::LearningAgent::trainOneGeneration
virtual void trainOneGeneration(uint64_t generationNumber)
Train the TPGGraph for one generation.
Definition learningAgent.cpp:239
Learn::LearningAgent::resultsPerRoot
std::map< const TPG::TPGVertex *, std::shared_ptr< EvaluationResult > > resultsPerRoot
Map associating root TPG::TPGVertex to their EvaluationResult.
Definition learningAgent.h:100
Learn::LearningAgent::LearningAgent
LearningAgent(LearningEnvironment &le, const Instructions::Set &iSet, const LearningParameters &p, const TPG::TPGFactory &factory=TPG::TPGFactory())
Constructor for LearningAgent.
Definition learningAgent.h:132
Learn::LearningAgent::getBestScoreLastGen
virtual double getBestScoreLastGen() const
Get the best score reached at the last generation trained.
Definition learningAgent.cpp:542
Learn::LearningAgent::archive
Archive archive
Archive used during the training process.
Definition learningAgent.h:73
Learn::LearningAgent::init
virtual void init(uint64_t seed=0)
Initialize the LearningAgent.
Definition learningAgent.cpp:71
Learn::LearningAgent::forgetPreviousResults
virtual void forgetPreviousResults()
This method resets the previous registered scores per root.
Definition learningAgent.cpp:601
Learn::LearningAgent::makeJobs
virtual std::queue< std::shared_ptr< Learn::Job > > makeJobs(Learn::LearningMode mode, TPG::TPGGraph *tpgGraph=nullptr)
Puts all roots into jobs to be able to use them in simulation later.
Definition learningAgent.cpp:586
Learn::LearningAgent::evaluateOneRoot
virtual std::shared_ptr< EvaluationResult > evaluateOneRoot(uint64_t generationNumber, LearningMode mode, const TPG::TPGVertex *root)
Evaluate one root TPGVertex of the TPGGraph.
Definition learningAgent.cpp:209
Learn::LearningAgent::rng
Mutator::RNG rng
Random Number Generator for this Learning Agent.
Definition learningAgent.h:103
Learn::LearningAgent::learningEnvironment
LearningEnvironment & learningEnvironment
LearningEnvironment with which the LearningAgent will interact.
Definition learningAgent.h:67
Learn::LearningAgent::train
virtual uint64_t train(volatile bool &altTraining, bool printProgressBar)
Train the TPGGraph for a given number of generation.
Definition learningAgent.cpp:437
Learn::LearningAgent::decimateWorstRoots
virtual void decimateWorstRoots(std::multimap< std::shared_ptr< EvaluationResult >, const TPG::TPGVertex * > &results)
Removes from the TPGGraph the root TPGVertex with the worst results.
Definition learningAgent.cpp:359
Learn::LearningAgent::loggers
std::vector< std::reference_wrapper< Log::LALogger > > loggers
Set of LALogger called throughout the training process.
Definition learningAgent.h:115
Learn::LearningAgent::bestRoot
std::pair< const TPG::TPGVertex *, std::shared_ptr< EvaluationResult > > bestRoot
Definition learningAgent.h:84
Learn::LearningAgent::isRootEvalSkipped
bool isRootEvalSkipped(const TPG::TPGVertex &root, std::shared_ptr< Learn::EvaluationResult > &previousResult) const
Method detecting whether a root should be evaluated again.
Definition learningAgent.cpp:96
Learn::LearningAgent::~LearningAgent
virtual ~LearningAgent()=default
Default destructor for polymorphism.
Learn::LearningAgent::getRNG
Mutator::RNG & getRNG()
Getter for the RNG used by the LearningAgent.
Definition learningAgent.cpp:66
Learn::LearningAgent::keepBestPolicy
virtual void keepBestPolicy()
This method keeps only the bestRoot policy in the TPGGraph.
Definition learningAgent.cpp:547
Learn::LearningAgent::updateBestScoreLastGen
virtual void updateBestScoreLastGen(std::multimap< std::shared_ptr< Learn::EvaluationResult >, const TPG::TPGVertex * > &results)
This method update the best score reached at the last generation trained.
Definition learningAgent.cpp:534
Learn::LearningAgent::tpg
std::shared_ptr< TPG::TPGGraph > tpg
TPGGraph built during the learning process.
Definition learningAgent.h:79
Learn::LearningAgent::decimateWithTournament
virtual void decimateWithTournament(std::multimap< std::shared_ptr< EvaluationResult >, const TPG::TPGVertex * > &results)
Removes from the TPGGraph the root TPGVertex with the worst results with tournament selection.
Definition learningAgent.cpp:293
Learn::LearningAgent::bestScoreLastGen
double bestScoreLastGen
Definition learningAgent.h:119
Learn::LearningAgent::env
Environment env
Environment for executing Program of the LearningAgent.
Definition learningAgent.h:70
Learn::LearningAgent::evaluateJob
virtual std::shared_ptr< EvaluationResult > evaluateJob(TPG::TPGExecutionEngine &tee, const Job &job, uint64_t generationNumber, LearningMode mode, LearningEnvironment &le) const
Evaluates policy starting from the given root.
Definition learningAgent.cpp:115
Learn::LearningAgent::getBestRoot
virtual const std::pair< const TPG::TPGVertex *, std::shared_ptr< EvaluationResult > > & getBestRoot() const
Get the best root TPG::Vertex encountered since the last init.
Definition learningAgent.cpp:529
Learn::LearningAgent::makeJob
virtual std::shared_ptr< Learn::Job > makeJob(const TPG::TPGVertex *vertex, Learn::LearningMode mode, int idx=0, TPG::TPGGraph *tpgGraph=nullptr)
Takes a given TPGVertex and creates a job containing it. Useful for example in adversarial mode where...
Definition learningAgent.cpp:565
Learn::LearningAgent::getEnvironment
const Environment & getEnvironment() const
Accessor to the Environment of the TPGGraph.
Definition learningAgent.cpp:61
Learn::LearningAgent::updateEvaluationRecords
virtual void updateEvaluationRecords(const std::multimap< std::shared_ptr< EvaluationResult >, const TPG::TPGVertex * > &results)
Update the bestRoot and resultsPerRoot attributes.
Definition learningAgent.cpp:481
Learn::LearningEnvironment
Interface for creating a Learning Environment.
Definition learningEnvironment.h:81
Log::LALogger
Learning Agent logger class that will be called during LearningAgent executions.
Definition laLogger.h:65
Mutator::RNG
Definition rng.h:52
TPG::TPGExecutionEngine
Definition tpgExecutionEngine.h:57
TPG::TPGFactory
Factory for creating all elements constituting a TPG.
Definition tpgFactory.h:71
TPG::TPGGraph
Class for storing a Tangled-Program-Graph.
Definition tpgGraph.h:58
TPG::TPGVertex
Abstract class representing the vertices of a TPGGraph.
Definition tpgVertex.h:55
Learn
Definition adversarialEvaluationResult.h:45
Learn::LearningMode
LearningMode
Different modes in which the LearningEnvironment can be reset.
Definition learningEnvironment.h:59
Learn::LearningParameters
Structure for simplifying the transmission of LearningParameters to functions.
Definition learningParameters.h:54