def role_assignment():
    url = request.args.get("url")
    try:
        role_names, top_words = assign_roles(url, 2.0, 0.25)
        top_entity_names = []
        for name in role_names:
            name_string = name if name else "None"
            top_entity_names.append(name_string)
        return jsonify(top_entity_names, top_words)
    except NewspaperError:
        return jsonify("Extraction error")
    except:
        return jsonify("Entity recognition/role assignment errors")
    def test_pos_cost(self):
        """Ensure that when requirements specify a target position, it is taken
        into account in assignment"""

        bot1 = robocup.OurRobot(self.context, 1)
        bot1.set_pos_for_testing(robocup.Point(1, 6))

        bot2 = robocup.OurRobot(self.context, 2)
        bot2.set_pos_for_testing(robocup.Point(2, 3))

        req1 = role_assignment.RoleRequirements()
        req1.destination_shape = robocup.Point(1, 7)

        req2 = role_assignment.RoleRequirements()
        req2.destination_shape = robocup.Point(3, 4)

        req_tree = {'role1': req1, 'role2': req2}
        assignments = role_assignment.assign_roles([bot1, bot2], req_tree)
        self.assertEqual(len(assignments), 2)
        self.assertEqual(assignments['role1'][1], bot1)
        self.assertEqual(assignments['role2'][1], bot2)
    def test_pos_cost(self):
        """Ensure that when requirements specify a target position, it is taken
        into account in assignment"""

        bot1 = robocup.OurRobot(1, None)
        bot1.pos = robocup.Point(1, 6)

        bot2 = robocup.OurRobot(2, None)
        bot2.pos = robocup.Point(2, 3)

        req1 = role_assignment.RoleRequirements()
        req1.pos = robocup.Point(1, 7)

        req2 = role_assignment.RoleRequirements()
        req2.pos = robocup.Point(3, 4)

        req_tree = {'role1': req1, 'role2': req2}
        assignments = role_assignment.assign_roles([bot1, bot2], req_tree)
        self.assertEqual(len(assignments), 2)
        self.assertEqual(assignments['role1'][1], bot1)
        self.assertEqual(assignments['role2'][1], bot2)
Example #4
0
    def execute_running(self):
        # update double touch tracker
        evaluation.double_touch.tracker().spin()

        # cache and calculate the score() function for each play class
        main.play_registry().recalculate_scores()

        # Play Selection
        ################################################################################

        if main.game_state().is_stopped():
            evaluation.double_touch.tracker().restart()
            if main.game_state().is_placement():
                if not isinstance(self.play,
                                  plays.restarts.placement.Placement):
                    logging.info("Placing Ball")
                    self.play = plays.restarts.placement.Placement()
                    self._currently_restarting = True
            else:
                if self.play is None or not self.play.run_during_stopped():
                    logging.info(
                        "Running 'Stopped' play due to game state change")
                    self.play = plays.stopped.Stopped()
                    self._currently_restarting = True
        elif main.game_state().is_halted():
            evaluation.double_touch.tracker().restart()
            self.play = None
        else:
            # (play_class, score value) tuples
            enabled_plays_and_scores = [
                p for p in main.play_registry().get_enabled_plays_and_scores()
            ]

            # only let restart play run once
            enabled_plays_and_scores = [
                p for p in enabled_plays_and_scores
                if not p[0].is_restart() or (
                    p[0].is_restart() and self._currently_restarting)
            ]

            # handle temporary blacklisting
            # we remove the blacklisted play class from selection for this iteration, then unblacklist it
            enabled_plays_and_scores = [
                p for p in enabled_plays_and_scores
                if p[0] != self.temporarily_blacklisted_play_class
            ]
            self.temporarily_blacklisted_play_class = None

            # see if we need to kill current play or if it's done running
            if self.play is not None:
                if self.play.__class__ not in map(lambda tup: tup[0],
                                                  enabled_plays_and_scores):
                    logging.info("Current play '" +
                                 self.play.__class__.__name__ +
                                 "' no longer enabled, aborting")
                    self.play.terminate()
                    self.play = None
                elif self.play.is_done_running():
                    logging.info("Current play '" +
                                 self.play.__class__.__name__ +
                                 "' finished running")
                    if self.play.is_restart:
                        self._currently_restarting = False
                    self.play = None
                elif self.play.__class__.score() == float("inf"):
                    logging.info("Current play '" +
                                 self.play.__class__.__name__ +
                                 "' no longer applicable, ending")
                    self.play.terminate()
                    self.play = None

            if self.play is None:
                try:
                    if len(enabled_plays_and_scores) > 0:
                        # select the play with the smallest value for score()
                        play_class_and_score = min(enabled_plays_and_scores,
                                                   key=lambda tup: tup[1])

                        # run the play with the lowest score, as long as it isn't inf
                        if play_class_and_score[1] != float("inf"):
                            play_class = play_class_and_score[0]
                            self.play = play_class()  # instantiate it
                    else:
                        # there's no available plays to run
                        pass
                except Exception as e:
                    logging.error(
                        "Exception occurred during play selection: " + str(e))
                    traceback.print_exc()
                if self.play is not None:
                    logging.info("Chose new play: '" +
                                 self.play.__class__.__name__ + "'")

        # Role Assignment
        ################################################################################
        try:
            assignments = role_assignment.assign_roles(
                self.robots, self.role_requirements())
        except role_assignment.ImpossibleAssignmentError as e:
            logging.error(
                "Unable to satisfy role assignment constraints.  Dropping and temp. blacklisting current play..."
            )
            self.drop_current_play(temporarily_blacklist=True)
        else:
            self.assign_roles(assignments)
Example #5
0
    def execute_running(self):
        # update double touch tracker
        evaluation.double_touch.tracker().spin()

        # cache and calculate the score() function for each play class
        main.play_registry().recalculate_scores()

        # Play Selection
        ################################################################################

        if main.game_state().is_stopped():
            if not isinstance(self.play, plays.stopped.Stopped):
                logging.info("Running 'Stopped' play due to game state change")
                self.play = plays.stopped.Stopped()
                self._currently_restarting = True
        elif main.game_state().is_halted():
            self.play = None
        else:
            # (play_class, score value) tuples
            enabled_plays_and_scores = [p for p in main.play_registry().get_enabled_plays_and_scores()]

            # only let restart play run once
            enabled_plays_and_scores = [p for p in enabled_plays_and_scores if not p[0].is_restart() or ( p[0].is_restart() and self._currently_restarting)]

            # handle temporary blacklisting
            # we remove the blacklisted play class from selection for this iteration, then unblacklist it
            enabled_plays_and_scores = [p for p in enabled_plays_and_scores if p[0] != self.temporarily_blacklisted_play_class]
            self.temporarily_blacklisted_play_class = None

            # see if we need to kill current play or if it's done running
            if self.play != None:
                if self.play.__class__ not in map(lambda tup: tup[0], enabled_plays_and_scores):
                    logging.info("Current play '" + self.play.__class__.__name__ + "' no longer enabled, aborting")
                    self.play.terminate()
                    self.play = None
                elif self.play.is_done_running():
                    logging.info("Current play '" + self.play.__class__.__name__ + "' finished running")
                    if self.play.is_restart:
                        self._currently_restarting = False
                    self.play = None
                elif self.play.__class__.score() == float("inf"):
                    logging.info("Current play '" + self.play.__class__.__name__ + "' no longer applicable, ending")
                    self.play.terminate()
                    self.play = None

            if self.play == None:
                # reset the double-touch tracker
                evaluation.double_touch.tracker().restart()

                try:
                    if len(enabled_plays_and_scores) > 0:
                        # select the play with the smallest value for score()
                        play_class_and_score = min(enabled_plays_and_scores, key=lambda tup: tup[1])

                        # run the play with the lowest score, as long as it isn't inf
                        if play_class_and_score[1] != float("inf"):
                            play_class = play_class_and_score[0]
                            self.play = play_class() # instantiate it
                    else:
                        # there's no available plays to run
                        pass
                except Exception as e:
                    logging.error("Exception occurred during play selection: " + str(e))
                    traceback.print_exc()
                if self.play != None:
                    logging.info("Chose new play: '" + self.play.__class__.__name__ + "'")


        # Role Assignment
        ################################################################################
        try:
            assignments = role_assignment.assign_roles(self.robots, self.role_requirements())
        except role_assignment.ImpossibleAssignmentError as e:
            logging.error("Unable to satisfy role assignment constraints.  Dropping and temp. blacklisting current play...")
            self.drop_current_play(temporarily_blacklist=True)
        else:
            self.assign_roles(assignments)