Spaces:
Sleeping
Sleeping
mendozame23
commited on
Commit
•
ca7b8bb
1
Parent(s):
0489920
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,46 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
import optuna
|
|
|
|
|
5 |
|
6 |
st.title("Optimization tool")
|
7 |
|
8 |
func_user = st.selectbox("Choose a function:", ("cuadratic", "sine", "gaussian"))
|
9 |
-
# ("sin(x/10)", "(x-2)^2", "exp-(x-4)^2")
|
10 |
-
|
11 |
|
12 |
def func_cuadratic(x):
|
13 |
-
return (x - 2)
|
14 |
-
|
15 |
|
16 |
def func_sine(x):
|
17 |
return np.sin(x / 10)
|
18 |
|
19 |
-
|
20 |
def func_gauss(x):
|
21 |
-
return np.exp(-(
|
22 |
-
|
23 |
|
24 |
if func_user == "cuadratic":
|
25 |
-
st.latex(r"(x
|
26 |
func_to_use = func_cuadratic
|
27 |
-
|
28 |
elif func_user == "sine":
|
29 |
-
st.latex(r"sin({
|
30 |
func_to_use = func_sine
|
31 |
-
|
32 |
else:
|
33 |
st.latex(r"e^{-(x-4)^2}")
|
34 |
func_to_use = func_gauss
|
35 |
|
36 |
-
|
37 |
opt_user = st.selectbox("Choose the optimization direction:", ("minimize", "maximize"))
|
38 |
|
39 |
-
x_low = st.number_input("Please, give me the lower bound:"
|
40 |
-
x_upp = st.number_input("Please, give me the upper bound:"
|
41 |
-
|
42 |
|
43 |
def objective(trial):
|
44 |
-
x = trial.suggest_float(
|
45 |
return func_to_use(x)
|
46 |
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
study.optimize(objective, n_trials=500)
|
50 |
-
|
51 |
-
x_opt = study.best_params["x"]
|
52 |
y_opt = func_to_use(x_opt)
|
53 |
|
54 |
st.write(f"The critical point found is ({x_opt:,.4f}, {y_opt:,.4f}).")
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
|
|
3 |
import optuna
|
4 |
+
from optuna.samplers import TPESampler
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
|
7 |
st.title("Optimization tool")
|
8 |
|
9 |
func_user = st.selectbox("Choose a function:", ("cuadratic", "sine", "gaussian"))
|
|
|
|
|
10 |
|
11 |
def func_cuadratic(x):
|
12 |
+
return (x - 2)**2
|
|
|
13 |
|
14 |
def func_sine(x):
|
15 |
return np.sin(x / 10)
|
16 |
|
|
|
17 |
def func_gauss(x):
|
18 |
+
return np.exp(-(x - 4)**2)
|
|
|
19 |
|
20 |
if func_user == "cuadratic":
|
21 |
+
st.latex(r"(x-2)^2")
|
22 |
func_to_use = func_cuadratic
|
|
|
23 |
elif func_user == "sine":
|
24 |
+
st.latex(r"sin(\frac{x}{10})")
|
25 |
func_to_use = func_sine
|
|
|
26 |
else:
|
27 |
st.latex(r"e^{-(x-4)^2}")
|
28 |
func_to_use = func_gauss
|
29 |
|
|
|
30 |
opt_user = st.selectbox("Choose the optimization direction:", ("minimize", "maximize"))
|
31 |
|
32 |
+
x_low = st.number_input("Please, give me the lower bound:")
|
33 |
+
x_upp = st.number_input("Please, give me the upper bound:")
|
|
|
34 |
|
35 |
def objective(trial):
|
36 |
+
x = trial.suggest_float('x', x_low, x_upp)
|
37 |
return func_to_use(x)
|
38 |
|
39 |
+
direction = "minimize" if opt_user == "minimize" else "maximize"
|
40 |
+
study = optuna.create_study(direction=direction, sampler=TPESampler())
|
41 |
+
study.optimize(objective, n_trials=100, show_progress_bar=True)
|
42 |
|
43 |
+
x_opt = study.best_params['x']
|
|
|
|
|
|
|
44 |
y_opt = func_to_use(x_opt)
|
45 |
|
46 |
st.write(f"The critical point found is ({x_opt:,.4f}, {y_opt:,.4f}).")
|