-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlap_checking.py
134 lines (82 loc) · 3.8 KB
/
overlap_checking.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
import numpy as np
from seperated_axis import *
from scipy.spatial import ConvexHull
from copy import deepcopy
from itertools import combinations
from fight import fight
from check_strength import check_strength
def overlap(step, colonies, relationship, inner_step):
'''
Describe:
Check if two colonies overlap with each other or not. If they overlap, synergistic colonies will merge into a big one. Otherwise,
they will fight until they do not overlap.
Positional arguments:
-> self:
-> colonies: a dictionary containing all colonies during the simulation
-> relationship: relationship between two colonies
-> inner_step: pseudo steps
Return:
None
'''
# EXIT. Only one colony left.
if len(colonies.keys()) <= 1:
check_strength(step, colonies)
return inner_step
# If fewer than three bacteria are in one colony, they cannot form a well-defiend convexhull.
# Another algorithm will be applied.
colonies_keys_1 = list(colonies.keys())
colonies_keys_2 = deepcopy(colonies_keys_1)
for colony in colonies_keys_2:
if len(colonies[colony].points) <= 3 and len(colonies[colony].points) > 0:
colonies_keys_1.remove(colony)
elif len(colonies[colony].points) == 0:
del colonies[colony]
colonies_keys_1.remove(colony)
else:
pass
# If the colony does not contain any bacteria, then delete it from the colonies
# Combinations between two arbitrary colonies
comb = list(combinations(colonies_keys_1, 2))
vertices_set = dict()
overlap_colony = list()
# Generate vertices of the Convex hulls for each existing colony
for index in colonies_keys_1:
convexhull = ConvexHull(colonies[index].points) # Get convex hull model for the colony
points = deepcopy(colonies[index].points)
condition = np.array([[True, True] if i in convexhull.vertices else [False, False] for i in range(len(colonies[index].points))])
vertices = np.extract(condition, points)
vertices = vertices.reshape(int(len(vertices) / 2), 2)
vertices_set[index] = vertices # five arrays of vertices for the convex hulls for each colony
# Synergistic
if relationship == 'Synergistic':
# Check whether two colonies overlap with each other or not
for pair in comb:
check = separating_axis_theorem(vertices_set[pair[0]], vertices_set[pair[1]])
if check:
overlap_colony = pair # tend to merge one combination of two colonies
break
else:
pass
# EXIT. Return when no colonies overlap
if overlap_colony == list():
check_strength(step, colonies)
return inner_step
else:
colonies[overlap_colony[0]].merge(colonies[overlap_colony[1]])
del colonies[overlap_colony[1]]
inner_step = overlap(step, colonies, relationship, inner_step)
return inner_step
else: # Competitive
# Check whether two colonies overlap with each other or not
for pair in comb:
check = separating_axis_theorem(vertices_set[pair[0]], vertices_set[pair[1]])
if check:
overlap_colony.append(pair) # Two colonies will fight
# Return when no colonies overlap
if overlap_colony == list():
check_strength(step, colonies)
return inner_step
else:
inner_step = fight(step, colonies, overlap_colony, inner_step)
inner_step = overlap(step, colonies, relationship, inner_step)
return inner_step