-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneration_crossmutate.py
63 lines (50 loc) · 2.3 KB
/
generation_crossmutate.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
import random
import numpy as np
from functions.func import (generate_points, fitness, generate_random_unit_vector)
RADIUS = 10
# input_areas = [2,3,5,2,3,1,4,7,2,3,6,9,2,3,1] # 15 areas
AREAS_LENGTH = 15
input_areas = np.random.randint(0,10,AREAS_LENGTH).tolist()
GENERATIONS = 500
TRESHOLD = 999
DEVIATION = 0.01
ITERATIONS = 500
BEST_AMOUNT = 5
prevGen = []
for _ in range(ITERATIONS): # generate x random lists of length n with each tuple set within circle bounds
points = generate_points(AREAS_LENGTH, RADIUS, "circle")
prevGen.append(points)
#print(prevGen)
#print(points)
#print(fitness(points, input_areas, RADIUS)) # big waste of resources to print this
points = [] # flush points array
for g in range(GENERATIONS): ######## GENERATION LOOP ##########
ranked = []
for solution in prevGen: # list every solution with its fitness score, [solution] == [points]
fitness_score = fitness(solution, input_areas, RADIUS)
ranked.append((fitness_score, solution))
ranked.sort()
ranked.reverse()
print("top score: ", ranked[0][0])
if ranked[0][0] > TRESHOLD: # stop "evolving" when good enough fitness score threshold is
print("Approximate solution found")
break
best = ranked[:BEST_AMOUNT] # grab top x best solutions
# TODO: trim off the fitness scores from this list (best) so we don't have to use [1] in best_ordered
best_ordered = []
for r in range(AREAS_LENGTH):
best_ordered.append([]) # generate a list for each region ordered by the region index
for s in best: # for every solution in best
best_ordered[r].append(s[1][r]) # grab a point by the region index and add it to its respective list
# [1] skips the fitness score and [r] selects
newGen = []
for _ in range(ITERATIONS): # generate x solutions based on the previous hundred random solutions
for r in range(AREAS_LENGTH): # for each region
random_vector = generate_random_unit_vector(2)
rand = random.choice(best_ordered[r]) # pick a random point (x,y)
x = rand[0] + random_vector[0] * DEVIATION
y = rand[1] + random_vector[1] * DEVIATION
points.append((x, y))
newGen.append(points)
points = []
prevGen = newGen