yunusk commited on
Commit
ab3d6ca
·
1 Parent(s): a7a8c12

Problem with 10d funcs is fixed. Adaptive hdmr bugs are fixed.

Browse files
Files changed (2) hide show
  1. app.py +17 -14
  2. src/main.py +17 -13
app.py CHANGED
@@ -32,11 +32,11 @@ with st.container():
32
  st.title("HDMR Optimization")
33
  st.write(page_text)
34
 
35
- available_functions = ["testfunc_2d", "camel3_2d", "camel16_2d", "treccani_2d", "goldstein_2d", "branin_2d",
36
- "rosenbrock_2d", "ackley_2d"]
37
-
38
  # available_functions = ["testfunc_2d", "camel3_2d", "camel16_2d", "treccani_2d", "goldstein_2d", "branin_2d",
39
- # "rosenbrock_2d", "ackley_2d", "rosenbrock_10d", "griewank_10d", "rastrigin_10d"]
 
 
 
40
 
41
  st.sidebar.header('User Inputs')
42
 
@@ -87,18 +87,21 @@ if st.sidebar.button("Calculate HDMR"):
87
 
88
  st.subheader("Plots")
89
 
90
- if interactive_plot:
91
- col3, col4 = st.columns(spec=[0.4, 0.6], gap='small')
92
- else:
93
- col3, col4 = st.columns(spec=2, gap='small')
94
-
95
- with col3:
96
- st.pyplot(plt1)
97
- with col4:
98
  if interactive_plot:
99
- st.plotly_chart(plt2)
100
  else:
101
- st.pyplot(plt2)
 
 
 
 
 
 
 
 
 
 
102
 
103
 
104
 
 
32
  st.title("HDMR Optimization")
33
  st.write(page_text)
34
 
 
 
 
35
  # available_functions = ["testfunc_2d", "camel3_2d", "camel16_2d", "treccani_2d", "goldstein_2d", "branin_2d",
36
+ # "rosenbrock_2d", "ackley_2d"]
37
+
38
+ available_functions = ["testfunc_2d", "camel3_2d", "camel16_2d", "treccani_2d", "goldstein_2d", "branin_2d",
39
+ "rosenbrock_2d", "ackley_2d", "rosenbrock_10d", "griewank_10d", "rastrigin_10d"]
40
 
41
  st.sidebar.header('User Inputs')
42
 
 
87
 
88
  st.subheader("Plots")
89
 
90
+ if n == 2:
 
 
 
 
 
 
 
91
  if interactive_plot:
92
+ col3, col4 = st.columns(spec=[0.4, 0.6], gap='small')
93
  else:
94
+ col3, col4 = st.columns(spec=2, gap='small')
95
+
96
+ with col3:
97
+ st.pyplot(plt1)
98
+ with col4:
99
+ if interactive_plot:
100
+ st.plotly_chart(plt2)
101
+ else:
102
+ st.pyplot(plt2)
103
+ else:
104
+ st.pyplot(plt1)
105
 
106
 
107
 
src/main.py CHANGED
@@ -223,11 +223,11 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
223
  return fig
224
 
225
 
226
- def calculate_distances(x0, arr):
227
- return np.sqrt(np.sum((x0 - np.array(arr)) ** 2, axis=1))
228
 
229
- def find_closest_points(x0, arr, k):
230
- distances = calculate_distances(x0, arr)
231
  indexes = np.argsort(distances)[:k]
232
  return arr[indexes]
233
 
@@ -262,7 +262,7 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
262
  print(f"new x0 = {new_x0}")
263
 
264
  print("---------------------------------------------------------------------------------")
265
- closest_points = find_closest_points(x0=new_x0, arr=xs, k=k)
266
  new_a = np.min(closest_points, axis=0)
267
  new_b = np.max(closest_points, axis=0)
268
 
@@ -279,10 +279,6 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
279
  new_a[i] = middle_point - (new_range / 2)
280
  new_b[i] = middle_point + (new_range / 2)
281
 
282
- print("Old a: ", old_a)
283
- print("Old b: ", old_b)
284
- print("New a: ", new_a)
285
- print("New b: ", new_b)
286
 
287
  print("Creating new sample...", end=" ")
288
  new_xs = (new_b - new_a)*np.random.random((N,n)) + new_a
@@ -297,11 +293,12 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
297
  temp_status.append(status.x[0])
298
  result = OptimizeResult(x=temp_status, fun=fun(new_x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
299
  result.nfev = N
300
- results.append(result)
301
 
302
  if np.sqrt(np.sum((old_x0 - new_x0) ** 2)) < epsilon:
303
  print("Convergence occured!")
304
  break
 
 
305
 
306
  old_x0 = np.array(new_x0)
307
  xs = new_xs
@@ -312,7 +309,11 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
312
  b = old_b
313
  x0 = old_x0
314
  plt1 = plot_results()
315
- plt2 = plot_with_function()
 
 
 
 
316
  else:
317
  xs = (b-a)*np.random.random((N,n))+a # Generate sampling data
318
  print('XS: ', xs.shape)
@@ -325,9 +326,12 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
325
  result = OptimizeResult(x=temp_status, fun=fun(x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
326
  result.nfev = N
327
  plt1 = plot_results()
328
- plt2 = plot_with_function()
 
 
 
329
 
330
- return result
331
 
332
  def main_function(N_, n_, function_name_, m_, a_, b_, random_init_, is_adaptive_, k_=None, epsilon_=None, clip_=None):
333
  global N, n, function_name, m, a, b, random_init, is_adaptive, k, epsilon, clip
 
223
  return fig
224
 
225
 
226
+ def calculate_distances(x0_, arr):
227
+ return np.sqrt(np.sum((x0_ - np.array(arr)) ** 2, axis=1))
228
 
229
+ def find_closest_points(x0_, arr, k):
230
+ distances = calculate_distances(x0_, arr)
231
  indexes = np.argsort(distances)[:k]
232
  return arr[indexes]
233
 
 
262
  print(f"new x0 = {new_x0}")
263
 
264
  print("---------------------------------------------------------------------------------")
265
+ closest_points = find_closest_points(x0_=new_x0, arr=xs, k=k)
266
  new_a = np.min(closest_points, axis=0)
267
  new_b = np.max(closest_points, axis=0)
268
 
 
279
  new_a[i] = middle_point - (new_range / 2)
280
  new_b[i] = middle_point + (new_range / 2)
281
 
 
 
 
 
282
 
283
  print("Creating new sample...", end=" ")
284
  new_xs = (new_b - new_a)*np.random.random((N,n)) + new_a
 
293
  temp_status.append(status.x[0])
294
  result = OptimizeResult(x=temp_status, fun=fun(new_x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
295
  result.nfev = N
 
296
 
297
  if np.sqrt(np.sum((old_x0 - new_x0) ** 2)) < epsilon:
298
  print("Convergence occured!")
299
  break
300
+ else:
301
+ results.append(result)
302
 
303
  old_x0 = np.array(new_x0)
304
  xs = new_xs
 
309
  b = old_b
310
  x0 = old_x0
311
  plt1 = plot_results()
312
+ if n == 2:
313
+ plt2 = plot_with_function()
314
+ else:
315
+ plt2 = None
316
+ return results[-1]
317
  else:
318
  xs = (b-a)*np.random.random((N,n))+a # Generate sampling data
319
  print('XS: ', xs.shape)
 
326
  result = OptimizeResult(x=temp_status, fun=fun(x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
327
  result.nfev = N
328
  plt1 = plot_results()
329
+ if n == 2:
330
+ plt2 = plot_with_function()
331
+ else:
332
+ plt2 = None
333
 
334
+ return result
335
 
336
  def main_function(N_, n_, function_name_, m_, a_, b_, random_init_, is_adaptive_, k_=None, epsilon_=None, clip_=None):
337
  global N, n, function_name, m, a, b, random_init, is_adaptive, k, epsilon, clip