-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgo.py
65 lines (59 loc) · 1.71 KB
/
GeneticAlgo.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
import sys
from getPopulation import *
from grid import *
from selection import *
from crossover import *
import matplotlib.pyplot as plt
g = grid(10)
if len(sys.argv) == 2:
pop = getPopulation(g, filename=sys.argv[1])
else:
pop = getPopulation(g)
numGen = 100
totalPopSize = 100
for i in range(numGen):
selected = selection(pop, 80)
children = breed(selected,g)
pop = pop + children
pop = list(set(pop))
assert len(pop) >= totalPopSize
print("Population Size after breed = ", len(pop))
pop.sort(key=lambda x:x.aep, reverse=True)
pop = pop[:totalPopSize]
print("Generation ", i+1, "Max AEP = ", pop[0].aep, "Min AEP = ", pop[-1].aep)
with open("population.txt", "w") as f:
i = 1
for ind in pop:
f.write("Wind Farm " + str(i) + "\t AEP = " + str(ind.aep) + "\n")
for point in ind.locs:
f.write(str(point.x) + "," + str(point.y) + "\n")
i += 1
f.write("END")
'''
def plot_area(filename = "population.txt", numTurbines=50):
population = []
locs = []
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
if "Wind Farm" in line:
if len(locs) > 0:
population.append(WindFarm(numTurbines, locs))
locs = []
elif "END" in line:
population.append(WindFarm(numTurbines, locs))
else:
line = line[:-1].split(",")
x, y = float(line[0]), float(line[1])
locs.append(Point(x,y))
coord = population[1].locs
x=[]
y=[]
#for i in coord:
# x.append(i.x)
# y.append(i.y)
#plt.scatter(x,y)
#plt.show()
return population
plot_area()
'''