-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleplanner.py
68 lines (58 loc) · 2.47 KB
/
battleplanner.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
from datetime import datetime
import pygame
from population import Population
# from planner_tab import Planner_Tab
from colors import BLACK
# Font config
pygame.font.init()
FONT = pygame.font.SysFont(pygame.font.get_default_font(), 15)
# TODO Not working on linux
def log(attack_plan, generation_amount, population_size, mutation_rate):
f = open("result_logs.txt", "a")
f.write("=" * 60 + "\n")
f.write("|" + " " * 19 + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + " " * 20 + "|" + "\n")
f.write("=" * 60 + "\n")
f.write("\t%29s: %d\n" % ("Generations", generation_amount))
f.write("\t%29s: %d\n" % ("Population", population_size))
f.write("\t%29s: %.3f\n" % ("Mutation rate", mutation_rate))
f.write("\t%29s: %.4f\n" % ("Time", attack_plan.total_time))
f.write("\t%29s: %.4f\n" % (" Fitness", attack_plan.fitness))
f.write("=" * 60 + "\n")
f.write("|" + " " * 23 + "ATTACK PLAN" + " " * 24 + "|" + "\n")
f.write("=" * 60 + "\n")
f.write(attack_plan.self_to_string())
f.write("=" * 60 + "\n")
f.write("\n")
f.close()
def find_weakest(available_knights):
weakest = available_knights[0]
for knight in available_knights:
if knight.power < weakest.power:
weakest = knight
weakest.weakest = True
# ------------------------------------------------------------------------
def algorithm(planner_tab, generation_amount, pop_max, mutation_rate, weakest_live, available_knights, houses):
if weakest_live:
find_weakest(available_knights)
population = Population(houses, available_knights, pop_max, weakest_live)
the_best = population.population[0]
for _generation in range(0, generation_amount):
# print("NAT SELECTION")
population.natural_selection()
# print("GENERATE")
population.generate(mutation_rate)
# print("CALC FITNESS")
population.calc_fitness()
# print("ORDER BY")
population.order_by_fitness()
print("GENERATION %4d" % population.current_generation, " | Best Time: %.3f" % population.population[0].total_time, " | Best Fitness: %.3f" % population.population[0].fitness)
if population.population[0].fitness > the_best.fitness:
the_best = population.population[0]
planner_tab.draw(pop_max, mutation_rate, population.current_generation, the_best)
# population.print_self()
print("-"*60)
print("The best time is ", the_best.total_time, " and its fitness is ", the_best.fitness)
print("BATTLEPLAN")
the_best.print_self()
print("-"*60)
log(the_best, generation_amount, pop_max, mutation_rate)