-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelism.py
More file actions
73 lines (56 loc) · 2.22 KB
/
Copy pathparallelism.py
File metadata and controls
73 lines (56 loc) · 2.22 KB
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
import time
import os
from multiprocessing import Pool
import pandas as pd
import utility
def multi_start(fun, starting_points, n_cpu, timeout=2000, i_start=0,):
"""Conduct multi_start with parallel computing
fun is the function to implement computation
starting_points is the list of all the starting points
n_cpu is the number of cpu cores
timeout is the maximum time allocated to each core
"""
# Work on the copied starting_points
starting_points = starting_points.copy()
sol_pool = []
i_end = i_start + len(starting_points)
t0 = time.time()
for k in range(len(starting_points)):
print(f'Current start_i is {i_start}')
pool = Pool(n_cpu)
results = [pool.apply_async(fun, (init,)) for init in starting_points]
for i, res in enumerate(results):
i_global = i_start + i
try:
res.get(timeout=timeout)
except:
pool.terminate()
print(f'The cpu time at initial point {i_global} exceeded {timeout}.')
# Delete the starting point i and result i
starting_points.pop(i)
results.pop(i)
break
# Collect all the available solution
pop_item = []
for i, res in enumerate(results):
if res.ready():
sol_pool.append(res.get())
pop_item.append(i)
# Update starting_points (Remove unachievable point and successful points)
starting_points = [init for i, init in enumerate(starting_points) if i not in pop_item]
results = [res for i, res in enumerate(starting_points) if i not in pop_item]
i_start = i_end - len(starting_points)
if len(results) == 0:
break
cpu_time = time.time() - t0
# Sort solutions
sol_pool = utility.sort_solution(sol_pool)
sol_best = sol_pool[0]
return sol_best, sol_pool, cpu_time
def export_sol(sol_pool, prop, directory=''):
if directory is None:
directory = ''
sol_prop_pool = [s[prop].flatten() for s in sol_pool]
file_name = f'sol_{prop}_pool.xlsx'
save_path = os.path.join(directory, file_name)
pd.DataFrame(sol_prop_pool).to_excel(save_path)