Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into space_example
Browse files Browse the repository at this point in the history
  • Loading branch information
SRFU-NN committed Aug 12, 2024
2 parents 620961a + 7f54f48 commit 2225c98
Show file tree
Hide file tree
Showing 26 changed files with 314 additions and 320 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes

- Documentation about the features in control parameters and sampling control parameters.
- ModelSystems moved to creator systems, so they are only created when you ask for them. You now need to use `ProcessOptimizer.model_systems.get_model_system(model_system_name)` to create them. This has two advantages: If you change a model system, it doesn't affect a new instance of it. And ProcessOptimizer should import faster, since fewer objects are created i memory.

### Bugfixes

Expand Down
23 changes: 2 additions & 21 deletions ProcessOptimizer/model_systems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@
parse_noise_model,
noise_model_factory,
)
from .branin_hoo import branin, branin_no_noise
from .color_pH import color_pH
from .gold_map import gold_map
from .gold_map_with_wells import gold_map_with_wells
from .hart3 import hart3, hart3_no_noise
from .hart6 import hart6, hart6_no_noise
from .poly2 import poly2, poly2_no_noise
from .peaks import peaks, peaks_no_noise
from .model_system_getter import get_model_system

__all__ = [
"branin",
"branin_no_noise",
"color_pH",
"ModelSystem",
"DataDependentNoise",
"ZeroNoise",
Expand All @@ -29,14 +19,5 @@
"SumNoise",
"parse_noise_model",
"noise_model_factory",
"hart3",
"hart3_no_noise",
"hart6",
"hart6_no_noise",
"poly2",
"poly2_no_noise",
"peaks",
"peaks_no_noise",
"gold_map",
"gold_map_with_wells",
"get_model_system",
]
46 changes: 31 additions & 15 deletions ProcessOptimizer/model_systems/branin_hoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,34 @@ def branin_score(x):


# Create model system
branin = ModelSystem(
branin_score,
[Real(-5, 10, name="x1"), Real(0, 15, name="x2")],
noise_model={"model_type": "proportional", "noise_size": 0.1},
true_max=308.13,
true_min=0.397887,
)

branin_no_noise = ModelSystem(
branin_score,
[Real(-5, 10, name="x1"), Real(0, 15, name="x2")],
noise_model=None,
true_max=308.13,
true_min=0.397887,
)

def create_branin(noise: bool = True) -> ModelSystem:
"""
Create the Branin-Hoo model system.
Parameters
----------
* `noise` [bool]:
Whether to add noise to the system.
Returns
-------
* `branin` [ModelSystem]:
The Branin-Hoo model
"""
if noise:
return ModelSystem(
branin_score,
[Real(-5, 10, name="x1"), Real(0, 15, name="x2")],
noise_model={"model_type": "proportional", "noise_size": 0.1},
true_max=308.13,
true_min=0.397887,
)
else:
return ModelSystem(
branin_score,
[Real(-5, 10, name="x1"), Real(0, 15, name="x2")],
noise_model=None,
true_max=308.13,
true_min=0.397887,
)
25 changes: 15 additions & 10 deletions ProcessOptimizer/model_systems/color_pH.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,18 @@ def score(coordinates: List[int], evaluation_target: str='F8') -> float:
data_lookup_position = find_closest_match(file_name, coordinates, 'percent_acid', 'Indicator')[0]
evaluation = color_difference(file_name, data_lookup_position, evaluation_target)
return evaluation

#Create model system
color_pH = ModelSystem(
score,
space = [Integer(30, 85, name='percent_acid'),
Integer(5, 40, name='Indicator'),
],
noise_model = None,
true_min = 0,
)


# Create model system
def create_color_ph() -> ModelSystem:
"""
Create the color_pH model system.
"""
return ModelSystem(
score,
space=[Integer(30, 85, name='percent_acid'),
Integer(5, 40, name='Indicator'),
],
noise_model=None,
true_min=0,
)
14 changes: 8 additions & 6 deletions ProcessOptimizer/model_systems/gold_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def score(coordinates: Sequence[float]):
return gold_found


gold_map = ModelSystem(
score,
space=[(0.0, 15.0), (0.0, 15.0)],
noise_model=None,
true_min=-3.09,
)
def create_gold_map() -> ModelSystem:
"""Create the gold map model system."""
return ModelSystem(
score,
space=[(0.0, 15.0), (0.0, 15.0)],
noise_model=None,
true_min=-3.09,
)
11 changes: 6 additions & 5 deletions ProcessOptimizer/model_systems/gold_map_with_wells.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def normal_distribution(x, sigma):
return gold_found


gold_map_with_wells = ModelSystem(
score,
space=[(0.0, 15.0), (0.0, 15.0)],
noise_model=None,
)
def create_gold_map_with_wells():
return ModelSystem(
score,
space=[(0.0, 15.0), (0.0, 15.0)],
noise_model=None,
)
28 changes: 13 additions & 15 deletions ProcessOptimizer/model_systems/hart3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ def hart3_score(x):
return -np.sum(alpha * np.exp(-np.sum(A * (np.array(x) - P) ** 2, axis=1)))


hart3 = ModelSystem(
hart3_score,
[(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
noise_model="constant",
true_max=0.0,
true_min=-3.863,
)

hart3_no_noise = ModelSystem(
hart3_score,
[(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
noise_model=None,
true_max=0.0,
true_min=-3.863,
)
def create_hart3(noise=bool)-> ModelSystem:
hart3 = ModelSystem(
hart3_score,
[(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
noise_model="constant",
true_max=0.0,
true_min=-3.863,
)
if noise:
return hart3
else:
hart3.set_noise_model(noise_model=None)
return hart3
24 changes: 9 additions & 15 deletions ProcessOptimizer/model_systems/hart6.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,12 @@ def hart6_score(x):
return -np.sum(alpha * np.exp(-np.sum(A * (np.array(x) - P) ** 2, axis=1)))


hart6 = ModelSystem(
hart6_score,
[(0.0, 1.0) for _ in range(6)],
noise_model="constant",
true_max=0.0,
true_min=-3.3224,
)

hart6_no_noise = ModelSystem(
hart6_score,
[(0.0, 1.0) for _ in range(6)],
noise_model=None,
true_max=0.0,
true_min=-3.3224,
)
def create_hart6(noise: bool = True) -> ModelSystem:
noise_model = "constant" if noise else None
return ModelSystem(
hart6_score,
[(0.0, 1.0) for _ in range(6)],
noise_model=noise_model,
true_max=0.0,
true_min=-3.3224,
)
8 changes: 4 additions & 4 deletions ProcessOptimizer/model_systems/model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def __init__(
scores = [score(point) for point in points]
true_max = np.max(scores)
self.true_max = true_max
noise_size_is_default = type(noise_model) == str or (
type(noise_model) == dict and "noise_size" not in noise_model.keys()
noise_size_is_default = isinstance(noise_model, str) or (
isinstance(noise_model, dict) and "noise_size" not in noise_model.keys()
) # Determining whether the noise size is given, or if it is the default.
if type(self.noise_model) == ConstantNoise and noise_size_is_default:
if isinstance(self.noise_model, ConstantNoise) and noise_size_is_default:
# For ConstantNoise models, the noise size is set to 1% of the span of the score.
self.noise_size = 0.01 * (self.true_max - self.true_min)
self.noise_model.noise_size = 0.01 * (self.true_max - self.true_min)

def result_loss(self, result):
"""
Expand Down
53 changes: 53 additions & 0 deletions ProcessOptimizer/model_systems/model_system_getter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from .branin_hoo import create_branin
from .color_pH import create_color_ph
from .gold_map import create_gold_map
from .gold_map_with_wells import create_gold_map_with_wells
from .hart3 import create_hart3
from .hart6 import create_hart6
from .model_system import ModelSystem
from .peaks import create_peaks
from .poly2 import create_poly2


def get_model_system(model_system: str) -> ModelSystem:
"""
Get the model system object for the given model system name.
Parameters
----------
* `model_system` [str]:
The name of the model system to get.
Returns
-------
* `model_system` [ModelSystem]:
The model system object.
"""
creator_dict = {
"branin_hoo": (create_branin,),
"branin_no_noise": (create_branin, False),
"color_ph": (create_color_ph,),
"color_pH": (create_color_ph,),
"colour_ph": (create_color_ph,),
"colour_pH": (create_color_ph,),
"gold_map": (create_gold_map,),
"gold_map_with_wells": (create_gold_map_with_wells,),
"hart3": (create_hart3,),
"hart3_no_noise": (create_hart3, False),
"hart6": (create_hart6,),
"hart6_no_noise": (create_hart6, False),
"poly2": (create_poly2,),
"poly2_no_noise": (create_poly2, False),
"peaks": (create_peaks,),
"peaks_no_noise": (create_peaks, False),
}
if model_system not in creator_dict:
raise ValueError(
f"Model system {model_system} not found. "
f"Choose from {list(creator_dict.keys())}."
)
model_system_tuple = creator_dict[model_system]
if len(model_system_tuple) == 1:
return model_system_tuple[0]()
else:
return model_system_tuple[0](noise=model_system_tuple[1])
24 changes: 9 additions & 15 deletions ProcessOptimizer/model_systems/peaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ def peaks_score(x):
return score


peaks = ModelSystem(
peaks_score,
[(-3.0, 3.0), (-3.0, 3.0)],
noise_model="constant",
true_max=8.106,
true_min=-6.5511,
)

peaks_no_noise = ModelSystem(
peaks_score,
[(-3.0, 3.0), (-3.0, 3.0)],
noise_model=None,
true_max=8.106,
true_min=-6.5511,
)
def create_peaks(noise: bool = True) -> ModelSystem:
noise_model = "constant" if noise else None
return ModelSystem(
peaks_score,
[(-3.0, 3.0), (-3.0, 3.0)],
noise_model=noise_model,
true_max=8.106,
true_min=-6.5511,
)
26 changes: 9 additions & 17 deletions ProcessOptimizer/model_systems/poly2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import numpy as np

from .model_system import ModelSystem


Expand Down Expand Up @@ -35,18 +33,12 @@ def poly2_score(x):
)


poly2 = ModelSystem(
poly2_score,
[(-1.0, 1.0), (-1.0, 1.0)],
noise_model="constant",
true_max=-1.270,
true_min=-2.0512,
)

poly2_no_noise = ModelSystem(
poly2_score,
[(-1.0, 1.0), (-1.0, 1.0)],
noise_model=None,
true_max=-1.270,
true_min=-2.0512,
)
def create_poly2(noise: bool = True) -> ModelSystem:
noise_model = "constant" if noise else None
return ModelSystem(
poly2_score,
[(-1.0, 1.0), (-1.0, 1.0)],
noise_model=noise_model,
true_max=-1.270,
true_min=-2.0512,
)
36 changes: 0 additions & 36 deletions ProcessOptimizer/model_systems/second_order_polynomial_2d.py

This file was deleted.

Loading

0 comments on commit 2225c98

Please sign in to comment.