-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmaes_query_generator.py
46 lines (38 loc) · 1.66 KB
/
cmaes_query_generator.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
import numpy as np
from cmaes import CMA
class CMAESGenerator:
def __init__(self, dim, limits, population_size=10, sigma=1.0):
self.optimizer = CMA(mean=np.zeros(dim), sigma=sigma, population_size=population_size)
self.sigma = sigma
self.dimension = dim
self.population_size = population_size
self.limits = limits
def get_query(self, number_items, reward_parameterization=None, input_model=None):
'''
'''
queries = []
for _ in range(number_items):
x = self.optimizer.ask()
x = np.clip(x, [lim[0] for lim in self.limits], [lim[1] for lim in self.limits])
queries.append(x)
return np.array(queries)
def tell(self, solutions, rankings):
'''
allows the CMA-ES optimizer to learn from the rankings of the solutions.
Args:
solutions (list): a list of features that correspond to the items
presented to the user.
rankings (list): the index that the user ranked that particular item
at. For example, if the user ranked the first item in `solutions`
as the worst, the second item as the best, and the third
item as the second best, then `rankings` would be [2, 0, 1].
'''
answer = []
for i, solution in enumerate(solutions):
answer.append((solution, -rankings[i]))
self.optimizer.tell(answer)
def reset(self):
'''
Resets the optimizer to its initial state.
'''
self.optimizer = CMA(mean=np.zeros(self.dimension), sigma=self.sigma, population_size=self.population_size)