-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.py
291 lines (232 loc) · 10.8 KB
/
simulate.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
#########################################################
# CUED Python Statistical Spoken Dialogue System Software
#########################################################
#
# Copyright 2015-16 Cambridge University Engineering Department
# Dialogue Systems Group
#
# Principal Authors: Dongho Kim and David Vandyke
#
# 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.
#########################################################
'''
simulate.py - semantic level user simulator system.
====================================================
Authors: Dongho Kim, David Vandyke (Copyright CUED Dialogue Systems Group 2015)
**Basic Execution**:
>>> python simulate.py [-h] -C CONFIG [-n -r -l -t -g -s]
Optional arguments/flags [default values]::
-n Number of dialogs [1]
-r semantic error rate [0]
-l set the system to use the given policy file for simulation or as learning prior
-s set random seed
-t train the policy
-g generate text prompts
-h help
**Relevant Config variables** [Default values]::
[simulate]
maxturns = 30
continuewhensuccessful = False
forcenullpositive = False
confscorer = additive
[hub]
semo = PassthroughSemO
[eval]
successmeasure = objective
.. seealso:: CUED Imports/Dependencies:
import :class:`ContextLogger` |.|
import :class:`Settings` |.|
import :class:`DMan` |.|
import :class:`UserSimulator` |.|
import :class:`SemO` |.|
import :class:`DomainUtils` |.|
import :class:`ErrorSimulator`
************************
'''
__author__ = "donghokim, davidvandyke"
import argparse
import numpy as np
import Settings
import DMan
import UserSimulator
import ErrorSimulator
from semo import SemO
import TopicManager
import DomainUtils
import Evaluation
import ContextLogger
try:
import cPickle as pickle
except:
import pickle
logger = ContextLogger.getLogger(__name__)
class SimulationSystem:
'''Semantic level simulated dialog system
:param generate_prompts: (bool) semo output
'''
def __init__(self, generate_prompts):
configs = []
if not Settings.config.has_option("DEFAULT","domains"):
logger.error("You must specify the domains under the DEFAULT section of the config")
domains = Settings.config.get("DEFAULT",'domains')
logger.info('--Simulating dialogues over the domains: ', domains)
self.possible_domains = domains.split(',')
DomainUtils.checkDomainStrings(domainStrings=self.possible_domains)
self.maxTurns = 30
if Settings.config.has_option("simulate", "maxturns"):
configs.append('maxturns')
self.maxTurns = Settings.config.getint("simulate", "maxturns")
self.forceNullPositive = False
if Settings.config.has_option("simulate", "forcenullpositive"):
configs.append('forcenullpositive')
self.forceNullPositive = Settings.config.getboolean("simulate", "forcenullpositive")
conf_scorer_name = 'additive'
if Settings.config.has_option('simulate', 'confscorer'):
conf_scorer_name = Settings.config.get('simulate', 'confscorer')
if Settings.config.has_section('simulate'):
for opt in Settings.config.options('simulate'):
if opt not in configs and opt not in Settings.config.defaults():
logger.error('Invalid config: '+opt)
# [MultiDomain?] Dialogue Management/policy.
#-----------------------------------------
self.topic_manager = TopicManager.TopicManager()
# Simulated User.
#-----------------------------------------
# TODO - deal with multi domain simulation - whilst changing Settings.py ill just pass domain here for now
logger.debug('simulate.py -- XXXXXXX -- directly passing domain name in simulate at present...')
self.simulator = UserSimulator.SimulatedUsersManager(domainStrings=self.possible_domains)
# Error Simulator.
#-----------------------------------------
# TODO - it is a hack for now passing the domain string directly from config via self.possible_domains. look at this
#self.errorSimulator = ErrorSimulator.CuedErrorSimulator(conf_scorer_name, domainString=self.possible_domains[0])
self.errorSimulator = ErrorSimulator.SimulatedErrorManager(conf_scorer_name, self.possible_domains)
# SemO.
#-----------------------------------------
self.semoClass = None
if generate_prompts:
self.semo_name = 'PassthroughSemO'
if Settings.config.has_option('hub', 'semo'):
self.semo_name = Settings.config.get('hub', 'semo')
# SemO.
if self.semo_name == 'PassthroughSemO':
self.semoClass = SemO.PassthroughSemO()
elif self.semo_name == 'BasicSemO':
self.semoClass = SemO.BasicSemO()
else:
logger.warning('Invalid SemO: %s. Using PassthroughSemO.' % self.semo_name)
self.semoClass = SemO.PassthroughSemO()
# Evaluation Manager.
#-----------------------------------------
self.evaluator = Evaluation.EvaluationManager(self.possible_domains)
def set_error_rate(self, r):
'''Sets the semantic error rate
:param r: (int) error rate
:returns: None
'''
self.errorSimulator.set_error_rate(r)
def run(self, episodeNum):
'''Runs one episode through the simulator
:param None:
:returns None:
'''
sys_act = None
user_act = ''
reward = None
hyps = []
self.currentDomain = None
constraints = None # used to conditionally set the belief state of a new domain
# reset the user simulator:
self.simulator.restart()
# reset the dialogue management:
self.topic_manager.restart()
# reset the Success evaluator
self.evaluator.restart()
# determine the number of domains in this dialog > and set maxTurns appropriately
maxNumTurnsTotal = self.maxTurns * len(self.simulator.using_domains)
for t in range(maxNumTurnsTotal):
if 'bye(' in user_act:
break
print ' Turn %d' % t
# SYSTEM ACT:
sys_act = self.topic_manager.act_on(sys_act, hyps, constraints)
print ' Sys >',
print sys_act
if t > 0:
self.topic_manager.recordReward(reward)
if self.semoClass is not None:
prompt = self.semoClass.generate(sys_act)
print ' Prompt >', prompt
# USER ACT:
user_act, user_actsDomain = self.simulator.act_on(sys_act)
print ' User >', user_act
# Housecleaning required if we switched domains:
if self.currentDomain != user_actsDomain:
previous_domain = self.currentDomain # this works at start and transitions
self.currentDomain = user_actsDomain
sys_act = None
logger.info('After user_act - domain is now: '+self.currentDomain)
else:
previous_domain = None
# Confused user act.
hyps = self.errorSimulator.confuse_act(user_act, self.currentDomain)
null_prob = 0.0
for (act, prob) in hyps:
if act == 'null()':
null_prob += prob
print ' Semi >', act, '[%.6f]' % prob
if self.forceNullPositive and null_prob < 0.001:
hyps.append(('null()', 0.001))
print ' Semi > null() [0.001]'
constraints = self.topic_manager.track_topic(domainString=self.currentDomain,
previousDomainString=previous_domain, episodeNum=episodeNum)
# MEASURE REWARD AND SUCCESS
reward = self.evaluator.reward_and_success(domainString=self.currentDomain,
um=self.simulator.domainSimulatedUsers[self.currentDomain].um)
#-------- and perform cleanup/finalisation if we completed previous domain
if previous_domain is not None:
_ = self.evaluator.reward_and_success(domainString=previous_domain,
um=self.simulator.domainSimulatedUsers[previous_domain].um)
#---------------------------------------------------------------------------------------------------------
# Process ends.
self.evaluator.finalise_dialog()
self.topic_manager.endLearningEpisode(domainRewards = self.evaluator.final_rewards)
# Print dialog summary.
self.evaluator.print_dialog_summary()
return
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Simulate')
parser.add_argument('-C', '--config', help='set config file', required=True, type=argparse.FileType('r'))
parser.add_argument('-n', '--number', help='set the number of dialogues', type=int)
parser.add_argument('-r', '--error', help='set error rate', type=int)
parser.set_defaults(use_color=True)
parser.add_argument('--nocolor', dest='use_color',action='store_false', help='no color in logging. best to\
turn off if dumping to file. Will be overriden by [logging] config setting of "usecolor=".')
parser.add_argument('-g', '--generate', help='generate text prompts', action='store_true')
parser.add_argument('-s', '--seed', help='set random seed', type=int)
args = parser.parse_args()
if args.error is None:
args.error = 0
if args.number is None:
args.number = 1
Settings.load_config(args.config.name)
ContextLogger.createLoggingHandlers(config=Settings.config, use_color=args.use_color)
Settings.set_seed(args.seed)
simulator = SimulationSystem(args.generate)
simulator.set_error_rate(float(args.error)/100)
logger.info('Simulating with error rate: '+str(float(args.error)/100))
for i in range(args.number):
logger.info('Dialogue %d' % (i+1))
simulator.run(i)
simulator.topic_manager.savePolicy()
simulator.evaluator.print_summary()
#END OF FILE