Skip to content

Commit eee0a40

Browse files
0.34.10
1 parent 0a9b540 commit eee0a40

17 files changed

Lines changed: 1358 additions & 1 deletion

notebooks/00_spotPython_tests.ipynb

Lines changed: 393 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15595,6 +15595,399 @@
1559515595
"df = spot_1.get_spot_attributes_as_df()"
1559615596
]
1559715597
},
15598+
{
15599+
"cell_type": "code",
15600+
"execution_count": 11,
15601+
"metadata": {},
15602+
"outputs": [
15603+
{
15604+
"name": "stderr",
15605+
"output_type": "stream",
15606+
"text": [
15607+
"Seed set to 123\n"
15608+
]
15609+
},
15610+
{
15611+
"name": "stdout",
15612+
"output_type": "stream",
15613+
"text": [
15614+
"S.y_mo after first call: [[1. 2.]\n",
15615+
" [3. 4.]]\n",
15616+
"S.y_mo after second call: [[1. 2.]\n",
15617+
" [3. 4.]\n",
15618+
" [5. 6.]\n",
15619+
" [7. 8.]]\n"
15620+
]
15621+
}
15622+
],
15623+
"source": [
15624+
"import numpy as np\n",
15625+
"from spotpython.spot import Spot\n",
15626+
"from spotpython.utils.init import fun_control_init\n",
15627+
"fun_control = fun_control_init(\n",
15628+
" lower=np.array([-1, -1]),\n",
15629+
" upper=np.array([1, 1])\n",
15630+
")\n",
15631+
"fun = lambda x: np.array([[x[0]**2 + x[1]**2, (x[0]-1)**2 + (x[1]-1)**2] for x in x])\n",
15632+
"S = Spot(fun=fun, fun_control=fun_control)\n",
15633+
"y_mo_1 = np.array([[1.0, 2.0], [3.0, 4.0]])\n",
15634+
"S._store_mo(y_mo_1)\n",
15635+
"print(f\"S.y_mo after first call: {S.y_mo}\")\n",
15636+
"y_mo_2 = np.array([[5.0, 6.0], [7.0, 8.0]])\n",
15637+
"S._store_mo(y_mo_2)\n",
15638+
"print(f\"S.y_mo after second call: {S.y_mo}\")\n"
15639+
]
15640+
},
15641+
{
15642+
"cell_type": "code",
15643+
"execution_count": 12,
15644+
"metadata": {},
15645+
"outputs": [
15646+
{
15647+
"name": "stderr",
15648+
"output_type": "stream",
15649+
"text": [
15650+
"Seed set to 123\n"
15651+
]
15652+
},
15653+
{
15654+
"name": "stdout",
15655+
"output_type": "stream",
15656+
"text": [
15657+
"Single-objective values (default): [1. 3.]\n",
15658+
"Single-objective values (custom): [3. 7.]\n"
15659+
]
15660+
}
15661+
],
15662+
"source": [
15663+
"from spotpython.spot import Spot\n",
15664+
"from spotpython.utils.init import fun_control_init\n",
15665+
"fun_control = fun_control_init(\n",
15666+
" lower=np.array([-1, -1]),\n",
15667+
" upper=np.array([1, 1])\n",
15668+
")\n",
15669+
"fun = lambda x: np.array([[x[0]**2 + x[1]**2, (x[0]-1)**2 + (x[1]-1)**2] for x in x])\n",
15670+
"S = Spot(fun=fun, fun_control=fun_control)\n",
15671+
"y_mo = np.array([[1.0, 2.0], [3.0, 4.0]])\n",
15672+
"y_so = S._mo2so(y_mo)\n",
15673+
"print(f\"Single-objective values (default): {y_so}\")\n",
15674+
"\n",
15675+
"# Now define a custom function to convert multi-objective to single-objective\n",
15676+
"def custom_mo2so(y_mo):\n",
15677+
" return y_mo[:, 0] + y_mo[:, 1]\n",
15678+
"S.fun_control[\"fun_mo2so\"] = custom_mo2so\n",
15679+
"y_so_custom = S._mo2so(y_mo)\n",
15680+
"print(f\"Single-objective values (custom): {y_so_custom}\")\n"
15681+
]
15682+
},
15683+
{
15684+
"cell_type": "code",
15685+
"execution_count": 13,
15686+
"metadata": {},
15687+
"outputs": [
15688+
{
15689+
"name": "stderr",
15690+
"output_type": "stream",
15691+
"text": [
15692+
"Seed set to 123\n"
15693+
]
15694+
},
15695+
{
15696+
"name": "stdout",
15697+
"output_type": "stream",
15698+
"text": [
15699+
"S.X: [[ 0. 0. ]\n",
15700+
" [ 0. 1. ]\n",
15701+
" [ 1. 0. ]\n",
15702+
" [ 1. 1. ]\n",
15703+
" [ 0.86352963 0.7892358 ]\n",
15704+
" [-0.24407197 -0.83687436]\n",
15705+
" [ 0.36481882 0.8375811 ]\n",
15706+
" [ 0.415331 0.54468512]\n",
15707+
" [-0.56395091 -0.77797854]\n",
15708+
" [-0.90259409 -0.04899292]\n",
15709+
" [-0.16484832 0.35724741]\n",
15710+
" [ 0.05170659 0.07401196]\n",
15711+
" [-0.78548145 -0.44638164]\n",
15712+
" [ 0.64017497 -0.30363301]]\n",
15713+
"S.y: [0. 1. 1. 2. 1.36857656 0.75992983\n",
15714+
" 0.83463487 0.46918172 0.92329124 0.8170764 0.15480068 0.00815134\n",
15715+
" 0.81623768 0.502017 ]\n"
15716+
]
15717+
}
15718+
],
15719+
"source": [
15720+
"import numpy as np\n",
15721+
"from spotpython.fun.objectivefunctions import Analytical\n",
15722+
"from spotpython.spot import Spot\n",
15723+
"from spotpython.utils.init import fun_control_init\n",
15724+
"fun_control = fun_control_init(\n",
15725+
" lower=np.array([-1, -1]),\n",
15726+
" upper=np.array([1, 1])\n",
15727+
")\n",
15728+
"fun = Analytical().fun_sphere\n",
15729+
"S = Spot(fun=fun, fun_control=fun_control)\n",
15730+
"X0 = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])\n",
15731+
"S.initialize_design_matrix(X_start=X0)\n",
15732+
"S.evaluate_initial_design()\n",
15733+
"print(f\"S.X: {S.X}\")\n",
15734+
"print(f\"S.y: {S.y}\")"
15735+
]
15736+
},
15737+
{
15738+
"cell_type": "code",
15739+
"execution_count": 14,
15740+
"metadata": {},
15741+
"outputs": [
15742+
{
15743+
"name": "stderr",
15744+
"output_type": "stream",
15745+
"text": [
15746+
"Seed set to 123\n"
15747+
]
15748+
}
15749+
],
15750+
"source": [
15751+
"import numpy as np\n",
15752+
"from spotpython.fun.objectivefunctions import Analytical\n",
15753+
"from spotpython.spot import spot\n",
15754+
"from spotpython.utils.init import fun_control_init\n",
15755+
"fun = Analytical().fun_sphere\n",
15756+
"fun_control = fun_control_init(\n",
15757+
" lower = np.array([-1]),\n",
15758+
" upper = np.array([1])\n",
15759+
" )\n",
15760+
"S = spot.Spot(fun=fun,\n",
15761+
" fun_control=fun_control,\n",
15762+
" )\n",
15763+
"S.initialize_design()\n",
15764+
"S.write_initial_tensorboard_log()"
15765+
]
15766+
},
15767+
{
15768+
"cell_type": "code",
15769+
"execution_count": 16,
15770+
"metadata": {},
15771+
"outputs": [
15772+
{
15773+
"name": "stderr",
15774+
"output_type": "stream",
15775+
"text": [
15776+
"Seed set to 123\n"
15777+
]
15778+
}
15779+
],
15780+
"source": [
15781+
"import numpy as np\n",
15782+
"from spotpython.fun.objectivefunctions import Analytical\n",
15783+
"from spotpython.spot import spot\n",
15784+
"from spotpython.utils.init import (\n",
15785+
" fun_control_init, optimizer_control_init, surrogate_control_init, design_control_init\n",
15786+
" )\n",
15787+
"# number of initial points:\n",
15788+
"ni = 0\n",
15789+
"X_start = np.array([[0, 0], [0, 1], [1, 0], [1, 1], [1, 1]])\n",
15790+
"fun = Analytical().fun_sphere\n",
15791+
"fun_control = fun_control_init(\n",
15792+
" lower = np.array([-1, -1]),\n",
15793+
" upper = np.array([1, 1])\n",
15794+
" )\n",
15795+
"design_control=design_control_init(init_size=ni)\n",
15796+
"S = spot.Spot(fun=fun,\n",
15797+
" fun_control=fun_control, \n",
15798+
" design_control=design_control\n",
15799+
" )"
15800+
]
15801+
},
15802+
{
15803+
"cell_type": "code",
15804+
"execution_count": 1,
15805+
"metadata": {},
15806+
"outputs": [
15807+
{
15808+
"name": "stderr",
15809+
"output_type": "stream",
15810+
"text": [
15811+
"Seed set to 123\n",
15812+
"Seed set to 123\n",
15813+
"Seed set to 123\n"
15814+
]
15815+
},
15816+
{
15817+
"name": "stdout",
15818+
"output_type": "stream",
15819+
"text": [
15820+
"S.X: [[0 0]\n",
15821+
" [0 1]\n",
15822+
" [1 0]\n",
15823+
" [1 1]\n",
15824+
" [1 1]]\n",
15825+
"S.y: [0. 1. 1. 2. 2.]\n",
15826+
"X_shape_before: (5, 2)\n",
15827+
"y_size_before: 5\n",
15828+
"S.X: [[ 0.00000000e+00 0.00000000e+00]\n",
15829+
" [ 0.00000000e+00 1.00000000e+00]\n",
15830+
" [ 1.00000000e+00 0.00000000e+00]\n",
15831+
" [ 1.00000000e+00 1.00000000e+00]\n",
15832+
" [ 1.00000000e+00 1.00000000e+00]\n",
15833+
" [ 3.51844236e-08 -2.62569919e-08]]\n",
15834+
"S.y: [0.00000000e+00 1.00000000e+00 1.00000000e+00 2.00000000e+00\n",
15835+
" 2.00000000e+00 1.92737329e-15]\n",
15836+
"S.n_points: 1\n",
15837+
"X_shape_after: (6, 2)\n",
15838+
"y_size_after: 6\n",
15839+
"S.X: [[ 0. 1. ]\n",
15840+
" [ 1. 0. ]\n",
15841+
" [ 1. 1. ]\n",
15842+
" [ 1. 1. ]\n",
15843+
" [ 0.54509876 -0.36921401]\n",
15844+
" [ 0.54509876 -0.36921401]\n",
15845+
" [ 0.18642675 0.87708546]\n",
15846+
" [ 0.18642675 0.87708546]\n",
15847+
" [-0.45060393 -0.208063 ]\n",
15848+
" [-0.45060393 -0.208063 ]]\n",
15849+
"S.y: [0.98021757 0.99264427 2.02575851 2.00387949 0.45185626 0.44499372\n",
15850+
" 0.79130456 0.81487288 0.24000221 0.23988634]\n",
15851+
"X_shape_before: (10, 2)\n",
15852+
"y_size_before: 10\n",
15853+
"S.X: [[ 0. 1. ]\n",
15854+
" [ 1. 0. ]\n",
15855+
" [ 1. 1. ]\n",
15856+
" [ 1. 1. ]\n",
15857+
" [ 0.54509876 -0.36921401]\n",
15858+
" [ 0.54509876 -0.36921401]\n",
15859+
" [ 0.18642675 0.87708546]\n",
15860+
" [ 0.18642675 0.87708546]\n",
15861+
" [-0.45060393 -0.208063 ]\n",
15862+
" [-0.45060393 -0.208063 ]\n",
15863+
" [-0.40547021 -0.25416702]]\n",
15864+
"S.y: [0.98021757 0.99264427 2.02575851 2.00387949 0.45185626 0.44499372\n",
15865+
" 0.79130456 0.81487288 0.24000221 0.23988634 0.20922454]\n",
15866+
"S.n_points: 1\n",
15867+
"S.ocba_delta: 1\n",
15868+
"X_shape_after: (11, 2)\n",
15869+
"y_size_after: 11\n"
15870+
]
15871+
}
15872+
],
15873+
"source": [
15874+
"# 1. Without OCBA points:\n",
15875+
"import numpy as np\n",
15876+
"from spotpython.fun.objectivefunctions import Analytical\n",
15877+
"from spotpython.utils.init import (\n",
15878+
" fun_control_init, optimizer_control_init, surrogate_control_init, design_control_init\n",
15879+
" )\n",
15880+
"from spotpython.spot import Spot\n",
15881+
"# number of initial points:\n",
15882+
"ni = 0\n",
15883+
"X_start = np.array([[0, 0], [0, 1], [1, 0], [1, 1], [1, 1]])\n",
15884+
"fun = Analytical().fun_sphere\n",
15885+
"fun_control = fun_control_init(\n",
15886+
" lower = np.array([-1, -1]),\n",
15887+
" upper = np.array([1, 1])\n",
15888+
" )\n",
15889+
"design_control=design_control_init(init_size=ni)\n",
15890+
"S = Spot(fun=fun,\n",
15891+
" fun_control=fun_control,\n",
15892+
" design_control=design_control,)\n",
15893+
"S.initialize_design(X_start=X_start)\n",
15894+
"print(f\"S.X: {S.X}\")\n",
15895+
"print(f\"S.y: {S.y}\")\n",
15896+
"X_shape_before = S.X.shape\n",
15897+
"print(f\"X_shape_before: {X_shape_before}\")\n",
15898+
"print(f\"y_size_before: {S.y.size}\")\n",
15899+
"y_size_before = S.y.size\n",
15900+
"S.update_stats()\n",
15901+
"S.fit_surrogate()\n",
15902+
"S.update_design()\n",
15903+
"print(f\"S.X: {S.X}\")\n",
15904+
"print(f\"S.y: {S.y}\")\n",
15905+
"print(f\"S.n_points: {S.n_points}\")\n",
15906+
"print(f\"X_shape_after: {S.X.shape}\")\n",
15907+
"print(f\"y_size_after: {S.y.size}\")\n",
15908+
"#\n",
15909+
"# 2. Using the OCBA points:\n",
15910+
"import numpy as np\n",
15911+
"from spotpython.fun.objectivefunctions import Analytical\n",
15912+
"from spotpython.spot import Spot\n",
15913+
"from spotpython.utils.init import fun_control_init, design_control_init\n",
15914+
"# number of initial points:\n",
15915+
"ni = 3\n",
15916+
"X_start = np.array([[0, 1], [1, 0], [1, 1], [1, 1]])\n",
15917+
"fun = Analytical().fun_sphere\n",
15918+
"fun_control = fun_control_init(\n",
15919+
" sigma=0.02,\n",
15920+
" lower = np.array([-1, -1]),\n",
15921+
" upper = np.array([1, 1]),\n",
15922+
" noise=True,\n",
15923+
" ocba_delta=1,\n",
15924+
" )\n",
15925+
"design_control=design_control_init(init_size=ni, repeats=2)\n",
15926+
"S = Spot(fun=fun,\n",
15927+
" design_control=design_control,\n",
15928+
" fun_control=fun_control\n",
15929+
")\n",
15930+
"S.initialize_design(X_start=X_start)\n",
15931+
"print(f\"S.X: {S.X}\")\n",
15932+
"print(f\"S.y: {S.y}\")\n",
15933+
"X_shape_before = S.X.shape\n",
15934+
"print(f\"X_shape_before: {X_shape_before}\")\n",
15935+
"print(f\"y_size_before: {S.y.size}\")\n",
15936+
"y_size_before = S.y.size\n",
15937+
"S.update_stats()\n",
15938+
"S.fit_surrogate()\n",
15939+
"S.update_design()\n",
15940+
"print(f\"S.X: {S.X}\")\n",
15941+
"print(f\"S.y: {S.y}\")\n",
15942+
"print(f\"S.n_points: {S.n_points}\")\n",
15943+
"print(f\"S.ocba_delta: {S.ocba_delta}\")\n",
15944+
"print(f\"X_shape_after: {S.X.shape}\")\n",
15945+
"print(f\"y_size_after: {S.y.size}\")\n",
15946+
"# compare the shapes of the X and y values before and after the update_design method\n",
15947+
"assert X_shape_before[0] + S.ocba_delta == S.X.shape[0]\n",
15948+
"assert X_shape_before[1] == S.X.shape[1]\n",
15949+
"assert y_size_before + S.ocba_delta == S.y.size"
15950+
]
15951+
},
15952+
{
15953+
"cell_type": "code",
15954+
"execution_count": 2,
15955+
"metadata": {},
15956+
"outputs": [
15957+
{
15958+
"name": "stderr",
15959+
"output_type": "stream",
15960+
"text": [
15961+
"Seed set to 123\n"
15962+
]
15963+
},
15964+
{
15965+
"name": "stdout",
15966+
"output_type": "stream",
15967+
"text": [
15968+
"Experiment saved to 000_res.pkl\n"
15969+
]
15970+
}
15971+
],
15972+
"source": [
15973+
"import numpy as np\n",
15974+
"from spotpython.fun.objectivefunctions import Analytical\n",
15975+
"from spotpython.spot import Spot\n",
15976+
"from spotpython.utils.init import fun_control_init\n",
15977+
"fun_control = fun_control_init(\n",
15978+
" lower=np.array([-1, -1]),\n",
15979+
" upper=np.array([1, 1])\n",
15980+
")\n",
15981+
"fun = Analytical().fun_sphere\n",
15982+
"S = Spot(fun=fun, fun_control=fun_control)\n",
15983+
"S.initialize_design()\n",
15984+
"S.evaluate_initial_design()\n",
15985+
"S.update_stats()\n",
15986+
"S.fit_surrogate()\n",
15987+
"S.update_design()\n",
15988+
"S.save_result()"
15989+
]
15990+
},
1559815991
{
1559915992
"cell_type": "code",
1560015993
"execution_count": null,

0 commit comments

Comments
 (0)