Spaces:
Runtime error
Runtime error
Added x0 initial and timing to GUI
Browse files- app.py +8 -5
- app_utils.py +21 -1
- src/functions.py +3 -0
- src/main.py +10 -9
app.py
CHANGED
|
@@ -58,7 +58,9 @@ with col2:
|
|
| 58 |
|
| 59 |
|
| 60 |
random_init = st.sidebar.checkbox("Random Initialization")
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
is_adaptive = st.sidebar.checkbox("Adaptive HDMR")
|
| 64 |
|
|
@@ -74,13 +76,14 @@ if st.sidebar.button("Calculate HDMR"):
|
|
| 74 |
|
| 75 |
main.is_streamlit = interactive_plot
|
| 76 |
if is_adaptive:
|
| 77 |
-
status_hdmr, plt1, plt2, file_name = main.main_function(N, n, function_name, legendreDegree, interval[0], interval[1],
|
| 78 |
-
random_init, is_adaptive, num_closest_points, epsilon, clip)
|
| 79 |
else:
|
| 80 |
-
status_hdmr, plt1, plt2, file_name = main.main_function(N, n, function_name, legendreDegree, interval[0], interval[1],
|
| 81 |
-
random_init, is_adaptive)
|
| 82 |
st.subheader("Results")
|
| 83 |
st.write(f"hdmr_opt status Success: {status_hdmr.success} - X: {status_hdmr.x}")
|
|
|
|
| 84 |
|
| 85 |
with st.expander("Click to see the full result", expanded=False):
|
| 86 |
st.write(status_hdmr)
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
random_init = st.sidebar.checkbox("Random Initialization")
|
| 61 |
+
if not random_init:
|
| 62 |
+
default_x0 = utils.get_function_x0(function_name=function_name.split('_')[0])
|
| 63 |
+
x0 = st.sidebar.text_input("required format: x1,x2,...,xn",str(default_x0))
|
| 64 |
|
| 65 |
is_adaptive = st.sidebar.checkbox("Adaptive HDMR")
|
| 66 |
|
|
|
|
| 76 |
|
| 77 |
main.is_streamlit = interactive_plot
|
| 78 |
if is_adaptive:
|
| 79 |
+
status_hdmr, runtime, plt1, plt2, file_name = main.main_function(N, n, function_name, legendreDegree, interval[0], interval[1],
|
| 80 |
+
random_init, x0, is_adaptive, num_closest_points, epsilon, clip)
|
| 81 |
else:
|
| 82 |
+
status_hdmr, runtime, plt1, plt2, file_name = main.main_function(N, n, function_name, legendreDegree, interval[0], interval[1],
|
| 83 |
+
random_init, x0, is_adaptive)
|
| 84 |
st.subheader("Results")
|
| 85 |
st.write(f"hdmr_opt status Success: {status_hdmr.success} - X: {status_hdmr.x}")
|
| 86 |
+
st.write("Runtime: {runtime} seconds")
|
| 87 |
|
| 88 |
with st.expander("Click to see the full result", expanded=False):
|
| 89 |
st.write(status_hdmr)
|
app_utils.py
CHANGED
|
@@ -10,14 +10,34 @@ default_function_intervals = {
|
|
| 10 |
"griewank": (-600.0, 600.0),
|
| 11 |
"rastrigin": (-5.12, 5.12)
|
| 12 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def get_function_interval(function_name):
|
| 15 |
try:
|
| 16 |
interval = default_function_intervals[function_name]
|
| 17 |
except:
|
| 18 |
-
raise KeyError("You have entered non existing
|
| 19 |
|
| 20 |
return interval
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def get_dims(function_name):
|
| 23 |
return int(function_name.split('_')[1][:-1])
|
|
|
|
| 10 |
"griewank": (-600.0, 600.0),
|
| 11 |
"rastrigin": (-5.12, 5.12)
|
| 12 |
}
|
| 13 |
+
default_function_x0 = {
|
| 14 |
+
"testfunc": '2.0, 2.0',
|
| 15 |
+
"camel3": '2.0, 2.0',
|
| 16 |
+
"camel16": '2.0, 2.0',
|
| 17 |
+
"treccani": '2.0, 2.0',
|
| 18 |
+
"goldstein": '2.0, 2.0',
|
| 19 |
+
"branin": '2.0, 2.0',
|
| 20 |
+
"rosenbrock": '2.0, 2.0',
|
| 21 |
+
"ackley": '2.0, 2.0',
|
| 22 |
+
"griewank": '2.0, 2.0',
|
| 23 |
+
"rastrigin": '2.0, 2.0'
|
| 24 |
+
}
|
| 25 |
|
| 26 |
def get_function_interval(function_name):
|
| 27 |
try:
|
| 28 |
interval = default_function_intervals[function_name]
|
| 29 |
except:
|
| 30 |
+
raise KeyError("You have entered non existing function name.")
|
| 31 |
|
| 32 |
return interval
|
| 33 |
|
| 34 |
+
def get_function_x0(function_name):
|
| 35 |
+
try:
|
| 36 |
+
x0 = default_function_x0[function_name]
|
| 37 |
+
except:
|
| 38 |
+
raise KeyError("You have entered non existing function name.")
|
| 39 |
+
|
| 40 |
+
return x0
|
| 41 |
+
|
| 42 |
def get_dims(function_name):
|
| 43 |
return int(function_name.split('_')[1][:-1])
|
src/functions.py
CHANGED
|
@@ -152,6 +152,9 @@ if __name__ == "__main__":
|
|
| 152 |
elif f == rastrigin_2d:
|
| 153 |
x1_min = x2_min = -5.12
|
| 154 |
x1_max = x2_max = 5.12
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
N = 100
|
|
|
|
| 152 |
elif f == rastrigin_2d:
|
| 153 |
x1_min = x2_min = -5.12
|
| 154 |
x1_max = x2_max = 5.12
|
| 155 |
+
elif f == testfunc_2d:
|
| 156 |
+
x1_min = x2_min = -5
|
| 157 |
+
x1_max = x2_max = 5
|
| 158 |
|
| 159 |
|
| 160 |
N = 100
|
src/main.py
CHANGED
|
@@ -3,6 +3,7 @@ import numpy as np
|
|
| 3 |
import math
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
try:
|
| 7 |
import src.functions as functions
|
| 8 |
except:
|
|
@@ -333,7 +334,7 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=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
|
| 338 |
|
| 339 |
N = N_
|
|
@@ -355,11 +356,8 @@ def main_function(N_, n_, function_name_, m_, a_, b_, random_init_, is_adaptive_
|
|
| 355 |
else:
|
| 356 |
file_name = f"results/{function_name}_a{a}_b{b}_N{N}_m{m}"
|
| 357 |
|
| 358 |
-
if not random_init:
|
| 359 |
-
|
| 360 |
-
x0 = np.array([0.0, 0.0]) # Initial value of function for optimizing process
|
| 361 |
-
elif function_name.split('_')[1] == '10d':
|
| 362 |
-
x0 = np.zeros((10,))
|
| 363 |
else:
|
| 364 |
file_name += '_randomInit'
|
| 365 |
if function_name.split('_')[1] == '2d':
|
|
@@ -370,9 +368,11 @@ def main_function(N_, n_, function_name_, m_, a_, b_, random_init_, is_adaptive_
|
|
| 370 |
|
| 371 |
# status_bfgs = minimize(test_function, x0, method="BFGS") # Applying direct optimization method to the function
|
| 372 |
# print(f"BFGS status: {status_bfgs}")
|
|
|
|
| 373 |
status_hdmr = minimize(getattr(functions, function_name), x0, args=(), method=hdmr_opt) # Applying hdmr-opt method to the function
|
| 374 |
-
|
| 375 |
-
|
|
|
|
| 376 |
|
| 377 |
plt1 = plt2 = None
|
| 378 |
|
|
@@ -408,8 +408,9 @@ if __name__ == "__main__":
|
|
| 408 |
epsilon_ = global_args.epsilon
|
| 409 |
clip_ = global_args.clip
|
| 410 |
|
| 411 |
-
status_hdmr, _, _, file_name = main_function(N_, n_, function_name_, m_, a_, b_, random_init_,
|
| 412 |
is_adaptive_, k_, epsilon_, clip_)
|
|
|
|
| 413 |
print(f"hdmr_opt status: {status_hdmr}")
|
| 414 |
|
| 415 |
with open(file_name + '.txt', 'w') as f:
|
|
|
|
| 3 |
import math
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import pandas as pd
|
| 6 |
+
import time
|
| 7 |
try:
|
| 8 |
import src.functions as functions
|
| 9 |
except:
|
|
|
|
| 334 |
|
| 335 |
return result
|
| 336 |
|
| 337 |
+
def main_function(N_, n_, function_name_, m_, a_, b_, random_init_, x0_, is_adaptive_, k_=None, epsilon_=None, clip_=None):
|
| 338 |
global N, n, function_name, m, a, b, random_init, is_adaptive, k, epsilon, clip
|
| 339 |
|
| 340 |
N = N_
|
|
|
|
| 356 |
else:
|
| 357 |
file_name = f"results/{function_name}_a{a}_b{b}_N{N}_m{m}"
|
| 358 |
|
| 359 |
+
if not random_init:
|
| 360 |
+
x0 = np.fromstring(x0_,dtype=float,sep=',')
|
|
|
|
|
|
|
|
|
|
| 361 |
else:
|
| 362 |
file_name += '_randomInit'
|
| 363 |
if function_name.split('_')[1] == '2d':
|
|
|
|
| 368 |
|
| 369 |
# status_bfgs = minimize(test_function, x0, method="BFGS") # Applying direct optimization method to the function
|
| 370 |
# print(f"BFGS status: {status_bfgs}")
|
| 371 |
+
start=time.time()
|
| 372 |
status_hdmr = minimize(getattr(functions, function_name), x0, args=(), method=hdmr_opt) # Applying hdmr-opt method to the function
|
| 373 |
+
end=time.time()
|
| 374 |
+
runtime=end-start
|
| 375 |
+
return status_hdmr,runtime, plt1, plt2, file_name
|
| 376 |
|
| 377 |
plt1 = plt2 = None
|
| 378 |
|
|
|
|
| 408 |
epsilon_ = global_args.epsilon
|
| 409 |
clip_ = global_args.clip
|
| 410 |
|
| 411 |
+
status_hdmr, runtime, _, _, file_name = main_function(N_, n_, function_name_, m_, a_, b_, random_init_,
|
| 412 |
is_adaptive_, k_, epsilon_, clip_)
|
| 413 |
+
print(f"{runtime} seconds")
|
| 414 |
print(f"hdmr_opt status: {status_hdmr}")
|
| 415 |
|
| 416 |
with open(file_name + '.txt', 'w') as f:
|