Skip to content

Commit ffd00a3

Browse files
random seed handling
1 parent a694f7c commit ffd00a3

5 files changed

Lines changed: 75 additions & 40 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,20 +1193,9 @@
11931193
},
11941194
{
11951195
"cell_type": "code",
1196-
"execution_count": 7,
1197-
"metadata": {},
1198-
"outputs": [
1199-
{
1200-
"data": {
1201-
"text/plain": [
1202-
"array([0.01087865, 1.63221335, 4.28792526, 4.19397442, 5.9202309 ])"
1203-
]
1204-
},
1205-
"execution_count": 7,
1206-
"metadata": {},
1207-
"output_type": "execute_result"
1208-
}
1209-
],
1196+
"execution_count": null,
1197+
"metadata": {},
1198+
"outputs": [],
12101199
"source": [
12111200
"from spotPython.fun.objectivefunctions import analytical\n",
12121201
"import numpy as np\n",
@@ -1217,24 +1206,69 @@
12171206
},
12181207
{
12191208
"cell_type": "code",
1220-
"execution_count": 1,
1221-
"metadata": {},
1222-
"outputs": [
1223-
{
1224-
"name": "stdout",
1225-
"output_type": "stream",
1226-
"text": [
1227-
"[1 2 3 4 5]\n"
1228-
]
1229-
}
1230-
],
1209+
"execution_count": null,
1210+
"metadata": {},
1211+
"outputs": [],
12311212
"source": [
12321213
"from spotPython.fun.objectivefunctions import analytical\n",
12331214
"import numpy as np\n",
12341215
"print(np.array([1, 2, 3, 4, 5]))\n",
12351216
"\n"
12361217
]
12371218
},
1219+
{
1220+
"cell_type": "code",
1221+
"execution_count": null,
1222+
"metadata": {},
1223+
"outputs": [],
1224+
"source": [
1225+
"import numpy as np\n",
1226+
"from math import inf\n",
1227+
"from spotPython.fun.objectivefunctions import analytical\n",
1228+
"from spotPython.spot import spot\n",
1229+
"from scipy.optimize import shgo\n",
1230+
"from scipy.optimize import direct\n",
1231+
"from scipy.optimize import differential_evolution\n",
1232+
"import matplotlib.pyplot as plt\n",
1233+
"from spotPython.utils.init import fun_control_init\n",
1234+
"fun_control = fun_control_init(seed=4321, sigma=0.1)\n",
1235+
"fun = analytical(seed=222, sigma=0.0).fun_sphere"
1236+
]
1237+
},
1238+
{
1239+
"cell_type": "code",
1240+
"execution_count": null,
1241+
"metadata": {},
1242+
"outputs": [],
1243+
"source": [
1244+
"spot_1 = spot.Spot(fun=fun,\n",
1245+
" lower = np.array([-10]),\n",
1246+
" upper = np.array([100]),\n",
1247+
" fun_evals = 100,\n",
1248+
" fun_repeats = 3,\n",
1249+
" max_time = inf,\n",
1250+
" noise = True,\n",
1251+
" tolerance_x = np.sqrt(np.spacing(1)),\n",
1252+
" var_type=[\"num\"],\n",
1253+
" infill_criterion = \"y\",\n",
1254+
" n_points = 1,\n",
1255+
" seed=111,\n",
1256+
" log_level = 10,\n",
1257+
" show_models=False,\n",
1258+
" fun_control = fun_control,\n",
1259+
" design_control={\"init_size\": 5,\n",
1260+
" \"repeats\": 1},\n",
1261+
" surrogate_control={\"noise\": True,\n",
1262+
" \"cod_type\": \"norm\",\n",
1263+
" \"min_theta\": -4,\n",
1264+
" \"max_theta\": 3,\n",
1265+
" \"n_theta\": 1,\n",
1266+
" \"model_optimizer\": differential_evolution,\n",
1267+
" \"model_fun_evals\": 1000,\n",
1268+
" })\n",
1269+
"spot_1.run()"
1270+
]
1271+
},
12381272
{
12391273
"cell_type": "code",
12401274
"execution_count": null,

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.7.2"
10+
version = "0.7.3"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/fun/objectivefunctions.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,8 @@ def fun_sphere(self, X: np.ndarray, fun_control: Optional[Dict] = None) -> np.nd
202202
y = np.array([], dtype=float)
203203
for i in range(X.shape[0]):
204204
y = np.append(y, np.sum((X[i] - offset) ** 2))
205-
# TODO: move to a separate function:
206205
if self.fun_control["sigma"] > 0:
207-
# Use own rng:
208-
if self.fun_control["seed"] is not None:
209-
rng = default_rng(seed=fun_control["seed"])
210-
# Use class rng:
211-
else:
212-
rng = self.rng
213-
noise_y = np.array([], dtype=float)
214-
for y_i in y:
215-
noise_y = np.append(noise_y, y_i + rng.normal(loc=0, scale=fun_control["sigma"], size=1))
216-
return noise_y
206+
return self.add_noise(y)
217207
else:
218208
return y
219209

src/spotPython/spot/spot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ def __init__(
319319
self.optimizer_control.update(optimizer_control)
320320
if self.optimizer is None:
321321
self.optimizer = optimize.differential_evolution
322+
logger.debug("In Spot() init(): fun_control: %s", self.fun_control)
323+
logger.debug("In Spot() init(): optimizer_control: %s", self.optimizer_control)
324+
logger.debug("In Spot() init(): surrogate_control: %s", self.surrogate_control)
322325

323326
def get_spot_attributes_as_df(self):
324327
"""Get all attributes of the spot object as a pandas dataframe.
@@ -547,6 +550,11 @@ def update_design(self):
547550
# (S-18): Evaluating New Solutions:
548551
X0 = self.append_X_ocba(X_ocba, X0)
549552
X_all = self.to_all_dim_if_needed(X0)
553+
logger.debug(
554+
"In update_design(): self.fun_control sigma and seed passed to fun(): %s %s",
555+
self.fun_control["sigma"],
556+
self.fun_control["seed"],
557+
)
550558
y0 = self.fun(X=X_all, fun_control=self.fun_control)
551559
X0, y0 = remove_nan(X0, y0)
552560
# Append New Solutions:

src/spotPython/utils/init.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ def fun_control_init(
3535
The number of workers to use for the data loading.
3636
device (str):
3737
The device to use for the training. It can be either "cpu", "mps", or "cuda".
38+
3839
Returns:
3940
fun_control (dict):
4041
A dictionary containing the information about the core model,
4142
loss function, metrics, and the hyperparameters.
43+
4244
Examples:
43-
>>> fun_control = fun_control_init(_L_in=64, _L_out=11, num_workers=0, device=None)
44-
>>> fun_control
45-
>>> {'CHECKPOINT_PATH': 'saved_models/',
45+
>>> from spotPy.utils.init import fun_control_init
46+
fun_control = fun_control_init(_L_in=64, _L_out=11, num_workers=0, device=None)
47+
fun_control
48+
{'CHECKPOINT_PATH': 'saved_models/',
4649
'DATASET_PATH': 'data/',
4750
'RESULTS_PATH': 'results/',
4851
'TENSORBOARD_PATH': 'runs/',

0 commit comments

Comments
 (0)