示例#1
0
    def Step(self):
        #TODO:check that stop is greater than start
        start = float(self.lineEdit_StartTime.text())
        stop = float(self.lineEdit_StopTime.text())
        step = float(self.lineEdit_StepTime.text())

        intnumofsteps = int((stop - start) / step)
        t = np.linspace(start, stop, intnumofsteps)
        u = np.ones_like(t) * float(self.lineEdit_inputTime.text())

        sys = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())

        if self.comboBoxTimeDomainType.currentText() == "Step":
            sys.step(t, u, output=False, plot=True)
        elif self.comboBoxTimeDomainType.currentText() == "Impulse":
            #TODO: Code for Impulse time domain
            pass
示例#2
0
    def gen(self, domain, complexity, num_sess=1):
        """
        Generate synthetic dialogs in the given domain. 

        :param domain: a domain specification dictionary
        :param complexity: an implmenetaiton of Complexity
        :param num_sess: how dialogs to generate
        :return: a list of dialogs. Each dialog is a list of turns.
        """
        dialogs = []
        action_channel = ActionChannel(domain, complexity)
        word_channel = WordChannel(domain, complexity)

        # natural language generators
        sys_nlg = SysNlg(domain, complexity)
        usr_nlg = UserNlg(domain, complexity)

        # bar = progressbar.ProgressBar(max_value=num_sess)
        for i in range(num_sess):
            # bar.update(i)
            usr = User(domain, complexity)
            sys = System(domain, complexity)

            # begin conversation
            noisy_usr_as = []
            dialog = []
            conf = 1.0
            while True:
                # make a decision
                sys_r, sys_t, sys_as, sys_s = sys.step(noisy_usr_as, conf)
                sys_utt, sys_str_as = sys_nlg.generate_sent(sys_as,
                                                            domain=domain)
                dialog.append(
                    self.pack_msg("SYS",
                                  sys_utt,
                                  actions=sys_str_as,
                                  domain=domain.name,
                                  state=sys_s))
                if sys_t:
                    break

                usr_r, usr_t, usr_as = usr.step(sys_as)
                # passing through noise, nlg and noise!
                noisy_usr_as, conf = action_channel.transmit2sys(usr_as)
                usr_utt = usr_nlg.generate_sent(noisy_usr_as)
                noisy_usr_utt = word_channel.transmit2sys(usr_utt)

                dialog.append(
                    self.pack_msg("USR",
                                  noisy_usr_utt,
                                  actions=noisy_usr_as,
                                  conf=conf,
                                  domain=domain.name))

            dialogs.append(dialog)

        return dialogs
def main(mode, episodes, max_iterations, epsilon, gamma, lr, weight_out,
         returns_out):
    sys = MountainCar(mode)
    #initialize states. add one extra dimensions in row for bias term
    returns_out_list = []
    rolling_mean_out_list = []
    W = np.zeros((sys.state_space, 3))
    b = 0

    for i in range(0, episodes):
        print('episodes: %s' % i)
        return_out = 0
        curr_state = sys.reset()
        curr_state = convert_state_to_vector(curr_state, mode)
        for itr in range(max_iterations):
            q_all = np.transpose(curr_state).dot(W) + b
            best_action = find_best_action(q_all, epsilon)
            future_state, reward, done = sys.step(best_action)
            future_state = convert_state_to_vector(future_state, mode)
            # print('future state', future_state)
            return_out += reward

            W, b = update_weight(mode, W, b, q_all, best_action, lr, gamma,
                                 reward, curr_state, future_state)
            curr_state = future_state
            if done:
                # sys.reset()
                break

        returns_out_list.append(return_out)

    for i in range(len(returns_out_list)):
        print(i)
        if i < 25:
            rolling_mean_out_list.append(np.mean(returns_out_list[:i + 1]))
        else:
            rolling_mean_out_list.append(np.mean(returns_out_list[i - 25:i]))
        # rolling_sum+=return_out
        # print('rolling_sum',rolling_sum)
        # if i%25==0:
        #     print(1)
        #     rolling_mean_out_list.append(rolling_sum/25)
        #     rolling_sum=0

    #writing to the weight out file
    flattened_W = np.ndarray.flatten(W)
    weight_out = open(weight_out, 'w')
    weight_out.writelines("%s\n" % b)
    weight_out.writelines("%s\n" % w for w in flattened_W)
    weight_out.close()

    #writing to the return out file
    returns_out = open(returns_out, 'w')
    returns_out.writelines("%s " % r for r in returns_out_list)
    returns_out.close()

    rolling_mean_out = open('output/rolling_mean_plotting1.out', 'w')
    rolling_mean_out.writelines(("%s " % r for r in rolling_mean_out_list))
    rolling_mean_out.close()
    print(W)
    print(b)
    return
示例#4
0
    def gen(self, domain, complexity, num_sess=1):
        """
        Generate synthetic dialogs in the given domain. 

        :param domain: a domain specification dictionary
        :param complexity: an implmenetaiton of Complexity
        :param num_sess: how dialogs to generate
        :return: a list of dialogs. Each dialog is a list of turns.
        """
        dialogs = []
        action_channel = ActionChannel(domain, complexity)
        word_channel = WordChannel(domain, complexity)

        # natural language generators
        sys_nlg = SysNlg(domain, complexity)
        usr_nlg = UserNlg(domain, complexity)

        sys_slots_list = [
            slot.name for slot in domain.sys_slots if slot.name != '#default'
        ]

        bar = progressbar.ProgressBar(maxval=num_sess)
        bar.start()

        for i in range(num_sess):
            bar.update(i)
            usr = User(domain, complexity)
            sys = System(domain, complexity)

            # begin conversation
            noisy_usr_as = []
            dialog = []
            conf = 1.0
            turn_num = 0

            sys_r, sys_t, sys_as, sys_s = sys.step(noisy_usr_as, conf)
            sys_utt, sys_str_as = sys_nlg.generate_sent(sys_as, domain=domain)

            # set domain name
            domain_name = domain.name
            if domain_name == "rest_pitt" or \
                domain_name == "restaurant_style":
                domain_name = "restaurant"

            while True:

                if sys_t:
                    break

                usr_r, usr_t, usr_as = usr.step(sys_as)

                # passing through noise, nlg and noise!
                noisy_usr_as, conf = action_channel.transmit2sys(usr_as)
                usr_utt = usr_nlg.generate_sent(noisy_usr_as)
                noisy_usr_utt = word_channel.transmit2sys(usr_utt)
                # dialog.append(self.pack_msg("USR", noisy_usr_utt, actions=noisy_usr_as, conf=conf, domain=domain.name))

                # make a decision
                sys_r, sys_t, sys_as, sys_s = sys.step(noisy_usr_as, conf)
                sys_utt, sys_str_as = sys_nlg.generate_sent(sys_as,
                                                            domain=domain)
                # dialog.append(self.pack_msg("SYS", sys_utt, actions=sys_str_as, domain=domain.name, state=state))

                if "\"RET\"" in noisy_usr_utt:
                    # change the last dialogue

                    dialog[-1]["sys"]["sent"] = sys_utt

                else:
                    # append a new dialogue

                    usr_tmp_dict = {
                        # "transcript" : domain_name + " " + noisy_usr_utt,
                        "transcript": noisy_usr_utt,
                        "slu": []
                    }
                    for inform_dict in sys_s["usr_slots"]:
                        if inform_dict["max_val"] != None:
                            usr_tmp_dict["slu"].append({
                                "act":
                                "inform",
                                "slots": [[
                                    inform_dict["name"][1:],
                                    inform_dict["max_val"]
                                ]]
                            })
                    for action_dict in noisy_usr_as:
                        if action_dict["act"] == "request" and  \
                            action_dict["parameters"][0][0] in sys_slots_list:

                            usr_tmp_dict["slu"].insert(
                                0, {
                                    "act":
                                    "request",
                                    "slots": [[
                                        "slot",
                                        action_dict["parameters"][0][0][1:]
                                    ]]
                                })

                    sys_tmp_dict = {
                        # "sent" : domain_name + " " + sys_utt
                        "sent": sys_utt
                    }

                    dialog.append({
                        "turn": turn_num,
                        "usr": usr_tmp_dict,
                        "sys": sys_tmp_dict
                    })

                    turn_num += 1

            # print("turn_num: ", turn_num, '\n')

            dialogs.append({"dial": dialog})

        return dialogs
示例#5
0
    def gen(self, domain, complexity, num_sess=1):
        """
        Generate synthetic dialogs in the given domain.

        :param domain: a domain specification dictionary
        :param complexity: an implmenetaiton of Complexity
        :param num_sess: how dialogs to generate
        :return: a list of dialogs. Each dialog is a list of turns.
        """
        dialogs = []
        action_channel = ActionChannel(domain,
                                       complexity)  # action 等级上的 error Channel
        word_channel = WordChannel(domain, complexity)  # word 等级上的 channel

        # natural language generators
        sys_nlg = SysNlg(domain, complexity)  # 配置系统nlg
        usr_nlg = UserNlg(domain, complexity)  # 配置用户nlg

        bar = progressbar.ProgressBar(num_sess)
        for i in range(num_sess):
            bar.update(i)
            usr = User(domain, complexity)  # 初始化用户模拟器
            sys = System(domain, complexity)  # 初始化概率 dm

            # begin conversation
            noisy_usr_as = []
            dialog = []
            conf = 1.0
            while True:
                # make a decision
                sys_r, sys_t, sys_as, sys_s = sys.step(
                    noisy_usr_as, conf)  # 系统reward 系统结束标志 系统动作 系统状态
                sys_utt, sys_str_as = sys_nlg.generate_sent(
                    sys_as, domain=domain)  # nlg
                # 打包系统信息封装到dialog中
                dialog.append(
                    self.pack_msg("SYS",
                                  sys_utt,
                                  actions=sys_str_as,
                                  domain=domain.name,
                                  state=sys_s))

                if sys_t:
                    break

                usr_r, usr_t, usr_as = usr.step(
                    sys_as)  #用户 reward 用户是否终止 用户动作列表

                # 通过各个等级的error channel 添加噪声
                # passing through noise, nlg and noise!
                noisy_usr_as, conf = action_channel.transmit2sys(usr_as)
                usr_utt = usr_nlg.generate_sent(noisy_usr_as)  # nlg 生成用户语句
                noisy_usr_utt = word_channel.transmit2sys(usr_utt)

                # 打包用户信息封装到dialog中
                dialog.append(
                    self.pack_msg("USR",
                                  noisy_usr_utt,
                                  actions=noisy_usr_as,
                                  conf=conf,
                                  domain=domain.name))

            dialogs.append(dialog)

        return dialogs