GEGELATI

Loading...
Searching...
No Matches
environment.h
1
38#ifndef ENVIRONMENT_H
39#define ENVIRONMENT_H
40
41#include <cmath>
42#include <iostream>
43
44#include "data/constantHandler.h"
45#include "data/dataHandler.h"
46#include "data/primitiveTypeArray.h"
47#include "instructions/instruction.h"
48#include "instructions/set.h"
49#include "learn/learningParameters.h"
50
52 typedef struct LineSize
53{
55 size_t nbInstructionBits;
57 size_t nbDestinationBits;
59 size_t nbOperandsBits;
61 size_t nbOperandDataSourceIndexBits;
63 size_t nbOperandLocationBits;
65 size_t totalNbBits;
66
68 operator size_t() const
69 {
70 return totalNbBits;
71 }
72} LineSize;
73
85 class Environment
86{
87 protected:
89 const Instructions::Set instructionSet;
90
92 const Learn::LearningParameters params;
93
95 const std::vector<std::reference_wrapper<const Data::DataHandler>>
96 dataSources;
97
99 const size_t nbRegisters;
100
102 const size_t nbConstants;
103
105 std::vector<std::reference_wrapper<const Data::DataHandler>>
106 fakeDataSources;
107
109 const Data::PrimitiveTypeArray<double> fakeRegisters;
110
112 const Data::ConstantHandler fakeConstants;
113
115 const size_t nbContinuousActions;
116
118 const size_t nbInstructions;
119
121 const size_t maxNbOperands;
122
124 const size_t nbDataSources;
125
127 const size_t largestAddressSpace;
128
130 const LineSize lineSize;
131
142 static size_t computeLargestAddressSpace(
143 const size_t nbRegisters, const size_t nbConstants,
144 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
145 dHandlers);
146
165 static const LineSize computeLineSize(const Environment& env);
166
178 static Instructions::Set filterInstructionSet(
179 const Instructions::Set& iSet, const size_t nbRegisters,
180 const size_t nbConstants,
181 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
182 dataSources);
183
184 private:
186 Environment() = delete;
187
188 public:
204 Environment(
205 const Instructions::Set& iSet, const Learn::LearningParameters& p,
206 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
207 dHandlers,
208 size_t nbContinuousAct = 0)
209 : instructionSet{filterInstructionSet(iSet, p.nbRegisters,
210 p.nbProgramConstant, dHandlers)},
211 params{p}, dataSources{dHandlers}, nbRegisters{p.nbRegisters},
212 nbConstants{p.nbProgramConstant}, fakeRegisters(p.nbRegisters),
213 fakeConstants(p.nbProgramConstant),
214 nbInstructions{instructionSet.getNbInstructions()},
215 maxNbOperands{instructionSet.getMaxNbOperands()},
216 nbContinuousActions{nbContinuousAct},
217 nbDataSources{
218 dHandlers.size() +
219 (p.nbProgramConstant > 0
220 ? 2
221 : 1)}, // if Constants are used, we need an extra
222 // datasource to store them in the environment
223 largestAddressSpace{computeLargestAddressSpace(
224 p.nbRegisters, p.nbProgramConstant, dHandlers)},
225 lineSize{computeLineSize(*this)}
226 {
227 this->fakeDataSources.push_back(
228 (std::reference_wrapper<const Data::DataHandler>)this
229 ->fakeRegisters);
230
231 if (p.nbProgramConstant > 0) {
232 this->fakeDataSources.push_back(this->fakeConstants);
233 }
234
235 for (auto& elem : this->dataSources)
236 this->fakeDataSources.push_back(elem);
237 };
238
242 const Learn::LearningParameters& getParams() const;
243
249 size_t getNbContinuousActions() const;
250
257 size_t getNbInstructions() const;
258
265 size_t getMaxNbOperands() const;
266
272 size_t getNbDataSources() const;
273
279 size_t getLargestAddressSpace() const;
280
286 const LineSize& getLineSize() const;
287
294 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
295 getDataSources() const;
296
305 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
306 getFakeDataSources() const;
307
314 const Instructions::Set& getInstructionSet() const;
315};
316
317#endif
Data::ConstantHandler
Data::DataHandler used by Program::Program to handle their set of Constant values.
Definition constantHandler.h:56
Data::PrimitiveTypeArray2D
DataHandler for 2D arrays of primitive types.
Definition primitiveTypeArray2D.h:69
Environment
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
Environment::getDataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the Environment.
Definition environment.cpp:204
Environment::fakeRegisters
const Data::PrimitiveTypeArray< double > fakeRegisters
DataHandler whost type corresponds to registers.
Definition environment.h:109
Environment::getLargestAddressSpace
size_t getLargestAddressSpace() const
Get the size of the largestAddressSpace of DataHandlers.
Definition environment.cpp:193
Environment::getParams
const Learn::LearningParameters & getParams() const
Get the instance of parameters used.
Definition environment.cpp:168
Environment::getLineSize
const LineSize & getLineSize() const
Get the size of the line for this environment (in bits).
Definition environment.cpp:198
Environment::params
const Learn::LearningParameters params
Parameters for the learning process.
Definition environment.h:92
Environment::fakeConstants
const Data::ConstantHandler fakeConstants
DataHandler whost type corresponds to the programs constants.
Definition environment.h:112
Environment::nbContinuousActions
const size_t nbContinuousActions
Number of continuous actions.
Definition environment.h:115
Environment::lineSize
const LineSize lineSize
Size of lines within this Environment.
Definition environment.h:130
Environment::dataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > dataSources
List of DataHandler that can be accessed within this Environment.
Definition environment.h:96
Environment::getNbInstructions
size_t getNbInstructions() const
Get the size of the number of Instruction within the Instructions::Set.
Definition environment.cpp:178
Environment::getNbContinuousActions
size_t getNbContinuousActions() const
Get the number of continuous actions.
Definition environment.cpp:173
Environment::nbRegisters
const size_t nbRegisters
Number of registers.
Definition environment.h:99
Environment::fakeDataSources
std::vector< std::reference_wrapper< const Data::DataHandler > > fakeDataSources
Vector of DataHandlers containing the environment's dataSources.
Definition environment.h:106
Environment::maxNbOperands
const size_t maxNbOperands
Maxmimum number of operands of the Instructions::Set.
Definition environment.h:121
Environment::filterInstructionSet
static Instructions::Set filterInstructionSet(const Instructions::Set &iSet, const size_t nbRegisters, const size_t nbConstants, const std::vector< std::reference_wrapper< const Data::DataHandler > > &dataSources)
Filter an InstructionSet to keep only Instruction with operand types provided by the given DataHandle...
Definition environment.cpp:56
Environment::nbConstants
const size_t nbConstants
Number of constants.
Definition environment.h:102
Environment::getFakeDataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getFakeDataSources() const
Definition environment.cpp:210
Environment::getMaxNbOperands
size_t getMaxNbOperands() const
Get the size of the maximum number of operands of Instructions::Set.
Definition environment.cpp:183
Environment::instructionSet
const Instructions::Set instructionSet
Set of Instruction used by Program running within this Environment.
Definition environment.h:89
Environment::getNbDataSources
size_t getNbDataSources() const
Get the size of the number of DataHandlers.
Definition environment.cpp:188
Environment::Environment
Environment(const Instructions::Set &iSet, const Learn::LearningParameters &p, const std::vector< std::reference_wrapper< const Data::DataHandler > > &dHandlers, size_t nbContinuousAct=0)
Constructor with initialization of all attributes.
Definition environment.h:204
Environment::nbDataSources
const size_t nbDataSources
Number of DataHandler from which data can be accessed.
Definition environment.h:124
Environment::largestAddressSpace
const size_t largestAddressSpace
Size of the largestAddressSpace of DataHandlers.
Definition environment.h:127
Environment::nbInstructions
const size_t nbInstructions
Number of Instruction in the Instructions::Set.
Definition environment.h:118
Environment::getInstructionSet
const Instructions::Set & getInstructionSet() const
Get the Instruction Set of the Environment.
Definition environment.cpp:215
Environment::computeLineSize
static const LineSize computeLineSize(const Environment &env)
Static method used to compute the size of Program lines based on information from the Enviroment.
Definition environment.cpp:125
Environment::computeLargestAddressSpace
static size_t computeLargestAddressSpace(const size_t nbRegisters, const size_t nbConstants, const std::vector< std::reference_wrapper< const Data::DataHandler > > &dHandlers)
Static method used when constructing a new Environment to compute the largest AddressSpace of a set o...
Definition environment.cpp:43
Instructions::Set
Class for storing a set of Instruction.
Definition set.h:55
Learn::LearningParameters
Structure for simplifying the transmission of LearningParameters to functions.
Definition learningParameters.h:54
Learn::LearningParameters::nbProgramConstant
size_t nbProgramConstant
Number of Constants available in a program.
Definition learningParameters.h:194
LineSize
LineSize structure to be used within the Environment.
Definition environment.h:53
LineSize::nbInstructionBits
size_t nbInstructionBits
Number of bits used to encode the instructionIndex.
Definition environment.h:55
LineSize::nbOperandLocationBits
size_t nbOperandLocationBits
Number of bits used for each operand pair, to encode location.
Definition environment.h:63
LineSize::nbDestinationBits
size_t nbDestinationBits
Number of bits used to encode the destinationIndex.
Definition environment.h:57
LineSize::nbOperandsBits
size_t nbOperandsBits
Total number of bits used to encode the operands info.
Definition environment.h:59
LineSize::nbOperandDataSourceIndexBits
size_t nbOperandDataSourceIndexBits
Number of bits used for each operand pair, to encode dataSourceIndex.
Definition environment.h:61
LineSize::totalNbBits
size_t totalNbBits
Total number of bits to encode a program line.
Definition environment.h:65