Diff of /cmaes/test_problems.py [000000] .. [077a87]

Switch to unified view

a b/cmaes/test_problems.py
1
# Copyright (c) 2015, Disney Research
2
# All rights reserved.
3
#
4
# Author(s): Sehoon Ha <sehoon.ha@disneyresearch.com>
5
# Disney Research Robotics Group
6
7
8
import numpy as np
9
10
11
class QuadProb(object):
12
    def __init__(self, x=None):
13
        self.dim = 4
14
        self.x = np.array([0.5, 0.7, -0.5, 1.0])
15
        if x is not None:
16
            self.dim = len(x)
17
            self.x = x
18
19
    def f(self, x):
20
        diff = x - self.x
21
        return 0.5 * np.linalg.norm(diff) ** 2
22
23
24
class Rosen(object):
25
    def __init__(self):
26
        self.dim = 10
27
28
    def f(self, x):
29
        import scipy.optimize
30
        return scipy.optimize.rosen(x)
31
32
    def bounds(self):
33
        return [(-0.5, 0.5)] * self.dim
34
35
    def on_eval_f(self, solver):
36
        counter = solver.eval_counter
37
        value = solver.last_f
38
        # if counter % 10 == 0:
39
        #     solver.plot_convergence()
40
41
42
class ConstrainedQuadProb(object):
43
    def __init__(self, x=None):
44
        self.dim = 4
45
        self.x = x if x is not None else np.array([0.5, 0.7, -0.5, 1.0])
46
47
    def f(self, x):
48
        diff = x - self.x
49
        return 0.5 * np.linalg.norm(diff) ** 2
50
51
    def num_eq_constraints(self):
52
        return 2
53
54
    def c_eq(self, x, index):
55
        if index == 0:
56
            return x[0] + x[1] - 1.0
57
        elif index == 1:
58
            return x[1] + x[2] - 1.0
59
60
    def c_eq_jac(self, x, index):
61
        """ (optional) """
62
        if index == 0:
63
            return np.array([1.0, 1.0, 0.0, 0.0])
64
        elif index == 1:
65
            return np.array([0.0, 1.0, 1.0, 0.0])
66
67
    def num_ineq_constraints(self):
68
        return 1
69
70
    def c_ineq(self, x, index):
71
        if index == 0:
72
            return x[3] - 1.2
73
74
75
if __name__ == '__main__':
76
    print('test optimization problems')
77
    prob = QuadProb(np.array([0.5, -0.3, 0.9]))
78
    print('prob.dim = %d' % (prob.dim))
79
    from optimization.solver_cma import CMASolver as Solver
80
    solver = Solver(prob)
81
    res = solver.solve()
82
    print(res)