-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA.py
More file actions
166 lines (143 loc) · 4.51 KB
/
GA.py
File metadata and controls
166 lines (143 loc) · 4.51 KB
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
import random
import array
import numpy
import math
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
import create_maze_graph as cm
def mazePrint(maze,goal,start,start2):
wall = "■ ■ "
for i in range(len(maze)):
wall+= "■ "
print(wall)
for i in range(len(maze)):
line = "■ "
for j in range(len(maze[0])):
if([i,j] == goal):
line = line + "G "
elif([i,j] == start):
line = line + "O "
elif([i,j] == start2):
line = line + "X "
elif (maze[i][j] == True):
line = line + " "
else:
line = line + "■ "
line = line + "■ "
print(line)
print(wall)
MAX_MOVES = 20
maze_test = cm.create_maze(4,4,0.75,0.75)
maze_easy = cm.create_maze(5,5,0.8,0.8)
maze_medium = cm.create_maze(8,8,0.25,0.25)
maze_hard = cm.create_maze(10,10,0.40,0.2)
maze = maze_test
#Test
#[0,0],[0,1],[4,4]
#Easy
#[0,0],[4,0],[2,2]
#Medium
#[0,0],[0,8],[4,4]
#hard
#[0,0],[0,10],[2,8]
start = [0,0]
start2 = [0,4]
end = [4,4]
mazePrint(maze,end,start,start2)
#Heuristic/fitness function. Input is individual (can treat like a list)
#output is score, Note: the comma is important
def evalOneMax(individual):
print(individual)
return sum(individual),
def evalMaze(ind, verbose = False):
seen = set()
penalty = 0
c_c=[0,0]
c_c[0] = start[0]
c_c[1] = start[1]
n_c = [-1,-1]
path_length = 0
for i in range(len(ind)):
n_c[0] = c_c[0]
n_c[1] = c_c[1]
path_length += 1
seen.add((c_c[0],c_c[1]))
if ind[i] == 1:
n_c[0] = c_c[0] - 1
if verbose:
print("moving up " + str(c_c) + " to " + str(n_c))
elif (ind[i] == 2):
n_c[1] = c_c[1] + 1
if verbose:
print("moving right " + str(c_c) + " to " + str(n_c))
elif (ind[i] == 3):
n_c[0] = c_c[0] + 1
if verbose:
print("moving down " + str(c_c) + " to " + str(n_c))
elif (ind[i] == 4):
n_c[1] = c_c[1] - 1
if verbose:
print("moving left " + str(c_c) + " to " + str(n_c))
else:
if verbose:
print("waiting at " + str(c_c))
nx = n_c[0]
ny = n_c[1]
if (nx >= len(maze) or ny >= len(maze[0]) or nx < 0 or ny < 0):
if verbose:
print("OUT OF BOUNDS")
penalty+=2
elif(maze[nx][ny] == False):
if verbose:
print("INVALID MOVE")
penalty+=2
else:
c_c[0] = n_c[0]
c_c[1] = n_c[1]
if((n_c[0],n_c[1]) in seen and ind[i] != 0):
if verbose: print("seen")
penalty+=1
if(c_c == end):
if verbose:
print("Reached Goal!!")
break
#calculate distance from goal
distance = math.sqrt((end[0]-c_c[0])**2 + (end[1]-c_c[1])**2)
bonus = 0
if distance == 0:
bonus = MAX_MOVES-path_length + 100
if verbose:
print("distance to goal:" + str(distance))
print("penalty:" + str(penalty))
print("path length:"+ str(path_length))
return distance + penalty - bonus,
#determins if we are minimizing or maximizing
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
#what one individual looks like, in thise case it is a list
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
#generation function for an element of the individual
toolbox.register("attr_bool", random.randint, 1, 4)
#structure initializers, int at the end is size of the individual
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, MAX_MOVES)
#structure initializer, do not touch
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
#specify heuristic here
toolbox.register("evaluate", evalMaze)
#mutation function, don't think we need another one
toolbox.register("mutate", tools.mutUniformInt, indpb=0.05,low = 1, up = 4)
#idk
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("select", tools.selTournament, tournsize=3)
# pop = toolbox.population(n = 1000)
# hof = tools.HallOfFame(1)
#
# pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.50, mutpb=0.25, ngen=70,
# halloffame=hof, verbose=True)
#
# print(hof[0])
#
# print(evalMaze(hof[0],verbose = True))