-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachineLearningLibrary1052770205147735127.autosave
183 lines (148 loc) · 4.01 KB
/
MachineLearningLibrary1052770205147735127.autosave
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
//#region [rgba(100,0,100,0.5)]
//Auto Format for Processing(Ctrl-T)
/**********************************************************************
* NEURAL NETWORK
* Designed by :
______
/ ____/___ ___ ___________ __ __
/ / __/ __ `/ / / / ___/ __ `/ | / /
/ /_/ / /_/ / /_/ / / / /_/ /| |/ /
\____/\__,_/\__,_/_/ \__,_/ |___/
__ __ __ _
/ //_// /_ ____ _(_)________ ____ ______
/ ,< / __ \/ __ `/ / ___/ __ \/ __ `/ ___/
/ /| |/ / / / /_/ / / / / / / / /_/ / /
/_/ |_/_/ /_/\__,_/_/_/ /_/ /_/\__,_/_/
****************************************************************
//July 2019
@ Website : gaurav1620.rf.gd
@ Github : gaurav1620
@ Insta : may_be_gaurav
@ Gmail : [email protected]
*/
//************************************************************************************************
//#endregion
// ArrayList<snake> sL = new ArrayList<snake>();
// void init(){
// for(int i = 0;i < 10;i++){
// sL.add(new snake());
// }
// for(int i = 0;i < 10;i++){
// sL.get(i).init();
// }
// }
// void setup(){
// size(600,400);
// init();
// }
// void draw(){
// background(0);
// for(int i = 0;i < 10;i++){
// sL.get(i).show();
// sL.get(i).doPredictedMovement();
// }
// for(int i = 0;i < 10;i++){
// sL.get(i).doPredictedMovement();
// }
// }
int noOfSnakes = 1;
int noOfFoods = 30;
ArrayList<snake> snakes = new ArrayList<snake>();
ArrayList<food> foods = new ArrayList<food>();
void setup(){
newGeneration();
size(600,400);
frameRate(20);
}
void draw(){
background(0);
showFoods();
showSnakes();
checkHitForAllSnakes();
checkEatenFood();
moveSnakes();
if(snakes.size() == 0){
newGeneration();
}
}
//Move snakes by predicted movement
void moveSnakes(){
for(int i = 0;i < snakes.size();i++){
snakes.get(i).doPredictedMovement(foods);
}
}
//Stores the indexes to be removed and removes from back
void checkHitForAllSnakes(){
ArrayList<Integer> toRemove = new ArrayList<Integer>();
for(int i = 0;i < snakes.size();i++){
if(snakes.get(i).hit()){
toRemove.add(i);
println("Added a remove snake : ",i);
}
}
for(int i = toRemove.size()-1 ;i >= 0 ;i--){
println("Removed snake : ",toRemove.get(i));
int c = toRemove.get(i);
snakes.remove(c);
}
}
//Check if the food is eaten then remove it
void checkEatenFood(){
ArrayList<Integer> foodRemInd = new ArrayList<Integer>();
ArrayList<Integer> toAddPartSnakes = new ArrayList<Integer>();
for(int i = 0;i < snakes.size();i++){
for(int j = 0;j < foods.size();j++){
if(sqrt((snakes.get(i).x + 15*0.5 - foods.get(j).x + 10*0.5)*(snakes.get(i).x + 15*0.5 - foods.get(j).x + 10*0.5)
+ (snakes.get(i).y + 15*0.5 - foods.get(j).y + 10*0.5)*(snakes.get(i).y + 15*0.5 - foods.get(j).y)) < 15 + 10*0.5){
if(!foodRemInd.contains(j)){
foodRemInd.add(j);
}
if(!toAddPartSnakes.contains(i)){
toAddPartSnakes.add(i);
}
}
}
}
// for(int i = 0;i < foodRemInd.size();i++){
// foods.remove(foodRemInd.get(i));
// }
for(int i = foodRemInd.size() -1 ;i >=0;i--){
int j = foodRemInd.get(i);
foods.remove(j);
}
for(int i =0;i < toAddPartSnakes.size();i++){
int k = toAddPartSnakes.get(i);
snakes.get(k).addAtBack();
}
}
void showSnakes(){
for(int i = 0;i < snakes.size();i++){
snakes.get(i).update();
snakes.get(i).show();
}
}
void showFoods(){
for(int i = 0;i < foods.size();i++){
foods.get(i).show();
}
}
//With brain passed as arg and also does mutation
void newGeneration(NeuralNetwork b){
b = b.mutate();
for(int i = 0;i < noOfSnakes;i++){
snakes.add(new snake(b));
}
for(int i = 0;i < noOfFoods;i++){
foods.add(new food());
}
}
//For first init
void newGeneration(){
for(int i = 0;i < noOfSnakes;i++){
snakes.add(new snake());
}
foods = new ArrayList<food>();
for(int i = 0;i < noOfFoods;i++){
foods.add(new food());
}
}