-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a scaler object to re-implement control scaling
- Loading branch information
Showing
5 changed files
with
73 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from collections.abc import Sequence | ||
|
||
import numpy as np | ||
from ropt.transforms import Transforms, VariableScaler | ||
|
||
from everest.config import EverestConfig | ||
from everest.config.utils import FlattenedControls | ||
|
||
|
||
class ControlScaler(VariableScaler): | ||
def __init__( | ||
self, | ||
lower_bounds: Sequence[float], | ||
upper_bounds: Sequence[float], | ||
scaled_ranges: Sequence[tuple[float, float]], | ||
auto_scales: Sequence[bool], | ||
) -> None: | ||
scales = [ | ||
(ub - lb) / (sr[1] - sr[0]) if au else 1.0 | ||
for au, lb, ub, sr in zip( | ||
auto_scales, lower_bounds, upper_bounds, scaled_ranges, strict=True | ||
) | ||
] | ||
offsets = [ | ||
lb - sr[0] * sc if au else 0.0 | ||
for au, lb, sc, sr in zip( | ||
auto_scales, lower_bounds, scales, scaled_ranges, strict=True | ||
) | ||
] | ||
super().__init__(np.array(scales), np.array(offsets)) | ||
|
||
|
||
def get_transforms(ever_config: EverestConfig) -> Transforms: | ||
controls = FlattenedControls(ever_config.controls) | ||
if any(controls.auto_scales): | ||
variable_scaler = ControlScaler( | ||
controls.lower_bounds, | ||
controls.upper_bounds, | ||
controls.scaled_ranges, | ||
controls.auto_scales, | ||
) | ||
else: | ||
variable_scaler = None | ||
return Transforms(variables=variable_scaler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters