46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
import numpy as np
|
|
from function import *
|
|
z_history = []
|
|
x1_history = []
|
|
x2_history = []
|
|
# fName = 'semicircle'
|
|
# fName = 'rosenbrock'
|
|
# fName = 'semicircle2'
|
|
fName = 'himmelblau'
|
|
dfName = 'df_'+fName
|
|
def objective(trial):
|
|
x1 = -0.3
|
|
x2 = -3
|
|
lr = 0.002
|
|
# x1 = trial.suggest_float("x1", 0, 3)
|
|
# x2 = trial.suggest_float("x2", 0, 3)
|
|
beta = 0.5
|
|
# lr = trial.suggest_float("lr", 0, 0.1)
|
|
epochs = 100
|
|
z = eval(fName)(x1, x2)
|
|
z_history.append(z)
|
|
|
|
for epoch in range(epochs):
|
|
|
|
dx1, dx2 = eval(dfName)(x1, x2)
|
|
x1 -= lr * dx1
|
|
x2 -= lr * dx2
|
|
|
|
x1_history.append(x1)
|
|
x2_history.append(x2)
|
|
z = eval(fName)(x1, x2)
|
|
z_history.append(z)
|
|
|
|
return eval(fName)(x1, x2)
|
|
|
|
def main():
|
|
study = optuna.create_study(direction="minimize")
|
|
study.optimize(objective, n_trials=1, timeout=600)
|
|
plot_optimization_history(study).show()
|
|
plotAll(fName,np.array(x1_history), np.array(x2_history))
|
|
print(study.best_value,study.best_trial)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |