-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_task_xor_evaluate.py
69 lines (51 loc) · 2 KB
/
simulation_task_xor_evaluate.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
# Created by Jesper Slik and modified by Jacqueline Heinerman in 2018
# Questions: j.v.heinerman@vu.nl
# This files evluates an individual (=network) for the XOR task. parameters are set in the simulation_tast_xor
### IMPORTS ###
import random
# Libraries
import numpy as np
# Local
from peas.networks import NeuralNetwork
class XORTask(object):
# Default XOR input/output pairs
INPUTS = [(0,0), (0,1), (1,0), (1,1)]
OUTPUTS = [(-1,), (1,), (1,), (-1,)]
EPSILON = 1e-100
def __init__(self, do_all=True):
self.do_all = do_all
self.INPUTS = np.array(self.INPUTS, dtype=float)
self.OUTPUTS = np.array(self.OUTPUTS, dtype=float)
def evaluate(self, network, verbose=False):
if not isinstance(network, NeuralNetwork):
network = NeuralNetwork(network)
if not network.node_types[-1](-1000) < -0.95:
raise Exception("Network should be able to output value of -1, e.g. using a tanh node.")
#print network.cm
pairs = zip(self.INPUTS, self.OUTPUTS)
# random.shuffle(pairs)
if not self.do_all:
pairs = [random.choice(pairs)]
rmse = 0.0
for (i, target) in pairs:
# Feed with bias, bias is added as the first input node
i = np.hstack((1.0, i))
# print "intput"
#print i
output = network.feed(i, add_bias=False)
# Grab the output
#print "output"
#print output
output = output[-len(target):]
err = (target - output)
err[abs(err) < self.EPSILON] = 0
err = (err ** 2).mean()
# Add error
if verbose:
print "%r -> %r (%.2f)" % (i, output, err)
rmse += err
score = 1/(1+np.sqrt(rmse / len(pairs)))
# print(score)
return {'fitness': score}
def solve(self, network):
return int(self.evaluate(network) > 0.9)