-
Notifications
You must be signed in to change notification settings - Fork 4
/
optimizers.py
201 lines (157 loc) · 8.67 KB
/
optimizers.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import scipy.stats as stats
import torch
from config.utils import torch_truncated_normal
import collections
class Optimizer:
def __init__(self, *args, **kwargs):
pass
def setup(self, cost_function):
raise NotImplementedError("Must be implemented in subclass.")
def reset(self):
raise NotImplementedError("Must be implemented in subclass.")
def obtain_solution(self, *args, **kwargs):
raise NotImplementedError("Must be implemented in subclass.")
class CEMOptimizer(Optimizer):
def __init__(self, sol_dim, max_iters, popsize, num_elites, cost_function,
upper_bound=None, lower_bound=None, epsilon=0.001, alpha=0.25):
"""Creates an instance of this class.
Arguments:
sol_dim (int): The dimensionality of the problem space
max_iters (int): The maximum number of iterations to perform during optimization
popsize (int): The number of candidate solutions to be sampled at every iteration
num_elites (int): The number of top solutions that will be used to obtain the distribution
at the next iteration.
upper_bound (np.array): An array of upper bounds
lower_bound (np.array): An array of lower bounds
epsilon (float): A minimum variance. If the maximum variance drops below epsilon, optimization is
stopped.
alpha (float): Controls how much of the previous mean and variance is used for the next iteration.
next_mean = alpha * old_mean + (1 - alpha) * elite_mean, and similarly for variance.
"""
super().__init__()
self.sol_dim, self.max_iters, self.popsize, self.num_elites = sol_dim, max_iters, popsize, num_elites
self.ub, self.lb = upper_bound, lower_bound
self.epsilon, self.alpha = epsilon, alpha
self.cost_function = cost_function
if num_elites > popsize:
raise ValueError("Number of elites must be at most the population size.")
def reset(self):
pass
def obtain_solution(self, init_mean, init_var):
"""Optimizes the cost function using the provided initial candidate distribution
Arguments:
init_mean (np.ndarray): The mean of the initial candidate distribution.
init_var (np.ndarray): The variance of the initial candidate distribution.
"""
mean, var, t = init_mean, init_var, 0
X = stats.truncnorm(-2, 2, loc=np.zeros_like(mean), scale=np.ones_like(var))
while (t < self.max_iters) and np.max(var) > self.epsilon:
lb_dist, ub_dist = mean - self.lb, self.ub - mean
constrained_var = np.minimum(np.minimum(np.square(lb_dist / 2), np.square(ub_dist / 2)), var)
samples = X.rvs(size=[self.popsize, self.sol_dim]) * np.sqrt(constrained_var) + mean
samples = samples.astype(np.float32)
costs = self.cost_function(samples)
elites = samples[np.argsort(costs)][:self.num_elites]
new_mean = np.mean(elites, axis=0)
new_var = np.var(elites, axis=0)
mean = self.alpha * mean + (1 - self.alpha) * new_mean
var = self.alpha * var + (1 - self.alpha) * new_var
t += 1
return mean
class DiscreteRandomOptimizer(Optimizer):
def __init__(self, sol_dim, max_iters, popsize, num_elites, cost_function,
upper_bound=None, lower_bound=None, epsilon=0.001, alpha=0.25):
"""Creates an instance of this class.
Arguments:
sol_dim (int): The dimensionality of the problem space
max_iters (int): The maximum number of iterations to perform during optimization
popsize (int): The number of candidate solutions to be sampled at every iteration
num_elites (int): The number of top solutions that will be used to obtain the distribution
at the next iteration.
upper_bound (np.array): An array of upper bounds
lower_bound (np.array): An array of lower bounds
epsilon (float): A minimum variance. If the maximum variance drops below epsilon, optimization is
stopped.
alpha (float): Controls how much of the previous mean and variance is used for the next iteration.
next_mean = alpha * old_mean + (1 - alpha) * elite_mean, and similarly for variance.
"""
super().__init__()
self.sol_dim, self.max_iters, self.popsize, self.num_elites = sol_dim, max_iters, popsize, num_elites
self.ub, self.lb = upper_bound, lower_bound
self.epsilon, self.alpha = epsilon, alpha
self.cost_function = cost_function
if num_elites > popsize:
raise ValueError("Number of elites must be at most the population size.")
def reset(self):
pass
def obtain_solution(self, init_mean, possible_actions):
"""Optimizes the cost function using the provided initial candidate distribution
Arguments:
init_mean: The starting mean, this is unused.
possible_actions (np.ndarray): The possible actions this discrete env allows
"""
samples = np.random.choice(np.arange(possible_actions.shape[-1]),
size=[self.popsize * (self.sol_dim // possible_actions.shape[-1])],
replace=True)
samples = possible_actions[samples]
samples = samples.astype(np.float32).reshape(self.popsize, self.sol_dim)
costs = self.cost_function(samples)
elite = samples[np.argsort(costs)][:1]
return elite.flatten()
class DiscreteCEMOptimizer(Optimizer):
def __init__(self, sol_dim, max_iters, popsize, num_elites, cost_function,
upper_bound=None, lower_bound=None, epsilon=0.001, alpha=0.25):
"""Creates an instance of this class.
Arguments:
sol_dim (int): The dimensionality of the problem space
max_iters (int): The maximum number of iterations to perform during optimization
popsize (int): The number of candidate solutions to be sampled at every iteration
num_elites (int): The number of top solutions that will be used to obtain the distribution
at the next iteration.
upper_bound (np.array): An array of upper bounds
lower_bound (np.array): An array of lower bounds
epsilon (float): A minimum variance. If the maximum variance drops below epsilon, optimization is
stopped.
alpha (float): Controls how much of the previous mean and variance is used for the next iteration.
next_mean = alpha * old_mean + (1 - alpha) * elite_mean, and similarly for variance.
"""
super().__init__()
self.sol_dim, self.max_iters, self.popsize, self.num_elites = sol_dim, max_iters, popsize, num_elites
self.ub, self.lb = upper_bound, lower_bound
self.epsilon, self.alpha = epsilon, alpha
self.cost_function = cost_function
if num_elites > popsize:
raise ValueError("Number of elites must be at most the population size.")
self.one_hot = None
self.discrete_actions = None
def reset(self):
pass
def sample_from_categorical(self, probs, possible_actions, num_samples):
probs += 0.001 # prevent torch from thinking anything is < 0
samples = torch.distributions.categorical.Categorical(probs=torch.from_numpy(probs)).sample(
[num_samples])
samples = possible_actions[samples.numpy()].astype(np.float32)
return samples
# performing one hot encoding
def obtain_solution(self, init_mean, possible_actions):
"""Optimizes the cost function using the provided initial candidate distribution
Arguments:
init_mean (np.ndarray): The mean of the initial candidate distribution.
possible_actions (np.ndarray): The possible actions this discrete env allows
"""
mean, t = init_mean, 0
while t < self.max_iters:
samples = self.sample_from_categorical(mean, possible_actions, self.popsize)
samples = samples.reshape(self.popsize, self.sol_dim)
costs = self.cost_function(samples)
elites = samples[np.argsort(costs)][:self.num_elites].reshape(self.num_elites, *mean.shape)
new_mean = np.mean(elites, axis=0)
mean = self.alpha * mean + (1 - self.alpha) * new_mean
normalization_factor = mean.sum(axis=1)
mean = mean / normalization_factor[:, np.newaxis]
t += 1
return mean