Skip to content

Commit 3a3387f

Browse files
committed
Translated Matthews Java Code into a Python Equivalent
Currently doesn't run. We still need to connect up the script fully by resolving the bugs and translating some things into python equivalents in particular the java mappings need to be translated into a python equivalent.
1 parent 91721aa commit 3a3387f

9 files changed

+493
-0
lines changed

GroupProject.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import random
2+
from robot import Robot
3+
from environment import Environment
4+
from brain import EvolvableBrain
5+
6+
# rxs = []
7+
# rys = []
8+
# ras = []
9+
#
10+
# food_xs = []
11+
# food_ys = []
12+
# water_xs = []
13+
# water_ys = []
14+
# trap_xs = []
15+
# trap_ys = []
16+
#
17+
# aspect_ratio_x = 2.0/3.0
18+
# text_size = 20
19+
# arena_width = 40
20+
# timestep = 0.04
21+
# iteration = 0
22+
23+
def setup():
24+
pop_size = 30
25+
env = Environment()
26+
evolvable_brains = []
27+
robots = []
28+
29+
for i in range(0, pop_size):
30+
b = EvolvableBrain()
31+
b.randomise()
32+
evolvable_brains.append(b)
33+
34+
for i in range(0, pop_size):
35+
r = Robot()
36+
r.set_brain(evolvable_brains[i])
37+
r.set_environment(env)
38+
robots.append(r)
39+
40+
def main():
41+
#user_input = input("Press Enter to start")
42+
setup()
43+
44+
main()

__pycache__/brain.cpython-37.pyc

3.93 KB
Binary file not shown.
2.99 KB
Binary file not shown.

__pycache__/robot.cpython-37.pyc

2.84 KB
Binary file not shown.

brain.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class Mapping:
2+
def __init__(self):
3+
self.n_points = 4
4+
self.xs = []
5+
self.ys = []
6+
7+
randomise()
8+
9+
def __init__(self, copy_me):
10+
for i in range(0, n_points):
11+
self.xs[i] = copy_me.xs[i]
12+
self.ys[i] = copy_me.ys[i]
13+
14+
def randomise():
15+
for i in range(n_points):
16+
xs[i] = random.randrange(0, 2)
17+
ys[i] = random.randrange(-1, 2)
18+
19+
xs[0] = 0.0
20+
xs[n_points - 1] = 1.0
21+
22+
def mutate():
23+
mu_1 = 0.01
24+
mu_2 = 0.02
25+
for i in range(i, n_points):
26+
xs[i] += random_gaussian() * mu_1
27+
ys[i] += random_gaussian() * mu_2
28+
29+
xs[i] = constrain(xs[i], 0.0, 1.0)
30+
ys[i] = constrain(ys[i], -1.0, 1.0)
31+
32+
if random.randrange(0, 1.0) < mu_2:
33+
xs[i] = random.randrange(0.0, 1.0)
34+
ys[i] = random.randrange(-1.0, 1.0)
35+
36+
for i in range(i, n_points):
37+
if xs[i] > xs[i + 1]:
38+
tmp = xs[i]
39+
xs[i] = xs[i + 1]
40+
xs[i + 1] = tmp
41+
42+
xs[0] = 0.0
43+
xs[n_points - 1] = 1.0
44+
45+
def f(x):
46+
x = min(1.0, x)
47+
for i in range(0, self.n_points):
48+
if x <= self.xs[i + 1]:
49+
output = lerp(ys[i], ys[i + 1], x - self.xs[i] / self.xs[i + 1] - self.xs[i])
50+
return output
51+
52+
print("non-interpolatable input: error")
53+
print(x)
54+
exit()
55+
return 0.0
56+
57+
def printxs(pre):
58+
for i in range(0, self.n_points):
59+
print(pre, end = "")
60+
print("[" + i + "]=", end = "")
61+
print(self.xs[i], end = "")
62+
print(";")
63+
64+
def printys(pre):
65+
for i in range(0, self.n_points):
66+
print(pre, end = "")
67+
print("[" + i + "]=", end = "")
68+
print(self.ys[i], end = "")
69+
print(";")
70+
71+
def draw():
72+
for i in range(0, self.n_points):
73+
#draw line
74+
return True
75+
76+
class EvolvableBrain:
77+
def __init__(self):
78+
self.n_senses = 3
79+
self.n_motors = 2
80+
self.maps = []
81+
82+
for i in range(0, self.n_senses + 1):
83+
for j in range(0, self.n_motors + 1):
84+
copy_me = self.maps[i][j]
85+
self.maps[i][j] = Mapping(copy_me)
86+
87+
def randomise(self):
88+
for i in range(0, self.n_senses + 1):
89+
for j in range(0, self.n_motors + 1):
90+
self.maps[i][j].randomise()
91+
92+
def imprint(self):
93+
n_genes = self.maps[0][0].n_points*3*2
94+
copy = EvolvableBrain(loser)
95+
96+
for i in range(0, self.n_senses):
97+
for j in range(0, self.n_motors):
98+
if random.randrange(0, 1.0) < 0.5:
99+
copy.self.maps[i][j] = Mapping(self.maps[i][j])
100+
copy.self.maps[i][j].mutate()
101+
return copy
102+
103+
def iterate(robot):
104+
lm_accum = 0.5
105+
rm_accum = -0.5
106+
107+
for sensor_i in range(0, self.n_senses):
108+
leftm_ipsi = self.maps[sensor_i][0].f(robot.sensor_values[sensor_i][0])
109+
right_ipsi = self.maps[sensor_i][0].f(robot.sensor_values[sensor_i][1])
110+
111+
leftm_contra = self.maps[sensor_i][1].f(robot.sensor_values[sensor_i][1])
112+
rightm_contra = self.maps[sensor_i][1].f(robot.sensor_values[sensor_i][0])
113+
114+
lm_accum += leftm_ipsi + leftm_contra
115+
rm_accum += right_ipsi + rightm_contra
116+
robot.l_motor = lm_accum / 6.0
117+
robot.r_motor = rm_accum / 6.0

environment.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import random
2+
import enum
3+
4+
arena_width = 50
5+
# timestep
6+
# mode_evolve
7+
8+
class ThingType(enum.Enum):
9+
Food = 1
10+
Water = 2
11+
Trap = 3
12+
13+
class Thing:
14+
def __init__(self, ntype):
15+
self.x = random.randrange(0, arena_width)
16+
self.y = random.randrange(0, arena_width)
17+
18+
self.amount = 1.0
19+
self.radius = 1.0
20+
21+
self.type = ntype
22+
23+
if type == ThingType.Food:
24+
food_xs.add(self.x)
25+
food_ys.add(self.y)
26+
27+
elif type == ThingType.Water:
28+
water_xs.add(self.x)
29+
water_ys.add(self.y)
30+
31+
elif type == ThingType.Trap:
32+
trap_xs.add(self.x)
33+
trap_ys.add(self.y)
34+
35+
# def __init__(self, nx, ny, ntype):
36+
# self.x = nx
37+
# self.y = ny
38+
#
39+
# self.type = ntype
40+
#
41+
# self.amount = 1.0
42+
# self.radius = 1.0
43+
44+
def draw():
45+
#
46+
return True
47+
48+
def update():
49+
if self.amount < 0.0:
50+
self.amount = 1.0
51+
self.x = random.randrange(0, arena_width)
52+
self.y = random.randrange(0, arena_width)
53+
54+
if type == ThingType.Food:
55+
self.food_xs.append(self.x)
56+
self.food_ys.append(self.y)
57+
elif type == ThingType.Water:
58+
self.water_xs.append(self.x)
59+
self.water_ys.append(self.y)
60+
elif type == ThingType.Trap:
61+
self.trap_ys.append(self.x)
62+
self.trap_ys.append(self.y)
63+
64+
class Environment:
65+
66+
def __init__(self):
67+
self.foods = []
68+
self.waters = []
69+
self.traps = []
70+
71+
#create 4 food sources and 4 water sources
72+
for i in range(0, 4):
73+
self.foods.append(ThingType.Food)
74+
self.waters.append(ThingType.Water)
75+
76+
#create 2 traps
77+
self.traps.append(Thing(ThingType.Trap))
78+
self.traps.append(Thing(ThingType.Trap))
79+
80+
def reset():
81+
for thing in self.foods:
82+
thing.amount = -1.0
83+
thing.update()
84+
for thing in self.waters:
85+
thing.amount = -1.0
86+
thing.update()
87+
for thing in self.traps:
88+
thing.amount = -1.0
89+
thing.update()
90+
91+
def interact_with_robot(robot):
92+
consumption_rate = 0.25 * 10.0
93+
fill_rate = 0.25
94+
#draw stroke here
95+
96+
for thing in self.foods:
97+
if dist(thing.x, thing.y, robot.x, robot.y) < thing.radius:
98+
thing.amount -= consumption_rate * timestep
99+
robot.food_battery += fill_rate * timestep
100+
101+
102+
for thing in self.waters:
103+
if dist(thing.x, thing.y, robot.x, robot.y) < thing.radius:
104+
thing.amount -= consumption_rate * timestep
105+
robot.water_battery += fill_rate * timestep
106+
107+
108+
for thing in self.traps:
109+
if dist(thing.x, thing.y, robot.x, robot.y) < thing.radius:
110+
robot.food_battery = 0.0
111+
robot.water_battery = 0.0
112+
robot.is_alive = False
113+
114+
115+
def update():
116+
for thing in self.foods:
117+
thing.update()
118+
for thing in self.waters:
119+
thing.update()
120+
for thing in self.traps:
121+
thing.update()
122+
123+
def draw():
124+
#draw stuff
125+
return True

evolve.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
import robot
3+
4+
def iterate_evolve():
5+
tournament+=1
6+
a_id = random.randrange(0, population_size)
7+
b_id = random.randrange(0, population_size)
8+
9+
while (a_id == b_id):
10+
random.randrange(0, population_size)
11+
12+
print(a_id, b_id)
13+
14+
#a = robot()
15+
#a.set_brain(evolvable_brains[a_id])
16+
#a.set_environment(env)
17+
#b = robot()
18+
#b.set_brain(evolvable_brains[b_id])
19+
#b.set_environment(env)
20+
21+
def main():
22+
tournament = 1
23+
evolvable_brains = []
24+
iterate_evolve()

evolve_mode.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
generation = 0
2+
tournament = 0
3+
4+
trial_length = 1500
5+
n_trials = 1
6+
fitnesses = []
7+
history_size = 500
8+
9+
mean_fitness_history = []
10+
peak_fitness_history = []
11+
12+
13+
def iterate_evolve():
14+
tournament+=1
15+
a_id = random.randrange(0, pop_size)
16+
b_id = random.randrange(0, pop_size)
17+
18+
while (a_id == b_id):
19+
random.randrange(0, pop_size)
20+
21+
a = Robot()
22+
a.set_brain(evolvable_brains[a_id])
23+
a.set_environment(env)
24+
b = robot()
25+
b.set_brain(evolvable_brains[b_id])
26+
b.set_environment(env)
27+
28+
a_fitness = 0
29+
b_fitness = 0
30+
31+
for i in range(0, n_trials):
32+
env.reset()
33+
a.reset()
34+
b.reset()
35+
36+
for i in range(0, trial_length):
37+
if a.is_alive:
38+
a.calculate_change()
39+
a.update()
40+
a_fitness += (a.food_battery + a.water_battery)/2.0/trial_length
41+
if b.is_alive:
42+
b.calculate_change()
43+
b.update()
44+
b_fitness += (b.food_battery + a.water_battery)/2.0/trial_length
45+
env.update()
46+
if not a.is_alive and not b.is_alive:
47+
break
48+
49+
a_fitness /= n_trials
50+
b_fitness /= n_trials
51+
52+
fitnesses[a_id] = a_fitness
53+
fitnesses[b_id] = b_fitness
54+
55+
winner_id = b_id
56+
loser_id = a_id
57+
58+
if fitnesses[a_id] > fitnesses[b_id]:
59+
winner_id = a_id
60+
loser_id = b_id
61+
62+
evolvable_brains[loser_id] = evolvable_brains[winner_id].imprint(evolvable_brains[loser_id])
63+
64+
def main():
65+
tournament = 1
66+
evolvable_brains = []
67+
iterate_evolve()
68+
69+
main()

0 commit comments

Comments
 (0)