Ejemplo n.º 1
0
    def get_correction(self, user_input, action, args, test=False):
        # visible = {}
        # since this action is incorrect, ensure it is not done again
        not_on_xy = pddl_functions.create_formula('on', args[:2], op='not')
        self.tmp_goal = goals.update_goal(self.tmp_goal, not_on_xy)

        if not test:

            for test in self.active_tests:
                if (test.objects[0] == args[0] and test.objects[1] == args[1]) or test.failed:
                    correct_rule = test.rule1
                    self.active_tests.remove(test)
                else:
                    continue
                self.goal = goals.update_goal(self.goal, correct_rule.to_formula())

            # get the relevant parts of the message
            message = read_sentence(user_input, use_dmrs=False)

            # build the rule model
            self.build_model(message, args)

        self.update_goal()
        # logger.debug(self.goal.asPDDL())
        self.world.back_track()
Ejemplo n.º 2
0
    def to_goal(self, goal):
        out_rules = [self.rules[i][d] for i, d in enumerate(self.decision)]
        out_goal = None
        for rule in out_rules:
            out_goal = goals.update_goal(out_goal, rule)
        g = goals.update_goal(goal, out_goal)

        return g
Ejemplo n.º 3
0
    def evaluate_current_state(self, default_plan=False):
        if default_plan:
            #print('Doing default plan')
            self.current_state.state = []
            self.current_state.colour_counts = {
                c: 0
                for c in self.current_state.colour_counts.keys()
            }
        success, increase, decrease = self.constraints.evaluate(
            self.current_state)

        if success:
            self.problem.goal = goals.update_goal(self.goal, self.tmp_goal)
            # print(self.problem.goal.asPDDL())
            self.problem.initialstate = self.current_state.asPDDL()

            # for formula in self.current_state.asPDDL():
            #     print(formula.asPDDL())
            # print(self.problem.goal.asPDDL())

            with open(self.search_file, 'w') as f:
                f.write(self.problem.asPDDL())
            try:
                # print("trying to plan")
                # print(self.domain_file, self.use_metric_ff)
                plan = ff.run(self.domain_file,
                              self.search_file,
                              use_metric_ff=self.use_metric_ff)
                # print('Plan successful')
                return plan
            except (NoPlanError, IDontKnowWhatIsGoingOnError,
                    ImpossibleGoalError) as e:
                # print(e)
                # for p in self.problem.initialstate:
                #     print(p.asPDDL())
                # print(self.problem.goal.asPDDL())
                # n = len(os.listdir('errors/domain'))
                # with open('errors/domain/error{}.domain'.format(n), 'w') as f:
                #     f.write(self.problem.asPDDL())
                try:
                    score, self.current_state = self._pop()
                    return False
                except IndexError:
                    self.generate_candidates(increase, decrease)

        else:
            self.generate_candidates(increase, decrease)

        try:
            score, self.current_state = self._pop()
            return False
        except IndexError:
            print("Goal", self.goal.asPDDL())
            print("tmp goal", self.tmp_goal.asPDDL())
            print("state")
            for f in self.current_state.initialstate.predicates:
                print(f.asPDDL())

            raise NoPlanError('Search could not find a possible plan')
Ejemplo n.º 4
0
 def get_correction(self, user_input, action, args, test=False):
     # since this action is incorrect, ensure it is not done again
     args = args[:2]
     not_on_xy = pddl_functions.create_formula('on', args, op='not')
     self.tmp_goal = goals.update_goal(self.tmp_goal, not_on_xy)
     tower = args[2] if len(args) == 3 else None
     self.world.back_track(tower)
     self.sense()
Ejemplo n.º 5
0
    def __init__(self,
                 colour_choices,
                 obs,
                 tests,
                 goal,
                 tmp_goal,
                 problem,
                 domain_file='blocks-domain.pddl',
                 **kwargs):

        super().__init__(colour_choices,
                         obs,
                         goal,
                         tmp_goal,
                         problem,
                         domain_file=domain_file,
                         **kwargs)
        self.initial_state = self.current_state
        self.initial_goal = self.goal
        self.tests = tests

        tmp_goal = goals.update_goal(self.goal, tmp_goal)
        for test in tests:

            test_goal = goals.update_goal(tmp_goal, test.test_formula)

            self.problem.goal = test_goal
            self.problem.initialstate = self.current_state.asPDDL()
            # print(test_goal.asPDDL())
            with open(self.search_file, 'w') as f:
                f.write(self.problem.asPDDL())
            try:
                plan = ff.run(self.domain_file,
                              self.search_file,
                              use_metric_ff=self.use_metric_ff)
            except (ImpossibleGoalError, IDontKnowWhatIsGoingOnError,
                    NoPlanError):

                test.failed = True
                continue
            else:
                self.goal = goals.update_goal(self.goal, test.test_formula)
                tmp_goal = goals.update_goal(tmp_goal, test.test_formula)
Ejemplo n.º 6
0
 def no_correction(self, action, args):
     for test in self.active_tests:
         if (test.objects[0] == args[0] and test.objects[1] == args[1]):
             correct_rule = test.rule2
             self.active_tests.remove(test)
         elif test.failed:
             correct_rule = test.rule1
             self.active_tests.remove(test)
         else:
             continue
         self.goal = goals.update_goal(self.goal, correct_rule.to_formula())
Ejemplo n.º 7
0
    def __init__(self, name='blocks-problem', domainname='blocks-domain', objects=None, n=10, m=1,
                 colours=[], rules=None, metric=None):
        self.domainname = domainname
        if objects is not None:
            objects = pddl_functions.make_variable_list(objects)
        else:
            objects = BlocksWorldProblem.create_objects(n, m)

        if isinstance(rules[0], Ruledef):
            rules = [generate_rule(rule) for rule in rules]
        elif isinstance(rules[0], correctingagent.world.rules.Rule):
            rules = [rule.to_formula() for rule in rules]

        initialstate = self.generate_default_position(objects)
        initialstate = BlocksWorldProblem.add_colours(initialstate, objects, colours)
        goal = self.create_default_goal()
        for rule in rules:
            goal = goals.update_goal(goal, rule)
        super(BlocksWorldProblem, self).__init__(name, domainname, objects, initialstate, goal, metric)
Ejemplo n.º 8
0
    def get_correction(self, user_input, action, args, test=False):
        visible = {}
        # since this action is incorrect, ensure it is not done again
        not_on_xy = pddl_functions.create_formula('on', args, op='not')
        self.tmp_goal = goals.update_goal(self.tmp_goal, not_on_xy)
        if not test:
            # get the relevant parts of the message
            message = read_sentence(user_input, use_dmrs=False)

            # build the rule model
            rule_model, rules = self.build_model(message)

            # if isinstance(self, CorrectingAgent):
            #     log_cm(rule_model.c1)
            #     log_cm(rule_model.c2)
            #     if message.T.lower() == 'table':
            #         log_cm(rule_model.c3)

            logger.debug('rule priors' + str(rule_model.rule_prior))

            # gets F(o1), F(o2), and optionally F(o3)
            data = self.get_data(message, args)

            priors = self.priors.get_priors(message, args)
            logger.debug('object priors: ' + str(priors))
            r1, r2 = rule_model.get_message_probs(data, priors=priors)
            logger.debug('predictions: ' + str((r1, r2)))

            #print(r1, r2)

            colour1 = message.o1[0]
            colour2 = message.o2[0]

            _, results = self.sense()
            try:
                result_c1 = results[args[0]][colour1]
            except KeyError:
                result_c1 = 0.
            try:
                result_c2 = results[args[1]][colour2]
            except KeyError:
                result_c2 = 0.
            # if there is no confidence in the update then ask for help
            if max(r1, r2) < self.threshold or (result_c1 == 0.5 or result_c2 == 0.5):
                # logger.debug('asking question')
                question = 'Is the top object {}?'.format(message.o1[0])
                dialogue.info("R: " + question)
                if len(args) == 2:
                    tower = None
                else:
                    tower = args[-1]
                answer = self.teacher.answer_question(question, self.world, tower)
                dialogue.info("T: " +  answer)

                bin_answer = int(answer.lower() == 'yes')
                visible[message.o1[0]] = bin_answer
                message_probs = rule_model.get_message_probs(data, visible=copy.copy(visible), priors=priors)
                r1, r2 = message_probs


            objs = [args[0], args[1], message.o3]
            prior_updates = rule_model.updated_object_priors(data, objs, priors, visible=copy.copy(visible))

            # update the goal belief
            #logger.debug(prior_updates)

            rule_model.update_belief_r(r1, r2)
            # print(r1, r2)
            # print(rule_model, rule_model.rule_belief.belief)
            # print(args)
            # print(self.sense()[1])
            which_to_update = [1,1,1]
            if self.update_once:
                for i, o in enumerate(objs):
                    if o in self.objects_used_for_update:
                        which_to_update[i] = 0
                    self.objects_used_for_update.add(o)

            rule_model.update_c(data, priors=self.priors.get_priors(message, args), visible=visible, update_negative=self.update_negative, which_to_update=which_to_update)

            self.rule_models[(rule_model.rule_names, message.T)] = rule_model
            self.priors.update(prior_updates)
        self.update_goal()
        logger.debug(self.goal.asPDDL())
        self.world.back_track()
Ejemplo n.º 9
0
    def get_correction(self,
                       user_input,
                       actions,
                       args,
                       test=False,
                       ask_question=None):
        self.time += 1
        self.last_correction = self.time

        violations, data, message = self.update_model(user_input, args)
        tmp_goal = None
        mark_block = True
        ignore_correction = False

        print(user_input)

        if "sorry" not in user_input:
            tmp_goal = pddl_functions.create_formula('on', args[:2], op='not')
            #self.tmp_goal = goals.update_goal(self.tmp_goal, not_on_xy)

        else:
            on_xy = pddl_functions.create_formula('on', message.o3)
            self.tmp_goal = goals.update_goal(self.tmp_goal, on_xy)

        time = self.pgm_model.observe(data)
        self.inference_times.append(time)

        try:
            q = self.pgm_model.query(list(violations))
        except InferenceFailedError:
            self.world.back_track()
            return
        # print(q)

        if ask_question is not None and ask_question:
            if message.T in ['table', 'tower']:
                self.ask_question(message, args)
                try:
                    q = self.pgm_model.query(list(violations))
                except InferenceFailedError:
                    self.world.back_track()
                    return

        elif all([v < 1 - self.threshold
                  for v in q.values()]):  # and "sorry" not in user_input:
            print("ignoring correction")
            tmp_goal = None
            mark_block = False
            ignore_correction = True

        elif ask_question is None and max(q.values(
        )) < self.threshold:  #min(q.values()) > 1-self.threshold:

            if message.T in ['table', 'tower']:
                self.ask_question(message, args)
                try:
                    q = self.pgm_model.query(list(violations))
                except InferenceFailedError:
                    self.world.back_track()
                    return

        if tmp_goal is not None:
            self.tmp_goal = goals.update_goal(self.tmp_goal, tmp_goal)

        if mark_block is True:
            most_likely_violation = max(q, key=q.get)
            self.mark_block(most_likely_violation, message, args)

        self.update_cms()
        self.update_goal()
        if ignore_correction is not True:
            self.world.back_track()