-
Notifications
You must be signed in to change notification settings - Fork 2
/
getQTableAction.m
76 lines (49 loc) · 1.95 KB
/
getQTableAction.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
function [ action_and_qvalue_indices ] = getQTableAction(G, ACTIONS, player, QTABLE, qTableSize, epsilon )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% QValVactor = zeros(40,1);
% State = table2array(G.Nodes);
% State_size = size(State);
% State = reshape(State,[State_size(1)*State_size(2),1]);
% epsilon = 0.3;
random_qvalue_reward_index = randi([1 3], 1);
action_qvalue_index = nan(length(ACTIONS),2);
action_and_qvalue_indices = [0,0];
% length(ACTIONS)
if (rand() >= epsilon)
for n = 1 : size(ACTIONS,1)
% for m = 1: size(QTABLE,1)
for m = 1: qTableSize
if(isempty(QTABLE{m,1} ))
break;
end
% save the Q-value of the find state-action pair
if (isequal(QTABLE{m,1},G) & isequal(QTABLE{m,2},[ACTIONS{n},ACTIONS{n,2}]) & QTABLE{m,3} == player)
action_qvalue_index(n,1) = m;
action_qvalue_index(n,2) = QTABLE{m,4}(random_qvalue_reward_index);
break;
end
end
end
if(~isempty(find(~isnan(action_qvalue_index(:,1)))))
chosen_action_index = datasample(find(action_qvalue_index(:,2) == max(action_qvalue_index(:,2))),1);
action_and_qvalue_indices = [chosen_action_index , action_qvalue_index(chosen_action_index,1)];
else
action_and_qvalue_indices = [randi(size(ACTIONS,1)), 0];
end
else
chosen_action_index = randi(size(ACTIONS,1));
chosen_qvalue_index = 0;
% for o = 1: size(QTABLE,1)
for o = 1: qTableSize
if(isempty(QTABLE{o,1} ))
break;
end
if (isequal(QTABLE{o,1},G) & isequal(QTABLE{o,2},[ACTIONS{chosen_action_index},ACTIONS{chosen_action_index,2}]) & QTABLE{o,3} == player)
chosen_qvalue_index = o;
break;
end
end
action_and_qvalue_indices = [chosen_action_index, chosen_qvalue_index];
end
end