Skip to content

Commit 9e18564

Browse files
committed
Remove simplification constraints now that some bugs are fixed
1 parent 3399e57 commit 9e18564

File tree

4 files changed

+15
-213
lines changed

4 files changed

+15
-213
lines changed

GroupProject.py

-146
Original file line numberDiff line numberDiff line change
@@ -26,159 +26,13 @@ def setup():
2626
"smell_signature": [0, 0, 1, 0, 0]
2727
}]
2828
})
29-
test_robot_genome = RobotGenome()
30-
test_robot_genome.randomise()
31-
test_robot_genome.sensors.list[0].mapping.from_flattened([ # contra food/water
32-
{"x": 0.0, "y": -1.0/2},
33-
# {"x": 0.3, "y": -0.3/2},
34-
# {"x": 0.7, "y": 0.3/2},
35-
{"x": 1.0, "y": 1.0/2},
36-
])
37-
test_robot_genome.sensors.list[3].mapping.from_flattened([ # ipsi food/water
38-
{"x": 0.0, "y": 1.0/2},
39-
# {"x": 0.3, "y": 0.7/2},
40-
# {"x": 0.7, "y": 0.5/2},
41-
{"x": 1.0, "y": 0.5/2},
42-
])
43-
test_robot_genome.sensors.list[5].mapping.from_flattened([ # contra trap
44-
{"x": 0.5, "y": 0.0},
45-
# {"x": 0.3, "y": 0.0},
46-
# {"x": 0.5, "y": 0.0},
47-
{"x": 1.0, "y": -1.0},
48-
])
49-
test_robot_genome.sensors.list[2].mapping.from_flattened([ # ipsi trap
50-
{"x": 0.3, "y": 1.0},
51-
# {"x": 0.3, "y": 1.0},
52-
# {"x": 0.7, "y": 0.0},
53-
{"x": 1.0, "y": -0.5},
54-
])
55-
test_robot_genome.sensors.list[4].mapping.from_flattened(
56-
test_robot_genome.sensors.list[3].mapping.flatten()
57-
)
58-
test_robot_genome.sensors.list[1].mapping.from_flattened(
59-
test_robot_genome.sensors.list[0].mapping.flatten()
60-
)
61-
# test_robot_genome.sensors.list[8].mapping.from_flattened(
62-
# test_robot_genome.sensors.list[5].mapping.flatten()
63-
# )
64-
# test_robot_genome.sensors.list[11].mapping.from_flattened(
65-
# test_robot_genome.sensors.list[2].mapping.flatten()
66-
# )
67-
"""
68-
test_robot_genome.sensors.list[0].mapping.from_flattened([ # contra food/water
69-
{"x": 0.000, "y": 0.694},
70-
{"x": 0.026, "y": 0.588},
71-
{"x": 0.164, "y": 0.127},
72-
{"x": 1.000, "y": 0.536},
73-
])
74-
test_robot_genome.sensors.list[3].mapping.from_flattened([ # ipsi food/water
75-
{"x": 0.000, "y": -0.980},
76-
{"x": 0.023, "y": -0.001},
77-
{"x": 0.734, "y": 0.851},
78-
{"x": 1.000, "y": 0.659},
79-
])
80-
test_robot_genome.sensors.list[5].mapping.from_flattened([ # contra trap
81-
{"x": 0.000, "y": 0.853},
82-
{"x": 0.537, "y": 0.707},
83-
{"x": 0.909, "y": 0.432},
84-
{"x": 1.000, "y": -0.982},
85-
])
86-
test_robot_genome.sensors.list[2].mapping.from_flattened([ # ipsi trap
87-
{"x": 0.000, "y": 0.390},
88-
{"x": 0.018, "y": 0.732},
89-
{"x": 0.488, "y": 0.017},
90-
{"x": 1.000, "y": 0.148},
91-
])
92-
for i in [4, 6, 7]:
93-
test_robot_genome.sensors.list[i].mapping.from_flattened(
94-
test_robot_genome.sensors.list[3].mapping.flatten()
95-
)
96-
for i in [1, 9, 10]:
97-
test_robot_genome.sensors.list[i].mapping.from_flattened(
98-
test_robot_genome.sensors.list[0].mapping.flatten()
99-
)
100-
test_robot_genome.sensors.list[8].mapping.from_flattened(
101-
test_robot_genome.sensors.list[5].mapping.flatten()
102-
)
103-
test_robot_genome.sensors.list[11].mapping.from_flattened(
104-
test_robot_genome.sensors.list[2].mapping.flatten()
105-
)
106-
"""
107-
'''
108-
test_robot_genome.from_flattened({
109-
"sensors": [
110-
{
111-
"threshold": 0.0,
112-
"angle": math.pi / 4,
113-
"smell_signature": [1, 0, 0, 0, 0],
114-
"mapping": [
115-
{"x": 0.0, "y": 0.1},
116-
{"x": 1.0, "y": 1.0},
117-
],
118-
"motor_side": "LEFT"
119-
},
120-
{
121-
"threshold": 0.0,
122-
"angle": math.pi * 7 / 4,
123-
"smell_signature": [1, 0, 0, 0, 0],
124-
"mapping": [
125-
{"x": 0.0, "y": -0.1},
126-
{"x": 1.0, "y": 1.0},
127-
],
128-
"motor_side": "RIGHT"
129-
},
130-
{
131-
"threshold": 0.0,
132-
"angle": math.pi / 4,
133-
"smell_signature": [0, 1, 0, 0, 0],
134-
"mapping": [
135-
{"x": 0.0, "y": 0.1},
136-
{"x": 1.0, "y": 1.0},
137-
],
138-
"motor_side": "LEFT"
139-
},
140-
{
141-
"threshold": 0.0,
142-
"angle": math.pi * 7 / 4,
143-
"smell_signature": [0, 1, 0, 0, 0],
144-
"mapping": [
145-
{"x": 0.0, "y": -0.1},
146-
{"x": 1.0, "y": 1.0},
147-
],
148-
"motor_side": "RIGHT"
149-
},
150-
{
151-
"threshold": 0.0,
152-
"angle": math.pi / 4,
153-
"smell_signature": [0, 0, 1, 0, 0],
154-
"mapping": [
155-
{"x": 0.0, "y": 0.1},
156-
{"x": 1.0, "y": -1.0},
157-
],
158-
"motor_side": "LEFT"
159-
},
160-
{
161-
"threshold": 0.0,
162-
"angle": math.pi * 7 / 4,
163-
"smell_signature": [0, 0, 1, 0, 0],
164-
"mapping": [
165-
{"x": 0.0, "y": -0.1},
166-
{"x": 1.0, "y": -1.0},
167-
],
168-
"motor_side": "RIGHT"
169-
},
170-
]
171-
})
172-
'''
17329
# Setup the population of robot and environment genomes
17430
for _ in range(simulation_state.pop_size):
17531
robot_genome = RobotGenome()
17632
robot_genome.randomise()
17733
simulation_state.robot_genomes.append(robot_genome)
17834
simulation_state.environment_genomes.append(environment_genome)
17935

180-
simulation_state.robot_genomes[0] = test_robot_genome
181-
18236
if LOAD_INITIAL_POPULATION_FROM is not None:
18337
best_robot_index = None
18438
with open(f"Data/tournament_data{LOAD_INITIAL_POPULATION_FROM}.json") as file:

brain.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import numpy as np
22
import turtle
3-
import itertools
43
from genome import Laterality
54

65

76
class Mapping:
8-
def __init__(self, gene, is_left):
7+
def __init__(self, gene):
98
self.n_points = len(gene.mapping.list)
10-
# self.side_weighting = np.array([1, 0] if gene.motor_side.laterality == Laterality.LEFT else [0, 1])
11-
self.side_weighting = np.array([1, 0] if is_left else [0, 1])
9+
self.side_weighting = np.array([1, 0] if gene.motor_side.laterality == Laterality.LEFT else [0, 1])
1210
self.xs = np.array([point.x.value for point in gene.mapping.list])
1311
self.ys = np.array([point.y.value for point in gene.mapping.list])
1412

@@ -26,10 +24,10 @@ def draw(self):
2624

2725
class EvolvableBrain:
2826
def __init__(self, genome):
29-
self.mappings = [Mapping(gene, i < 6) for i, gene in enumerate(itertools.chain(genome.sensors.list, genome.sensors.list))]
27+
self.mappings = [Mapping(gene) for gene in genome.sensors.list]
3028

3129
def iterate(self, sensor_values):
32-
accumulative_motor_power = [0.5, -0.5]
30+
accumulative_motor_power = np.zeros(2)
3331

3432
for mapping, sensor_value in zip(self.mappings, sensor_values):
3533
accumulative_motor_power += mapping.f(sensor_value)

genome.py

+8-41
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ def crossover(self, source):
247247

248248
def normalise(self):
249249
self.list.sort(key=attrgetter('x.value'))
250-
# if len(self.list) < 2:
251-
# self.list += [PiecemealPoint() for _ in range(2 - len(self.list))]
252-
# self.list[0].x.value = 0.0
253-
# self.list[-1].x.value = 1.0
254250

255251

256252
class ThingGene(Genetic):
@@ -312,52 +308,23 @@ def __init__(self):
312308
class SensorGene(Genetic):
313309
def __init__(self):
314310
super().__init__()
315-
316-
# Either
317-
# self.threshold = FloatGene()
318-
# or
319311
self.mapping = PiecemealMappingGene()
320-
321-
# self.angle = FloatGene(bounds=(0.0, 2.0 * math.pi), wrap=True)
312+
self.angle = FloatGene(bounds=(0.0, 2.0 * math.pi), wrap=True)
322313
self.smell_signature = SmellSignatureGene()
323-
324-
# self.motor_side = LateralityGene()
314+
self.motor_side = LateralityGene()
325315

326316
def incompatibility_with(self, other_sensor):
327-
# laterality_incompatibility = (
328-
# 0 if self.motor_side.laterality == other_sensor.motor_side.laterality
329-
# else SmellSignatureGene.MAX_INCOMPATIBILITY
330-
# )
331-
# return laterality_incompatibility + self.smell_signature.incompatibility_with(other_sensor.smell_signature)
332-
return self.smell_signature.incompatibility_with(other_sensor.smell_signature)
317+
laterality_incompatibility = (
318+
0 if self.motor_side.laterality == other_sensor.motor_side.laterality
319+
else SmellSignatureGene.MAX_INCOMPATIBILITY
320+
)
321+
return laterality_incompatibility + self.smell_signature.incompatibility_with(other_sensor.smell_signature)
333322

334323

335324
class RobotGenome(Genetic):
336325
def __init__(self):
337326
super().__init__()
338-
self.sensors = DynamicListGene(SensorGene, init_size_range=(6, 7), addition_probability=0.0, removal_probability=0.0)
339-
340-
def randomise(self):
341-
super().randomise()
342-
self.normalise()
343-
344-
def mutate(self):
345-
super().mutate()
346-
self.normalise()
347-
348-
def crossover(self, source):
349-
super().crossover(source)
350-
self.normalise()
351-
352-
def normalise(self):
353-
for i, sensor in enumerate(self.sensors.list):
354-
# sensor.angle.value = math.pi / 4 if i < 6 else math.pi * 7 / 4
355-
sensor.smell_signature.list[0].value = 1 if i % 3 == 0 else 0
356-
sensor.smell_signature.list[1].value = 1 if i % 3 == 1 else 0
357-
sensor.smell_signature.list[2].value = 1 if i % 3 == 2 else 0
358-
sensor.smell_signature.list[3].value = 0
359-
sensor.smell_signature.list[4].value = 0
360-
# sensor.motor_side.laterality = Laterality.LEFT if (i // 3) % 2 == 0 else Laterality.RIGHT
327+
self.sensors = DynamicListGene(SensorGene, init_size_range=(3, 7), addition_probability=0.05, removal_probability=0.05)
361328

362329

363330
def full_examples():

robot.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from maths import polar2vec, rotation2d
88
import math
99
import turtle
10-
import itertools
1110

1211
from robot_artist import RobotArtist
1312

@@ -40,10 +39,10 @@ def __init__(self, genome):
4039

4140
self.brain = EvolvableBrain(genome)
4241
self.env = None
43-
self.sensor_values = np.zeros(12) # np.zeros(len(genome.sensors.list))
44-
self.sensor_angles = [math.pi / 4 if i < 3 or i >= 9 else math.pi * 7 / 4 for i in range(12)] # [gene.angle.value for gene in genome.sensors.list]
42+
self.sensor_values = np.zeros(len(genome.sensors.list))
43+
self.sensor_angles = [gene.angle.value for gene in genome.sensors.list]
4544
self.sensor_vectors = np.array([polar2vec(angle) for angle in self.sensor_angles]).transpose()
46-
self.sensor_signatures = np.array([gene.smell_signature.flatten() for gene in itertools.chain(genome.sensors.list, genome.sensors.list)]).transpose()
45+
self.sensor_signatures = np.array([gene.smell_signature.flatten() for gene in genome.sensors.list]).transpose()
4746
self.smell_alignment = None
4847

4948
# An artist for handling the GUI
@@ -111,22 +110,6 @@ def calculate_change(self):
111110

112111
self.sensor_values = excitements.max(0)
113112

114-
"""
115-
print("\n"*4)
116-
print(f"water left contra: {'#' * int(self.sensor_values[0] * 50)}")
117-
print(f"food left contra: {'#' * int(self.sensor_values[1] * 50)}")
118-
print(f"trap left contra: {'#' * int(self.sensor_values[2] * 50)}")
119-
print(f"water right ipsi: {'#' * int(self.sensor_values[9] * 50)}")
120-
print(f"food right ipsi: {'#' * int(self.sensor_values[10] * 50)}")
121-
print(f"trap right ipsi: {'#' * int(self.sensor_values[11] * 50)}")
122-
print(f"water left ipsi: {'#' * int(self.sensor_values[3] * 50)}")
123-
print(f"food left ipsi: {'#' * int(self.sensor_values[4] * 50)}")
124-
print(f"trap left ipsi: {'#' * int(self.sensor_values[5] * 50)}")
125-
print(f"water right contra: {'#' * int(self.sensor_values[6] * 50)}")
126-
print(f"food right contra: {'#' * int(self.sensor_values[7] * 50)}")
127-
print(f"trap right contra: {'#' * int(self.sensor_values[8] * 50)}")
128-
"""
129-
130113
def draw(self):
131114
self.artist\
132115
.x_position(self.position[0][0])\

0 commit comments

Comments
 (0)