Xopt Parallel ExamplesĀ¶
Xopt provides methods to parallelize optimizations using Processes, Threads, MPI, and Dask using the concurrent.futures
interface as defined in https://www.python.org/dev/peps/pep-3148/ .
from xopt import AsynchronousXopt as Xopt
# Helpers for this notebook
import multiprocessing
N_CPUS=multiprocessing.cpu_count()
N_CPUS
import os
# directory for data.
os.makedirs("temp", exist_ok=True)
# Notebook printing output
#from xopt import output_notebook
#output_notebook()
# Nicer plotting
%config InlineBackend.figure_format = 'retina'
The Xopt
object can be instantiated from a JSON or YAML file, or a dict, with the proper structure.
Here we will make one
# Make a proper input file.
YAML = """
max_evaluations: 1000
generator:
name: cnsga
output_path: temp
population_size: 64
evaluator:
function: xopt.resources.test_functions.tnk.evaluate_TNK
function_kwargs:
sleep: 0
random_sleep: 0.1
vocs:
variables:
x1: [0, 3.14159]
x2: [0, 3.14159]
objectives: {y1: MINIMIZE, y2: MINIMIZE}
constraints:
c1: [GREATER_THAN, 0]
c2: [LESS_THAN, 0.5]
constants: {a: dummy_constant}
"""
X = Xopt(YAML)
X
Xopt ________________________________ Version: 0+untagged.1.ge872ea5 Data size: 0 Config as YAML: dump_file: null evaluator: function: xopt.resources.test_functions.tnk.evaluate_TNK function_kwargs: raise_probability: 0 random_sleep: 0.1 sleep: 0 max_workers: 1 vectorized: false generator: crossover_probability: 0.9 mutation_probability: 1.0 name: cnsga output_path: temp population: null population_file: null population_size: 64 supports_multi_objective: true is_done: false max_evaluations: 1000 serialize_inline: false serialize_torch: false strict: true vocs: constants: a: dummy_constant constraints: c1: - GREATER_THAN - 0.0 c2: - LESS_THAN - 0.5 objectives: y1: MINIMIZE y2: MINIMIZE observables: [] variables: x1: - 0.0 - 3.14159 x2: - 0.0 - 3.14159
%%timeit
# Check that the average time is close to random_sleep
X.evaluator.function({"x1": 0.5, "x2": 0.5}, random_sleep = .1)
97.4 ms Ā± 20.8 ms per loop (mean Ā± std. dev. of 7 runs, 10 loops each)
%%time
X.run()
CPU times: user 3.89 s, sys: 22.2 ms, total: 3.92 s Wall time: 1min 43s
ProcessesĀ¶
from concurrent.futures import ProcessPoolExecutor
%%time
X = Xopt(YAML)
with ProcessPoolExecutor(max_workers=N_CPUS) as executor:
X.evaluator.executor = executor
X.evaluator.max_workers = N_CPUS
X.run()
len(X.data)
CPU times: user 3.8 s, sys: 184 ms, total: 3.98 s Wall time: 25.8 s
1000
ThreadsĀ¶
Continue running, this time with threads.
from concurrent.futures import ThreadPoolExecutor
%%time
X = Xopt(YAML)
with ThreadPoolExecutor(max_workers=N_CPUS) as executor:
X.evaluator.executor = executor
X.evaluator.max_workers = N_CPUS
X.run()
len(X.data)
CPU times: user 3.64 s, sys: 63.1 ms, total: 3.71 s Wall time: 26.4 s
1000
MPIĀ¶
The test.yaml
file completely defines the problem. We will also direct the logging to an xopt.log
file. The following invocation recruits 4 MPI workers to solve this problem.
We can also continue by calling .save
with a JSON filename. This will write all of previous results into the file.
X = Xopt(YAML)
X.dump('test.yaml') # Write this input to file
!cat test.yaml
data: null dump_file: null evaluator: function: xopt.resources.test_functions.tnk.evaluate_TNK function_kwargs: raise_probability: 0 random_sleep: 0.1 sleep: 0 max_workers: 1 vectorized: false generator: crossover_probability: 0.9 mutation_probability: 1.0 name: cnsga output_path: temp population: null population_file: null population_size: 64 supports_multi_objective: true is_done: false max_evaluations: 1000 serialize_inline: false serialize_torch: false strict: true vocs: constants: a: dummy_constant constraints: c1: - GREATER_THAN - 0.0 c2: - LESS_THAN - 0.5 objectives: y1: MINIMIZE y2: MINIMIZE observables: [] variables: x1: - 0.0 - 3.14159 x2: - 0.0 - 3.14159
%%time
!mpirun -n 8 python -m mpi4py.futures -m xopt.mpi.run -vv --logfile xopt.log test.yaml
Namespace(input_file='test.yaml', logfile='xopt.log', verbose=2, asynchronous=True) Parallel execution with 8 workers Enabling async mode Initialized generator cnsga Created toolbox with 2 variables, 2 constraints, and 2 objectives. Using selection algorithm: nsga2 Xopt ________________________________ Version: 0+untagged.1.ge872ea5 Data size: 0 Config as YAML: dump_file: null evaluator: function: xopt.resources.test_functions.tnk.evaluate_TNK function_kwargs: raise_probability: 0 random_sleep: 0.1 sleep: 0 max_workers: 1 vectorized: false generator: crossover_probability: 0.9 mutation_probability: 1.0 name: cnsga output_path: temp population: null population_file: null population_size: 64 supports_multi_objective: true is_done: false max_evaluations: 1000 serialize_inline: false serialize_torch: false strict: true vocs: constants: a: dummy_constant constraints: c1: - GREATER_THAN - 0.0 c2: - LESS_THAN - 0.5 objectives: y1: MINIMIZE y2: MINIMIZE observables: [] variables: x1: - 0.0 - 3.14159 x2: - 0.0 - 3.14159
Xopt is done. Max evaluations 1000 reached.
CPU times: user 275 ms, sys: 47.4 ms, total: 323 ms Wall time: 23.2 s
!tail xopt.log
2024-04-24T15:50:17+0000 - xopt - INFO - Parallel execution with 8 workers 2024-04-24T15:50:17+0000 - xopt - INFO - Enabling async mode 2024-04-24T15:50:17+0000 - xopt.generator - INFO - Initialized generator cnsga 2024-04-24T15:50:17+0000 - xopt.generators.ga.cnsga - INFO - Created toolbox with 2 variables, 2 constraints, and 2 objectives. 2024-04-24T15:50:17+0000 - xopt.generators.ga.cnsga - INFO - Using selection algorithm: nsga2 2024-04-24T15:50:37+0000 - xopt.base - INFO - Xopt is done. Max evaluations 1000 reached.
DaskĀ¶
from dask.distributed import Client
client = Client()
executor = client.get_executor()
client
Client
Client-65cf445d-0252-11ef-8912-6045bd4d16b6
Connection method: Cluster object | Cluster type: distributed.LocalCluster |
Dashboard: http://127.0.0.1:8787/status |
Cluster Info
LocalCluster
f19cc222
Dashboard: http://127.0.0.1:8787/status | Workers: 4 |
Total threads: 4 | Total memory: 15.61 GiB |
Status: running | Using processes: True |
Scheduler Info
Scheduler
Scheduler-5be15bf0-0038-4c12-b7ac-d42509912fe1
Comm: tcp://127.0.0.1:35789 | Workers: 4 |
Dashboard: http://127.0.0.1:8787/status | Total threads: 4 |
Started: Just now | Total memory: 15.61 GiB |
Workers
Worker: 0
Comm: tcp://127.0.0.1:46835 | Total threads: 1 |
Dashboard: http://127.0.0.1:42443/status | Memory: 3.90 GiB |
Nanny: tcp://127.0.0.1:45391 | |
Local directory: /tmp/dask-scratch-space/worker-y9xeqmsi |
Worker: 1
Comm: tcp://127.0.0.1:43183 | Total threads: 1 |
Dashboard: http://127.0.0.1:43131/status | Memory: 3.90 GiB |
Nanny: tcp://127.0.0.1:43011 | |
Local directory: /tmp/dask-scratch-space/worker-p8r9n7qd |
Worker: 2
Comm: tcp://127.0.0.1:40567 | Total threads: 1 |
Dashboard: http://127.0.0.1:43967/status | Memory: 3.90 GiB |
Nanny: tcp://127.0.0.1:42265 | |
Local directory: /tmp/dask-scratch-space/worker-wsxi65__ |
Worker: 3
Comm: tcp://127.0.0.1:32999 | Total threads: 1 |
Dashboard: http://127.0.0.1:33565/status | Memory: 3.90 GiB |
Nanny: tcp://127.0.0.1:33403 | |
Local directory: /tmp/dask-scratch-space/worker-yf86z6hz |
%%time
X = Xopt(YAML)
X.evaluator.executor = executor
X.evaluator.max_workers = N_CPUS
X.run()
len(X.data)
CPU times: user 9.44 s, sys: 979 ms, total: 10.4 s Wall time: 31.8 s
1000
Load output into PandasĀ¶
This algorithm writes two types of files: gen_{i}.json
with all of the new individuals evaluated in a generation, and pop_{i}.json
with the latest best population. Xopt provides some functions to load these easily into a Pandas dataframe for further analysis.
import pandas as pd
X.data
x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
---|---|---|---|---|---|---|---|---|---|
4 | 0.382501 | 0.807825 | dummy_constant | 0.382501 | 0.807825 | -0.271331 | 0.108562 | 0.033313 | False |
1 | 0.482798 | 0.929654 | dummy_constant | 0.482798 | 0.929654 | 0.078454 | 0.184898 | 0.172427 | False |
2 | 1.522689 | 0.344159 | dummy_constant | 1.522689 | 0.344159 | 1.528541 | 1.070180 | 0.126450 | False |
3 | 1.318749 | 2.413036 | dummy_constant | 1.318749 | 2.413036 | 6.576645 | 4.330057 | 0.166681 | False |
4 | 2.288811 | 3.019624 | dummy_constant | 2.288811 | 3.019624 | 13.414741 | 9.548351 | 0.176454 | False |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
995 | 0.628656 | 0.819976 | dummy_constant | 0.628656 | 0.819976 | 0.118137 | 0.118937 | 0.079245 | False |
996 | 0.154080 | 0.853929 | dummy_constant | 0.154080 | 0.853929 | -0.151109 | 0.244926 | 0.003674 | False |
997 | 0.996647 | 0.587566 | dummy_constant | 0.996647 | 0.587566 | 0.400570 | 0.254326 | 0.181564 | False |
998 | 0.929675 | 0.449129 | dummy_constant | 0.929675 | 0.449129 | 0.005227 | 0.187209 | 0.100391 | False |
999 | 1.093507 | 0.157932 | dummy_constant | 1.093507 | 0.157932 | 0.286952 | 0.469262 | 0.068036 | False |
1000 rows Ć 9 columns
df = pd.concat([X.data, X.vocs.feasibility_data(X.data)], axis=1)
df[df['feasible']]
x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | feasible_c1 | feasible_c2 | feasible | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 0.482798 | 0.929654 | dummy_constant | 0.482798 | 0.929654 | 0.078454 | 0.184898 | 0.172427 | False | True | True | True |
10 | 0.329635 | 0.987300 | dummy_constant | 0.329635 | 0.987300 | 0.040518 | 0.266486 | 0.193671 | False | True | True | True |
128 | 1.079722 | 0.864268 | dummy_constant | 1.079722 | 0.864268 | 0.932165 | 0.468770 | 0.141401 | False | True | True | True |
170 | 0.262945 | 1.127692 | dummy_constant | 0.262945 | 1.127692 | 0.427430 | 0.450193 | 0.125253 | False | True | True | True |
186 | 1.002152 | 0.915497 | dummy_constant | 1.002152 | 0.915497 | 0.767428 | 0.424794 | 0.144672 | False | True | True | True |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
994 | 0.996647 | 0.291052 | dummy_constant | 0.996647 | 0.291052 | 0.094572 | 0.290318 | 0.102244 | False | True | True | True |
995 | 0.628656 | 0.819976 | dummy_constant | 0.628656 | 0.819976 | 0.118137 | 0.118937 | 0.079245 | False | True | True | True |
997 | 0.996647 | 0.587566 | dummy_constant | 0.996647 | 0.587566 | 0.400570 | 0.254326 | 0.181564 | False | True | True | True |
998 | 0.929675 | 0.449129 | dummy_constant | 0.929675 | 0.449129 | 0.005227 | 0.187209 | 0.100391 | False | True | True | True |
999 | 1.093507 | 0.157932 | dummy_constant | 1.093507 | 0.157932 | 0.286952 | 0.469262 | 0.068036 | False | True | True | True |
460 rows Ć 12 columns
# Plot the feasible ones
feasible_df = df[df["feasible"]]
feasible_df.plot("y1", "y2", kind="scatter").set_aspect("equal")
# Plot the infeasible ones
infeasible_df = df[~df["feasible"]]
infeasible_df.plot("y1", "y2", kind="scatter").set_aspect("equal")
# This is the final population
df1 = X.generator.population
df1.plot("y1", "y2", kind="scatter").set_aspect("equal")
matplotlib plottingĀ¶
You can always use matplotlib for customizable plotting
import matplotlib.pyplot as plt
%matplotlib inline
# Extract objectives from output
k1, k2 = "y1", "y2"
fig, ax = plt.subplots(figsize=(6, 6))
ax.scatter(
infeasible_df[k1],
infeasible_df[k2],
color="blue",
marker=".",
alpha=0.5,
label="infeasible",
)
ax.scatter(
feasible_df[k1], feasible_df[k2], color="orange", marker=".", label="feasible"
)
ax.scatter(df1[k1], df1[k2], color="red", marker=".", label="final population")
ax.set_xlabel(k1)
ax.set_ylabel(k2)
ax.set_aspect("auto")
ax.set_title(f"Xopt's CNSGA algorithm")
plt.legend()
<matplotlib.legend.Legend at 0x7fa81cf68b50>
# Cleanup
#!rm -r dask-worker-space
!rm -r temp
!rm xopt.log*
!rm test.yaml