/
Solve_Xor_and_Cartpole.py
282 lines (225 loc) · 8.21 KB
/
Solve_Xor_and_Cartpole.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
## Hunar Ahmad @ Brainxyz.com
import random;
import math;
from matplotlib import pylab as plt
################ helper functions #####################
def gen_weight():
return random.uniform(-1,1);
def activationF(val):
return math.tanh(val)
# return max(0, val)
############### MODEL ##########################
class Edge:
# defines two weighted connections between 2 nodes
def __init__(self):
self.fW = gen_weight(); #father weight
self.cW = self.fW; #child weight
self._from = None; #from source Node
self.to = None; #to target Node
# mutate the child weight
def mutateEdge(self, lr):
self.cW = self.fW + ( gen_weight()* lr)
# replaces the father weight (fW) with child weight (cW)
def childBecomesFather(self):
self.fW = self.cW;
class Node:
# defines node structure with their connections (Edges)
def __init__(self):
self.fY =0; #initial father activity
self.cY =0; #initial child activity
self.from_edges =[];
self.to_edges=[];
def spreadOut(self):
# spreading the activation to the connected nodes
for e in self.to_edges:
e.to.fY += self.fY * e.fW;
e.to.cY += self.cY * e.cW;
# defines the connection
def connectTo(self, net, targNode, weight):
edge = Edge();
edge.fW = weight;
edge.cW = weight;
edge._from = self;
edge.to = targNode;
net.edgeList.append(edge); # stores all the edges into a list
self.to_edges.append(edge);
targNode.from_edges.append(edge);
class Layer:
# defines the layer
def __init__(self):
self.nodes = [];
def appendNodes(self, num):
for i in range(num):
self.nodes.append(Node())
def resetLayer(self):
for n in self.nodes:
n.fY =0;
n.cY =0;
def spreadOut(self):
for n in self.nodes:
n.spreadOut()
def activate(self):
for n in self.nodes:
n.fY = activationF(n.fY);
n.cY = activationF(n.cY);
## fully connected layers
def fconnectTo(self, net, targLayer):
for sn in self.nodes:
for tn in targLayer.nodes:
sn.connectTo(net, tn, gen_weight());
## sparsely connected layers, randomly connected
def sconnectTo(self, net, targLayer, sparse_rate):
for sn in self.nodes:
for tn in targLayer.nodes:
if(random.random() < sparse_rate):
sn.connectTo(net, tn, gen_weight());
################# NETWORK STRUCTURE ############################
class Network:
# defines the network
def __init__(self, L1, L2, L3):
self.edgeList =[];
self.sensor = Layer();
self.hidden = Layer();
self.out = Layer();
self.sensor.appendNodes(L1);
self.hidden.appendNodes(L2);
self.out.appendNodes(L3);
def fullyConnect(self):
## fully connected
self.sensor.fconnectTo(self, self.hidden);
self.hidden.fconnectTo(self, self.out);
def sparseConnect(self, amount):
## sparsely connected
self.sensor.sconnectTo(self, self.hidden, amount[0]);
self.hidden.sconnectTo(self, self.out, amount[1]);
def setInput(self, inputs):
for i in range(len(inputs)):
self.sensor.nodes[i].fY = inputs[i];
self.sensor.nodes[i].cY = inputs[i];
def forward(self, inputs):
self.setInput(inputs);
self.sensor.spreadOut();
self.hidden.activate();
self.hidden.spreadOut();
def mutateWeights(self, lr):
for e in self.edgeList:
e.mutateEdge(lr);
def updateWeights(self):
for e in self.edgeList:
e.childBecomesFather();
def resetNet(self):
self.sensor.resetLayer();
self.hidden.resetLayer();
self.out.resetLayer();
################## USER AREA ########################
Problem = "cartpole" ## Problem types: cartpole, xor
##### 1. XOR #######
if(Problem == "xor"):
print("XOR Problem")
epochs = 1000;
lr = 0.1; #mutation rate similar to learning rate
## network configuration: input=2, hidden_nodes = 5, output =1
net = Network(2, 5, 1);
net.fullyConnect();
#net.sparseConnect([0.5, 1]);
# data & labels: XOR problem
inputs=[[0,0],[1,1],[0,1],[1,0]];
labels=[0,0,1,1];
# training loop
print("Training Started ...")
ers =[];
for i in range(epochs):
fE=0; cE=0; # initialize father Erorr (fE) and child Error (cE)
for j in range(len(labels)):
net.resetNet();
net.forward(inputs[j]);
# calculates the network error
fE = fE + abs(net.out.nodes[0].fY - labels[j]);
cE = cE + abs(net.out.nodes[0].cY - labels[j]);
# update if the child performs better than the father i.e. the child network becomes the father of the next gneration
if(fE > cE):
net.updateWeights()
## mutate again
net.mutateWeights(lr)
if(i % 10 ==0):
ers.append(fE);
print("Training Finished")
## asses the trained network
print("Network Assesment - XOR problem")
for j in range(len(labels)):
net.resetNet();
inp = inputs[j];
label = labels[j];
net.forward(inp);
print( "target:",label ," pred:", net.out.nodes[0].fY )
plt.plot(ers)
plt.title("Error")
#### 2. Cartpole Problem #####
elif(Problem == "cartpole"):
print("Cartpole Problem")
import gym
lr = 0.1 ## mutation rate (similar to learning rate)
epi = 100 ## number of episodes
step_limit = 200
## configure the network structure
n_input=4
n_nodes=10
n_output=1
net = Network(n_input, n_nodes, n_output);
net.fullyConnect();
#net.sparseConnect([0.5, 1]);
env = gym.make("CartPole-v0")
print("Training Started...")
observation = env.reset()
step_progress=[];
for i in range(epi):
net.mutateWeights(lr) ## makes a mutated child copy from the father copy
stepsF = [];
stepsC = []
### evaluates the father network
observation = env.reset()
action = 0
for step in range(step_limit):
net.resetNet()
net.forward(observation)
output = net.out.nodes[0].fY ##output of father network
action = 1 if output > 0 else 0
observation, reward, done, info = env.step(action)
if(done==True):
stepFather = step
break
### evaluates the mutated child network
observation = env.reset()
action = 0
for step in range(step_limit):
net.resetNet()
net.forward(observation)
output = net.out.nodes[0].cY ##output of child network
action = 1 if output > 0 else 0
observation, reward, done, info = env.step(action)
if(done==True):
stepChild = step
break
if(stepChild > stepFather):
net.updateWeights()
step_progress.append(stepChild)
env.close()
print("Training Finished")
plt.figure(1)
plt.plot(step_progress)
plt.title("Performance")
# #### render the trained network
# observation = env.reset()
# action = 0
# for t in range(500):
# env.render()
# net.resetNet()
# net.forward(observation)
# output = net.out.nodes[0].cY
# action = 1 if output > 0 else 0
# observation, reward, done, info = env.step(action)
# if(done==True):
# steps = t
# break
# print("step:", steps)
# env.close()