-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCMAES_test.py
More file actions
40 lines (35 loc) · 1.05 KB
/
Copy pathCMAES_test.py
File metadata and controls
40 lines (35 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/user/zhao/miniconda3/envs/torch-0
# -*- coding: utf_8 -*-
# @Time : 2025/3/30 10:18
# @Author: ZhaoKe
# @File : CMAES_test.py
# @Software: PyCharm
import cma
import numpy as np
# 定义目标函数(香蕉函数)
def rosenbrock(x):
return (1 - x[0]) ** 2 + 100 * (x[1] - x[0] ** 2) ** 2
# 使用 CMA-ES 求解
if __name__ == "__main__":
# 初始参数
x0 = [0., 0.]
sigma0 = 0.5
options = {
'bounds': [-5., 5.],
# 'verbose': -9,
'maxfevals': 1000
}
cmaes = cma.CMAEvolutionStrategy(x0, sigma0, options)
ind = 0
while not cmaes.stop():
solutions = cmaes.ask() # generate solutions
fitness = [rosenbrock(x) for x in solutions]
cmaes.tell(solutions, fitness) # update parameters
cmaes.logger.add(cmaes) # logging
print(ind)
ind += 1
best_solution = cmaes.result.xbest
best_fitness = cmaes.result.fbest
print("\nOptimization Results:")
print("Optimal:".format(best_solution))
print("Minimum:".format(best_fitness))