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 202 203 204 205 206 207 208 209 210 211
|
clear all close all ## 定義 state human_states_str = {'no_trust', 'trust'}; Label.BOTTLE = 1; Label.GLASS = 2; Label.ON_TABLE = 1; Label.PICKED_BY_ROBOT = 2; Label.PICKED_BY_HUMAN = 3; Label.NO_TRUST = 1; Label.TRUST = 2; objstate_str = {'on_table','picked_robot','picked_human'}; ractions_str = {'pick_bottle','pick_glass'}; ## 定義一些基本的轉換機率(為了計算 transition matrix)
PROB_TRUST_INCREASE = [0.8 0.9; 0.0 0.0];
PROB_TRUST_INTERVENE = [0.3 0.8; 0.1 0.2]; counter = 1; for ii = 1:3 for jj = 1:3 world_states(counter,:) = [ii,jj]; counter = counter + 1; end end num_human_states = length(human_states_str); num_world_states = counter -1; num_trust_states = 2; num_states = num_world_states*num_trust_states; num_ractions = length(ractions_str); ## 計算 transition matrix Trans = zeros(num_states,num_ractions,num_states); for sh = 1:num_human_states for sw = 1:num_world_states for ra = 1:num_ractions world_state = world_states(sw,:); ss = (sh-1)*num_world_states + sw; new_world_state = world_state; new_world_state(ra) =Label.PICKED_BY_ROBOT; nsw = findWorldState(new_world_state,world_states); nsh = sh; nss = (nsh-1)*num_world_states + nsw; Trans(ss,ra,nss) = (1-PROB_TRUST_INTERVENE(sh,ra))*(1-PROB_TRUST_INCREASE(sh,ra)); if sh == 1 nsh = sh+1; nss = (nsh-1)*num_world_states + nsw; Trans(ss,ra,nss) = (1-PROB_TRUST_INTERVENE(sh,ra))*PROB_TRUST_INCREASE(sh,ra); end new_world_state = world_state; new_world_state(ra) =Label.PICKED_BY_HUMAN; nsw = findWorldState(new_world_state,world_states); nsh = sh; nss = (nsh-1)*num_world_states + nsw; Trans(ss,ra,nss) = PROB_TRUST_INTERVENE(sh,ra); end end end disp(' ') disp('Transition Matrix') for ss = 1:num_states for ra = 1:num_ractions sh = floor(ss/(num_world_states+1)) + 1; sw = ss - (sh-1)*num_world_states; nIndices = find(Trans(ss,ra,:)>0); if (world_states(sw,ra) == Label.ON_TABLE) str = strcat('if~', human_states_str{sh} , ' and bottle is~' , objstate_str(world_states(sw,1)) , ' and glass is~' , objstate_str(world_states(sw,2)) , ' and robot does~' , ractions_str{ra},':'); disp(str); for nn = 1:length(nIndices) nss = nIndices(nn); nsh = floor(nss/(num_world_states+1)) + 1; nsw = nss - (nsh-1)*num_world_states; str = strcat('then the prob of ~', human_states_str{nsh} , ' and bottle is~' , objstate_str(world_states(nsw,1)) , ' and glass is~' , objstate_str(world_states(nsw,2)),':',num2str(Trans(ss,ra,nss))); disp(str); end end end end ## 定義 reward
Rew = zeros(num_states,num_ractions); for ss = 1:num_states for ra = 1:num_ractions sh = floor(ss/(num_world_states+1)) + 1; sw = ss - (sh-1)*num_world_states; if (world_states(sw,Label.GLASS)==Label.PICKED_BY_ROBOT)&& (world_states(sw,Label.BOTTLE)==Label.ON_TABLE) Rew(ss,ra) = 5; end if (world_states(sw,1)==Label.ON_TABLE) || (world_states(sw,2)==Label.ON_TABLE) if world_states(sw,ra)~= Label.ON_TABLE Rew(ss,ra) = -1000; end end end end
goal = findWorldState([Label.PICKED_BY_ROBOT, Label.PICKED_BY_ROBOT],world_states); for ra = 1:num_ractions for sh = 1:2 ss = (sh-1)*num_world_states + goal; Rew(ss,ra) = 10; end end
disp(' ') disp('reward function') for ss = 1:num_states for ra = 1:num_ractions sh = floor(ss/(num_world_states+1)) + 1; sw = ss - (sh-1)*num_world_states; str = strcat('if~', human_states_str{sh} , ' and bottle is~' , objstate_str(world_states(sw,1)) , ' and glass is~' , objstate_str(world_states(sw,2)) , 'and robot action is~',ractions_str{ra}, ' then reward is: ',num2str(Rew(ss,ra))); disp(str); end end ## 用 value iteration 算出 optimal policy
T = 3; V = zeros(num_states,1); policy = zeros(num_states,T); new_V = zeros(num_states,1); Q = zeros(num_states, num_ractions); for tt = T:-1:1 for ss = 1:num_states if ss == 12 debug = 1; end sh = floor(ss/(num_world_states+1)) + 1; sw = ss - (sh-1)*num_world_states; if ((world_states(sw,1)~=Label.ON_TABLE) && (world_states(sw,2)~=Label.ON_TABLE)) new_V(ss) = Rew(ss,1); policy(ss,tt) = 1; continue; end maxV = -1e6; maxIndx = -1; for ra = 1:num_ractions res = Rew(ss,ra); for nss = 1:num_states res = res + Trans(ss,ra,nss)*V(nss); end Q(ss,ra) = res; if res > maxV maxV = res; maxIndx = ra; end end new_V(ss) = maxV; policy(ss,tt) = maxIndx; end V = new_V; end disp(' ') disp('policy')
for tt = 1 tt for ss = 1:num_states sh = floor(ss/(num_world_states+1)) + 1; sw = ss - (sh-1)*num_world_states; if ((world_states(sw,1) == Label.ON_TABLE)||(world_states(sw,2) == Label.ON_TABLE)) str = strcat('if~', human_states_str{sh} , ' and bottle is~' , objstate_str(world_states(sw,1)) , ' and glass is~' , objstate_str(world_states(sw,2)) , 'then robot does: ',ractions_str{policy(ss,tt)}); disp(str); end end end
|
留言討論