[535f03]: / cmaes / test_problems.py

Download this file

83 lines (62 with data), 2.0 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright (c) 2015, Disney Research
# All rights reserved.
#
# Author(s): Sehoon Ha <sehoon.ha@disneyresearch.com>
# Disney Research Robotics Group
import numpy as np
class QuadProb(object):
def __init__(self, x=None):
self.dim = 4
self.x = np.array([0.5, 0.7, -0.5, 1.0])
if x is not None:
self.dim = len(x)
self.x = x
def f(self, x):
diff = x - self.x
return 0.5 * np.linalg.norm(diff) ** 2
class Rosen(object):
def __init__(self):
self.dim = 10
def f(self, x):
import scipy.optimize
return scipy.optimize.rosen(x)
def bounds(self):
return [(-0.5, 0.5)] * self.dim
def on_eval_f(self, solver):
counter = solver.eval_counter
value = solver.last_f
# if counter % 10 == 0:
# solver.plot_convergence()
class ConstrainedQuadProb(object):
def __init__(self, x=None):
self.dim = 4
self.x = x if x is not None else np.array([0.5, 0.7, -0.5, 1.0])
def f(self, x):
diff = x - self.x
return 0.5 * np.linalg.norm(diff) ** 2
def num_eq_constraints(self):
return 2
def c_eq(self, x, index):
if index == 0:
return x[0] + x[1] - 1.0
elif index == 1:
return x[1] + x[2] - 1.0
def c_eq_jac(self, x, index):
""" (optional) """
if index == 0:
return np.array([1.0, 1.0, 0.0, 0.0])
elif index == 1:
return np.array([0.0, 1.0, 1.0, 0.0])
def num_ineq_constraints(self):
return 1
def c_ineq(self, x, index):
if index == 0:
return x[3] - 1.2
if __name__ == '__main__':
print('test optimization problems')
prob = QuadProb(np.array([0.5, -0.3, 0.9]))
print('prob.dim = %d' % (prob.dim))
from optimization.solver_cma import CMASolver as Solver
solver = Solver(prob)
res = solver.solve()
print(res)