RCDS
RCDS Optimization¶
In this example we demonstrate RCDS optimization.
In [1]:
Copied!
# If you encounter the "Initializing libomp.dylib, but found libomp.dylib already initialized." error
# Please run this cell
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# If you encounter the "Initializing libomp.dylib, but found libomp.dylib already initialized." error
# Please run this cell
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
In [2]:
Copied!
import time
import numpy as np
from xopt.generators.rcds.rcds import RCDSGenerator
from xopt.vocs import VOCS
from xopt.evaluator import Evaluator
from xopt import Xopt
import pandas as pd
from tqdm.auto import tqdm
import warnings
warnings.filterwarnings("ignore")
import time
import numpy as np
from xopt.generators.rcds.rcds import RCDSGenerator
from xopt.vocs import VOCS
from xopt.evaluator import Evaluator
from xopt import Xopt
import pandas as pd
from tqdm.auto import tqdm
import warnings
warnings.filterwarnings("ignore")
RCDS test problem¶
This test problem is a 2-D quadratic function.
In [3]:
Copied!
def f_RCDS_minimize(input_dict):
p = []
for i in range(2):
p.append(input_dict[f'p{i}'])
obj = np.linalg.norm(p)
outcome_dict = {'f': obj}
return outcome_dict
def f_RCDS_minimize(input_dict):
p = []
for i in range(2):
p.append(input_dict[f'p{i}'])
obj = np.linalg.norm(p)
outcome_dict = {'f': obj}
return outcome_dict
Run RCDS on the test problem (YAML method)¶
In [4]:
Copied!
YAML = """
max_evaluations: 100
generator:
name: rcds
x0: null
init_mat: null
noise: 0.00001
step: 0.01
tol: 0.00001
evaluator:
function: __main__.f_RCDS_minimize
vocs:
variables:
p0: [0, 1]
p1: [0, 1]
objectives:
f: MINIMIZE
"""
X = Xopt.from_yaml(YAML)
X
YAML = """
max_evaluations: 100
generator:
name: rcds
x0: null
init_mat: null
noise: 0.00001
step: 0.01
tol: 0.00001
evaluator:
function: __main__.f_RCDS_minimize
vocs:
variables:
p0: [0, 1]
p1: [0, 1]
objectives:
f: MINIMIZE
"""
X = Xopt.from_yaml(YAML)
X
Out[4]:
Xopt ________________________________ Version: 0+untagged.1.ge872ea5 Data size: 0 Config as YAML: dump_file: null evaluator: function: __main__.f_RCDS_minimize function_kwargs: {} max_workers: 1 vectorized: false generator: init_mat: null name: rcds noise: 1.0e-05 step: 0.01 tol: 1.0e-05 x0: null max_evaluations: 100 serialize_inline: false serialize_torch: false strict: true vocs: constants: {} constraints: {} objectives: f: MINIMIZE observables: [] variables: p0: - 0.0 - 1.0 p1: - 0.0 - 1.0
In [5]:
Copied!
X.run()
X.run()
Now you can go directly to the Visualization section and check out the results.
Run RCDS on the test problem (API method)¶
VOCS¶
We'll set the bounds for all the variables pi to [0, 1].
In [6]:
Copied!
n_var = 2
n_var = 2
In [7]:
Copied!
variables = {}
for i in range(n_var):
variables[f'p{i}'] = [0, 1]
vocs = VOCS(
variables=variables,
objectives={'f': 'MINIMIZE'},
)
variables = {}
for i in range(n_var):
variables[f'p{i}'] = [0, 1]
vocs = VOCS(
variables=variables,
objectives={'f': 'MINIMIZE'},
)
In [8]:
Copied!
vocs
vocs
Out[8]:
VOCS(variables={'p0': [0.0, 1.0], 'p1': [0.0, 1.0]}, constraints={}, objectives={'f': 'MINIMIZE'}, constants={}, observables=[])
Evaluator¶
In [9]:
Copied!
evaluator = Evaluator(function=f_RCDS_minimize)
evaluator = Evaluator(function=f_RCDS_minimize)
Generator¶
In [10]:
Copied!
generator = RCDSGenerator(vocs=vocs)
generator = RCDSGenerator(vocs=vocs)
In [11]:
Copied!
generator.model_dump()
generator.model_dump()
Out[11]:
{'x0': None, 'init_mat': None, 'noise': 1e-05, 'step': 0.01, 'tol': 1e-05}
Run the optimization¶
In [12]:
Copied!
X = Xopt.from_yaml(YAML)
X = Xopt.from_yaml(YAML)
In [13]:
Copied!
for i in tqdm(range(X.max_evaluations)):
X.step()
for i in tqdm(range(X.max_evaluations)):
X.step()
Visualization¶
In [14]:
Copied!
X.data.plot(y='f')
X.data.plot(y='f')
Out[14]:
<Axes: >
In [14]:
Copied!