Skip to content

Commit 46d3106

Browse files
authored
Merge pull request #281 from optimas-org/vocs
Implement VOCS interface
2 parents 8f74454 + 7f8ea9b commit 46d3106

49 files changed

Lines changed: 946 additions & 754 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/unix-noax.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
conda install numpy pandas pytorch cpuonly -c pytorch
3232
conda install -c conda-forge mpi4py mpich
3333
pip install .[test]
34+
pip install git+https://github.com/campa-consortium/gest-api.git
3435
pip uninstall --yes ax-platform # Run without Ax
3536
- shell: bash -l {0}
3637
name: Run unit tests without Ax

.github/workflows/unix-openmpi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
conda install -c pytorch numpy pandas
3333
conda install -c conda-forge mpi4py openmpi=5.*
3434
pip install .[test]
35+
pip install git+https://github.com/campa-consortium/gest-api.git
3536
- shell: bash -l {0}
3637
name: Run unit tests with openMPI
3738
run: |

.github/workflows/unix.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
conda install -c pytorch numpy pandas
3333
conda install -c conda-forge mpi4py mpich
3434
pip install .[test]
35+
pip install git+https://github.com/campa-consortium/gest-api.git
3536
- shell: bash -l {0}
3637
name: Run unit tests with MPICH
3738
run: |

doc/environment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies:
1111
- matplotlib
1212
- nbsphinx
1313
- numpydoc
14+
- git+https://github.com/campa-consortium/gest-api.git
1415
- pydata-sphinx-theme
1516
- sphinx-copybutton
1617
- sphinx-design

doc/source/user_guide/advanced_usage/build_gp_surrogates.ipynb

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"\n",
1212
"The :class:`~optimas.diagnostics.ExplorationDiagnostics` class\n",
1313
"provides a simple way of fitting a Gaussian process (GP) model to any of the\n",
14-
"objectives or analyzed parameters of an ``optimas``\n",
14+
"objectives or observables of an ``optimas``\n",
1515
":class:`~optimas.explorations.Exploration`, independently of which generator\n",
1616
"was used. This is useful to get a better understanding of the underlying\n",
1717
"function, make predictions, etc.\n",
@@ -29,7 +29,7 @@
2929
"\n",
3030
"The following cell sets up and runs an optimization with two input parameters\n",
3131
"``x1`` and ``x2``, two objectives ``f1`` and ``f2``, and one additional\n",
32-
"analyzed parameter ``p1``.\n",
32+
"observable ``p1``.\n",
3333
"At each evaluation, the ``eval_func_sf_moo`` function is run,\n",
3434
"which assigns a value to each outcome parameter according to the analytical\n",
3535
"formulas\n",
@@ -55,9 +55,9 @@
5555
"source": [
5656
"import numpy as np\n",
5757
"from optimas.explorations import Exploration\n",
58-
"from optimas.core import VaryingParameter, Objective, Parameter\n",
5958
"from optimas.generators import RandomSamplingGenerator\n",
6059
"from optimas.evaluators import FunctionEvaluator\n",
60+
"from gest_api.vocs import VOCS\n",
6161
"\n",
6262
"\n",
6363
"def eval_func_sf_moo(input_params, output_params):\n",
@@ -70,17 +70,20 @@
7070
" output_params[\"p1\"] = np.sin(x1) + np.cos(x2)\n",
7171
"\n",
7272
"\n",
73-
"var1 = VaryingParameter(\"x1\", 0.0, 5.0)\n",
74-
"var2 = VaryingParameter(\"x2\", -5.0, 5.0)\n",
75-
"par1 = Parameter(\"p1\")\n",
76-
"obj1 = Objective(\"f1\", minimize=True)\n",
77-
"obj2 = Objective(\"f2\", minimize=False)\n",
78-
"\n",
79-
"gen = RandomSamplingGenerator(\n",
80-
" varying_parameters=[var1, var2],\n",
81-
" objectives=[obj1, obj2],\n",
82-
" analyzed_parameters=[par1],\n",
73+
"# Create VOCS object defining variables, objectives, and observables.\n",
74+
"vocs = VOCS(\n",
75+
" variables={\n",
76+
" \"x1\": [0.0, 5.0],\n",
77+
" \"x2\": [-5.0, 5.0],\n",
78+
" },\n",
79+
" objectives={\n",
80+
" \"f1\": \"MINIMIZE\",\n",
81+
" \"f2\": \"MAXIMIZE\",\n",
82+
" },\n",
83+
" observables=[\"p1\"],\n",
8384
")\n",
85+
"\n",
86+
"gen = RandomSamplingGenerator(vocs=vocs)\n",
8487
"ev = FunctionEvaluator(function=eval_func_sf_moo)\n",
8588
"exploration = Exploration(\n",
8689
" generator=gen,\n",
@@ -125,18 +128,18 @@
125128
"raw_mimetype": "text/restructuredtext"
126129
},
127130
"source": [
128-
"Building a GP model of each objective and analyzed parameter\n",
129-
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
131+
"Building a GP model of each objective and observable\n",
132+
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
130133
"\n",
131134
"To build a GP model, simply call\n",
132135
":meth:`~optimas.diagnostics.Exploration.build_gp_model` on the diagnostics,\n",
133136
"indicating the name of the variable to which the model should be fitted.\n",
134-
"This variable can be any ``objective`` or ``analyzed_parameter`` of the\n",
137+
"This variable can be any ``objective`` or ``observable`` of the\n",
135138
"optimization.\n",
136139
"\n",
137-
"Note that when building a surrogate model of an analyzed parameter, it is\n",
140+
"Note that when building a surrogate model of an observable, it is\n",
138141
"required to provide a value to the ``minimize`` argument. This parameter\n",
139-
"should therefore be ``True`` if lower values of the analyzed parameter are\n",
142+
"should therefore be ``True`` if lower values of the observable are\n",
140143
"better than higher values. This information is necessary, e.g., for determining\n",
141144
"the best point in the model."
142145
]
@@ -147,7 +150,7 @@
147150
"metadata": {},
148151
"outputs": [],
149152
"source": [
150-
"# Build one model for each objective and analyzed parameter.\n",
153+
"# Build one model for each objective and observable.\n",
151154
"f1_model = diags.build_gp_model(\"f1\")\n",
152155
"f2_model = diags.build_gp_model(\"f2\")\n",
153156
"p1_model = diags.build_gp_model(\"p1\", minimize=False)"

doc/source/user_guide/basic_usage/exploration_diagnostics.ipynb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,17 @@
125125
"import matplotlib.pyplot as plt\n",
126126
"\n",
127127
"fig, ax = plt.subplots()\n",
128-
"vps = diags.varying_parameters\n",
128+
"vocs = diags._exploration.generator.vocs\n",
129129
"df = diags.history\n",
130-
"f1 = diags.objectives[0]\n",
131-
"ax.axvline(vps[0].lower_bound)\n",
132-
"ax.axvline(vps[0].upper_bound)\n",
133-
"ax.set_xlabel(vps[0].name)\n",
134-
"ax.axhline(vps[1].lower_bound)\n",
135-
"ax.axhline(vps[1].upper_bound)\n",
136-
"ax.set_ylabel(vps[1].name)\n",
137-
"ax.scatter(df[vps[0].name], df[vps[1].name], c=df[f1.name])"
130+
"vps = list(vocs.variables.keys())\n",
131+
"f1 = list(vocs.objectives.keys())[0]\n",
132+
"ax.axvline(vocs.variables[vps[0]].domain[0])\n",
133+
"ax.axvline(vocs.variables[vps[0]].domain[1])\n",
134+
"ax.set_xlabel(vps[0])\n",
135+
"ax.axhline(vocs.variables[vps[1]].domain[0])\n",
136+
"ax.axhline(vocs.variables[vps[1]].domain[1])\n",
137+
"ax.set_ylabel(vps[1])\n",
138+
"ax.scatter(df[vps[0]], df[vps[1]], c=df[f1])"
138139
]
139140
}
140141
],
@@ -144,5 +145,5 @@
144145
}
145146
},
146147
"nbformat": 4,
147-
"nbformat_minor": 2
148+
"nbformat_minor": 4
148149
}

examples/astra/run_optimization_serial_ASTRA.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,32 @@
99
https://optimas.readthedocs.io/en/latest/index.html
1010
"""
1111

12-
from optimas.core import VaryingParameter, Objective, Parameter
1312
from optimas.generators import AxSingleFidelityGenerator
1413
from optimas.evaluators import TemplateEvaluator
1514
from optimas.explorations import Exploration
15+
from gest_api.vocs import VOCS
1616
from analysis_script import analyze_simulation
1717

18-
# Create varying parameters and objectives.
18+
# Create VOCS object.
1919
# name of parameter, lower bound of values to be explored,
2020
# upper bound of values to be explored
21-
var_1 = VaryingParameter("RF_phase", -2.5, 2.5)
22-
var_2 = VaryingParameter("B_sol", 0.12, 0.38)
23-
# Objectives that will be minimized:
24-
obj_1 = Objective("bunch_length", minimize=True)
25-
obj_2 = Objective("emittance", minimize=True)
26-
# Additional example parameters that will be analyzed but are not used for the
27-
# optimization:
28-
em_x = Parameter("emittance_x")
29-
em_y = Parameter("emittance_y")
21+
vocs = VOCS(
22+
variables={
23+
"RF_phase": [-2.5, 2.5],
24+
"B_sol": [0.12, 0.38],
25+
},
26+
objectives={
27+
"bunch_length": "MINIMIZE",
28+
"emittance": "MINIMIZE",
29+
},
30+
observables=["emittance_x", "emittance_y"],
31+
)
3032

3133
# Create generator.
3234
# Pick the generator to be used, here Single-fidelity Bayesian optimization.
33-
# The analyzed_parameters are parameters that are calculated for each
34-
# simulation but not used for the optimization.
3535
gen = AxSingleFidelityGenerator(
36-
varying_parameters=[var_1, var_2],
37-
objectives=[obj_1, obj_2],
36+
vocs=vocs,
3837
n_init=8,
39-
analyzed_parameters=[em_x, em_y],
4038
)
4139

4240

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 gest_api.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 gest_api.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 gest_api.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.

0 commit comments

Comments
 (0)