Skip to content

Commit d019af5

Browse files
committed
Update dummy examples to use vocs
1 parent 844ff1a commit d019af5

File tree

6 files changed

+87
-72
lines changed

6 files changed

+87
-72
lines changed

examples/dummy/run_example.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Basic example of parallel Bayesian optimization with Ax."""
22

3-
from optimas.core import VaryingParameter, Objective
3+
from generator_standard.vocs import VOCS
44
from optimas.generators import AxSingleFidelityGenerator
55
from optimas.evaluators import TemplateEvaluator
66
from optimas.explorations import Exploration
@@ -10,16 +10,16 @@ def analyze_simulation(simulation_directory, output_params):
1010
"""Analyze the simulation output.
1111
1212
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
13+
obtain the value of the optimization objective and other observables.
14+
The value of these parameters has to be given to the
15+
`output_params` dictionary.
1616
1717
Parameters
1818
----------
1919
simulation_directory : str
2020
Path to the simulation folder where the output was generated.
2121
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
22+
Dictionary where the value of the objectives and observables
2323
will be stored. There is one entry per parameter, where the key
2424
is the name of the parameter given by the user.
2525
@@ -37,16 +37,18 @@ def analyze_simulation(simulation_directory, output_params):
3737
return output_params
3838

3939

40-
# Create varying parameters and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0)
43-
obj = Objective("f", minimize=True)
40+
# Create VOCS object defining variables, objectives.
41+
vocs = VOCS(
42+
variables={
43+
"x0": [0.0, 15.0],
44+
"x1": [0.0, 15.0],
45+
},
46+
objectives={"f": "MINIMIZE"},
47+
)
4448

4549

4650
# Create generator.
47-
gen = AxSingleFidelityGenerator(
48-
varying_parameters=[var_1, var_2], objectives=[obj], n_init=2
49-
)
51+
gen = AxSingleFidelityGenerator(vocs=vocs, n_init=2)
5052

5153

5254
# Create evaluator.

examples/dummy_grid_sampling/run_example.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Basic example of parallel grid sampling with simulations."""
22

3-
from optimas.core import VaryingParameter, Objective
3+
from generator_standard.vocs import VOCS
44
from optimas.generators import GridSamplingGenerator
55
from optimas.evaluators import TemplateEvaluator
66
from optimas.explorations import Exploration
@@ -10,16 +10,16 @@ def analyze_simulation(simulation_directory, output_params):
1010
"""Analyze the simulation output.
1111
1212
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
13+
obtain the value of the optimization objective and other observables.
14+
The value of these parameters has to be given to the
15+
`output_params` dictionary.
1616
1717
Parameters
1818
----------
1919
simulation_directory : str
2020
Path to the simulation folder where the output was generated.
2121
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
22+
Dictionary where the value of the objectives and observables
2323
will be stored. There is one entry per parameter, where the key
2424
is the name of the parameter given by the user.
2525
@@ -37,16 +37,18 @@ def analyze_simulation(simulation_directory, output_params):
3737
return output_params
3838

3939

40-
# Create varying parameters and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0)
43-
obj = Objective("f")
40+
# Create VOCS object defining variables, objectives.
41+
vocs = VOCS(
42+
variables={
43+
"x0": [0.0, 15.0],
44+
"x1": [0.0, 15.0],
45+
},
46+
objectives={"f": "MAXIMIZE"},
47+
)
4448

4549

4650
# Create generator.
47-
gen = GridSamplingGenerator(
48-
varying_parameters=[var_1, var_2], objectives=[obj], n_steps=[5, 7]
49-
)
51+
gen = GridSamplingGenerator(vocs=vocs, n_steps=[5, 7])
5052

5153

5254
# Create evaluator.

examples/dummy_line_sampling/run_example.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Basic example of parallel line sampling with simulations."""
22

3-
from optimas.core import VaryingParameter, Objective
3+
from generator_standard.vocs import VOCS, ContinuousVariable
44
from optimas.generators import LineSamplingGenerator
55
from optimas.evaluators import TemplateEvaluator
66
from optimas.explorations import Exploration
@@ -10,16 +10,16 @@ def analyze_simulation(simulation_directory, output_params):
1010
"""Analyze the simulation output.
1111
1212
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
13+
obtain the value of the optimization objective and other observables.
14+
The value of these parameters has to be given to the
15+
`output_params` dictionary.
1616
1717
Parameters
1818
----------
1919
simulation_directory : str
2020
Path to the simulation folder where the output was generated.
2121
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
22+
Dictionary where the value of the objectives and observables
2323
will be stored. There is one entry per parameter, where the key
2424
is the name of the parameter given by the user.
2525
@@ -37,16 +37,18 @@ def analyze_simulation(simulation_directory, output_params):
3737
return output_params
3838

3939

40-
# Create varying parameters and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0, default_value=5.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0, default_value=6.0)
43-
obj = Objective("f")
40+
# Create VOCS object defining variables, objectives.
41+
vocs = VOCS(
42+
variables={
43+
"x0": ContinuousVariable(domain=[0.0, 15.0], default_value=5.0),
44+
"x1": ContinuousVariable(domain=[0.0, 15.0], default_value=6.0),
45+
},
46+
objectives={"f": "MAXIMIZE"},
47+
)
4448

4549

4650
# Create generator.
47-
gen = LineSamplingGenerator(
48-
varying_parameters=[var_1, var_2], objectives=[obj], n_steps=[5, 7]
49-
)
51+
gen = LineSamplingGenerator(vocs=vocs, n_steps=[5, 7])
5052

5153

5254
# Create evaluator.

examples/dummy_mf/run_example.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Basic example of parallel multi-fidelity Bayesian optimization with Ax."""
22

3-
from optimas.core import VaryingParameter, Objective
3+
from generator_standard.vocs import VOCS
44
from optimas.generators import AxMultiFidelityGenerator
55
from optimas.evaluators import TemplateEvaluator
66
from optimas.explorations import Exploration
@@ -10,16 +10,16 @@ def analyze_simulation(simulation_directory, output_params):
1010
"""Analyze the simulation output.
1111
1212
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
13+
obtain the value of the optimization objective and other observables.
14+
The value of these parameters has to be given to the
15+
`output_params` dictionary.
1616
1717
Parameters
1818
----------
1919
simulation_directory : str
2020
Path to the simulation folder where the output was generated.
2121
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
22+
Dictionary where the value of the objectives and observables
2323
will be stored. There is one entry per parameter, where the key
2424
is the name of the parameter given by the user.
2525
@@ -37,22 +37,24 @@ def analyze_simulation(simulation_directory, output_params):
3737
return output_params
3838

3939

40-
# Create varying parameters (including fidelity) and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0)
43-
res = VaryingParameter(
44-
"resolution", 1.0, 8.0, is_fidelity=True, fidelity_target_value=8.0
40+
# Create VOCS object defining variables, objectives.
41+
vocs = VOCS(
42+
variables={
43+
"x0": [0.0, 15.0],
44+
"x1": [0.0, 15.0],
45+
"resolution": [1.0, 8.0],
46+
},
47+
objectives={"f": "MINIMIZE"},
4548
)
46-
obj = Objective("f", minimize=True)
4749

4850

4951
# Create generator.
5052
gen = AxMultiFidelityGenerator(
51-
varying_parameters=[var_1, var_2, res],
52-
objectives=[obj],
53+
vocs=vocs,
5354
n_init=4,
5455
fidel_cost_intercept=2.0,
5556
)
57+
gen.set_fidelity_param("resolution", fidelity_target_value=8.0)
5658

5759

5860
# Create evaluator.

examples/dummy_mt/run_example.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Basic example of parallel multitask Bayesian optimization with Ax."""
22

3-
from optimas.core import VaryingParameter, Objective, Task
3+
from generator_standard.vocs import VOCS
4+
from optimas.core import Task
45
from optimas.generators import AxMultitaskGenerator
56
from optimas.evaluators import TemplateEvaluator, MultitaskEvaluator
67
from optimas.explorations import Exploration
@@ -10,16 +11,16 @@ def analyze_simulation(simulation_directory, output_params):
1011
"""Analyze the simulation output.
1112
1213
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
14+
obtain the value of the optimization objective and other observables.
15+
The value of these parameters has to be given to the
16+
`output_params` dictionary.
1617
1718
Parameters
1819
----------
1920
simulation_directory : str
2021
Path to the simulation folder where the output was generated.
2122
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
23+
Dictionary where the value of the objectives and observables
2324
will be stored. There is one entry per parameter, where the key
2425
is the name of the parameter given by the user.
2526
@@ -37,10 +38,15 @@ def analyze_simulation(simulation_directory, output_params):
3738
return output_params
3839

3940

40-
# Create varying parameters and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0)
43-
obj = Objective("f", minimize=True)
41+
# Create VOCS object defining variables, objectives.
42+
vocs = VOCS(
43+
variables={
44+
"x0": [0.0, 15.0],
45+
"x1": [0.0, 15.0],
46+
"trial_type": {"cheap_model", "expensive_model"},
47+
},
48+
objectives={"f": "MINIMIZE"},
49+
)
4450

4551

4652
# Create tasks.
@@ -50,8 +56,7 @@ def analyze_simulation(simulation_directory, output_params):
5056

5157
# Create generator.
5258
gen = AxMultitaskGenerator(
53-
varying_parameters=[var_1, var_2],
54-
objectives=[obj],
59+
vocs=vocs,
5560
lofi_task=lofi_task,
5661
hifi_task=hifi_task,
5762
)

examples/dummy_random/run_example.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Basic example of parallel random sampling with simulations."""
22

3-
from optimas.core import VaryingParameter, Objective
3+
from generator_standard.vocs import VOCS
44
from optimas.generators import RandomSamplingGenerator
55
from optimas.evaluators import TemplateEvaluator
66
from optimas.explorations import Exploration
@@ -10,16 +10,16 @@ def analyze_simulation(simulation_directory, output_params):
1010
"""Analyze the simulation output.
1111
1212
This method analyzes the output generated by the simulation to
13-
obtain the value of the optimization objective and other analyzed
14-
parameters, if specified. The value of these parameters has to be
15-
given to the `output_params` dictionary.
13+
obtain the value of the optimization objective and other observables.
14+
The value of these parameters has to be given to the
15+
`output_params` dictionary.
1616
1717
Parameters
1818
----------
1919
simulation_directory : str
2020
Path to the simulation folder where the output was generated.
2121
output_params : dict
22-
Dictionary where the value of the objectives and analyzed parameters
22+
Dictionary where the value of the objectives and observables
2323
will be stored. There is one entry per parameter, where the key
2424
is the name of the parameter given by the user.
2525
@@ -37,16 +37,18 @@ def analyze_simulation(simulation_directory, output_params):
3737
return output_params
3838

3939

40-
# Create varying parameters and objectives.
41-
var_1 = VaryingParameter("x0", 0.0, 15.0)
42-
var_2 = VaryingParameter("x1", 0.0, 15.0)
43-
obj = Objective("f")
40+
# Create VOCS object defining variables, objectives.
41+
vocs = VOCS(
42+
variables={
43+
"x0": [0.0, 15.0],
44+
"x1": [0.0, 15.0],
45+
},
46+
objectives={"f": "MAXIMIZE"},
47+
)
4448

4549

4650
# Create generator.
47-
gen = RandomSamplingGenerator(
48-
varying_parameters=[var_1, var_2], objectives=[obj], distribution="normal"
49-
)
51+
gen = RandomSamplingGenerator(vocs=vocs, distribution="normal")
5052

5153

5254
# Create evaluator.

0 commit comments

Comments
 (0)