Skip to content

Commit 047780b

Browse files
0.27.17
1 parent 6458238 commit 047780b

5 files changed

Lines changed: 42 additions & 21 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.27.16"
10+
version = "0.27.17"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/spot/spot.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def _set_additional_attributes(self) -> None:
347347
self.mean_X = None
348348
self.mean_y = None
349349
self.var_y = None
350+
self.y_mo = None
350351

351352
def _design_setup(self, design) -> None:
352353
"""
@@ -1033,6 +1034,17 @@ def initialize_design_matrix(self, X_start=None) -> None:
10331034

10341035
self.X = repair_non_numeric(X0, self.var_type)
10351036

1037+
def _store_mo(self, y_mo) -> None:
1038+
# store y_mo in self.y_mo (append new values)
1039+
if self.y_mo is None:
1040+
self.y_mo = np.atleast_2d(y_mo)
1041+
else: # append new values
1042+
print(f"y_mo: {y_mo}")
1043+
print(f"self.y_mo: {self.y_mo}")
1044+
print(f"y_mo.shape: {y_mo.shape}")
1045+
print(f"self.y_mo.shape: {self.y_mo.shape}")
1046+
self.y_mo = np.concatenate((self.y_mo, y_mo), axis=1)
1047+
10361048
def _mo2so(self, y_mo) -> None:
10371049
"""
10381050
Converts multi-objective values to a single-objective value by applying a user-defined
@@ -1056,6 +1068,8 @@ def _mo2so(self, y_mo) -> None:
10561068
n, k = get_shape(y_mo)
10571069
# Ensure that y_mo is a (n, k) numpy array
10581070
y_mo = np.atleast_2d(y_mo)
1071+
# TODO
1072+
# self._store_mo(y_mo)
10591073
m = y_mo.shape[0] # Number of objectives
10601074
if m > 1:
10611075
if self.fun_control["fun_mo2so"] is not None:
@@ -1124,6 +1138,7 @@ def evaluate_initial_design(self) -> None:
11241138
logger.debug("In Spot() evaluate_initial_design(), before calling self.fun: fun_control: %s", self.fun_control)
11251139

11261140
y_mo = self.fun(X=X_all, fun_control=self.fun_control)
1141+
11271142
# Convert multi-objective values to single-objective values
11281143
# TODO: Store y_mo in self.y_mo (append new values)
11291144
self.y = self._mo2so(y_mo)

src/spotpython/utils/parallel.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
from typing import Callable, Any, Union
55

6+
67
def evaluate_row(row: Union[np.ndarray, list], objective_function: Callable[[np.ndarray, Any], Any], fun_control: Any) -> Any:
78
"""
89
Evaluates a single row using the provided objective function.
@@ -15,7 +16,7 @@ def evaluate_row(row: Union[np.ndarray, list], objective_function: Callable[[np.
1516
1617
Returns:
1718
The result of the objective function applied to the row.
18-
19+
1920
Examples:
2021
>>> from spotpython.utils.parallel import evaluate_row
2122
>>> import numpy as np
@@ -28,10 +29,11 @@ def evaluate_row(row: Union[np.ndarray, list], objective_function: Callable[[np.
2829
"""
2930
return objective_function(np.array([row]), fun_control)
3031

31-
def parallel_objective_function(objective_function, X, num_cores, fun_control, method)->np.ndarray:
32+
33+
def parallel_objective_function(objective_function, X, num_cores, fun_control, method) -> np.ndarray:
3234
"""
3335
Executes an objective function in parallel using either multiprocessing or joblib.
34-
36+
3537
Args:
3638
objective_function (callable): The function to be evaluated for each row in `X`.
3739
X (iterable): The input data, where each element represents a row to be processed.
@@ -40,13 +42,13 @@ def parallel_objective_function(objective_function, X, num_cores, fun_control, m
4042
method (str): The parallelization method to use. Options are:
4143
- 'mp': Use Python's multiprocessing module.
4244
- 'joblib': Use the joblib library.
43-
45+
4446
Returns:
4547
numpy.ndarray: A flattened array of results obtained by applying the objective function to each row in `X`.
46-
48+
4749
Raises:
4850
ValueError: If an unsupported `method` is provided.
49-
51+
5052
Examples:
5153
>>> from spotpython.utils.parallel import parallel_objective_function
5254
>>> import numpy as np
@@ -58,33 +60,34 @@ def parallel_objective_function(objective_function, X, num_cores, fun_control, m
5860
array([16, 25, 34])
5961
>>> parallel_objective_function(sample_objective, X, num_cores=2, fun_control=fun_control, method='joblib')
6062
array([16, 25, 34])
61-
"""
62-
with Manager() as manager:
63+
"""
64+
with Manager() as manager:
6365
shared_control = manager.dict(fun_control)
64-
if method=='mp':
66+
if method == "mp":
6567
with Pool(processes=num_cores) as pool:
6668
results = pool.starmap(evaluate_row, [(row, objective_function, shared_control) for row in X])
67-
elif method=='joblib':
69+
elif method == "joblib":
6870
results = Parallel(n_jobs=num_cores)(delayed(evaluate_row)(row, objective_function, shared_control) for row in X)
6971

7072
return np.array(results).flatten()
7173

72-
def make_parallel(obj_func, num_cores, method='mp')->Callable:
74+
75+
def make_parallel(obj_func, num_cores, method="mp") -> Callable:
7376
"""
7477
Creates a parallelized wrapper function for the given objective function.
75-
78+
7679
Args:
77-
obj_func (callable): The objective function to be parallelized.
80+
obj_func (callable): The objective function to be parallelized.
7881
It should accept the same arguments as the wrapper function.
7982
num_cores (int): The number of cores to use for parallel processing.
80-
method (str, optional): The parallelization method to use.
81-
Defaults to 'mp' (multiprocessing). Other methods may be supported
83+
method (str, optional): The parallelization method to use.
84+
Defaults to 'mp' (multiprocessing). Other methods may be supported
8285
depending on the implementation of `parallel_objective_function`.
83-
86+
8487
Returns:
85-
callable: A wrapper function that executes the objective function
88+
callable: A wrapper function that executes the objective function
8689
in parallel using the specified number of cores and method.
87-
90+
8891
Examples:
8992
>>> from spotpython.utils.parallel import make_parallel
9093
>>> def sample_function(x):
@@ -96,7 +99,8 @@ def make_parallel(obj_func, num_cores, method='mp')->Callable:
9699
[1, 4, 9, 16]
97100
"""
98101
global parallel_wrap
102+
99103
def parallel_wrap(X, fun_control=None):
100104
return parallel_objective_function(obj_func, X, num_cores, fun_control, method)
101-
105+
102106
return parallel_wrap

test/test_get_spot_attributes_as_df.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def test_get_spot_attributes_as_df():
7878
'var_type',
7979
'var_y',
8080
'verbosity',
81-
'y']
81+
'y',
82+
'y_mo']
8283

8384
# Check that the DataFrame has the correct attributes
8485
assert list(df['Attribute Name']) == expected_attributes

test/test_mo2so.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class MockSpot(Spot):
55
def __init__(self, fun_control):
66
self.fun_control = fun_control
7+
self.y_mo = None
78

89
def test_mo2so():
910
# Case 1: Multi-objective with a user-defined function

0 commit comments

Comments
 (0)