-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
134 lines (124 loc) · 5.56 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
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
import random
import GAMS.seeder
import GAMS.generation
from GAMS.mutations import weighted_swap_factory as wsf, swap2, swap3, swap_twice
from GAMS.ranker import rank_perm
from GAMS.interface import stats, epoch_start
from GAMS.crossover import oxc
def print_individual(ind: GAMS.generation.Individual, fp=None):
stats(ind.data, fp)
print("Ranking: ", ind.ranking, file=fp)
print("Age: ", ind.age, file=fp)
def main():
l = int(input("Board Size: "))
topk = int(input("Top k: "))
k = int(input("Population size: "))
kway_k = int(input("K way selection k (selection pressure, 3 is good): "))
pop = GAMS.seeder.create_population(k=k, l=l)
muts = [wsf(2, swap2), wsf(1.7, swap3), wsf(1.5, swap_twice)]
# muts = [swap2, swap3, swap_twice, complete_shuffle, no_mut]
pop = GAMS.generation.Population(rank_perm, pop, topk, k, muts, oxc, kway_k=kway_k)
print("Epoch 0")
print("Population size:", pop.size)
print("Best Sample:")
pop.population.sort()
stats(pop.population[0].data)
loops = 0
epoch = 1
while 1:
try:
if loops <= 0:
try:
r = input("Enter a number or command | ")
if r.lower() in {"q", "quit", "exit", "e"}:
return
elif r.lower()[:4] == "topk":
a = int(r[5:])
if a < k:
pop.topk = a
print("Updated topk")
else: print("Invalid topk value, size is ", k)
elif r.lower()[:5] == "cross":
a = float(r[6:])
if 0 <= a <= 1:
pop.cross_percent = a
print("Updated 'crossiness'")
else: print("Invalid value", a, "Expected between 0 to 1")
elif r.lower()[:3] == "mut":
a = float(r[4:])
if 0 <= a <= 1:
pop.mutation_prob = a
print("Updated mutation probability")
else: print("Invalid value", a, "Expected between 0 to 1")
elif r.lower()[:3] == "age":
a = int(r[4:])
if a > 0:
pop.max_top_age = a
print("Updated max top age")
else:
print("Invalid age", a, "Expected positive integer")
elif r.lower() == "config":
print("Population: ", pop.size)
print("Topk: ", pop.topk, f"{(pop.topk/pop.size):2.2%}")
print("Max Top age: ", pop.max_top_age)
print(f"Mutation Chance: {pop.mutation_prob:2.2%}")
print(f"Cross percentage: {pop.cross_percent:2.2%}")
elif r.lower() == "explore":
print("1. Random Sampling\n2. K way tournament\n3. topk\n4. All")
r = int(input("Mode: "))
fname = input("File name: ")
fp = open(fname, "w")
n = 0
if r != 4:
n = int(input("Number of samples: "))
else:
n = len(pop.population)
if r == 1:
for i in random.sample(pop.population, k=n):
print("====", file=fp)
print_individual(i, fp)
elif r == 2:
k = int(input("K way k: "))
if k <= 0:
print("Invalid k value")
fp.close()
continue
if k > 1:
for _ in range(n):
i = pop.population[pop.kway_select(k)]
print("====", file=fp)
print_individual(i, fp)
else:
l = len(pop.population) - 1
for _ in range(n):
i = pop.population[random.randint(0, l)]
print("====", file=fp)
print_individual(i, fp)
elif r == 3 or r == 4:
for i in pop.population[:min(n, len(pop.population))]:
print("====", file=fp)
print_individual(i, fp)
fp.close()
else:
loops = int(r)
except Exception:
continue
else:
epoch_start(epoch)
pop.next_gen()
print("Top stats:")
fam = stats(pop.population[0].data)
print("Ranking: ", pop.population[0].ranking)
print("Age: ", pop.population[0].age)
if fam == 0:
loops = 0
found = 0
for i in pop.population[1:]:
if i.ranking == 0: found += 1
else: break
print("Found", found, "more best samples.")
epoch += 1
loops -= 1
except KeyboardInterrupt:
loops = 0
main()