Skip to content

Commit 8c625e3

Browse files
committed
Update docs
1 parent a7b2867 commit 8c625e3

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

docs/examples/tutorials/tuning-a-process.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,31 @@ Plugboard's YAML config supports an optional `tune` section, allowing you to def
7373
3. Parameters need to reference a type, so that Plugboard knows the type of parameter to build.
7474

7575
Now run `plugboard process tune model-with-tuner.yaml` to execute the optimisation job from the CLI.
76+
77+
## Advanced usage: complex search spaces
78+
79+
Occasionally you may need to define more complex search spaces, which go beyond what can be defined with a simple parameter configuration. For example:
80+
81+
* Conditional parameters, e.g. where parameter `a` must be greater than parameter `b`; or
82+
* Looping, e.g. building up a list of tunable parameters that is of variable length.
83+
84+
These conditional search space functions are supported by Ray Tune and can be defined as described in the [Ray documentation](https://docs.ray.io/en/latest/tune/examples/optuna_example.html#conditional-search-spaces). To use such a function you will need to:
85+
86+
1. Setup the [`Tuner`][plugboard.tune.Tuner], defining your parameters as usual;
87+
2. Write a custom function to define the search space, where each tunable parameter has a name of the form `"{component_name.field_or_arg_name}"`; then
88+
3. Supply your custom function to the `OptunaSpec` algorithm configuration.
89+
90+
For example, the following search space makes the velocity depend on the angle:
91+
```python
92+
--8<-- "examples/tutorials/006_optimisation/hello_tuner.py:custom_search_space"
93+
```
94+
95+
Then use this configuration to point the tuner to the `custom_space` function.
96+
```yaml
97+
--8<-- "examples/tutorials/006_optimisation/model-with-tuner-custom.yaml"
98+
```
99+
100+
1. We can reference a [`Process`][plugboard.process.Process] from another YAML file here to avoid repetition.
101+
2. Add the algorithm configuration here.
102+
103+
Then run using `plugboard process tune model-with-tuner-custom.yaml`.

examples/tutorials/006_optimisation/hello_tuner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# fmt: off
44
import typing as _t
5-
5+
from optuna import Trial
66
from plugboard.component import Component, IOController as IO
77
from plugboard.process import ProcessBuilder
88
from plugboard.schemas import ComponentArgsDict, ProcessSpec, ProcessArgsSpec, ObjectiveSpec
@@ -64,6 +64,15 @@ async def step(self) -> None:
6464
# --8<-- [end:components]
6565

6666

67+
# --8<-- [start:custom_search_space]
68+
def custom_space(trial: Trial) -> dict[str, _t.Any] | None:
69+
"""Defines a custom search space for Optuna."""
70+
angle = trial.suggest_int("trajectory.angle", 0, 90)
71+
# Make velocity depend on angle
72+
trial.suggest_int("trajectory.velocity", angle, 100)
73+
# --8<-- [end:custom_search_space]
74+
75+
6776
if __name__ == "__main__":
6877
# --8<-- [start:define_process]
6978
process_spec = ProcessSpec(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugboard:
2+
process: model-with-tuner.yaml # (1)!
3+
tune:
4+
args:
5+
objective:
6+
object_name: max-height
7+
field_type: field
8+
field_name: max_y
9+
parameters:
10+
- type: ray.tune.uniform
11+
object_type: component
12+
object_name: trajectory
13+
field_type: arg
14+
field_name: angle
15+
lower: 0
16+
upper: 90
17+
- type: ray.tune.uniform
18+
object_type: component
19+
object_name: trajectory
20+
field_type: arg
21+
field_name: velocity
22+
lower: 0
23+
upper: 100
24+
num_samples: 40
25+
mode: max
26+
max_concurrent: 4
27+
algorithm: # (2)!
28+
type: ray.tune.search.optuna.OptunaSearch
29+
space: hello_tuner.custom_space
30+

0 commit comments

Comments
 (0)