Compare commits
No commits in common. "cdf16d00ccc6e604ca25b7708eeb3334bbb814dd" and "c23011ffc5c6d79a8dea57bd5147ae85cd3becb1" have entirely different histories.
cdf16d00cc
...
c23011ffc5
|
@ -1 +0,0 @@
|
||||||
./venv
|
|
|
@ -1 +0,0 @@
|
||||||
.venv/
|
|
169
a1/code/main.py
169
a1/code/main.py
|
@ -1,169 +0,0 @@
|
||||||
import pygad
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
global punish_matrix
|
|
||||||
global start_pos
|
|
||||||
global end_pos
|
|
||||||
global shortest_path
|
|
||||||
|
|
||||||
def walk_through_maze(solution_matrix):
|
|
||||||
global punish_matrix, start_pos, end_pos, maze
|
|
||||||
solution_matrix = solution_matrix.reshape((len(maze), len(maze)))
|
|
||||||
|
|
||||||
queue = [[start_pos]]
|
|
||||||
|
|
||||||
def add_to_queue(full_path, x, y):
|
|
||||||
if (x,y) not in full_path:
|
|
||||||
full_path.append((x, y))
|
|
||||||
queue.append(full_path)
|
|
||||||
|
|
||||||
while queue != []:
|
|
||||||
full_path = queue.pop()
|
|
||||||
x, y = full_path[-1]
|
|
||||||
if(maze[x][y] == 'E'):
|
|
||||||
return len(full_path)
|
|
||||||
if x + 1 < len(maze) :
|
|
||||||
if solution_matrix[x+1, y] == 1:
|
|
||||||
add_to_queue(full_path, x+1, y)
|
|
||||||
if x - 1 >= 0:
|
|
||||||
if solution_matrix[x-1, y] == 1:
|
|
||||||
add_to_queue(full_path, x-1, y)
|
|
||||||
if y + 1 < len(maze) :
|
|
||||||
if solution_matrix[x, y+1] == 1:
|
|
||||||
add_to_queue(full_path, x, y+1)
|
|
||||||
if y - 1 >= 0:
|
|
||||||
if solution_matrix[x, y-1] == 1:
|
|
||||||
add_to_queue(full_path, x, y-1)
|
|
||||||
return 42069
|
|
||||||
|
|
||||||
|
|
||||||
def fitness_func(path, solution_idx):
|
|
||||||
global punish_matrix, start_pos, end_pos, shortest_path
|
|
||||||
|
|
||||||
punish = np.sum(path * punish_matrix.reshape(-1))
|
|
||||||
print('Matrix multiplication', punish)
|
|
||||||
|
|
||||||
# chekc if path is valied from start to end
|
|
||||||
path = path.reshape(len(punish_matrix), len(punish_matrix))
|
|
||||||
|
|
||||||
|
|
||||||
if path[start_pos] == 0:
|
|
||||||
punish -= 1000
|
|
||||||
if path[end_pos] == 0:
|
|
||||||
punish -= 1000
|
|
||||||
if path[start_pos] == 1 and path[end_pos] == 1:
|
|
||||||
walk = walk_through_maze(path)
|
|
||||||
if walk < shortest_path:
|
|
||||||
punish += 10000
|
|
||||||
shortest_path = walk
|
|
||||||
return punish
|
|
||||||
|
|
||||||
|
|
||||||
def run_genetic_algorithm(m, pm, sp, ep):
|
|
||||||
# Set global punish matrix
|
|
||||||
global punish_matrix, start_pos, end_pos, maze, shortest_path
|
|
||||||
punish_matrix = pm
|
|
||||||
start_pos = sp
|
|
||||||
end_pos = ep
|
|
||||||
maze = m
|
|
||||||
shortest_path = len(maze) *2
|
|
||||||
|
|
||||||
fitness_function = fitness_func
|
|
||||||
|
|
||||||
num_generations = 500
|
|
||||||
num_parents_mating = 4
|
|
||||||
|
|
||||||
sol_per_pop = 20
|
|
||||||
num_genes = punish_matrix.size
|
|
||||||
|
|
||||||
init_range_low = 0
|
|
||||||
init_range_high = 1
|
|
||||||
|
|
||||||
parent_selection_type = "sss"
|
|
||||||
keep_parents = 1
|
|
||||||
|
|
||||||
crossover_type = "single_point"
|
|
||||||
|
|
||||||
mutation_type = "random"
|
|
||||||
|
|
||||||
ga_instance = pygad.GA(num_generations=num_generations,
|
|
||||||
num_parents_mating=num_parents_mating,
|
|
||||||
fitness_func=fitness_function,
|
|
||||||
sol_per_pop=sol_per_pop,
|
|
||||||
num_genes=num_genes,
|
|
||||||
init_range_low=init_range_low,
|
|
||||||
init_range_high=init_range_high,
|
|
||||||
parent_selection_type=parent_selection_type,
|
|
||||||
keep_parents=keep_parents,
|
|
||||||
crossover_type=crossover_type,
|
|
||||||
mutation_type=mutation_type,
|
|
||||||
mutation_num_genes=2,
|
|
||||||
gene_space=[0, 1])
|
|
||||||
ga_instance.run()
|
|
||||||
|
|
||||||
solution, solution_fitness, solution_idx = ga_instance.best_solution()
|
|
||||||
print("Parameters of the best solution : {solution}".format(
|
|
||||||
solution=solution))
|
|
||||||
print(solution.reshape(len(maze), len(maze)))
|
|
||||||
print("Fitness value of the best solution = {solution_fitness}".format(
|
|
||||||
solution_fitness=solution_fitness))
|
|
||||||
|
|
||||||
def read_mazes():
|
|
||||||
with open('./mazes.r', 'r') as f:
|
|
||||||
mazes = []
|
|
||||||
maze = []
|
|
||||||
for line in f:
|
|
||||||
if line == '\n':
|
|
||||||
mazes.append(maze)
|
|
||||||
maze = []
|
|
||||||
continue
|
|
||||||
maze.append(line.strip())
|
|
||||||
return mazes
|
|
||||||
|
|
||||||
def prepare_maze(maze_ix, mazes):
|
|
||||||
maze = mazes[maze_ix]
|
|
||||||
punish_matrix_t = np.zeros((len(maze), len(maze)), dtype=np.int64)
|
|
||||||
|
|
||||||
start_index_t = 0, 0
|
|
||||||
end_index_t = 0, 0
|
|
||||||
for i, x in enumerate(maze):
|
|
||||||
for j, y in enumerate(x):
|
|
||||||
if y == "#":
|
|
||||||
punish_matrix_t[i, j] = -300
|
|
||||||
if y == ".":
|
|
||||||
punish_matrix_t[i, j] = +200
|
|
||||||
if y == "S":
|
|
||||||
start_index_t = i, j
|
|
||||||
if y == "E":
|
|
||||||
end_index_t = i, j
|
|
||||||
|
|
||||||
return maze, punish_matrix_t, start_index_t, end_index_t
|
|
||||||
|
|
||||||
def print_maze(maze):
|
|
||||||
for row in maze:
|
|
||||||
print(row)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# Read mazes
|
|
||||||
mazes = read_mazes()
|
|
||||||
for i in range(1):
|
|
||||||
maze, punish_matrix_t, start_index_t, end_index_t = prepare_maze(1, mazes)
|
|
||||||
run_genetic_algorithm(maze, punish_matrix_t, start_index_t, end_index_t)
|
|
||||||
print_maze(maze)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
||||||
"""
|
|
||||||
mutascija -> nesme mutirati u zid
|
|
||||||
-> more mutirat v start ce tam ni poti
|
|
||||||
-> more mutirat v end ce tam ni poti
|
|
||||||
-> ce je na zidu enka mutiri v 0
|
|
||||||
tresure kdr bo
|
|
||||||
|
|
||||||
crossover ->
|
|
||||||
malo z weights
|
|
||||||
|
|
||||||
TRESURE -> spremeni fitrnes mutacijo in crossover
|
|
||||||
"""
|
|
282
a1/code/mazes.r
282
a1/code/mazes.r
|
@ -1,151 +1,153 @@
|
||||||
##E##
|
maze1 = c("##E##",
|
||||||
#...#
|
"#...#",
|
||||||
#...#
|
"#...#",
|
||||||
#S..#
|
"#S..#",
|
||||||
#####
|
"#####")
|
||||||
|
|
||||||
#####E#
|
maze2 = c("#####E#",
|
||||||
#.#.#.#
|
"#.#.#.#",
|
||||||
#.....#
|
"#.....#",
|
||||||
##.####
|
"##.####",
|
||||||
#....S#
|
"#....S#",
|
||||||
#.#..##
|
"#.#..##",
|
||||||
#######
|
"#######")
|
||||||
|
|
||||||
##########
|
maze3 = c("##########",
|
||||||
#S.......#
|
"#S.......#",
|
||||||
########.#
|
"########.#",
|
||||||
#........#
|
"#........#",
|
||||||
#.########
|
"#.########",
|
||||||
#........#
|
"#........#",
|
||||||
########.#
|
"########.#",
|
||||||
#........#
|
"#........#",
|
||||||
#.########
|
"#.########",
|
||||||
#E########
|
"#E########")
|
||||||
|
|
||||||
#####E####
|
maze4 = c("#####E####",
|
||||||
#...#....#
|
"#...#....#",
|
||||||
#.######.#
|
"#.######.#",
|
||||||
#.#......#
|
"#.#......#",
|
||||||
#.###.####
|
"#.###.####",
|
||||||
#....S...#
|
"#....S...#",
|
||||||
#####.##.#
|
"#####.##.#",
|
||||||
#..##.####
|
"#..##.####",
|
||||||
##.......#
|
"##.......#",
|
||||||
##########
|
"##########")
|
||||||
|
|
||||||
###E###########
|
maze5 = c("###E###########",
|
||||||
#........###..#
|
"#........###..#",
|
||||||
########.....##
|
"########.....##",
|
||||||
#.....##.###.##
|
"#.....##.###.##",
|
||||||
#.###.....##..#
|
"#.###.....##..#",
|
||||||
#.....###...###
|
"#.....###...###",
|
||||||
####.####.#.###
|
"####.####.#.###",
|
||||||
##......#.....#
|
"##......#.....#",
|
||||||
#######.#.###.#
|
"#######.#.###.#",
|
||||||
#.........#####
|
"#.........#####",
|
||||||
#.####.S#....##
|
"#.####.S#....##",
|
||||||
##....#.#######
|
"##....#.#######",
|
||||||
#..##.........#
|
"#..##.........#",
|
||||||
##...###.##.###
|
"##...###.##.###",
|
||||||
###############
|
"###############")
|
||||||
|
|
||||||
##################
|
maze6 = c("##################",
|
||||||
##...#####...#####
|
"##...#####...#####",
|
||||||
##.#.##....#.....#
|
"##.#.##....#.....#",
|
||||||
##.S....##...###.#
|
"##.S....##...###.#",
|
||||||
###.####.#.#.....#
|
"###.####.#.#.....#",
|
||||||
#........#.##.####
|
"#........#.##.####",
|
||||||
##.#######......##
|
"##.#######......##",
|
||||||
##......##########
|
"##......##########",
|
||||||
#####.####....##.#
|
"#####.####....##.#",
|
||||||
##.........##..#.#
|
"##.........##..#.#",
|
||||||
#######.#####.##.#
|
"#######.#####.##.#",
|
||||||
####.##.##.....#.#
|
"####.##.##.....#.#",
|
||||||
####.##.########.#
|
"####.##.########.#",
|
||||||
#................#
|
"#................#",
|
||||||
#..###############
|
"#..###############",
|
||||||
##.#.##.#.##.##.##
|
"##.#.##.#.##.##.##",
|
||||||
##...............#
|
"##...............#",
|
||||||
##########E#######
|
"##########E#######")
|
||||||
|
|
||||||
####################
|
maze7 = c("####################",
|
||||||
#..................#
|
"#..................#",
|
||||||
#.##############.###
|
"#.##############.###",
|
||||||
#.########.......###
|
"#.########.......###",
|
||||||
#.#........######..#
|
"#.#........######..#",
|
||||||
#.##.##.##........##
|
"#.##.##.##........##",
|
||||||
#.#...#.##.######.##
|
"#.#...#.##.######.##",
|
||||||
#.###.#.##.....##.##
|
"#.###.#.##.....##.##",
|
||||||
#.###..##########.##
|
"#.###..##########.##",
|
||||||
#.####.###.........#
|
"#.####.###.........#",
|
||||||
#.#....#...####.####
|
"#.#....#...####.####",
|
||||||
#.#.####.####.....##
|
"#.#.####.####.....##",
|
||||||
#.#......#......####
|
"#.#......#......####",
|
||||||
#.###.####.#####...#
|
"#.###.####.#####...#",
|
||||||
#.#......#.....#.###
|
"#.#......#.....#.###",
|
||||||
#.######...#####.#.#
|
"#.######...#####.#.#",
|
||||||
#.#.....##S#...#.#.#
|
"#.#.....##S#...#.#.#",
|
||||||
#.#########..#.....#
|
"#.#########..#.....#",
|
||||||
#..........###.##.##
|
"#..........###.##.##",
|
||||||
##########E#########
|
"##########E#########")
|
||||||
|
|
||||||
##########
|
|
||||||
#S.......#
|
|
||||||
########.#
|
|
||||||
#..T.....#
|
|
||||||
#.########
|
|
||||||
#........#
|
|
||||||
########.#
|
|
||||||
#..T.....#
|
|
||||||
#.########
|
|
||||||
#E########
|
|
||||||
|
|
||||||
#####E####
|
maze3_T = c("##########",
|
||||||
#...#....#
|
"#S.......#",
|
||||||
#.######.#
|
"########.#",
|
||||||
#.#T.....#
|
"#..T.....#",
|
||||||
#.###.####
|
"#.########",
|
||||||
#....S...#
|
"#........#",
|
||||||
#####.##.#
|
"########.#",
|
||||||
#..##.####
|
"#..T.....#",
|
||||||
##....T..#
|
"#.########",
|
||||||
##########
|
"#E########")
|
||||||
|
|
||||||
###E###########
|
maze4_T = c("#####E####",
|
||||||
#........###..#
|
"#...#....#",
|
||||||
########.....##
|
"#.######.#",
|
||||||
#..T..##.###.##
|
"#.#T.....#",
|
||||||
#.###.....##..#
|
"#.###.####",
|
||||||
#.....###...###
|
"#....S...#",
|
||||||
####.####.#.###
|
"#####.##.#",
|
||||||
##......#.....#
|
"#..##.####",
|
||||||
#######.#.###.#
|
"##....T..#",
|
||||||
#.........#####
|
"##########")
|
||||||
#.####.S#....##
|
|
||||||
##....#.#######
|
|
||||||
#..##T........#
|
|
||||||
##...###.##.###
|
|
||||||
###############
|
|
||||||
|
|
||||||
####################
|
maze5_T = c("###E###########",
|
||||||
#..................#
|
"#........###..#",
|
||||||
#.##############.###
|
"########.....##",
|
||||||
#.########.......###
|
"#..T..##.###.##",
|
||||||
#.#.T......######..#
|
"#.###.....##..#",
|
||||||
#.##.##.##........##
|
"#.....###...###",
|
||||||
#.#...#.##.######.##
|
"####.####.#.###",
|
||||||
#.###.#.##....T##.##
|
"##......#.....#",
|
||||||
#.###..##########.##
|
"#######.#.###.#",
|
||||||
#.####.###.........#
|
"#.........#####",
|
||||||
#.#....#...####.####
|
"#.####.S#....##",
|
||||||
#.#.####.####.....##
|
"##....#.#######",
|
||||||
#.#......#......####
|
"#..##T........#",
|
||||||
#.###.####.#####...#
|
"##...###.##.###",
|
||||||
#.#T.....#.....#.###
|
"###############")
|
||||||
#.######...#####.#.#
|
|
||||||
#.#.....##S....#.#.#
|
|
||||||
#.#########..#...T.#
|
maze7_T = c("####################",
|
||||||
#..........###.##.##
|
"#..................#",
|
||||||
##########E#########
|
"#.##############.###",
|
||||||
|
"#.########.......###",
|
||||||
|
"#.#.T......######..#",
|
||||||
|
"#.##.##.##........##",
|
||||||
|
"#.#...#.##.######.##",
|
||||||
|
"#.###.#.##....T##.##",
|
||||||
|
"#.###..##########.##",
|
||||||
|
"#.####.###.........#",
|
||||||
|
"#.#....#...####.####",
|
||||||
|
"#.#.####.####.....##",
|
||||||
|
"#.#......#......####",
|
||||||
|
"#.###.####.#####...#",
|
||||||
|
"#.#T.....#.....#.###",
|
||||||
|
"#.######...#####.#.#",
|
||||||
|
"#.#.....##S....#.#.#",
|
||||||
|
"#.#########..#...T.#",
|
||||||
|
"#..........###.##.##",
|
||||||
|
"##########E#########")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue