-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.m
127 lines (107 loc) · 4.77 KB
/
test.m
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
%% test function
items = -3:0.05:3;
contexts = -3:0.05:3;
[X,Y] = meshgrid(items, contexts);
Z = feedback_function(X, Y)';
% plot test function
figure;
surf(X,Y,Z)
title('underlying reward function', 'FontSize', 16)
xlabel('items', 'FontSize', 14)
ylabel('contexts', 'FontSize', 14)
zlabel('reward', 'FontSize', 14)
%%
% hyperparameters
T = 100;
batch_size = 5;
feedback_delay_t = 100;
approx_t = 100;
delay_growth_ratio = 1.0/10;
% f handles
context_receiver_handle = @context_creator;
available_item_list_receiver_handle = @available_item_list_creator;
feedback_handle = @feedback_function;
% materniso is used to test Schaffer,
item_kernel = {@covMaterniso,3}; item_hyp = [log(.8); log(2.0)];
% item_kernel = {@covSEisoU}; item_hyp = 0;
mask = [1,0]; % binary mask excluding all but the 1st component
item_kernel_masked = {@covMask,{mask,item_kernel}};
context_kernel = {@covMaterniso,3}; context_hyp = [log(.8); log(2.0)];
% context_kernel = {@covSEisoU}; context_hyp = 0;
mask = [0,1]; % binary mask excluding all but the 2nd component
context_kernel_masked = {@covMask,{mask,context_kernel}};
% product of two seperate kernels
kernel ={@covProd,{item_kernel_masked, context_kernel_masked}};
GP_hyperparameters.cov = [item_hyp, context_hyp];
calculate_optimal_rewards = true;
tic
[item_choices, observations, oracle] = CGPRank(kernel, GP_hyperparameters,...
batch_size, T, context_receiver_handle, available_item_list_receiver_handle, ...
feedback_handle, calculate_optimal_rewards); %, feedback_delay_t, delay_growth_ratio, approx_t);
toc
%% plot regret
figure;
grouped_observations = sum(reshape(observations, batch_size, T),1)';
plot(cumsum(oracle))
hold on;
plot(cumsum(grouped_observations));
plot(cumsum(oracle)-cumsum(grouped_observations))
legend('Optimal', 'CGPRANK', 'Regret', 'FontSize', 14)
xlabel('rounds', 'FontSize', 14)
ylabel('reward', 'FontSize', 14)
title(sprintf('%s%d', 'Cumulative Reward of CGPRank, batchsize =', batch_size), 'FontSize', 16)
%% plot predictive mean
inference_points = [X(:), Y(:)];
[predictive_mean, predictive_var] = GPInference(GP_hyperparameters, kernel, item_choices, observations, inference_points);
% hold on;
figure;
surfc(X,Y,reshape(predictive_mean, length(items),length(contexts)))
hold on;
% surf(X,Y,reshape(predictive_mean + predictive_var, length(items),length(contexts)),'FaceColor', 'r', 'FaceAlpha', 0.2)
% legend('predictive mean', 'predictive mean + variance', 'Location', 'northwest')
title(sprintf('%s%d','predictive mean by GP at round T=', T), 'FontSize', 16)
xlabel('items', 'FontSize', 14)
ylabel('contexts', 'FontSize', 14)
zlabel('reward function mean', 'FontSize', 14)
%% Plot evolution over time to create cool gifs.
% inference_results_for_each_round(X, Y, GP_hyperparameters, kernel, item_choices, observations, batch_size, 40);
function inference_results_for_each_round(X, Y, GP_hyperparameters, kernel, item_choices, observations, batch_size, T)
inference_points = [X(:), Y(:)];
for i = 1:T
items_lower_bound = batch_size*(i-1) +1;
items_upper_bound = items_lower_bound + batch_size - 1;
new_items = item_choices(items_lower_bound:items_upper_bound,:);
new_results = observations(items_lower_bound:items_upper_bound);
[predictive_mean, predictive_var] = GPInference(GP_hyperparameters, kernel, item_choices(1:items_upper_bound,:), observations(1:items_upper_bound), inference_points);
fig = figure('visible','off');
surf(X,Y,reshape(predictive_mean, size(X,2), size(Y,2)))
hold on;
surf(X,Y,reshape(predictive_mean + predictive_var, size(X,2), size(Y,2)), 'EdgeColor', [0.6350 0.0780 0.1840], 'FaceAlpha', 0.3)
hold on;
plot3(new_items(:,1), new_items(:,2), new_results, 'kx', 'MarkerSize', 12, 'LineWidth', 2.);
title(sprintf('%s%d%s%0.2f','t = ', i, ' and context = ', new_items(1,2)), 'FontSize', 16)
xlabel('items', 'FontSize', 14)
ylabel('contexts', 'FontSize', 14)
zlabel('predictive results', 'FontSize', 14)
legend('predictive mean', 'predictive mean + var', 'observed item-context pairs', 'Location', 'northwest')
print(fig,sprintf('plots/%s%d.png','plot', i), '-dpng', '-r200');
end
end
%%
function reward = feedback_function(items, contexts)
X = items;
Y = contexts;
% SCHAFFER FUNCTION N. 2
reward = 0.5 + ((power(sin(X.^2 - Y.^2), 2) - 0.5) ./ ((1.0 + 0.001*(X.^2 + Y.^2)).^2));
%reward = sin(X) + cos(Y);
noise = randn(size(reward))*0.01;
reward = (reward + noise)';
end
function item_list = available_item_list_creator(t)
items = -3:0.05:3;
item_list = datasample(items, int8(length(items)/4), 'Replace', false);
end
function context = context_creator(t)
contexts = -3:0.05:3;
context = datasample(contexts, 1);
end