-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdpGUI.m
296 lines (252 loc) · 10.6 KB
/
mdpGUI.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
classdef mdpGUI < handle
% mdpGUI Compute the optimal multi-modality cancer treatment policy using
% backward induction for stationary transition probabilities.
properties
mdp % mdp object
pars % gui parameters
func % reward function parameters
end
methods
% Initialize mdp parameters and create GUI.
function gui = mdpGUI(pars)
% initialize mdp variables
if exist('pars','var')
gui.mdp = mdpMain(pars);
else
gui.mdp = mdpMain();
end
% set GUI parameters
gui.pars = gui.setPars();
% set function parameters
gui.func = gui.setFunc();
% create and then hide the GUI as it is begin constructed
f = figure('Visible','off','Position',gui.pars.f);
% add labels
background = axes('Position',[0 0 1 1],'Color','None');
set(background,...
'XColor',get(gcf,'Color'),...
'YColor',get(gcf,'Color'));
text(125,200,'State Transition Probabilities','Units','Pixels');
text(460,200,'Reward Functions','Units','Pixels');
text(396,150,'r_t = f(\phi; ) + g(\tau; )','Units','Pixels','FontSize',15);
text(380,110,'r_{T+1} = f(\phi; ) + g(\tau; )','Units','Pixels','FontSize',15);
% create transition probability input tables
mLabels = cell(1,gui.mdp.numActions);
mLabels2 = cell(1,2);
pTables = cell(2,gui.mdp.numActions);
for i = 1:gui.mdp.numActions
pos = gui.pars.m; pos(1) = pos(1)+(i-1)*gui.pars.column;
text(pos(1),pos(2),gui.pars.mStrings{i},'Units','Pixels')
pos = gui.pars.m1;
pos(1) = pos(1)+20;
pos(1) = pos(1)+(i-1)*gui.pars.column;
text(pos(1),pos(2)+10,gui.pars.mStrings2{1},'Units','Pixels');
pos = gui.pars.m2;
pos(1) = pos(1)+5;
pos(1) = pos(1)+(i-1)*gui.pars.column;
text(pos(1),pos(2)+10,gui.pars.mStrings2{2},'Units','Pixels');
pTables{1,i} = uitable('CreateFcn',{@gui.pCreate,1,i});
pTables{2,i} = uitable('CreateFcn',{@gui.pCreate,2,i});
end
% create inputs for epochs, discount factor, and reward functions
scaleInputs1 = cell(1,2);
scaleInputs2 = cell(1,2);
expInputs1 = cell(1,2);
expInputs2 = cell(1,2);
for i = 1:2
scaleInputs1{i} = uicontrol('CreateFcn',{@gui.scaleCreate1,i});
expInputs1{i} = uicontrol('CreateFcn',{@gui.expCreate1,i});
scaleInputs2{i} = uicontrol('CreateFcn',{@gui.scaleCreate2,i});
expInputs2{i} = uicontrol('CreateFcn',{@gui.expCreate2,i});
end
% compute policy button
button = uicontrol('Callback',@gui.bCallback,...
'Position',gui.pars.b,...
'String','Compute policy',...
'Style','pushbutton');
% assign the GUI a name to appear in the window title
set(f,'Name','Multi-Modality MDP')
% move the GUI to the center of the screen
movegui(f,'center')
% make the GUI visible
set(f,'Visible','on');
end
% Set GUI parameters.
function pars = setPars(gui)
% width and height
s2W = 94; s2H = 39; % tumor transition input tables
s1W = 94; s1H = 39; % OAR transition input tables
mW = 20; mH = 20; % input table labels
m1W = s2W; m1H = 15;
m2W = s2W; m2H = 15;
bW = 100; bH = 50; % calc policy button
% spacing
pars.border = 25;
pars.row = 30;
pars.column = pars.border+s2W;
% bottom and left
s2B = pars.border; s2L = pars.border;
m2B = s2B+s2H; m2L = pars.border;
s1B = s2B+s2H+m2H+15; s1L = pars.border;
m1B = s1B+s1H; m1L = pars.border;
mB = s1B+s1H+2*m1H; mL = pars.border+s2W/2-10;
bB = s2B; bL = pars.border+gui.mdp.numActions*pars.column + 70;
% set alignment parameters
pars.f = [500 500 650 225];
pars.m = [mL mB mW mH];
pars.m1 = [m1L m1B m1W m1H];
pars.m2 = [m2L m2B m2W m2H];
pars.s1 = [s1L s1B s1W s1H];
pars.s2 = [s2L s2B s2W s2H];
pars.b = [bL bB bW bH];
% set labels and functions
pars.mStrings = {'M_1','M_2','M_3'};
pars.mStrings2 = {'Side Effect','Tumor Progression'};
end
% Set reward function parameters.
function func = setFunc(gui)
% side effect function parameters
func.c = {0,1/2}; % scale coefficient
func.p = {2,2}; % exponent
% tumor progression function parameters
func.d = {0,1/2}; % scale coefficient
func.q = {2,2}; % exponent
% set individual functions
func.f = @(c,p,S) 100*c*(gui.mdp.m(2)^p-S(2)^p)/gui.mdp.m(2)^p;
func.g = @(d,q,S) 100*d*(gui.mdp.m(3)^q-S(3)^q)/gui.mdp.m(3)^q;
% set combined reward functions
gui.mdp.r{1} = @(S) func.f(func.c{1},func.p{1},S)+...
func.g(func.d{1},func.q{1},S);
gui.mdp.r{2} = @(S) func.f(func.c{2},func.p{2},S)+...
func.g(func.d{2},func.q{2},S);
end
% Create input for side effect coefficients.
function scaleCreate1(gui,hObject,~,i)
% create input
if i == 1
y = 140;
else
y = 100;
end
set(hObject,...
'Callback',{@gui.cCallback,i},...
'Position',[425 y 25 25],...
'String',gui.func.c{i},...
'Style','edit');
end
% Create input for side effect exponents.
function expCreate1(gui,hObject,~,i)
% create input
if i == 1
y = 140;
else
y = 100;
end
set(hObject,...
'Callback',{@gui.ppCallback,i},...
'Position',[479 y 25 25],...
'String',gui.func.p{i},...
'Style','edit');
end
% Create input for tumor progression coefficients.
function scaleCreate2(gui,hObject,~,i)
% create input
if i == 1
y = 140;
else
y = 100;
end
set(hObject,...
'Callback',{@gui.dCallback,i},...
'Position',[530 y 25 25],...
'String',gui.func.d{i},...
'Style','edit');
end
% Create input for tumor progression exponents.
function expCreate2(gui,hObject,~,i)
% create input
if i == 1
y = 140;
else
y = 100;
end
set(hObject,...
'Callback',{@gui.qCallback,i},...
'Position',[586 y 25 25],...
'String',gui.func.q{i},...
'Style','edit');
end
% Create transition probability input table.
function pCreate(gui,hObject,~,i,j)
% set labels and position
if i == 1
pos = gui.pars.s1;
colNames = {'-1','=','+1'};
else
pos = gui.pars.s2;
colNames = {'-1','=','+1'};
end
pos(1) = pos(1)+(j-1)*gui.pars.column;
% create table
set(hObject,...
'CellEditCallback',{@gui.pCallback,i,j},...
'ColumnEditable',true,...
'ColumnName',colNames,...
'ColumnWidth',{30 30 30},...
'Data',gui.mdp.Pn{i,j},...
'Position',pos,...
'RowName',{});
end
% Update side-effect coefficients.
function cCallback(gui,hObject,~,i)
newC = str2double(get(hObject,'String'));
gui.func.c{i} = newC;
gui.mdp.r{i} = @(S) gui.func.f(gui.func.c{i},gui.func.p{i},S)+...
gui.func.g(gui.func.d{i},gui.func.q{i},S);
end
% Update side effect exponents.
function dCallback(gui,hObject,~,i)
newD = str2double(get(hObject,'String'));
gui.func.d{i} = newD;
gui.mdp.r{i} = @(S) gui.func.f(gui.func.c{i},gui.func.p{i},S)+...
gui.func.g(gui.func.d{i},gui.func.q{i},S);
end
% Update tumor progression coefficients.
function ppCallback(gui,hObject,~,i)
newP = str2double(get(hObject,'String'));
gui.func.p{i} = newP;
gui.mdp.r{i} = @(S) gui.func.f(gui.func.c{i},gui.func.p{i},S)+...
gui.func.g(gui.func.d{i},gui.func.q{i},S);
end
% Update tumor progression exponents.
function qCallback(gui,hObject,~,i)
newQ = str2double(get(hObject,'String'));
gui.func.q{i} = newQ;
gui.mdp.r{i} = @(S) gui.func.f(gui.func.c{i},gui.func.p{i},S)+...
gui.func.g(gui.func.d{i},gui.func.q{i},S);
end
% Update transition probabilities.
function pCallback(gui,hObject,eventdata,i,j)
% store values
newP = str2double(eventdata.EditData);
oldP = gui.mdp.Pn{i,j};
temp = oldP;
% replace and check
row = eventdata.Indices(1);
col = eventdata.Indices(2);
temp(row,col) = newP;
gui.mdp.Pn{i,j} = temp;
flag = gui.mdp.checkAssumptions();
% if invalid values
if flag || ~isfinite(newP)
gui.mdp.Pn{i,j} = oldP;
set(hObject,'Data',oldP);
end
end
% Compute optimal policy.
function bCallback(gui,~,~)
gui.mdp.calcPolicy();
gui.mdp.plotPolicy();
end
end
end