|
a |
|
b/cmaes/solver_cma.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 |
# adapted by Seungmoon Song <seungmoon.song@gmail.com> |
|
|
8 |
|
|
|
9 |
from __future__ import division # '/' always means non-truncating division |
|
|
10 |
from cmaes.solver import Solver |
|
|
11 |
import numpy as np |
|
|
12 |
import cma |
|
|
13 |
import scipy.optimize |
|
|
14 |
import time |
|
|
15 |
from datetime import datetime |
|
|
16 |
import sys |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
class CMASolver(Solver): |
|
|
20 |
def __init__(self, prob): |
|
|
21 |
Solver.__init__(self, prob) |
|
|
22 |
opts = cma.CMAOptions() |
|
|
23 |
# for k, v in opts.iteritems(): |
|
|
24 |
# print k, v |
|
|
25 |
# exit(0) |
|
|
26 |
self.p_dir = 'optim_data/cma/' |
|
|
27 |
opts.set('verb_disp', 1) |
|
|
28 |
opts.set('popsize', 8) |
|
|
29 |
opts.set('verb_filenameprefix', self.p_dir) |
|
|
30 |
opts.set('maxiter', 2000) |
|
|
31 |
self.options = opts |
|
|
32 |
self.cen = None |
|
|
33 |
self.rng = None |
|
|
34 |
|
|
|
35 |
def set_verbose(self, verbose): |
|
|
36 |
self.verbose = verbose |
|
|
37 |
if verbose: |
|
|
38 |
self.options['verb_disp'] = 1 |
|
|
39 |
else: |
|
|
40 |
self.options['verb_disp'] = 0 |
|
|
41 |
|
|
|
42 |
def create_directory(self): |
|
|
43 |
verbose = (self.options['verb_disp'] > 0) |
|
|
44 |
import os |
|
|
45 |
path = self.p_dir |
|
|
46 |
if verbose: |
|
|
47 |
print('cma path = ', path) |
|
|
48 |
|
|
|
49 |
if not os.path.exists(path): |
|
|
50 |
if verbose: |
|
|
51 |
print('CMA-ES: create directory [%s]' % path) |
|
|
52 |
os.makedirs(path) |
|
|
53 |
|
|
|
54 |
def eval_f(self, y): |
|
|
55 |
x = self.unnormalize(y) |
|
|
56 |
ret = super(CMASolver, self).eval_f(x) |
|
|
57 |
|
|
|
58 |
# for i in range(self.prob.num_eq_constraints()): |
|
|
59 |
# ret_eq_i = self.prob.c_eq(x, i) |
|
|
60 |
# # ret += 100.0 * (ret_eq_i ** 2) |
|
|
61 |
# ret += 10.0 * (ret_eq_i) # Assume the quadratic form |
|
|
62 |
# for i in range(self.prob.num_ineq_constraints()): |
|
|
63 |
# ret_ineq_i = self.prob.c_ineq(x, i) |
|
|
64 |
# if ret_ineq_i < 0: |
|
|
65 |
# ret += 100.0 * (ret_ineq_i ** 2) |
|
|
66 |
return ret |
|
|
67 |
|
|
|
68 |
def clip(self, x): |
|
|
69 |
if self.rng is None: |
|
|
70 |
return x |
|
|
71 |
return np.clip(x, self.cen-self.rng, self.cen+self.rng) |
|
|
72 |
|
|
|
73 |
# normalize between [-1, 1] |
|
|
74 |
def normalize(self, x): |
|
|
75 |
if self.rng is None: |
|
|
76 |
return x |
|
|
77 |
return (x - self.cen) / self.rng |
|
|
78 |
|
|
|
79 |
def unnormalize(self, y): |
|
|
80 |
if self.rng is None: |
|
|
81 |
return y |
|
|
82 |
x = self.cen + y * self.rng |
|
|
83 |
return x |
|
|
84 |
|
|
|
85 |
def solve(self, x0=None, sigma=1.0): |
|
|
86 |
verbose = (self.options['verb_disp'] > 0) |
|
|
87 |
begin = time.time() |
|
|
88 |
if verbose: |
|
|
89 |
print('Optimization method = CMA-ES') |
|
|
90 |
if x0 is None: |
|
|
91 |
if verbose: |
|
|
92 |
print('Optimization: set x0 as zeros') |
|
|
93 |
if self.cen is not None: |
|
|
94 |
x0 = self.cen |
|
|
95 |
else: |
|
|
96 |
x0 = np.zeros(self.prob.dim) |
|
|
97 |
self.create_directory() |
|
|
98 |
if verbose: |
|
|
99 |
print('CMA-ES: cen = ', self.cen) |
|
|
100 |
print('CMA-ES: rng = ', self.rng) |
|
|
101 |
print('Optimization begins at ', str(datetime.now())) |
|
|
102 |
#print('normalized_center = ', self.normalize(x0)) |
|
|
103 |
# for k, v in self.options.iteritems(): |
|
|
104 |
# print(k, '\t', v) |
|
|
105 |
|
|
|
106 |
res = cma.fmin(self.eval_f, |
|
|
107 |
self.normalize(x0), |
|
|
108 |
sigma, |
|
|
109 |
options=self.options) |
|
|
110 |
if verbose: |
|
|
111 |
print('Optimization ends at ', str(datetime.now())) |
|
|
112 |
print('Total times = %.2fs' % (time.time() - begin)) |
|
|
113 |
|
|
|
114 |
ret = scipy.optimize.OptimizeResult() |
|
|
115 |
ret['y'] = res[0] |
|
|
116 |
ret['x'] = self.unnormalize(res[0]) |
|
|
117 |
ret['fun'] = res[1] |
|
|
118 |
# assert(np.allclose(res[1], self.prob.f(ret['x']))) |
|
|
119 |
ret['nfev'] = self.eval_counter |
|
|
120 |
# ret['jac'] = self.eval_g(ret['x']) |
|
|
121 |
ret['message'] = 'Optimization terminated successfully.' |
|
|
122 |
ret['status'] = 0 |
|
|
123 |
ret['success'] = True |
|
|
124 |
return ret |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
class CMASolverPar(CMASolver): |
|
|
128 |
def solve(self, x0=None, sigma=1.0): |
|
|
129 |
verbose = (self.options['verb_disp'] > 0) |
|
|
130 |
begin = time.time() |
|
|
131 |
if verbose: |
|
|
132 |
print('Optimization method = CMA-ES') |
|
|
133 |
if x0 is None: |
|
|
134 |
if verbose: |
|
|
135 |
print('Optimization: set x0 as zeros') |
|
|
136 |
if self.cen is not None: |
|
|
137 |
x0 = self.cen |
|
|
138 |
else: |
|
|
139 |
x0 = np.zeros(self.prob.dim) |
|
|
140 |
self.create_directory() |
|
|
141 |
if verbose: |
|
|
142 |
print('CMA-ES: cen = ', self.cen) |
|
|
143 |
print('CMA-ES: rng = ', self.rng) |
|
|
144 |
print('Optimization begins at ', str(datetime.now())) |
|
|
145 |
#print('normalized_center = ', self.normalize(x0)) |
|
|
146 |
# for k, v in self.options.iteritems(): |
|
|
147 |
# print(k, '\t', v) |
|
|
148 |
|
|
|
149 |
res = cma.fmin(None, |
|
|
150 |
self.normalize(x0), |
|
|
151 |
sigma, |
|
|
152 |
parallel_objective=self.eval_f, |
|
|
153 |
options=self.options) |
|
|
154 |
if verbose: |
|
|
155 |
print('Optimization ends at ', str(datetime.now())) |
|
|
156 |
print('Total times = %.2fs' % (time.time() - begin)) |
|
|
157 |
|
|
|
158 |
ret = scipy.optimize.OptimizeResult() |
|
|
159 |
ret['y'] = res[0] |
|
|
160 |
ret['x'] = self.unnormalize(res[0]) |
|
|
161 |
ret['fun'] = res[1] |
|
|
162 |
# assert(np.allclose(res[1], self.prob.f(ret['x']))) |
|
|
163 |
ret['nfev'] = self.eval_counter |
|
|
164 |
# ret['jac'] = self.eval_g(ret['x']) |
|
|
165 |
ret['message'] = 'Optimization terminated successfully.' |
|
|
166 |
ret['status'] = 0 |
|
|
167 |
ret['success'] = True |
|
|
168 |
return ret |
|
|
169 |
|
|
|
170 |
|
|
|
171 |
if __name__ == '__main__': |
|
|
172 |
import optimization.test_problems |
|
|
173 |
import numpy as np |
|
|
174 |
# prob = test_problems.QuadProb() |
|
|
175 |
prob = optimization.test_problems.Rosen() |
|
|
176 |
x0 = np.random.rand(prob.dim) - 0.5 |
|
|
177 |
|
|
|
178 |
solver = CMASolver(prob) |
|
|
179 |
res = solver.solve(x0) |
|
|
180 |
print(res) |