-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pik-gane/parallelization
Parallelization
- Loading branch information
Showing
6 changed files
with
149 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Test comparing the performance (time and simulation output) of the target function with and without parallelization. | ||
""" | ||
|
||
from time import time | ||
import numpy as np | ||
import pylab as plt | ||
from pyoptes import set_seed | ||
from pyoptes.optimization.budget_allocation import target_function as fp | ||
from tqdm import tqdm | ||
|
||
set_seed(2) | ||
|
||
n_simulations = 10000 | ||
n_nodes = 120 | ||
|
||
fp.prepare( | ||
n_nodes=n_nodes, # instead of 60000, since this should suffice in the beginning | ||
capacity_distribution=np.random.lognormal, # this is more realistic than a uniform distribution | ||
delta_t_symptoms=60, # instead of 30, since this gave a clearer picture in Sara's simulations | ||
parallel=True) | ||
|
||
total_budget = 1.0 * n_nodes | ||
|
||
# init test strategy | ||
weights = np.random.rand(n_nodes) | ||
shares = weights / weights.sum() | ||
x = shares * total_budget | ||
|
||
a = time() | ||
# y = np.mean([fp.evaluate(x, n_simulations=n_simulations, parallel=False) for _ in range(100)]) | ||
y = fp.evaluate(x, n_simulations=n_simulations, parallel=False) | ||
b = time()-a | ||
print('Non-parallel simulation') | ||
print(f'Time for {n_simulations} simulations of: {b}') | ||
print(f'y: {y}') | ||
|
||
|
||
a = time() | ||
# y = np.mean([fp.evaluate(x, n_simulations=n_simulations) for _ in range(100)]) | ||
y = fp.evaluate(x, n_simulations=n_simulations, parallel=True) | ||
b = time()-a | ||
print('Parallel simulation') | ||
print(f'Time for {n_simulations} simulations of: {b}') | ||
print(f'y: {y}') | ||
|
||
n = [1, 100, 1000, 10000, 100000, 1000000] | ||
|
||
tl = [] | ||
yl = [] | ||
|
||
tlp = [] | ||
ylp = [] | ||
for s in tqdm(n): | ||
a = time() | ||
yl.append(fp.evaluate(x, n_simulations=s, parallel=False)) | ||
tl.append(time() - a) | ||
|
||
a = time() | ||
ylp.append(fp.evaluate(x, n_simulations=s, parallel=True)) | ||
tlp.append(time() - a) | ||
|
||
plt.plot(n, yl, label='y non-parallel') | ||
plt.plot(n, ylp, label='y parallel') | ||
plt.title('Simulation output for 1, 100, 1000, 10000, 1000000 evaluations') | ||
plt.legend() | ||
plt.show() | ||
|
||
plt.plot(n, tl, label='time non-parallel') | ||
plt.plot(n, tlp, label='time parallel') | ||
plt.title('Evaluation time for for 1, 100, 1000, 10000, 1000000 evaluations') | ||
plt.legend() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters