-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplots.py
136 lines (114 loc) · 4.6 KB
/
plots.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
135
136
import random
import numpy as np
from logger import Logger
from copy import deepcopy
logger = Logger('Plot')
def initialise(gridArray):
# try:
buildingDict = {
"towncentre": [12,7],
"simplehouse": [3,3],
"bench": [1,1]
}
plotDict = {
"towncentre": 1,
"simplehouse": 9999,
"bench": 9999
}
plotWeightingDict = {
"towncentre": 100,
"simplehouse": 50,
"bench": 10
}
buildingNumDict = {}
population_size = 10
def getBuildableplot(individual):
posArray = []
pos = np.where(individual == 1)
for x, z in zip(pos[0], pos[1]):
posArray.append([x,z])
return (posArray)
def getRandomPlot(currentPlotWeightingDict, buildingDict, randomWeighting):
for key in currentPlotWeightingDict: #FIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIX
randomWeighting -= currentPlotWeightingDict[key]
if randomWeighting <= 0:
plotName = key #FIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIX
plot = buildingDict[key] #FIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIXFIX
# logger.info(u'{} {}'.format(plot, plotName))
return plot, plotName
def fitting(pos, plot):
for x in range(plot[0]):
for z in range(plot[1]):
if (pos[0] + x < len(individual) and pos[1] + z < len(individual[0])):
if ((individual[pos[0] + x][pos[1] + z])) != 1:
return False
else:
return False
return True
def placing(pos, plot, plotName, buildingNum):
for x in range(plot[0]):
for z in range(plot[1]):
individual[pos[0] + x][pos[1] + z] = buildingNum
buildingNumDict[buildingNum] = plotName
buildingNum += 1
plotDict[plotName] -= 1
if plotDict[plotName] == 0:
plotWeightingDict[plotName] = 0
return buildingNum
population = []
buildingNum = 100
for pop in range(population_size):
individual = deepcopy(gridArray)
plotRemaining = len(getBuildableplot(individual))
while plotRemaining >= 1: # Generating each individual
buildablePlotPos = getBuildableplot(individual)
pos = buildablePlotPos[(random.randint(0, len(buildablePlotPos)-1))]
currentPlotWeightingDict = dict(plotWeightingDict)
totalWeighting = sum(currentPlotWeightingDict.values())
randomWeighting = random.randint(0, totalWeighting)
plot, plotName = getRandomPlot(currentPlotWeightingDict, buildingDict, randomWeighting)
isFit = (fitting(pos, plot))
# logger.info(u'trying {} {}'.format(pos, plotName))
times = 0
while not isFit:
currentPlotWeightingDict[plotName] = 0 # remove weighting from dict
totalWeighting = sum(currentPlotWeightingDict.values())
randomWeighting = random.randint(0, totalWeighting)
# logger.info(u'{} {} {}'.format(currentPlotWeightingDict, totalWeighting, randomWeighting))
plot, plotName = getRandomPlot(currentPlotWeightingDict, buildingDict, randomWeighting) # get another random plot
isFit = (fitting(pos, plot))
times += 1
# logger.info(u'try again - {} {} {}'.format(times, pos, plotName))
buildingNum = placing(pos, plot, plotName, buildingNum)
# logger.info(u'placed {} {}'.format(pos, plotName))
plotRemaining = len(getBuildableplot(individual))
population.append(individual)
return population, buildingNumDict
# except Exception as e:
# logger.error(e)
def evaluate():
try:
pass
except Exception as e:
logger.error(e)
def select():
try:
pass
except Exception as e:
logger.error(e)
def crossover():
try:
pass
except Exception as e:
logger.error(e)
def mutation():
try:
pass
except Exception as e:
logger.error(e)
def run(gridArray):
# try:
pop, buildingNumDict = initialise(gridArray)
logger.info(pop)
# except Exception as e:
# logger.error(e)