/
multithread_sorting_cell_aggregation_disorder.py
220 lines (180 loc) · 8.11 KB
/
multithread_sorting_cell_aggregation_disorder.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
import sys, getopt
from tkinter import *
import threading
import time
from modules.multithread.SelectionSortCell import SelectionSortCell
from modules.multithread.BubbleSortCell import BubbleSortCell
from modules.multithread.InsertionSortCell import InsertionSortCell
from modules.multithread.MergeSortCell import MergeSortCell
from modules.multithread.CellGroup import CellGroup, GroupStatus
from modules.multithread.MultiThreadCell import CellStatus
from visualization.CellImage import CellImage
from visualization.CellGroupImage import CellGroupImage
from modules.multithread.StatusProbe import StatusProbe
import numpy as np
import random
VALUE_LIST = [28, 34, 6, 20, 7, 89, 34, 18, 29, 51]
#VALUE_LIST = range(20,0,-1)
def get_cell_type_list(total_cells, should_shuffle, bubble_pct):
bubble_cell_num = int(total_cells * bubble_pct)
selection_cell_num = total_cells - bubble_cell_num
b_list = [1 for _ in range(bubble_cell_num)]
s_list = [0 for _ in range(selection_cell_num)]
combined_list = []
for i in range(max(bubble_cell_num, selection_cell_num)):
if i < bubble_cell_num:
combined_list.append(b_list[i])
if i < selection_cell_num:
combined_list.append(s_list[i])
if should_shuffle:
random.shuffle(combined_list)
return combined_list
def get_cell_type_list_v2(total_cells_per_type, generate_bubble, generate_selection, generate_insertion):
combined_list = []
if generate_bubble:
b_list = [0 for _ in range(total_cells_per_type)]
combined_list.extend(b_list)
if generate_insertion:
b_list = [1 for _ in range(total_cells_per_type)]
combined_list.extend(b_list)
if generate_selection:
b_list = [2 for _ in range(total_cells_per_type)]
combined_list.extend(b_list)
random.shuffle(combined_list)
return combined_list
def create_cell_groups_based_on_value_list(value_list, threadLock, status_probe, total_cells_per_type, generate_bubble, generate_selection, generate_insertion):
if len(value_list) == 0:
return []
cells = []
cell_groups = []
cell_type_list = get_cell_type_list_v2(total_cells_per_type, generate_bubble, generate_selection, generate_insertion)
if len(cell_type_list) != len(value_list):
raise BaseException("unmatch cell number")
left_boundary = (0, 1)
right_boundary = (len(value_list) - 1, 1)
for i in range(0, len(value_list)):
cell = None
cell_type_number = cell_type_list[i]
if cell_type_number == 0:
cell = BubbleSortCell(i + 1, value_list[i], threadLock, (i, 1), cells, left_boundary, right_boundary, status_probe, disable_visualization=True, reverse_direction=False)
if cell_type_number == 1:
cell = InsertionSortCell(i + 1, value_list[i], threadLock, (i, 1), cells, left_boundary, right_boundary, status_probe, disable_visualization=True, reverse_direction=False)
if cell_type_number == 2:
cell = SelectionSortCell(i + 1, value_list[i], threadLock, (i, 1), cells, left_boundary, right_boundary, status_probe, disable_visualization=True, reverse_direction=True)
period = random.randint(100000, 200000000)
start_count_down = random.randint(100000, 200000000)
cell_group = CellGroup([cell], cells, i, (i, 1), (i, 1), GroupStatus.SLEEP, threadLock, start_count_down, period)
# cell.group = cell_group
if cell:
cells.append(cell)
# cells[random.randint(0, len(cells) - 1 )].set_cell_to_freeze()
# for c in random.choices(cells, k=50):
# c.reverse_direction = True
period = 1000000000
start_count_down = 1000000000
cell_group = CellGroup(cells, cells, 0, left_boundary, right_boundary, GroupStatus.ACTIVE, threadLock, start_count_down, period)
for cell in cells:
cell.group = cell_group
return cells, [cell_group]
def check_cell_status(cells):
print([c.value for c in cells])
prev_cell = cells[0]
for c in cells:
if c.value < prev_cell.value:
return True
prev_cell = c
for c in cells:
c.status = CellStatus.INACTIVE
return False
def print_cell_status(cells):
print([c.get_current_snapshot() for c in cells])
def activate(cells, cell_groups):
for cell in cells:
cell.start()
for group in cell_groups:
group.start()
def get_pass_in_args(argv):
cell_type = ""
try:
opts, args = getopt.getopt(argv, "h", ["cell_type="])
except getopt.GetoptError:
print("please specify cell type using '--cell_type='")
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print("multithread_cell_sorting.py --cell_type=<cell_type>")
sys.exit()
if opt == "--cell_type":
cell_type = arg
if not cell_type:
print("please specify cell type using '--cell_type='")
sys.exit(2)
return cell_type
def all_thread_inactive(cells):
for c in cells:
if c.status != CellStatus.INACTIVE:
return False
return True
def kill_all_thread(cells, groups):
while not all_thread_inactive(cells):
for c in cells:
c.status = CellStatus.INACTIVE
for g in groups:
g.status = GroupStatus.MERGED
def is_sorted(cells):
prev_cell = cells[0]
for c in cells:
if c.value < prev_cell.value:
return False
prev_cell = c
return True
def prepare_sorting_list():
return [i for i in range(100)]
def no_cells_should_move(cells):
for c in cells:
if c.status == CellStatus.SLEEP:
return False
if c.status == CellStatus.ACTIVE and c.should_move():
return False
return True
def main(argv):
sorting_list = prepare_sorting_list()
print(sorting_list)
for i in range(100):
random.shuffle(sorting_list)
sorting_steps_for_each_run = []
threadLock = threading.Lock()
status_probe = StatusProbe()
print(f">>>>>>>>>>>>>>>>> Prepare cells to sort for experiment {i} <<<<<<<<<<<<<<<<<<<<")
random.shuffle(sorting_list)
cells, cell_groups = create_cell_groups_based_on_value_list(sorting_list, threadLock, status_probe, 50, True, False, True)
threadLock.acquire()
print("Activating cells...")
activate(cells, cell_groups)
threadLock.release()
print("Start sorting......")
try:
while not no_cells_should_move(cells) and len(status_probe.sorting_steps) < 15000:
# print_cell_status(cells)
# print(no_cells_should_move(cells))
# print(len(status_probe.sorting_steps))
time.sleep(1)
threadLock.acquire()
kill_all_thread(cells, cell_groups)
threadLock.release()
time.sleep(1)
except:
print(len(status_probe.sorting_steps))
print_cell_status(cells)
print(">>>>>>>>>>>>>>>>> Sorting complete, killed all threads. <<<<<<<<<<<<<<<<<<<<\n")
#print(status_probe.cell_types[0])
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests/exp_{i}', status_probe.cell_types)
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests/exp_{i}_sorting_steps', status_probe.sorting_steps)
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests_insertion_selection_disorder/exp_{i}', status_probe.cell_types)
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests_insertion_selection_disorder/exp_{i}_sorting_steps', status_probe.sorting_steps)
np.save(f'csv/cell_type_aggregation_random_dist_100_tests_bubble_insertion/exp_{i}', status_probe.cell_types)
np.save(f'csv/cell_type_aggregation_random_dist_100_tests_bubble_insertion/exp_{i}_sorting_steps', status_probe.sorting_steps)
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests_bubble_selection_disorder/exp_{i}', status_probe.cell_types)
# np.save(f'csv/cell_type_aggregation_random_dist_100_tests_bubble_selection_disorder/exp_{i}_sorting_steps', status_probe.sorting_steps)
if __name__ == "__main__":
main(sys.argv[1:])