-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpecies.pde
150 lines (127 loc) · 2.22 KB
/
Species.pde
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
class organism{
int x, y;
float angle;
float[] genome;
int state; //0-Alive (-1)-Dead 1-Finish
organism()
{
x = 50;
y = 225;
angle = 315;
state = 0;
genome = new float[1000];
}
int get_X()
{
return x;
}
int get_Y()
{
return y;
}
float get_angle()
{
return angle;
}
void set_X(int xx)
{
x = xx;
}
void set_Y(int yy)
{
y = yy;
}
void set_angle(float ang)
{
angle = ang;
}
void set_state(int state1)
{
state = state1;
}
int get_state()
{
return state;
}
void reset()
{
x = 50;
y = 225;
state = 0;
angle=0;
}
void handle_mov(int turn)
{
angle = angle + genome[turn];
x = x + int(2*cos(angle));
y = y + int(2*sin(angle));
}
void random_genome()
{
for(int i=0; i<1000; i++)
{
genome[i]=random(-0.2,0.2);
}
}
float get_genome_index(int index)
{
return genome[index];
}
float[] get_genome()
{
return genome;
}
void set_genome(float[] g)
{
for(int i=0; i<1000; i++)
genome[i]=g[i];
}
void child(organism mom, organism dad, int team)
{
float random1, random2;
for(int i=0; i<1000; i++) //parents code
{
random1=random(0,1);
if(random1<0.5)
genome[i]=mom.get_genome_index(i);
else
genome[i]=dad.get_genome_index(i);
random2=random(0,1);
switch(team)
{
case 0:
if(random2<0.001) //mutations
genome[i]=random(-0.2,0.2);
break;
case 1:
if(random2<0.002) //mutations
genome[i]=random(-0.2,0.2);
break;
case 2:
if(random2<0.005) //mutations
genome[i]=random(-0.2,0.2);
break;
case 3:
if(random2<0.01) //mutations
genome[i]=random(-0.2,0.2);
break;
case 4:
if(random2<0.02) //mutations
genome[i]=random(-0.2,0.2);
break;
case 5:
if(random2<0.05) //mutations
genome[i]=random(-0.2,0.2);
break;
default: //all the same tests
if(random2<0.20) //mutations
genome[i]=random(-0.2,0.2);
break;
}
}
}
float compareTo(organism o)
{
return(angle - o.angle);
}
}