Skip to content

Commit 0f02a74

Browse files
committed
some new funcs
1 parent c98f7d7 commit 0f02a74

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

notebooks/OptimizationBasedModels/Modele1.5D_Auto_Optim_CMC.ipynb

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10967,7 +10967,7 @@
1096710967
"\n",
1096810968
"result_dict={}\n",
1096910969
"\n",
10970-
"def store_results(x, fp_gld, fp_slack, gld_params, experiment_key=None):\n",
10970+
"def store_results(x, fp_gld, fp_slack, gld_params, experiment_key=None, result_dict=result_dict):\n",
1097110971
" x_key = otaf.common.bidirectional_string_to_array_conversion(x)\n",
1097210972
" x_dict = {\"FP_GLD\": fp_gld, \"FP_SLACK\":fp_slack, \"GLD_PARAMS\": gld_params}\n",
1097310973
" if experiment_key is None:\n",
@@ -10992,7 +10992,7 @@
1099210992
"metadata": {},
1099310993
"outputs": [],
1099410994
"source": [
10995-
"SIZE_MC_PF = 500000 #int(1e6) #1e4\n",
10995+
"SIZE_MC_PF = 100000 #int(1e6) #1e4\n",
1099610996
"\n",
1099710997
"sample_gld = otaf.sampling.generate_and_transform_sequence(NDim_Defects, SIZE_MC_PF, RandDeviationVect) \n",
1099810998
"scale_factor = 1.0\n",
@@ -11015,7 +11015,7 @@
1101511015
" return np.squeeze(neural_model.evaluate_model_non_standard_space(x).detach().numpy())\n",
1101611016
"\n",
1101711017
"@otaf.optimization.scaling(scale_factor)\n",
11018-
"def optimization_function_mini(x, failure_slack=0.0, model=model2, experiment_key=None):\n",
11018+
"def optimization_function_mini(x, failure_slack=0.0, model=model2, experiment_key=None, result_dict=result_dict):\n",
1101911019
" # Here we search the minimal probability of failure\n",
1102011020
" slack = model(x)\n",
1102111021
" gld_params = gld.fit_LMM(slack, disp_fit=False, disp_optimizer=False)\n",
@@ -11028,12 +11028,12 @@
1102811028
" fp_gld = gld.CDF_num(failure_slack, gld_params)\n",
1102911029
" fp_out = fp_gld\n",
1103011030
" \n",
11031-
" store_results(x, fp_gld, fp_slack, gld_params, experiment_key)\n",
11031+
" store_results(x, fp_gld, fp_slack, gld_params, experiment_key, result_dict)\n",
1103211032
" return fp_out\n",
1103311033
"\n",
1103411034
"\n",
1103511035
"@otaf.optimization.scaling(scale_factor)\n",
11036-
"def optimization_function_maxi(x, failure_slack=0.0, model=model2, experiment_key=None):\n",
11036+
"def optimization_function_maxi(x, failure_slack=0.0, model=model2, experiment_key=None, result_dict=result_dict):\n",
1103711037
" # Here we search the maximal probability of failure so negative output\n",
1103811038
" slack = model(x)\n",
1103911039
" gld_params = gld.fit_LMM(slack, disp_fit=False, disp_optimizer=False)\n",
@@ -11046,7 +11046,7 @@
1104611046
" fp_gld = gld.CDF_num(failure_slack, gld_params)\n",
1104711047
" fp_out = fp_gld\n",
1104811048
" \n",
11049-
" store_results(x, fp_gld, fp_slack, gld_params, experiment_key)\n",
11049+
" store_results(x, fp_gld, fp_slack, gld_params, experiment_key, result_dict)\n",
1105011050
"\n",
1105111051
" return fp_out*-1"
1105211052
]
@@ -11078,7 +11078,7 @@
1107811078
" midof_funcs.two_parallel_straight_lines,\n",
1107911079
" mif_args = (t, X3),\n",
1108011080
" n_dof = 2,\n",
11081-
" n_sample = 100000,\n",
11081+
" n_sample = 40000,\n",
1108211082
" target = \"std\", #\"prob\",\n",
1108311083
" target_val = sigma_e_pos*np.sqrt(1-(2/np.pi)), #To be folded normal variance!\n",
1108411084
" isNormal = True, #The component distributions are normal\n",
@@ -11115,7 +11115,7 @@
1111511115
"metadata": {},
1111611116
"outputs": [],
1111711117
"source": [
11118-
"def assembly_constraint_no_mean(x, scale_factor=1.0, experiment_key=None):\n",
11118+
"def assembly_constraint_no_mean(x, scale_factor=1.0, result_dict=result_dict, experiment_key=None):\n",
1111911119
" \"\"\" The functions takes directly the concatenated list of all normalized parameters (between 0 and 1), \n",
1112011120
" and reconstructs the statistical constraint violation for each feature.\n",
1112111121
" \"\"\"\n",
@@ -11167,8 +11167,66 @@
1116711167
"id": "701e5c11-de56-4437-99f5-9019839cc18a",
1116811168
"metadata": {},
1116911169
"source": [
11170-
"# Optimizations to find the paramter values that maximize and minimize the rejection rate.\n",
11170+
"# Optimizations to find the paramter values that maximize and minimize the rejection rate."
11171+
]
11172+
},
11173+
{
11174+
"cell_type": "code",
11175+
"execution_count": null,
11176+
"id": "63241638-cc40-480a-9692-8e4dc7c9859b",
11177+
"metadata": {},
11178+
"outputs": [],
11179+
"source": [
11180+
"def pf_min_max_optimizer(failure_slack=0.0):\n",
11181+
" # Initial guess\n",
11182+
" x0 = [0.5] * NDim_Defects # Initial guess\n",
11183+
" \n",
11184+
" # Perform the local optimization using COBYQA directly\n",
11185+
" res_maxi = minimize(\n",
11186+
" optimization_function_maxi, x0,\n",
11187+
" args=(failure_slack),\n",
11188+
" method=\"COBYQA\", \n",
11189+
" jac=None, \n",
11190+
" bounds=Bounds(0.0, 1.0, keep_feasible=True),\n",
11191+
" constraints = nonLinearConstraint,\n",
11192+
" options={\n",
11193+
" \"f_target\": -0.1, \n",
11194+
" \"maxiter\": 1000,\n",
11195+
" \"maxfev\": 4000,\n",
11196+
" \"feasibility_tol\": 1e-9,\n",
11197+
" \"initial_tr_radius\": np.sqrt(2*10),\n",
11198+
" \"final_tr_radius\": 1e-5,\n",
11199+
" \"disp\": True,\n",
11200+
" \"scale\": False #True\n",
11201+
" }\n",
11202+
" )\n",
1117111203
"\n",
11204+
" # Perform the local optimization using COBYQA directly\n",
11205+
" res_mini = minimize(\n",
11206+
" optimization_function_mini, x0, \n",
11207+
" args=(failure_slack),\n",
11208+
" method=\"COBYQA\", \n",
11209+
" jac=None, \n",
11210+
" bounds=Bounds(0.0, 1.0, keep_feasible=True),\n",
11211+
" constraints = nonLinearConstraint,\n",
11212+
" options={\n",
11213+
" \"f_target\": -0.01,\n",
11214+
" \"maxiter\": 1000,\n",
11215+
" \"maxfev\": 4000,\n",
11216+
" \"feasibility_tol\": 1e-9,\n",
11217+
" \"initial_tr_radius\": np.sqrt(2*10),\n",
11218+
" \"final_tr_radius\": 1e-5,\n",
11219+
" \"disp\": True,\n",
11220+
" \"scale\": False #True\n",
11221+
" }\n",
11222+
" )\n"
11223+
]
11224+
},
11225+
{
11226+
"cell_type": "markdown",
11227+
"id": "5cb89faf-31a3-42f5-860c-043667015974",
11228+
"metadata": {},
11229+
"source": [
1117211230
"## First maximize Pf"
1117311231
]
1117411232
},
@@ -11358,29 +11416,6 @@
1135811416
}
1135911417
],
1136011418
"source": [
11361-
"# Initial guess\n",
11362-
"x0_maxi = [0.5] * NDim_Defects # Initial guess\n",
11363-
"\n",
11364-
"# Perform the local optimization using COBYQA directly\n",
11365-
"res_maxi = minimize(\n",
11366-
" optimization_function_maxi, \n",
11367-
" x0_maxi,\n",
11368-
" args=(0.05),\n",
11369-
" method=\"COBYQA\", \n",
11370-
" jac=None, \n",
11371-
" bounds=Bounds(0.0, 1.0, keep_feasible=True),\n",
11372-
" constraints = nonLinearConstraint,\n",
11373-
" options={\n",
11374-
" \"f_target\": -0.1, \n",
11375-
" \"maxiter\": 1000,\n",
11376-
" \"maxfev\": 4000,\n",
11377-
" \"feasibility_tol\": 1e-9,\n",
11378-
" \"initial_tr_radius\": np.sqrt(2*10),\n",
11379-
" \"final_tr_radius\": 1e-5,\n",
11380-
" \"disp\": True,\n",
11381-
" \"scale\": False #True\n",
11382-
" }\n",
11383-
")\n",
1138411419
"\n",
1138511420
"print(\"Maximization Result with COBYQA:\")\n",
1138611421
"print(res_maxi)"
@@ -11619,26 +11654,7 @@
1161911654
"# Initial guess\n",
1162011655
"x0_mini = [0.5] * NDim_Defects # Initial guess\n",
1162111656
"\n",
11622-
"# Perform the local optimization using COBYQA directly\n",
11623-
"res_mini = minimize(\n",
11624-
" optimization_function_mini, \n",
11625-
" x0_mini, \n",
11626-
" args=(0.05),\n",
11627-
" method=\"COBYQA\", \n",
11628-
" jac=None, \n",
11629-
" bounds=Bounds(0.0, 1.0, keep_feasible=True),\n",
11630-
" constraints = nonLinearConstraint,\n",
11631-
" options={\n",
11632-
" \"f_target\": -0.01,\n",
11633-
" \"maxiter\": 1000,\n",
11634-
" \"maxfev\": 4000,\n",
11635-
" \"feasibility_tol\": 1e-9,\n",
11636-
" \"initial_tr_radius\": np.sqrt(2*10),\n",
11637-
" \"final_tr_radius\": 1e-5,\n",
11638-
" \"disp\": True,\n",
11639-
" \"scale\": False #True\n",
11640-
" }\n",
11641-
")\n",
11657+
"\n",
1164211658
"\n",
1164311659
"print(\"minimization Result with COBYQA:\")\n",
1164411660
"print(res_mini)"

src/otaf/common/_common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"get_tqdm_range",
1616
"alphabet_generator",
1717
"threshold_for_percentile_positive_values_below",
18-
"bidirectional_string_to_array_conversion"
18+
"bidirectional_string_to_array_conversion",
19+
"arrays_close_enough"
1920
]
2021

2122
import re
@@ -654,3 +655,6 @@ def bidirectional_string_to_array_conversion(x):
654655
return str_to_arr(x)
655656
else:
656657
return arr_to_str(x)
658+
659+
def arrays_close_enough(arr1, arr2, tolerance=1e-6):
660+
return np.allclose(arr1, arr2, atol=tolerance)

0 commit comments

Comments
 (0)