-
Notifications
You must be signed in to change notification settings - Fork 3
/
GP.py
353 lines (321 loc) · 15.1 KB
/
GP.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# this file includes:
# - makeBluetree()
# - synthesisBlueTree()
# - genFullBlueTree()
# - convertFullBluetree_to_oriBluetree()
# - get_val_frombluetree()
# - addsub3()
# bachthuan03111997@gmail.com
import random
import geopandas as gpd
import Terminalset as ts
##############################################################################
######################## This part is the part of Genetic Programming process.
class node: # Define a node of tree.
# node class to save some attributes of that node.
def __init__(self):
self.childs = None
self.numberChilds = 0;
self.strname = ''
self.nodeID = 0 # the ID of that node in tree.
self.funcORter = 'ter' # 'ter' is the terminal node, else 'func' is the function node.
self.valueofnode = 0 # the value of that node. Ex: the value of the polygon type under Dataframe type.
self.numNodes = 0 # number of node in current node that it contains.
self.maxNodeID = 0 # max ID of node in current node that it contains.
self.synthesischeck = False
self.LeafClass = None # this attribute to save leaf class.
self.type = None # Type of the node like red,blue,substrate, etc.
self.isRed_SubstrateNode = False
self.isRed_TerminalNode = False
self.special = False # to show it's the third parameter of addsub tree or not
self.is_red_rota = False # to specify whether node is represented for rotation or not.
self.is_red_coords = False # to specify whether node is represented for coords or not.
self.depth = 0 # to show it's depth.
def makeBlueTree(maxdep,ismaxdepth,lastnode,MaxX,MaxY):
# NOTICE THAT: THIS FUNCTION RELATES TO NUMBER OF TERMINAL SET (now system has two terminals: lsubtree and Usubtree)
# MAXDEP is the maximum of depth that tree has(integer).
# ISMAXDEPTH whether tree is created by all branch is maxdep or not(boolean).
# LASTNODE is the last node id of previous tree(integer).
# MaxX is the maximum length of X axis.
# MaxY is the maximum length of Y axis.
# note:
# makeBlueTree is the recursive function.
# maxdep = maxdep - 1.
# RETURN: [tree,lastnode].
thisnode = lastnode + 1
tree = node()
if ismaxdepth: # Full method.
if maxdep == 0:
# we must chose a terminal.
LorU = random.randint(0,1) # 0 is the Ltree else is the Utree.
if LorU == 0:
xtype = random.randint(1,4)
temp = ts.Lsub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.valueofnode = temp.polygon
tree.LeafClass = temp # save class object.
tree.type = 1
else:
xtype = random.randint(1,4)
temp = ts.Usub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.valueofnode = temp.polygon
tree.LeafClass = temp
tree.type = 1
else:
maxdep = maxdep - 1
# all atributes of this node must be a function.
tree.numberChilds = 2
tree.nodeID = thisnode
tree.childs = []
tree.strname = 'union2'
tree.funcORter = 'func'
tree.type = 1
for i in range(2):
temp1 = makeBlueTree(maxdep,ismaxdepth,thisnode,MaxX,MaxY)
tree.childs.append(temp1[0])
lastnode = temp1[1]
thisnode = lastnode + 1
else: #### Grow method.
if maxdep == 0:
# we must chose a terminal.
LorU = random.randint(0,1) # 0 is the Ltree else is the Utree.
if LorU == 0:
xtype = random.randint(1,4)
temp = ts.Lsub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.type = 1
tree.valueofnode = temp.polygon
tree.LeafClass = temp
else:
xtype = random.randint(1,4)
temp = ts.Usub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.type = 1
tree.valueofnode = temp.polygon
tree.LeafClass = temp
else:
# we can choose randomly type of node(terminal node or function node)
chose = random.randint(0,1)
if chose == 1:
# we must chose a terminal type for this node.
LorU = random.randint(0,1) # 0 is the Ltree else is the Utree.
if LorU == 0:
xtype = random.randint(1,4)
temp = ts.Lsub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.type = 1
tree.valueofnode = temp.polygon
tree.LeafClass = temp
else:
xtype = random.randint(1,4)
temp = ts.Usub_tree(MaxX,MaxY,xtype)
tree.strname = temp.strname
tree.nodeID = thisnode
tree.funcORter = 'ter'
tree.type = 1
tree.valueofnode = temp.polygon
tree.LeafClass = temp
else:
# chose the function node.
maxdep = maxdep - 1
# all atributes of this node must be a function.
tree.numberChilds = 2
tree.nodeID = thisnode
tree.childs = []
tree.strname = 'union2'
tree.funcORter = 'func'
tree.type = 1
for i in range(2):
temp1 = makeBlueTree(maxdep,ismaxdepth,thisnode,MaxX,MaxY)
tree.childs.append(temp1[0])
lastnode = temp1[1]
thisnode = lastnode + 1
tree.numNodes = thisnode - tree.nodeID + 1
tree.maxNodeID = lastnode + 1
return [tree,lastnode]
def synthesisBlueTree(tree):
# This function is used for systhesis all polygons of tree, and update the valueofnode in function node in tree.
if tree.childs == None or tree.synthesischeck == True: # case 6.
if not tree.synthesischeck == True:
tree.synthesischeck = True
else:
if (tree.childs[0].synthesischeck == False) and (tree.childs[1].synthesischeck == False) and (tree.childs[0].childs == None) and (tree.childs[1].childs==None):
tree.valueofnode = union(tree.childs[0].valueofnode,tree.childs[1].valueofnode)
#print('Union ',tree.nodeID)
tree.synthesischeck = True # case 1
elif (tree.childs[0].synthesischeck == True) and (tree.childs[1].synthesischeck == True): # case 5
tree.valueofnode = union(tree.childs[0].valueofnode,tree.childs[1].valueofnode)
#print('Union ',tree.nodeID)
tree.synthesischeck = True
elif (tree.childs[0].synthesischeck == True) and (tree.childs[1].synthesischeck == False): # case 2.
if tree.childs[1].childs == None:
tree.valueofnode = union(tree.childs[0].valueofnode,tree.childs[1].valueofnode)
#print('Union ',tree.nodeID)
tree.synthesischeck = True
else:
synthesisBlueTree(tree.childs[1])
elif (tree.childs[0].synthesischeck == False) and (tree.childs[1].synthesischeck == True): # case 3
if tree.childs[0].childs == None:
tree.valueofnode = union(tree.childs[0].valueofnode,tree.childs[1].valueofnode)
#print('Union ',tree.nodeID)
tree.synthesischeck = True
else:
synthesisBlueTree(tree.childs[0])
else: # case 4
for i in range(len(tree.childs)):
synthesisBlueTree(tree.childs[i])
if (tree.synthesischeck == False):
synthesisBlueTree(tree)
return tree
def genFullBlueTree(tree):
# NOTICE THAT: THIS FUNCTION RELATES TO NUMBER OF TERMINAL SET (now system has two terminals: lsubtree and Usubtree)
# tree: this tree is the output of makeBlueTree function.
if (tree.strname == 'L1') or (tree.strname == 'L2') or (tree.strname == 'L3') or (tree.strname == 'L4'):
tree.strname = 'Lsubtree7'
tree.funcORter = 'func'
# branch 1 -- the rotation angle (0,90,180,270).
# 2,3 -- the coordinate of shapesubtree.
# 4,5,6,7 -- x1,x2,y1,y2 each of them relates with each of edge L shape.
tree.childs = []
for i in range(7):
temp = node()
if i == 0:
temp.is_red_rota = True
if i == 5 or i == 6:
temp.is_red_coords = True
temp.valueofnode = round(random.uniform(-1,1),2)
temp.strname = str(temp.valueofnode)
temp.funcORter = 'ter'
temp.isRed_TerminalNode = True
temp.type = 0
tree.childs.append(temp)
elif (tree.strname == 'U1') or (tree.strname == 'U2') or (tree.strname == 'U3') or (tree.strname == 'U4'):
tree.strname = 'Usubtree9'
tree.funcORter = 'func'
# branch 1 -- the rotation angle (0,90,180,270).
# 2,3 -- the coordinate of shapesubtree.
# 4,5,6,7,8,9 -- x1,x2,x3,y1,y2,y3 each of them relates with each of edge U shape.
tree.childs = []
for i in range(9):
temp = node()
if i == 0:
temp.is_red_rota = True
if i == 7 or i == 8:
temp.is_red_coords = True
temp.valueofnode = round(random.uniform(-1,1),2)
temp.strname = str(temp.valueofnode)
temp.funcORter = 'ter'
temp.isRed_TerminalNode = True
temp.type = 0
tree.childs.append(temp)
else: # tree.strname == 'union'
for i in range(len(tree.childs)):
genFullBlueTree(tree.childs[i])
return tree
def convertFullBluetree_to_oriBluetree(fullbluetree):
# NOTICE THAT: THIS FUNCTION RELATES TO NUMBER OF TERMINAL SET (now system has two terminals: lsubtree and Usubtree)
# fulbluetree (class type)
# convert fullbluetree to original blutree to sinthesis.
if fullbluetree.strname == 'Lsubtree7':
fullbluetree.funcORter = 'ter'
fullbluetree.childs = None
fullbluetree.strname = fullbluetree.LeafClass.strname
fullbluetree.numberChilds = 0
#fullbluetree.valueofnode = fullbluetree.LeafClass.polygon
fullbluetree.synthesischeck = False
elif fullbluetree.strname == 'Usubtree9':
fullbluetree.funcORter = 'ter'
fullbluetree.childs = None
fullbluetree.strname = fullbluetree.LeafClass.strname
fullbluetree.numberChilds = 0
#fullbluetree.valueofnode = fullbluetree.LeafClass.polygon
fullbluetree.synthesischeck = False
else: #fullbluetree.strname = 'union2':
for i in range(2):
fullbluetree.childs[i] = convertFullBluetree_to_oriBluetree(fullbluetree.childs[i])
bluetree = fullbluetree
bluetree = synthesisBlueTree(bluetree)
return bluetree
def updateFullBlueTree(fullbluetree,MaxXY,updateType):
# update all of nodes in fullbluetree after initing for gp operation.
# updateType = 1 : initilize all terminal shapes correspondly.
# updateType = 2 : change specified terminal shape correspondly after GP operation(like crossover, mutation,...).
# updateType = 3 : change specified terminal shape (maxXY) after GP operation.
# updateType = 4 : change all atributes of all terminal shapes in GP tree after using Lowlevel-optimizer.
if not ((updateType == 1) or (updateType == 2) or (updateType == 3) or (updateType == 4) or (updateType == 5)):
raise ValueError('the updateType must be 1 or 2 or 3.')
if fullbluetree.strname == 'Lsubtree7':
#print('Lsub, ID ',fullbluetree.nodeID)
temp = []
for i in range(7):
temp.append(fullbluetree.childs[i].valueofnode)
fullbluetree.childs[i].strname = str(fullbluetree.childs[i].valueofnode)
fullbluetree.LeafClass.resestMaxXY(MaxXY[0],MaxXY[1])
fullbluetree.LeafClass.initchange(temp)
fullbluetree.valueofnode = fullbluetree.LeafClass.polygon
fullbluetree.synthesischeck = False
elif fullbluetree.strname == 'Usubtree9':
#print('Usub, ID ',fullbluetree.nodeID)
temp = []
for i in range(9):
temp.append(fullbluetree.childs[i].valueofnode)
fullbluetree.childs[i].strname = str(fullbluetree.childs[i].valueofnode)
fullbluetree.LeafClass.resestMaxXY(MaxXY[0],MaxXY[1])
fullbluetree.LeafClass.initchange(temp)
fullbluetree.valueofnode = fullbluetree.LeafClass.polygon
fullbluetree.synthesischeck = False
else: #fullbluetree.strname = 'union2':
for i in range(2):
fullbluetree.childs[i] = updateFullBlueTree(fullbluetree.childs[i],MaxXY,updateType)
bluetree = fullbluetree
#bluetree = synthesisBlueTree(bluetree)
#del temp
return bluetree
#def update_a_specified_node_in_FullBlueTree(fullbluetree)
def get_val_frombluetree(tree):
temptree = convertFullBluetree_to_oriBluetree(tree)
return temptree.valueofnode
##############
### define function set.
def union(polygon1,polygon2):
return gpd.overlay(polygon1,polygon2,how = 'union')
#def genFulltree()
def addsub3():
# create randomly new substrate.
temp = node()
temp.strname = 'addsub3'
temp.funcORter = 'func'
temp.type = 3 # substrate type
temp.numberChilds = 3
temp.childs = []
temp.valueofnode = []
for i in range(3):
temp2 = node()
temp2.valueofnode = round(random.uniform(-1,1),2)
temp2.strname = str(temp2.valueofnode)
temp2.funcORter = 'ter'
temp2.isRed_SubstrateNode = True
temp2.isRed_TerminalNode = True
temp2.type = 0 # red type
if i == 2:
temp2.special = True
temp.childs.append(temp2)
temp.valueofnode.append(temp2.valueofnode)
return temp
def update_Depth_GP_tree(tree,current_dep):
# update the maxdepth able of all node in a tree.
tree.depth = current_dep
if tree.childs != None:
for i in range(len(tree.childs)):
update_Depth_GP_tree(tree.childs[i],current_dep+1)