forked from bu-air-lab/KB_augment_dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomdp_generator.py
executable file
·863 lines (658 loc) · 33.1 KB
/
pomdp_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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import numpy as np
import time
import conf
import subprocess
import os.path
class State(object):
def __init__(self, num_task, num_patient, num_recipient):
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
class StateRegular(State):
def __init__(self, task, patient, recipient, num_task, num_patient, num_recipient, index):
State.__init__(self, num_task, num_patient, num_recipient)
self.task = task
self.patient = patient
self.recipient = recipient
self.index = index
def getName(self):
return 't' + str(self.task) + '_p' + str(self.patient) + '_r' + \
str(self.recipient)
def getIndex(self):
return self.index
def gettaskIndex(self):
return self.task
def getpatientIndex(self):
return self.patient
def getrecipientIndex(self):
return self.recipient
def isTerminal(self):
return False
class StateTerminal(State):
def __init__(self, num_task, num_patient, num_recipient, index):
State.__init__(self, num_task, num_patient, num_recipient)
self.index = index
def getName(self):
return 'terminal'
def getIndex(self):
return self.index
def isTerminal(self):
return True
class Action(object):
# qd_type: type of this action: ask or deliver?
def __init__(self, qd_type):
assert qd_type in ['ask', 'executeTask']
self.qd_type = qd_type
class ActionAsk(Action):
def __init__(self, q_type):
assert q_type in ['wh', 'polar']
Action.__init__(self, 'ask')
self.q_type = q_type
class ActionAskWh(ActionAsk):
def __init__(self, var):
assert var in ['task', 'patient', 'recipient']
ActionAsk.__init__(self, 'wh')
self.var = var
def getName(self):
if self.var == 'task':
return 'ask_t'
elif self.var == 'patient':
return 'ask_p'
elif self.var == 'recipient':
return 'ask_r'
def getIndex(self):
if self.var == 'task':
return 0
elif self.var == 'patient':
return 1
elif self.var == 'recipient':
return 2
class ActionAskPolar(ActionAsk):
def __init__(self, var, num_task, num_patient, num_recipient):
ActionAsk.__init__(self, 'polar')
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
assert var in ['task', 'patient', 'recipient']
self.var = var
class ActionAskPolartask(ActionAskPolar):
def __init__(self, task, num_task, num_patient, num_recipient):
assert task < num_task
ActionAskPolar.__init__(self, 'task', num_task, num_patient, num_recipient)
self.task = task
def gettaskIndex(self):
return self.task
def getIndex(self):
return 3 + self.task
def getName(self):
return 'confirm_t' + str(self.task)
class ActionAskPolarpatient(ActionAskPolar):
def __init__(self, patient, num_task, num_patient, num_recipient):
assert patient < num_patient
ActionAskPolar.__init__(self, 'patient', num_task, num_patient, num_recipient)
self.patient = patient
def getpatientIndex(self):
return self.patient
def getIndex(self):
return 3 + self.num_task + self.patient
def getName(self):
return 'confirm_p' + str(self.patient)
class ActionAskPolarrecipient(ActionAskPolar):
def __init__(self, recipient, num_task, num_patient, num_recipient):
assert recipient < num_recipient
ActionAskPolar.__init__(self, 'recipient', num_task, num_patient, num_recipient)
self.recipient = recipient
def getrecipientIndex(self):
return self.recipient
def getIndex(self):
return 3 + self.num_task + self.num_patient + self.recipient
def getName(self):
return 'confirm_r' + str(self.recipient)
class ActionDeliver(Action):
def __init__(self, task, patient, recipient, num_task, num_patient, num_recipient, index):
assert task < num_task
assert patient < num_patient
assert recipient < num_recipient
Action.__init__(self, 'executeTask')
self.task = task
self.patient = patient
self.recipient = recipient
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
self.index = 3 + self.num_task + self.num_patient + self.num_recipient + index
def gettaskIndex(self):
return self.task
def getpatientIndex(self):
return self.patient
def getrecipientIndex(self):
return self.recipient
def getIndex(self):
return self.index
def getName(self):
return 'go_t' + str(self.task) + '_p' + str(self.patient) + '_r' + \
str(self.recipient)
class Observation(object):
def __init__(self, qd_type):
assert qd_type in ['wh', 'polar', 'none']
self.qd_type = qd_type
class ObservationWh(Observation):
def __init__(self, var):
assert var in ['task', 'patient', 'recipient']
Observation.__init__(self, 'wh')
self.var = var
class ObservationWhtask(ObservationWh):
def __init__(self, task, num_task, num_patient, num_recipient):
assert task < num_task
ObservationWh.__init__(self, 'task')
self.task = task
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
def gettaskIndex(self):
return self.task
def getIndex(self):
return self.task
def getName(self):
return 't' + str(self.task)
class ObservationWhpatient(ObservationWh):
def __init__(self, patient, num_task, num_patient, num_recipient):
assert patient < num_patient
ObservationWh.__init__(self, 'patient')
self.patient = patient
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
def getpatientIndex(self):
return self.patient
def getIndex(self):
return self.num_task + self.patient
def getName(self):
return 'p' + str(self.patient)
class ObservationWhrecipient(ObservationWh):
def __init__(self, recipient, num_task, num_patient, num_recipient):
assert recipient < num_recipient
ObservationWh.__init__(self, 'recipient')
self.recipient = recipient
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
def getrecipientIndex(self):
return self.recipient
def getIndex(self):
return self.num_task + self.num_patient + self.recipient
def getName(self):
return 'r' + str(self.recipient)
class ObservationPolar(Observation):
def __init__(self, polar, num_task, num_patient, num_recipient):
assert polar in ['yes', 'no']
Observation.__init__(self, 'polar')
self.polar = polar
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
def getName(self):
if self.polar == 'yes':
return 'yes'
elif self.polar == 'no':
return 'no'
def getIndex(self):
if self.polar == 'yes':
return self.num_task + self.num_patient + self.num_recipient
elif self.polar == 'no':
return self.num_task + self.num_patient + self.num_recipient + 1
class ObservationNone(Observation):
def __init__(self, num_task, num_patient, num_recipient):
Observation.__init__(self, 'none')
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
def getName(self):
return 'none'
def getIndex(self):
return self.num_task + self.num_patient + self.num_recipient + 2
class PomdpGenerator(object):
def __init__(self, num_task, num_patient, num_recipient, r_max, r_min, strategy, \
wh_cost, yesno_cost,pomdpfile,timeout=5,is_plus=False):
self.num_task = num_task
self.num_patient = num_patient
self.num_recipient = num_recipient
self.is_plus = is_plus
self.r_max = r_max
self.r_min = r_min
self.weight_t=np.eye(num_task, dtype=float)
self.weight_p=np.eye(num_patient, dtype=float)
self.weight_r=np.eye(num_recipient, dtype=float)
self.weight_t_bin = self.weight_t.astype(int)
self.weight_p_bin = self.weight_p.astype(int)
self.weight_r_bin = self.weight_r.astype(int)
# the larger, the more unreliable for the wh-questions.
self.magic_number = 0.3
self.polar_tp_rate = 0.8
self.polar_tn_rate = 0.8
#self.tablelist = conf.tablelist
self.tablelist = []
self.tlist(num_task,num_patient,num_recipient)
print "Tablelist is :"
print self.tablelist
self.state_set = []
self.action_set = []
self.observation_set = []
self.trans_mat = None
self.obs_mat = None
self.reward_mat = None
# compute the sets of states, actions, observations
self.state_set = self.computeStateSet(self.num_task, self.num_patient,
self.num_recipient)
self.action_set = self.computeActionSet(self.num_task, self.num_patient,
self.num_recipient)
self.observation_set = self.computeObservationSet(self.num_task,
self.num_patient, self.num_recipient)
# compute the functions of transition, observation
self.trans_mat = self.computeTransFunction(self.num_task,
self.num_patient, self.num_recipient)
self.obs_mat = self.computeObsFunction(self.num_task, self.num_patient,
self.num_recipient, self.magic_number, self.polar_tp_rate)
# compute two versions of reward function
reward_mat_float = self.computeRewardFunction(self.num_task,
self.num_patient, self.num_recipient, self.r_max, self.r_min, \
self.weight_t, self.weight_p, self.weight_r, wh_cost, yesno_cost)
reward_mat_bin = self.computeRewardFunction(self.num_task,
self.num_patient, self.num_recipient, self.r_max, self.r_min, \
self.weight_t_bin, self.weight_p_bin, self.weight_r_bin, wh_cost, yesno_cost)
# the idea is to keep the reward of fully correct deliveries and
# question-asking actions, while changing the other negative values
reward_mat_float_positive = reward_mat_float.clip(min=0)
reward_mat_float_negative = reward_mat_float.clip(max=0)
reward_mat_float_negative_deliveries = reward_mat_float_negative -\
reward_mat_float_negative.clip(min=min(wh_cost, yesno_cost))
reward_mat_float_negative_questions = reward_mat_float_negative - \
reward_mat_float_negative_deliveries
reward_mat_bin_positive = reward_mat_bin.clip(min=0)
reward_mat_bin_negative = reward_mat_bin.clip(max=0)
reward_mat_bin_negative_deliveries = reward_mat_bin_negative -\
reward_mat_bin_negative.clip(min=min(wh_cost, yesno_cost))
reward_mat_bin_negative_questions = reward_mat_bin_negative -\
reward_mat_bin_negative_deliveries
sum_float_negative_deliveries = np.sum(reward_mat_float_negative_deliveries)
sum_bin_negative_deliveries = np.sum(reward_mat_bin_negative_deliveries)
reweight_factor = sum_bin_negative_deliveries / sum_float_negative_deliveries
reward_mat_float = reward_mat_float_positive + \
reward_mat_float_negative_questions + \
reward_mat_float_negative_deliveries * reweight_factor
# writing to files
self.filename = pomdpfile
self.reward_mat = reward_mat_bin
self.writeToFile()
self.policyfile=strategy+'_new.policy'
self.reward_mat = reward_mat_float
#self.reward_mat = reward_mat_float_negative_deliveries
self.writeToFile()
print 'Training for '+ str(timeout)+' seconds'
pomdpsol_path = None
file_path = open('config/sarsop_path','r')
if file_path.mode == 'r':
pomdpsol_path = file_path.read()
else:
print "Error: could not open pomdp solver path file 'config/sarsop_path'"
exit(1)
if os.path.isfile(pomdpsol_path):
pomdpsol = pomdpsol_path
else:
print "Error: pomdpsol not installed..."
exit(1)
subprocess.check_output([pomdpsol, self.filename, \
'--timeout', str(timeout), '--output', self.policyfile])
print 'Finished training'
def tlist(self, num_task,num_patient, num_recipient):
for i in range(num_task):
for j in range(num_patient):
for k in range(num_recipient):
self.tablelist.append([i,j,k])
def computeTransFunction(self, num_task, num_patient, num_recipient):
num_state = len(self.state_set)
# as this problem is specific, this can be manually done
trans_mat = np.ones((len(self.action_set), len(self.state_set),
len(self.state_set)))
for action in self.action_set:
if action.qd_type == 'ask':
trans_mat[action.getIndex()] = np.eye(num_state, dtype=float)
elif action.qd_type == 'executeTask':
trans_mat[action.getIndex()] = np.zeros((num_state, num_state))
trans_mat[action.getIndex()][:, num_state-1] = 1.0
return trans_mat
# HERE WE INTRODUCE A NOVEL OBSERVATION MODEL
# true-positive rate = 1/(n^0.1), where n is the variable's range
def computeObsFunction(self, num_task, num_patient, num_recipient, magic_number,
polar_tp_rate):
num_action = len(self.action_set)
num_state = len(self.state_set)
num_obs = len(self.observation_set)
obs_mat = np.zeros((num_action, num_state, num_obs))
for action in self.action_set:
# no observation given 'terminal' state, no matter of the action
for state in self.state_set:
if state.isTerminal() == True:
obs_mat[action.getIndex()] = np.zeros((num_state, num_obs))
obs_mat[action.getIndex()][state.getIndex(), num_obs-1]=1.0
if action.qd_type == 'executeTask':
obs_mat[action.getIndex()] = np.zeros((num_state, num_obs))
obs_mat[action.getIndex()][:, num_obs-1] = 1.0
elif action.qd_type == 'ask':
if action.q_type == 'wh':
if action.var == 'task':
tp_rate = 1.0 / pow(num_task, magic_number)
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.qd_type == 'wh':
if observation.var == 'task':
if observation.gettaskIndex() == \
state.gettaskIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()] = \
tp_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()] = \
(1.0-tp_rate) / (num_task-1)
elif action.var == 'patient':
tp_rate = 1.0 / pow(num_patient, magic_number)
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.qd_type == 'wh':
if observation.var == 'patient':
if observation.getpatientIndex() == \
state.getpatientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= tp_rate
elif observation.getpatientIndex() == \
(num_patient - 1):
if self.is_plus:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= ((1.0-tp_rate) / (num_patient)) * 2
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_patient-1)
else:
if self.is_plus and (state.getpatientIndex() != (num_patient-1)):
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_patient)
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_patient-1)
elif action.var == 'recipient':
tp_rate = 1.0 / pow(num_recipient, magic_number)
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.qd_type == 'wh':
if observation.var == 'recipient':
if observation.getrecipientIndex() ==\
state.getrecipientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= tp_rate
elif observation.getrecipientIndex() == \
(num_recipient - 1):
if self.is_plus:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= ((1.0-tp_rate) / (num_recipient)) * 2
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_recipient-1)
else:
if self.is_plus and (state.getrecipientIndex() != (num_recipient-1)):
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_recipient)
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= (1.0-tp_rate) / (num_recipient-1)
elif action.q_type == 'polar':
if action.var == 'task':
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.getName() == 'yes':
if state.gettaskIndex() ==\
action.gettaskIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= self.polar_tp_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()] = \
1.0 - self.polar_tp_rate
elif observation.getName() == 'no':
if state.gettaskIndex() ==\
action.gettaskIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]= \
1.0 - self.polar_tn_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()] \
= self.polar_tn_rate
elif action.var == 'patient':
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.getName() == 'yes':
if state.getpatientIndex() == \
action.getpatientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= self.polar_tp_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]=\
1.0 - self.polar_tp_rate
elif observation.getName() == 'no':
if state.getpatientIndex() == \
action.getpatientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]= \
1.0 - self.polar_tn_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]=\
self.polar_tn_rate
elif action.var == 'recipient':
for state in self.state_set:
if state.isTerminal() == True:
continue
for observation in self.observation_set:
if observation.getName() == 'yes':
if state.getrecipientIndex() ==\
action.getrecipientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= self.polar_tp_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]=\
1.0 - self.polar_tp_rate
elif observation.getName() == 'no':
if state.getrecipientIndex() ==\
action.getrecipientIndex():
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]= \
1.0 - self.polar_tn_rate
else:
obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]\
= self.polar_tn_rate
return obs_mat
def computeRewardFunction(self, num_task, num_patient, num_recipient,
r_max, r_min, weight_t, weight_p, weight_r, wh_cost, yesno_cost):
reward_mat = np.zeros((len(self.action_set), len(self.state_set), ))
for action in self.action_set:
if action.qd_type == 'ask':
if action.q_type == 'wh':
reward_mat[action.getIndex()] = wh_cost
elif action.q_type == 'polar':
reward_mat[action.getIndex()] = yesno_cost
elif action.qd_type == 'executeTask':
for state in self.state_set:
if state.isTerminal() == False:
reward_mat[action.getIndex()][state.getIndex()] = \
self.executeTaskReward(r_max, r_min,
weight_t, weight_p, weight_r, action, state)
num_state = len(self.tablelist) + 1
reward_mat[action.getIndex()][num_state - 1] = 0.0
return reward_mat
def executeTaskReward(self, r_max, r_min, weight_t, weight_p, weight_r,
action, state):
if weight_t[action.gettaskIndex()][state.gettaskIndex()] == 1.0 and \
weight_p[action.getpatientIndex()][state.getpatientIndex()] == 1.0 and \
weight_r[action.getrecipientIndex()][state.getrecipientIndex()] == 1.0:
return r_max
else:
ret = r_min * (1.0 - \
weight_t[action.gettaskIndex()][state.gettaskIndex()] * \
weight_p[action.getpatientIndex()][state.getpatientIndex()] * \
weight_r[action.getrecipientIndex()][state.getrecipientIndex()])
return ret
def computeStateSet(self, num_task, num_patient, num_recipient):
ret = []
for t in range(num_task):
for p in range(num_patient):
for r in range(num_recipient):
for table in self.tablelist:
if t == table[0] and p == table[1] and r == table[2]:
ret.append(StateRegular(t, p, r, num_task, num_patient, num_recipient,self.tablelist.index(table)))
ret.append(StateTerminal(num_task, num_patient, num_recipient, len(self.tablelist)))
return ret
def computeActionSet(self, num_task, num_patient, num_recipient):
ret = []
ret.append(ActionAskWh('task'))
ret.append(ActionAskWh('patient'))
ret.append(ActionAskWh('recipient'))
for i in range(num_task):
ret.append(ActionAskPolartask(i, num_task, num_patient, num_recipient))
for c in range(num_patient):
ret.append(ActionAskPolarpatient(c, num_task, num_patient, num_recipient))
for y in range(num_recipient):
ret.append(ActionAskPolarrecipient(y, num_task, num_patient, num_recipient))
for i in range(num_task):
for c in range(num_patient):
for y in range(num_recipient):
for table in self.tablelist:
if i == table[0] and c == table[1] and y == table[2]:
ret.append(ActionDeliver(i, c, y, num_task, num_patient,
num_recipient, self.tablelist.index(table)))
return ret
def computeObservationSet(self, num_task, num_patient, num_recipient):
ret = []
for i in range(num_task):
ret.append(ObservationWhtask(i, num_task, num_patient, num_recipient))
for p in range(num_patient):
ret.append(ObservationWhpatient(p, num_task, num_patient, num_recipient))
for r in range(num_recipient):
ret.append(ObservationWhrecipient(r, num_task, num_patient, num_recipient))
ret.append(ObservationPolar('yes', num_task, num_patient, num_recipient))
ret.append(ObservationPolar('no', num_task, num_patient, num_recipient))
ret.append(ObservationNone(num_task, num_patient, num_recipient))
return ret
def writeToFile(self):
f = open(self.filename, 'w')
# first few lines
s = ''
s += 'discount : 0.999999\n\nvalues: reward\n\nstates: '
# section of states
for state in self.state_set:
s += state.getName() + ' '
# section of actions
s += '\n\nactions: '
for action in self.action_set:
s += action.getName() + ' '
# section of observations
s += '\n\nobservations: '
for observation in self.observation_set:
s += observation.getName() + ' '
# section of transition matrix
for action in self.action_set:
s += '\n\nT: ' + action.getName() + '\n'
for state_from in self.state_set:
for state_to in self.state_set:
prob = self.trans_mat[action.getIndex(), \
state_from.getIndex(), state_to.getIndex()]
s += str(prob) + ' '
s += '\n'
# section of observation matrix
for action in self.action_set:
s += '\nO: ' + action.getName() + '\n'
for state in self.state_set:
for observation in self.observation_set:
s += str(self.obs_mat[action.getIndex()]\
[state.getIndex()]\
[observation.getIndex()]) + ' '
s += '\n'
s += '\n'
# sectoin of reward matrix
for action in self.action_set:
for state in self.state_set:
s += 'R: ' + action.getName() + '\t\t: ' + state.getName() + \
'\t: *\t\t: * ' + \
str(self.reward_mat[action.getIndex()][state.getIndex()]) + '\n'
f.write(s)
f.close()
def main():
r_max = 50.0
r_min = -50.0
wh_cost = -1.5
yesno_cost = -1.0
for i in range(3,10):
num_task = 1
num_patient = i
num_recipient = i
# row corresponds to action, column to underlying state
# all
# strategy = str(num_task) + str(num_patient) + str(num_recipient)
# strategy = str(num_task) + str(num_patient) + str(num_recipient) + '_' + str(entry)
# strategy = str(num_task) + str(num_patient) + str(num_recipient) + '_' + str(entry1) + str(entry2)
strategy = str(num_task) + str(num_patient) + str(num_recipient)
pomdpfile=strategy+'_new.pomdp'
pg = PomdpGenerator(num_task, num_patient, num_recipient, r_max, r_min, strategy, \
wh_cost, yesno_cost,pomdpfile,timeout=20, is_plus=False )
if __name__ == '__main__':
main()