-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.cpp
197 lines (162 loc) · 4.66 KB
/
tree_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdexcept>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <vector>
#include <utils/random.hpp>
#include <tree/tree.hpp>
#include <tree/node.hpp>
#include <tree/function_db.hpp>
#include <tree/generator.hpp>
#include <tree/root_generator.hpp>
#include <tree/crossover.hpp>
#include <tree/limit_crossover.hpp>
#include <tree/mutator.hpp>
#include <utils/utils.hpp>
#include <utils/io.hpp>
Tree::TermType add(Tree::Args args)
{
return args[0]+args[1];
}
Tree::TermType sub(Tree::Args args)
{
return args[0]-args[1];
}
Tree::TermType mul(Tree::Args args)
{
return args[0]*args[1];
}
Tree::TermType inc(Tree::Args args)
{
return args[0]+1;
}
Tree::TermType div_func(Tree::Args args)
{
if (args[1]!=0)
return args[0]/args[1];
else
return args[0];
}
int main(int argc,char*argv[])
{
Tree::FunctionDB* fdb=new Tree::FunctionDB;
fdb->add_function(new Tree::FunctionNode("+",2,add)); // 0
fdb->add_function(new Tree::FunctionNode("-",2,sub)); // 1
fdb->add_function(new Tree::FunctionNode("*",2,mul)); // 2
fdb->add_function(new Tree::FunctionNode("/",2,div_func)); //3
fdb->add_variable("x1"); // 4
fdb->add_variable("x2"); // 5
for(double i=0.0;i<1.0;i+=0.1)
fdb->add_constant(i);
LOG("functions="<<fdb->functions());
LOG("variables="<<fdb->variables());
LOG("constants="<<fdb->constants());
Tree::Tree t3;
t3.set_fdb(fdb);
int root=t3.add_node(0); // +
int x1=t3.add_node(4); // x1
int x2=t3.add_node(5); // x5
t3.connect(root,x1);
t3.connect(root,x2);
t3.set_root(root);
LOG(t3.to_str());
Tree::VarMap vm;
vm["x1"]=1;
vm["x2"]=2;
LOG("Eval test")
if(t3.eval(vm)!=3){
throw std::logic_error("test_error : t3.eval(vm)!=3,"+boost::lexical_cast<std::string>(t3.eval(vm)));
}
LOG("Flat test")
ivector flat_test;
flat_test<<0<<0<<1<<0<<2<<-1<<0<<0<<1<<4<<2<<5;
if(t3.flat()!=flat_test){
LOG(t3.flat());
throw std::logic_error("test_error : t3.flat()");
}
Tree::Tree t4(fdb,t3.flat());
LOG(t4.to_str());
LOG("Sub tree");
LOG(t4.sub_tree(t4.root()).to_str());
t4.remove_node(t4.root());
if(t4.size()!=0){
LOG(t4.size());
throw std::logic_error("test_error : t4.size()");
}
LOG("SetSubTree");
Tree::Tree t33=t3.clone();
t3.set_sub_tree(x1,t33);
LOG("t3 "<<t3.to_str());
LOG("sub_tree "<<t3.sub_tree(t3.root()).to_str());
Tree::Tree t_sc2;
t_sc2.set_fdb(fdb);
int sc_02=t_sc2.add_node(8); // 0.2
t_sc2.set_root(sc_02);
LOG("sub_tree: "<<t_sc2.sub_tree(t_sc2.root()).to_str())
LOG("Generator test");
Tree::Generator*gnrt=new Tree::Generator(fdb,2);
for(int i=0;i<5;++i){
Tree::Tree t(gnrt->generate());
LOG(" ==> "<<t.eval(vm));
}
LOG("Root Generator");
ivector tiv(1);
tiv[0]=1;
Tree::Generator*root_gnrt=new Tree::RootGenerator(fdb,2,0,tiv);
for(int i=0;i<5;++i){
Tree::Tree t(root_gnrt->generate());
LOG(" ==> "<<t.to_str());
LOG(" ++> "<<t.eval(vm));
}
LOG("Crossover test");
Tree::Crossover crossover;
for(int i=0;i<10;++i){
Tree::Tree f(gnrt->generate());
Tree::Tree m(gnrt->generate());
LOG(' '<<f.eval(vm));
LOG(' '<<f.eval(vm));
Tree::Tree child=crossover.cross(f,m);
LOG("child="<<child.eval(vm));
}
LOG("Limit test");
ivector limits;
limits<<0<<1;
Tree::Crossover*limit_crossover=new Tree::LimitCrossover(limits);
for(int i=0;i<10;++i){
Tree::Tree f(gnrt->generate());
Tree::Tree m(gnrt->generate());
LOG(' '<<f.to_str());
LOG(' '<<f.to_str());
Tree::Tree child=limit_crossover->cross(f,m);
LOG("child="<<child.to_str());
}
LOG("Mutator test");
Tree::Tree f(gnrt->generate());
Tree::Mutator mtr(gnrt,&crossover);
Tree::Tree mutant=mtr.mutate(f,1.0);
LOG("mutatnt="<<mutant.to_str()<<" => "<<mutant.eval(vm));
std::string mtn_string=mutant.save_to_str();
f.restore_from_str(mtn_string);
LOG("loaded="<<f.to_str());
LOG("FUZZY")
{
Tree::FunctionDB* ffdb=new Tree::FunctionDB;
ffdb->add_function(new Tree::FunctionNode("+",2,add)); // 0
ffdb->add_function(new Tree::FunctionNode("-",2,sub)); // 1
ffdb->add_function(new Tree::FunctionNode("*",2,mul)); // 2
int number_of_inc=ffdb->add_function(new Tree::VarFuncNode("++","x1",1,inc)); //4
ffdb->add_variable("x1"); // 4
Tree::Tree ft;
ft.set_fdb(ffdb);
int froot=ft.add_node(number_of_inc); // ++
ft.set_root(froot);
Tree::VarMap fvm;
fvm["x1"]=1;
LOG(" >>> 2=="<<ft.eval(fvm)<<" : "<<ft.to_str());
LOG("Generator test");
Tree::Generator*gnrt=new Tree::Generator(ffdb,2);
for(int i=0;i<5;++i){
Tree::Tree t(gnrt->generate());
LOG(" ==> "<<t.to_str()<<" -> "<<t.eval(fvm));
}
}
}