ECF 1.5
PrimitiveSet.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3
4
5namespace Tree
6{
7
8PrimitiveSet::PrimitiveSet()
9{ }
10
11
12PrimitiveSet::~PrimitiveSet()
13{ }
14
15
16bool PrimitiveSet::initialize(StateP state)
17{
18 state_ = state;
19
20 // register existing primitives
21 PrimitiveP prim = (PrimitiveP) (new Primitives::Add);
22 mAllPrimitives_[prim->getName()] = prim;
23 prim = (PrimitiveP) (new Primitives::Sub);
24 mAllPrimitives_[prim->getName()] = prim;
25 prim = (PrimitiveP) (new Primitives::Mul);
26 mAllPrimitives_[prim->getName()] = prim;
27 prim = (PrimitiveP) (new Primitives::Div);
28 mAllPrimitives_[prim->getName()] = prim;
29 prim = (PrimitiveP) (new Primitives::Sin);
30 mAllPrimitives_[prim->getName()] = prim;
31 prim = (PrimitiveP) (new Primitives::Cos);
32 mAllPrimitives_[prim->getName()] = prim;
33 prim = (PrimitiveP) (new Primitives::Pos);
34 mAllPrimitives_[prim->getName()] = prim;
35 prim = (PrimitiveP) (new Primitives::Neg);
36 mAllPrimitives_[prim->getName()] = prim;
37 prim = (PrimitiveP) (new Primitives::Max);
38 mAllPrimitives_[prim->getName()] = prim;
39 prim = (PrimitiveP) (new Primitives::Min);
40 mAllPrimitives_[prim->getName()] = prim;
41
42 prim_iter primIter;
43 for(primIter = mAllPrimitives_.begin(); primIter != mAllPrimitives_.end(); ++primIter)
44 primIter->second->initialize(state);
45
46 // register terminal types
47 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("DOUBLE", Primitives::Double));
48 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("INT", Primitives::Int));
49 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("BOOL", Primitives::Bool));
50 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("CHAR", Primitives::Char));
51 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("STRING", Primitives::String));
52
53 return true;
54}
55
56
59{
60 return functionSet_[state_->getRandomizer()->getRandomInteger((int) functionSet_.size())];
61}
62
63
66{
67 return terminalSet_[state_->getRandomizer()->getRandomInteger((int) terminalSet_.size())];
68}
69
70
73{
74 return primitives_[state_->getRandomizer()->getRandomInteger((int) primitives_.size())];
75}
76
77
79PrimitiveP PrimitiveSet::getFunctionByName(std::string name)
80{
81 prim_iter iter = mFunctionSet_.find(name);
82 if(iter == mFunctionSet_.end())
83 return PrimitiveP();
84
85 return iter->second;
86}
87
88
90PrimitiveP PrimitiveSet::getTerminalByName(std::string name)
91{
92 prim_iter iter = mTerminalSet_.find(name);
93 if(iter == mTerminalSet_.end())
94 return PrimitiveP();
95
96 return iter->second;
97}
98
99
101PrimitiveP PrimitiveSet::getPrimitiveByName(std::string name)
102{
103 prim_iter iter = mPrimitiveSet_.find(name);
104 if(iter == mPrimitiveSet_.end())
105 return PrimitiveP();
106
107 return iter->second;
108}
109
110
111//void PrimitiveSet::addFunction(PrimitiveP functionPrimitive)
112//{
113// if(functionPrimitive->getNumberOfArguments() == 0) {
114// terminalSet_.push_back(functionPrimitive);
115// mTerminalSet_[functionPrimitive->getName()] = functionPrimitive;
116// }
117// else {
118// functionSet_.push_back(functionPrimitive);
119// mFunctionSet_[functionPrimitive->getName()] = functionPrimitive;
120// }
121//
122// primitives_.push_back(functionPrimitive);
123// mPrimitives_[functionPrimitive->getName()] = functionPrimitive;
124//}
125
126
131bool PrimitiveSet::addFunction(std::string name)
132{
133 prim_iter iter = mAllPrimitives_.find(name);
134 if(iter == mAllPrimitives_.end())
135 return false;
136
137 if(iter->second->getNumberOfArguments() == 0) {
138 terminalSet_.push_back(iter->second);
139 mTerminalSet_[iter->first] = iter->second;
140 }
141 else {
142 functionSet_.push_back(iter->second);
143 mFunctionSet_[iter->first] = iter->second;
144 }
145
146 primitives_.push_back(iter->second);
147 mPrimitiveSet_[iter->first] = iter->second;
148
149 return true;
150}
151
152
156void PrimitiveSet::addTerminal(PrimitiveP terminalPrimitive)
157{
158 terminalSet_.push_back(terminalPrimitive);
159 mTerminalSet_[terminalPrimitive->getName()] = terminalPrimitive;
160
161 primitives_.push_back(terminalPrimitive);
162 mPrimitiveSet_[terminalPrimitive->getName()] = terminalPrimitive;
163}
164
165
168{
169 return (uint) functionSet_.size();
170}
171
172
175{
176 return (uint) terminalSet_.size();
177}
178
179
182{
183 return (uint) primitives_.size();
184}
185
186}
PrimitiveP getRandomFunction()
Get random function from the set of active functions.
uint getPrimitivesSize()
Get the number of active primitives (functions and terminals).
std::vector< PrimitiveP > primitives_
vector of active (actual used) primitives
Definition: PrimitiveSet.h:52
PrimitiveP getTerminalByName(std::string name)
Access terminal by name (active terminals only).
std::map< std::string, PrimitiveP > mTerminalSet_
map of active (actual used) terminals
Definition: PrimitiveSet.h:49
void addTerminal(PrimitiveP terminalPrimitive)
Add a terminal primitive to the set of active primitives.
PrimitiveP getRandomPrimitive()
Get random primitive (function or terminal) from the set of active primitives.
std::map< std::string, PrimitiveP > mPrimitiveSet_
map of active (actual used) primitives
Definition: PrimitiveSet.h:53
uint getTerminalSetSize()
Get the number of active terminals.
uint getFunctionSetSize()
Get the number of active functions.
std::map< std::string, PrimitiveP > mFunctionSet_
map of active (actual used) functions
Definition: PrimitiveSet.h:51
bool addFunction(std::string name)
Add a function primitive to the set of active primitives - if found by name in collection of all prim...
PrimitiveP getPrimitiveByName(std::string name)
Access primitive by name (active functions or terminals only).
std::vector< PrimitiveP > terminalSet_
vector of active (actual used) terminals
Definition: PrimitiveSet.h:48
PrimitiveP getFunctionByName(std::string name)
Access function by name (active functions only).
std::vector< PrimitiveP > functionSet_
vector of active (actual used) functions
Definition: PrimitiveSet.h:50
std::map< std::string, PrimitiveP > mAllPrimitives_
map of all registered primitive functions
Definition: PrimitiveSet.h:42
PrimitiveP getRandomTerminal()
Get random terminal from the set of active terminals.