-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
105 lines (89 loc) · 2.89 KB
/
main.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
import random
import statistics
from time import sleep
from arena import arena
from matplotlib import pyplot as plt
import numpy as np
ROBOTS_LIST = []
NUM_ROBOTS = 4
CHROMOSOME_LENGTH = 32
CHROMOSOME_BITS_TO_MUTATE = 5
PERCENTAGE_ROBOTS_TO_MUTATE = 0.5
PERFORMANCE_MEANS = []
PERFORMANCE_MEDIANS = []
def plotList(y, label=None):
x = np.arange(1, len(y)+1)
plt.clf()
plt.xlabel("Generations")
plt.xticks(x)
if label is not None:
plt.ylabel(label)
plt.plot(x, y)
plt.savefig("Plot_" + label + ".png")
def constructFSM(chromosome):
def next_move(i):
''' Input is a 4 bit input, It can be from 0....15, extract the 2*i and 2*i + 1 '''
output = chromosome[2*i:2*i+2]
return int(output, 2)
return next_move # FUNCTION OBJECT
def generateRandomBinaryString(N: int):
s = ""
for i in range(N):
toAdd = str(random.randint(0, 1))
s = s + toAdd
return s
def flipBit(c):
if c == '0':
return '1'
else:
return '0'
def mutateChromosome(chromosome):
BITS_MUTATION_LIST = random.sample(range(len(chromosome)), CHROMOSOME_BITS_TO_MUTATE)
for i in BITS_MUTATION_LIST:
newChromosome = chromosome[0:i] + flipBit(chromosome[i])
if i != (len(chromosome) - 1):
newChromosome = newChromosome + chromosome[i + 1:]
chromosome = newChromosome
return chromosome
def mutateRobots(robots):
N = len(robots)
numberToMutate = round(PERCENTAGE_ROBOTS_TO_MUTATE * N)
ROBOTS_MUTATION_LIST = random.sample(range(N), numberToMutate)
for i in ROBOTS_MUTATION_LIST:
robots[i] = mutateChromosome(robots[i])
def main():
for i in range(NUM_ROBOTS):
chromosome = generateRandomBinaryString(CHROMOSOME_LENGTH)
ROBOTS_LIST.append(chromosome)
CURR_ROBOTS = ROBOTS_LIST
robotsData = {}
generation = 1
while len(CURR_ROBOTS) != 1:
lives = []
performanceList = []
for robot in CURR_ROBOTS:
fsm = constructFSM(robot)
performance = arena.startLife(fsm)
performanceList.append(performance)
lives.append((performance, robot))
print("\tRobot #", robot, " Time Lived: ", performance)
sleep(2)
lives.sort()
N = len(lives)
mean = statistics.mean(performanceList)
median = statistics.median(performanceList)
print("Generation #", generation)
print("Mean of Performance: ", mean)
print("Median: ", median)
robotsData[generation] = lives
CURR_ROBOTS = [robot for _, robot in lives[N // 2:N]]
mutateRobots(CURR_ROBOTS)
PERFORMANCE_MEANS.append(mean)
PERFORMANCE_MEDIANS.append(median)
generation += 1
print("Final Living Robot:")
print(CURR_ROBOTS)
plotList(PERFORMANCE_MEDIANS, 'Medians')
plotList(PERFORMANCE_MEANS, "Mean")
if __name__ == '__main__':
main()