DQNPolicy.py 37.8 KB
Newer Older
Carel van Niekerk's avatar
Init  
Carel van Niekerk committed
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
###############################################################################
# PyDial: Multi-domain Statistical Spoken Dialogue System Software
###############################################################################
#
# Copyright 2015 - 2019
# Cambridge University Engineering Department Dialogue Systems Group
#
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################

'''
DQNPolicy.py - deep Q network policy
==================================================

Copyright CUED Dialogue Systems Group 2015 - 2017

.. seealso:: CUED Imports/Dependencies: 

    import :class:`Policy`
    import :class:`utils.ContextLogger`

.. warning::
        Documentation not done.


************************

'''

import pickle as pickle
import copy
import json
import numpy as np
import os
import random
import sys
import tensorflow as tf
import time

from policy.DRL import dqn as dqn
from policy.DRL import utils as drlutils
from policy import Policy
from policy import SummaryAction
import ontology.FlatOntologyManager as FlatOnt
import utils
from policy.DRL.replay_buffer import ReplayBuffer
from policy.DRL.replay_prioritised import ReplayPrioritised
from policy.Policy import TerminalAction, TerminalState
from curiosity import model_prediction_curiosity as mpc
from curiosity.curiosity_module import Curious
from policy.feudalRL.DIP_parametrisation import DIP_state
from utils import ContextLogger, DiaAct
from utils.Settings import config as cfg  # this does not work! TODO

# from model_prediction_curiosity import constants

# logger = utils.ContextLogger.getLogger('')
logger = ContextLogger.getLogger('')


# --- for flattening the belief --- # 
def flatten_belief(belief, domainUtil, merge=False):
    belief = belief.getDomainState(domainUtil.domainString)
    if isinstance(belief, TerminalState):
        if domainUtil.domainString == 'CamRestaurants':
            return [0] * 268
        elif domainUtil.domainString == 'CamHotels':
            return [0] * 111
        elif domainUtil.domainString == 'SFRestaurants':
            return [0] * 633
        elif domainUtil.domainString == 'SFHotels':
            return [0] * 438
        elif domainUtil.domainString == 'Laptops11':
            return [0] * 257
        elif domainUtil.domainString == 'TV':
            return [0] * 188

    policyfeatures = ['full', 'method', 'discourseAct', 'requested', \
                      'lastActionInformNone', 'offerHappened', 'inform_info']

    flat_belief = []
    for feat in policyfeatures:
        add_feature = []
        if feat == 'full':
            # for slot in self.sorted_slots:
            for slot in domainUtil.ontology['informable']:
                for value in domainUtil.ontology['informable'][slot]:  # + ['**NONE**']:
                    add_feature.append(belief['beliefs'][slot][value])

                # pfb30 11.03.2017
                try:
                    add_feature.append(belief['beliefs'][slot]['**NONE**'])
                except:
                    add_feature.append(0.)  # for NONE
                try:
                    add_feature.append(belief['beliefs'][slot]['dontcare'])
                except:
                    add_feature.append(0.)  # for dontcare

        elif feat == 'method':
            add_feature = [belief['beliefs']['method'][method] for method in domainUtil.ontology['method']]
        elif feat == 'discourseAct':
            add_feature = [belief['beliefs']['discourseAct'][discourseAct]
                           for discourseAct in domainUtil.ontology['discourseAct']]
        elif feat == 'requested':
            add_feature = [belief['beliefs']['requested'][slot] \
                           for slot in domainUtil.ontology['requestable']]
        elif feat == 'lastActionInformNone':
            add_feature.append(float(belief['features']['lastActionInformNone']))
        elif feat == 'offerHappened':
            add_feature.append(float(belief['features']['offerHappened']))
        elif feat == 'inform_info':
            add_feature += belief['features']['inform_info']
        else:
            logger.error('Invalid feature name in config: ' + feat)

        flat_belief += add_feature

    return flat_belief


class DQNPolicy(Policy.Policy):
    '''Derived from :class:`Policy`
    '''

    def __init__(self, in_policy_file, out_policy_file, domainString='CamRestaurants', is_training=False, action_names=None):
        super(DQNPolicy, self).__init__(domainString, is_training)

        tf.reset_default_graph()

        self.domainString = domainString
        self.domainUtil = FlatOnt.FlatDomainOntology(self.domainString)
        self.in_policy_file = in_policy_file
        self.out_policy_file = out_policy_file
        self.is_training = is_training
        self.accum_belief = []

        self.prev_state_check = None

        # parameter settings
        if 0:#cfg.has_option('dqnpolicy', 'n_in'): #ic304: this was giving me a weird error, disabled it until i can check it deeper
            self.n_in = cfg.getint('dqnpolicy', 'n_in')
        else:
            self.n_in = self.get_n_in(domainString)

        self.learning_rate = 0.001
        if utils.Settings.config.has_option('dqnpolicy', 'learning_rate'):
            self.learning_rate = utils.Settings.config.getfloat('dqnpolicy', 'learning_rate')

        self.tau = 0.001
        if utils.Settings.config.has_option('dqnpolicy', 'tau'):
            self.tau = utils.Settings.config.getfloat('dqnpolicy', 'tau')

        # self.randomseed = 1234 #TODO cfg import doesn't work anymore therfore i changed all the cfg to u.S.config.
        # if cfg.has_option('GENERAL', 'seed'):
        #     self.randomseed = cfg.getint('GENERAL', 'seed') #see same below, this is just kept as example to try

        self.randomseed = 1234
        if utils.Settings.config.has_option('GENERAL', 'seed'):
            self.randomseed = utils.Settings.config.getint('GENERAL', 'seed')

        self.gamma = 1.0
        if utils.Settings.config.has_option('dqnpolicy', 'gamma'):
            self.gamma = utils.Settings.config.getfloat('dqnpolicy', 'gamma')

        self.regularisation = 'l2'
        if utils.Settings.config.has_option('dqnpolicy', 'regularisation'):
            self.regularisation = utils.Settings.config.get('dqnpolicy', 'regulariser')

        self.exploration_type = 'e-greedy'  # Boltzman
        if utils.Settings.config.has_option('dqnpolicy', 'exploration_type'):
            self.exploration_type = utils.Settings.config.get('dqnpolicy', 'exploration_type')

        self.episodeNum = 1000
        if utils.Settings.config.has_option('dqnpolicy', 'episodeNum'):
            self.episodeNum = utils.Settings.config.getfloat('dqnpolicy', 'episodeNum')

        self.maxiter = 5000
        if utils.Settings.config.has_option('dqnpolicy', 'maxiter'):
            self.maxiter = utils.Settings.config.getfloat('dqnpolicy', 'maxiter')

        self.epsilon = 1
        if utils.Settings.config.has_option('dqnpolicy', 'epsilon'):
            self.epsilon = utils.Settings.config.getfloat('dqnpolicy', 'epsilon')

        self.curiosityreward = False
        if utils.Settings.config.has_option('eval', 'curiosityreward'):
            self.curiosityreward = utils.Settings.config.getboolean('eval', 'curiosityreward')


        if not self.curiosityreward:  # no eps-greedy exploration when curious expl. is used
            self.epsilon_start = 1
            if cfg.has_option('dqnpolicy', 'epsilon_start'):
                self.epsilon_start = cfg.getfloat('dqnpolicy', 'epsilon_start')
        else:
            self.epsilon_start = 0

        self.epsilon_end = 1
        if utils.Settings.config.has_option('dqnpolicy', 'epsilon_end'):
            self.epsilon_end = utils.Settings.config.getfloat('dqnpolicy', 'epsilon_end')

        self.save_step = 100
        if utils.Settings.config.has_option('policy', 'save_step'):
            self.save_step = utils.Settings.config.getint('policy', 'save_step')

        self.priorProbStart = 1.0
        if utils.Settings.config.has_option('dqnpolicy', 'prior_sample_prob_start'):
            self.priorProbStart = utils.Settings.config.getfloat('dqnpolicy', 'prior_sample_prob_start')

        self.priorProbEnd = 0.1
        if utils.Settings.config.has_option('dqnpolicy', 'prior_sample_prob_end'):
            self.priorProbEnd = utils.Settings.config.getfloat('dqnpolicy', 'prior_sample_prob_end')

        self.policyfeatures = []
        if utils.Settings.config.has_option('dqnpolicy', 'features'):
            logger.info('Features: ' + str(utils.Settings.config.get('dqnpolicy', 'features')))
            self.policyfeatures = json.loads(utils.Settings.config.get('dqnpolicy', 'features'))

        self.max_k = 5
        if utils.Settings.config.has_option('dqnpolicy', 'max_k'):
            self.max_k = utils.Settings.config.getint('dqnpolicy', 'max_k')

        self.learning_algorithm = 'drl'
        if utils.Settings.config.has_option('dqnpolicy', 'learning_algorithm'):
            self.learning_algorithm = utils.Settings.config.get('dqnpolicy', 'learning_algorithm')
            logger.info('Learning algorithm: ' + self.learning_algorithm)

        self.minibatch_size = 32
        if utils.Settings.config.has_option('dqnpolicy', 'minibatch_size'):
            self.minibatch_size = utils.Settings.config.getint('dqnpolicy', 'minibatch_size')

        self.capacity = 1000
        if utils.Settings.config.has_option('dqnpolicy', 'capacity'):
            self.capacity = utils.Settings.config.getint('dqnpolicy', 'capacity')

        self.replay_type = 'vanilla'
        if utils.Settings.config.has_option('dqnpolicy', 'replay_type'):
            self.replay_type = utils.Settings.config.get('dqnpolicy', 'replay_type')

        self.architecture = 'vanilla'
        if utils.Settings.config.has_option('dqnpolicy', 'architecture'):
            self.architecture = utils.Settings.config.get('dqnpolicy', 'architecture')
            if self.architecture == 'dip':
                self.architecture = 'dip2'

        self.q_update = 'single'
        if utils.Settings.config.has_option('dqnpolicy', 'q_update'):
            self.q_update = utils.Settings.config.get('dqnpolicy', 'q_update')

        self.h1_size = 130
        if utils.Settings.config.has_option('dqnpolicy', 'h1_size'):
            self.h1_size = utils.Settings.config.getint('dqnpolicy', 'h1_size')

        self.h2_size = 130
        if utils.Settings.config.has_option('dqnpolicy', 'h2_size'):
            self.h2_size = utils.Settings.config.getint('dqnpolicy', 'h2_size')

        self.training_frequency = 2
        if utils.Settings.config.has_option('dqnpolicy', 'training_frequency'):
            self.training_frequency = utils.Settings.config.getint('dqnpolicy', 'training_frequency')

        # domain specific parameter settings (overrides general policy parameter settings)
        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'n_in'):
            self.n_in = utils.Settings.config.getint('dqnpolicy_' + domainString, 'n_in')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'learning_rate'):
            self.learning_rate = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'learning_rate')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'tau'):
            self.tau = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'tau')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'gamma'):
            self.gamma = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'gamma')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'regularisation'):
            self.regularisation = utils.Settings.config.get('dqnpolicy_' + domainString, 'regulariser')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'exploration_type'):
            self.exploration_type = utils.Settings.config.get('dqnpolicy_' + domainString, 'exploration_type')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'episodeNum'):
            self.episodeNum = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'episodeNum')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'maxiter'):
            self.maxiter = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'maxiter')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'epsilon'):
            self.epsilon = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'epsilon')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'epsilon_start'):
            self.epsilon_start = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'epsilon_start')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'epsilon_end'):
            self.epsilon_end = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'epsilon_end')

        if utils.Settings.config.has_option('policy_' + domainString, 'save_step'):
            self.save_step = utils.Settings.config.getint('policy_' + domainString, 'save_step')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'prior_sample_prob_start'):
            self.priorProbStart = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'prior_sample_prob_start')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'prior_sample_prob_end'):
            self.priorProbEnd = utils.Settings.config.getfloat('dqnpolicy_' + domainString, 'prior_sample_prob_end')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'features'):
            logger.info('Features: ' + str(utils.Settings.config.get('dqnpolicy_' + domainString, 'features')))
            self.policyfeatures = json.loads(utils.Settings.config.get('dqnpolicy_' + domainString, 'features'))

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'max_k'):
            self.max_k = utils.Settings.config.getint('dqnpolicy_' + domainString, 'max_k')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'learning_algorithm'):
            self.learning_algorithm = utils.Settings.config.get('dqnpolicy_' + domainString, 'learning_algorithm')
            logger.info('Learning algorithm: ' + self.learning_algorithm)

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'minibatch_size'):
            self.minibatch_size = utils.Settings.config.getint('dqnpolicy_' + domainString, 'minibatch_size')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'capacity'):
            self.capacity = utils.Settings.config.getint('dqnpolicy_' + domainString, 'capacity')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'replay_type'):
            self.replay_type = utils.Settings.config.get('dqnpolicy_' + domainString, 'replay_type')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'architecture'):
            self.architecture = utils.Settings.config.get('dqnpolicy_' + domainString, 'architecture')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'q_update'):
            self.q_update = utils.Settings.config.get('dqnpolicy_' + domainString, 'q_update')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'h1_size'):
            self.h1_size = utils.Settings.config.getint('dqnpolicy_' + domainString, 'h1_size')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'h2_size'):
            self.h2_size = utils.Settings.config.getint('dqnpolicy_' + domainString, 'h2_size')

        if utils.Settings.config.has_option('dqnpolicy_' + domainString, 'training_frequency'):
            self.training_frequency = utils.Settings.config.getint('dqnpolicy_' + domainString, 'training_frequency')

        """
        self.shuffle = False
        if cfg.has_option('dqnpolicy_'+domainString, 'experience_replay'):
            self.shuffle = cfg.getboolean('dqnpolicy_'+domainString, 'experience_replay')
        if not self.shuffle:
            # If we don't use experience replay, we don't need to maintain
            # sliding window of experiences with maximum capacity.
            # We only need to maintain the data of minibatch_size
            self.capacity = self.minibatch_size
        """

        self.episode_ave_max_q = []
        self.curiositypred_loss = []

        os.environ["CUDA_VISIBLE_DEVICES"] = ""
        policytype = 'dqn'
        self.dropout_rate = 0.
        if utils.Settings.config.has_option('dqnpolicy', 'dropout_rate'):
            self.dropout_rate = utils.Settings.config.getfloat('dqnpolicy', 'dropout_rate')
        if utils.Settings.config.has_option('policy', 'policytype'):
            policytype = utils.Settings.config.get('policy', 'policytype')
        if policytype != 'feudal':
            self.sess = tf.Session()

            with tf.device("/cpu:0"):

                np.random.seed(self.randomseed)
                tf.set_random_seed(self.randomseed)

                # initialise an replay buffer
                if self.replay_type == 'vanilla':
                    self.episodes[self.domainString] = ReplayBuffer(self.capacity, self.minibatch_size, self.randomseed)
                elif self.replay_type == 'prioritized':
                    self.episodes[self.domainString] = ReplayPrioritised(self.capacity, self.minibatch_size,
                                                                         self.randomseed)
                self.samplecount = 0
                self.episodecount = 0

                # construct the models
                self.state_dim = self.n_in
                if self.architecture == 'dip2':
                    self.state_dim = 89
                self.summaryaction = SummaryAction.SummaryAction(domainString)
                if action_names is None:
                    self.action_names = self.summaryaction.action_names
                else:
                    self.action_names = action_names
                self.action_dim = len(self.action_names)
                action_bound = len(self.action_names)
                self.stats = [0 for _ in range(self.action_dim)]

                self.dqn = dqn.DeepQNetwork(self.sess, self.state_dim, self.action_dim, \
                                            self.learning_rate, self.tau, action_bound, self.minibatch_size,
                                            self.architecture, self.h1_size,
                                            self.h2_size, dropout_rate=self.dropout_rate)

                # when all models are defined, init all variables
                init_op = tf.global_variables_initializer()
                self.sess.run(init_op)

                self.loadPolicy(self.in_policy_file)
                print('loaded replay size: ', self.episodes[self.domainString].size())

                self.curiosityFunctions = Curious()
                self.dqn.update_target_network()

    def get_n_in(self, domain_string):
        if domain_string == 'CamRestaurants':
            return 268
        elif domain_string == 'CamHotels':
            return 111
        elif domain_string == 'SFRestaurants':
            return 636
        elif domain_string == 'SFHotels':
            return 438
        elif domain_string == 'Laptops6':
            return 268 # ic340: this is wrong
        elif domain_string == 'Laptops11':
            return 257
        elif domain_string is 'TV':
            return 188
        else:
            print('DOMAIN {} SIZE NOT SPECIFIED, PLEASE DEFINE n_in'.format(domain_string))

    def act_on(self, state, hyps=None):
        if self.lastSystemAction is None and self.startwithhello:
            systemAct, nextaIdex = 'hello()', -1
            self.prev_state = state
        else:
            systemAct, nextaIdex = self.nextAction(state)
        self.lastSystemAction = systemAct
        self.summaryAct = nextaIdex
        self.prevbelief = state

        systemAct = DiaAct.DiaAct(systemAct)

        return systemAct

    def record(self, reward, domainInControl=None, weight=None, state=None, action=None):
        if domainInControl is None:
            domainInControl = self.domainString
        if self.actToBeRecorded is None:
            self.actToBeRecorded = self.summaryAct

        if state is None:
            state = self.prevbelief
        if action is None:
            action = self.actToBeRecorded

        if self.architecture != 'dip2':
            cState, cAction = self.convertStateAction(state, action)
        else:
            cState, cAction = self.convertDIPStateAction(state, action)
        # normalising total return to -1~1
        reward /= 20.0

        cur_cState = np.vstack([np.expand_dims(x, 0) for x in [cState]])
        Action_idx = np.eye(self.action_dim, self.action_dim)[[cAction]]
        if self.architecture != 'dip':
            cur_action_q = self.dqn.predict(cur_cState)
            cur_target_q = self.dqn.predict_target(cur_cState)
        else:
            cur_action_q = self.dqn.predict_dip(cur_cState, Action_idx)
            cur_target_q = self.dqn.predict_target_dip(cur_cState, Action_idx)
        execMask = self.summaryaction.getExecutableMask(state, cAction)

        if self.q_update == 'single':
            admissible = np.add(cur_target_q, np.array(execMask))
            if self.architecture != 'dip':
                Q_s_t_a_t_ = cur_action_q[0][cAction]
            else:
                Q_s_t_a_t_ = cur_action_q[0]
            gamma_Q_s_tplu1_maxa_ = self.gamma * np.max(admissible)
        elif self.q_update == 'double':
            admissible = np.add(cur_action_q, np.array(execMask))
            Q_s_t_a_t_ = cur_action_q[0][cAction]
            target_value_Q = cur_target_q[0]
            gamma_Q_s_tplu1_maxa_ = self.gamma * target_value_Q[np.argmax(admissible)]

        #print 'Q_s_t_a_t_', Q_s_t_a_t_
        #print 'gamma_Q_s_tplu1_maxa_', gamma_Q_s_tplu1_maxa_
        """
        s_batch = np.vstack([np.expand_dims(x, 0) for x in s_batch])
        s2_batch = np.vstack([np.expand_dims(x, 0) for x in s2_batch])
        #target_q = self.dqn.predict_target_with_action_maxQ(s2_batch)
        action_q = self.dqn.predict(s2_batch)
        target_q = self.dqn.predict_target(s2_batch)

        y_i = []
        for k in xrange(min(self.minibatch_size, self.episodes[self.domainString].size())):
            Q_bootstrap_label = 0
            if t_batch[k]:
                Q_bootstrap_label = r_batch[k]
            else:
                if self.q_update == 'single':
                    execMask = self.summaryaction.getExecutableMask(s2_ori_batch[k], a_batch[k])
                    action_Q = target_q[k]
                    admissible = np.add(action_Q, np.array(execMask))
                    #logger.info('action Q...')
                    #print admissible
                    Q_bootstrap_label = r_batch[k] + self.gamma * np.max(admissible)
                elif self.q_update == 'double':
                    execMask = self.summaryaction.getExecutableMask(s2_ori_batch[k], a_batch[k])
                    action_Q = action_q[k]
                    value_Q = target_q[k]
                    admissible = np.add(action_Q, np.array(execMask))
                    Q_bootstrap_label = r_batch[k] + self.gamma * value_Q[np.argmax(admissible)]
            y_i.append(Q_bootstrap_label)
        """

        if self.replay_type == 'vanilla':
            self.episodes[domainInControl].record(state=cState, \
                                                  state_ori=state, action=cAction, reward=reward)
        elif self.replay_type == 'prioritized':
            # heuristically assign 0.0 to Q_s_t_a_t_ and Q_s_tplu1_maxa_, doesn't matter as it is not used
            self.episodes[domainInControl].record(state=cState, \
                                                  state_ori=state, action=cAction, reward=reward, \
                                                  Q_s_t_a_t_=Q_s_t_a_t_,
                                                  gamma_Q_s_tplu1_maxa_=gamma_Q_s_tplu1_maxa_, uniform=False)
        self.actToBeRecorded = None
        self.samplecount += 1

    def finalizeRecord(self, reward, domainInControl=None):
        if domainInControl is None:
            domainInControl = self.domainString
        if self.episodes[domainInControl] is None:
            logger.warning("record attempted to be finalized for domain where nothing has been recorded before")
            return

        # print 'Episode Avg_Max_Q', float(self.episode_ave_max_q)/float(self.episodes[domainInControl].size())
        #print 'Episode Avg_Max_Q', np.mean(self.episode_ave_max_q)

        #print self.stats

        # normalising total return to -1~1
        reward /= 20.0

        terminal_state, terminal_action = self.convertStateAction(TerminalState(), TerminalAction())

        if self.replay_type == 'vanilla':
            self.episodes[domainInControl].record(state=terminal_state, \
                                                  state_ori=TerminalState(), action=terminal_action, reward=reward,
                                                  terminal=True)
        elif self.replay_type == 'prioritized':
            self.episodes[domainInControl].record(state=terminal_state, \
                                                      state_ori=TerminalState(), action=terminal_action, reward=reward, \
                                                      Q_s_t_a_t_=0.0, gamma_Q_s_tplu1_maxa_=0.0, uniform=False,
                                                      terminal=True)
            print('total TD', self.episodes[self.domainString].tree.total())

    def convertStateAction(self, state, action):
        '''
        nnType = 'dnn'
        #nnType = 'rnn'
        # expand one dimension to match the batch size of 1 at axis 0
        if nnType == 'rnn':
            belief = np.expand_dims(belief,axis=0)
        '''
        if isinstance(state, TerminalState):
            if self.domainUtil.domainString == 'CamRestaurants':
                return [0] * 268, action
            elif self.domainUtil.domainString == 'CamHotels':
                return [0] * 111, action
            elif self.domainUtil.domainString == 'SFRestaurants':
                return [0] * 633, action
            elif self.domainUtil.domainString == 'SFHotels':
                return [0] * 438, action
            elif self.domainUtil.domainString == 'Laptops11':
                return [0] * 257, action
            elif self.domainUtil.domainString == 'TV':
                return [0] * 188, action
        else:
            flat_belief = flatten_belief(state, self.domainUtil)
            self.prev_state_check = flat_belief

            return flat_belief, action

    def convertDIPStateAction(self, state, action):
        '''

        '''
        if isinstance(state, TerminalState):
            return [0] * 89, action

        else:
            dip_state = DIP_state(state.domainStates[state.currentdomain], self.domainString)
            action_name = self.actions.action_names[action]
            act_slot = 'general'
            for slot in dip_state.slots:
                if slot in action_name:
                    act_slot = slot
            flat_belief = dip_state.get_beliefStateVec(act_slot)
            self.prev_state_check = flat_belief

            return flat_belief, action

    def nextAction(self, beliefstate):
        '''
        select next action

        :param beliefstate:
        :param hyps:
        :returns: (int) next summary action
        '''
        if self.architecture != 'dip2':
            beliefVec = flatten_belief(beliefstate, self.domainUtil)
        else:
            dip_state = DIP_state(beliefstate.domainStates[beliefstate.currentdomain], self.domainString)
        execMask = self.summaryaction.getExecutableMask(beliefstate, self.lastSystemAction)

        if self.exploration_type == 'e-greedy':
            # epsilon greedy
            if self.is_training and utils.Settings.random.rand() < self.epsilon:
                admissible = [i for i, x in enumerate(execMask) if x == 0.0]
                random.shuffle(admissible)
                nextaIdex = admissible[0]
            else:
                if self.architecture != 'dip' and self.architecture != 'dip2':
                    action_Q = self.dqn.predict(np.reshape(beliefVec, (1, len(beliefVec))))  # + (1. / (1. + i + j))
                    admissible = np.add(action_Q, np.array(execMask))
                    logger.info('action Q...')
                    #print admissible.shape
                    #print admissible
                    nextaIdex = np.argmax(admissible)

                    # add current max Q to self.episode_ave_max_q
                    #print 'current maxQ', np.max(admissible)
                    self.episode_ave_max_q.append(np.max(admissible))
                elif self.architecture == 'dip2':
                    admissible = []
                    for idx, v in enumerate(execMask):
                        action_name = self.actions.action_names[idx]
                        act_slot = 'general'
                        for slot in dip_state.slots:
                            if slot in action_name:
                                act_slot = slot
                        beliefVec = dip_state.get_beliefStateVec(act_slot)
                        action_Q = self.dqn.predict(np.reshape(beliefVec, (1, len(beliefVec))))  # + (1. / (1. + i + j))
                        if v == 0:
                            admissible.append(action_Q[0][idx])
                        else:
                            admissible.append(v)
                    nextaIdex = np.argmax(admissible)
                    self.episode_ave_max_q.append(np.max(admissible))

                else:
                    admissible = []
                    for idx, v in enumerate(execMask):
                        if v > -sys.maxsize:
                            Action_idx = np.eye(self.action_dim, self.action_dim)[[idx]]
                            Qidx = self.dqn.predict_dip(np.reshape(beliefVec, (1, len(beliefVec))), Action_idx)
                            #print 'argmax Q',Qidx[0]
                            admissible.append(Qidx[0])
                        else:
                            admissible.append(-sys.maxsize)
                    # action_Q = self.dqn.predict(np.reshape(beliefVec, (1, len(beliefVec))))# + (1. / (1. + i + j))
                    # admissible = np.add(action_Q, np.array(execMask))
                    logger.info('action Q...')
                    #print admissible
                    nextaIdex = np.argmax(admissible)

                    # add current max Q to self.episode_ave_max_q
                    #print 'current maxQ', np.max(admissible)
                    self.episode_ave_max_q.append(np.max(admissible))

        elif self.exploration_type == 'Boltzman':
            # softmax
            if not self.is_training:
                self.epsilon = 0.001
            # self.epsilon here is served as temperature
            action_Q = self.dqn.predict(np.reshape(beliefVec, (1, len(beliefVec))))  # + (1. / (1. + i + j))
            action_Q_admissible = np.add(action_Q, np.array(execMask))  # enforce Q of inadmissible actions to be -inf

            action_prob = drlutils.softmax(action_Q_admissible / self.epsilon)
            logger.info('action Q...')
            #print action_Q_admissible
            logger.info('action prob...')
            #print action_prob
            sampled_prob = np.random.choice(action_prob[0], p=action_prob[0])
            nextaIdex = np.argmax(action_prob[0] == sampled_prob)

        self.stats[nextaIdex] += 1
        summaryAct = self.action_names[nextaIdex]
        beliefstate = beliefstate.getDomainState(self.domainUtil.domainString)
        masterAct = self.summaryaction.Convert(beliefstate, summaryAct, self.lastSystemAction)
        return masterAct, nextaIdex

    def train(self):
        '''
        call this function when the episode ends
        '''

        if not self.is_training:
            logger.info("Not in training mode")
            return
        else:
            logger.info("Update dqn policy parameters.")

        self.episodecount += 1
        logger.info("Sample Num so far: %s" % (self.samplecount))
        logger.info("Episode Num so far: %s" % (self.episodecount))

        if self.samplecount >= self.minibatch_size * 10 and self.episodecount % self.training_frequency == 0:
            logger.info('start training...')

            s_batch, s_ori_batch, a_batch, r_batch, s2_batch, s2_ori_batch, t_batch, idx_batch, _ = \
                self.episodes[self.domainString].sample_batch()

            # here?
            s_batch = np.vstack([np.expand_dims(x, 0) for x in s_batch])
            s2_batch = np.vstack([np.expand_dims(x, 0) for x in s2_batch])

            # change index-based a_batch to one-hot-based a_batch
            a_batch_one_hot = np.eye(self.action_dim, self.action_dim)[a_batch]

            # target_q = self.dqn.predict_target_with_action_maxQ(s2_batch)
            if self.architecture != 'dip':
                action_q = self.dqn.predict(s2_batch)
                target_q = self.dqn.predict_target(s2_batch)
            else:
                action_q = self.dqn.predict_dip(s2_batch, a_batch_one_hot)
                target_q = self.dqn.predict_target_dip(s2_batch, a_batch_one_hot)
            #print 'action Q and target Q:', action_q, target_q

            y_i = []
            for k in range(min(self.minibatch_size, self.episodes[self.domainString].size())):
                Q_bootstrap_label = 0
                if t_batch[k]:
                    Q_bootstrap_label = r_batch[k]
                else:
                    if self.q_update == 'single':
                        execMask = self.summaryaction.getExecutableMask(s2_ori_batch[k], a_batch[k])
                        action_Q = target_q[k]
                        admissible = np.add(action_Q, np.array(execMask))
                        # logger.info('action Q...')
                        # print admissible
                        Q_bootstrap_label = r_batch[k] + self.gamma * np.max(admissible)
                    elif self.q_update == 'double':
                        execMask = self.summaryaction.getExecutableMask(s2_ori_batch[k], a_batch[k])
                        action_Q = action_q[k]
                        value_Q = target_q[k]
                        admissible = np.add(action_Q, np.array(execMask))
                        Q_bootstrap_label = r_batch[k] + self.gamma * value_Q[np.argmax(admissible)]
                y_i.append(Q_bootstrap_label)

                if self.replay_type == 'prioritized':
                    # update the sum-tree
                    # update the TD error of the samples in the minibatch
                    currentQ_s_a_ = action_q[k][a_batch[k]]
                    error = abs(currentQ_s_a_ - Q_bootstrap_label)
                    self.episodes[self.domainString].update(idx_batch[k], error)

            # Update the critic given the targets
            reshaped_yi = np.vstack([np.expand_dims(x, 0) for x in y_i])

            if self.curiosityreward:
                curiosity_loss = self.curiosityFunctions.training(s2_batch, s_batch, a_batch_one_hot)
                # self.curiositypred_loss.append(curiosity_loss)  # for plotting

Carel van Niekerk's avatar
Carel van Niekerk committed
771
            predicted_q_value, _, currentLoss = self.dqn.train(s_batch, a_batch_one_hot, reshaped_yi)
Carel van Niekerk's avatar
Init  
Carel van Niekerk committed
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

            if self.episodecount % 1 == 0:
                # Update target networks
                self.dqn.update_target_network()

        self.savePolicyInc()  # self.out_policy_file)

    def savePolicy(self, FORCE_SAVE=False):
        """
        Does not use this, cause it will be called from agent after every episode.
        we want to save the policy only periodically.
        """
        pass

    def savePolicyInc(self, FORCE_SAVE=False):
        """
        save model and replay buffer
        """

        if self.episodecount % self.save_step == 0:
            # print "episode", self.episodecount
            # save_path = self.saver.save(self.sess, self.out_policy_file+'.ckpt')
            self.dqn.save_network(self.out_policy_file + '.dqn.ckpt')
            f = open(self.out_policy_file + '.episode', 'wb')
            for obj in [self.samplecount, self.episodes[self.domainString]]:
                pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
            f.close()
            # logger.info("Saving model to %s and replay buffer..." % save_path)

    def loadPolicy(self, filename):
        """
        load model and replay buffer
        """
        # load models
        self.dqn.load_network(filename + '.dqn.ckpt')

        # load replay buffer
        try:
            print('load from: ', filename)
            f = open(filename + '.episode', 'rb')
            loaded_objects = []
            for i in range(2):  # load nn params and collected data
                loaded_objects.append(pickle.load(f))
            self.samplecount = int(loaded_objects[0])
            self.episodes[self.domainString] = copy.deepcopy(loaded_objects[1])
            logger.info("Loading both model from %s and replay buffer..." % filename)
            f.close()
        except:
            logger.info("Loading only models...")

    def restart(self):
        self.summaryAct = None
        self.lastSystemAction = None
        self.prevbelief = None
        self.actToBeRecorded = None
        self.epsilon = self.epsilon_start - (self.epsilon_start - self.epsilon_end) * float(
            self.episodeNum + self.episodecount) / float(self.maxiter)
        #print 'current eps', self.epsilon
        # self.episodes = dict.fromkeys(OntologyUtils.available_domains, None)
        # self.episodes[self.domainString] = ReplayBuffer(self.capacity, self.randomseed)
        self.episode_ave_max_q = []

# END OF FILE