<commit_before>"""
<NAME> -- LAAS-CNRS -- 2014

TODO Descriptif
"""

# FIXME ensure that all geomaps are concistant ??
# (or deal with it ?!)

import json
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np
from pprint import pprint

from geomaps import *
from glpk_solver import *
from robot import Robot

from constant import *
from timer import Timer

class Mission:
    'Class describing and planning a mission'

    """ Init a mission upon a descriptive dictionary <mission>.
    This dictionary can be obtain through the load_mission function. """
    def __init__(self, _mission):

        self.mission    = _mission
        self.map_file   = self.mission[u'map']
        self.sampling   = self.mission[u'sampling']
        self.period     = self.mission[u'period']

        self.map        = UtilityMap(self.map_file)

        self.team = []
        for robot in self.mission[u'team']:
            self.team.append( Robot( robot ) )

        self.points = [] # the sampled points to be observed

        self.solver = GLPKSolver(self)

        self.loop_step = 0

    """ Sample the observable positions (= the objective).
    Assume that the map is a distribution of probabilily,
    e.g the value are the utility. The sampled points are far apart, with a
    minimal distance between them. """
    def sample_objective(self):
        apply_scale = self.map.length_meter2pix

        self.points = sampled_points( self.map, self.sampling, \
                min_dist = apply_scale( MIN_SAMPLING_DIST ) )

    """ Sample accessible positions each robot of the team """
    def sample_all_positions(self):
        for robot in self.team:
            robot.sample_positions( self.map, self.points, self.period )

    """ Solve (using glpk) """
    def solve(self, milp_formulation = 'Perception-based TSP'):

        available_milp_formulation = {
            'Perception-based TSP': self.solver.solve_perception_tsp,
            'Position-based TSP'  : self.solver.solve_position_tsp,
        }

        try:
            solver = available_milp_formulation[ milp_formulation ]
        except KeyError:
            print "!ERROR! '{}' is not a known MILP formulation. Problem remains unsolved.".format(milp_formulation)
        else:
            solver()

    """ Perform a whole planning loop. """
    def loop_once(self, DISPLAY = False):

        self.loop_step += 1
        print "Patrolling -- loop #{}".format(self.loop_step)

        with Timer('Updating poses and utility map'):
            self.update_poses()
            self.update_map()

        with Timer('Sampling observable points'):
            self.sample_objective()
        with Timer('Sampling positions'):
            self.sample_all_positions()

        with Timer('Solving'):
            self.solve()

        if DISPLAY:
            for robot in self.team:
                robot.display_weighted_map()
            self.display_situation()

    """ Update the robots poses according to <p>, a vector of positions. If no
    <p> is provided, then use the current robot's plan and set the pose to the
    last position of this plan."""<commit_msg>Add a loop n times function for mission.py<commit_after>"""
<NAME> -- LAAS-CNRS -- 2014

TODO Descriptif
"""

# FIXME ensure that all geomaps are concistant ??
# (or deal with it ?!)

import json
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np
from pprint import pprint

from geomaps import *
from glpk_solver import *
from robot import Robot

from constant import *
from timer import Timer

class Mission:
    'Class describing and planning a mission'

    """ Init a mission upon a descriptive dictionary <mission>.
    This dictionary can be obtain through the load_mission function. """
    def __init__(self, _mission):

        self.mission    = _mission
        self.map_file   = self.mission[u'map']
        self.sampling   = self.mission[u'sampling']
        self.period     = self.mission[u'period']

        self.map        = UtilityMap(self.map_file)

        self.team = []
        for robot in self.mission[u'team']:
            self.team.append( Robot( robot ) )

        self.points = [] # the sampled points to be observed

        self.solver = GLPKSolver(self)

        self.loop_step = 0

    """ Sample the observable positions (= the objective).
    Assume that the map is a distribution of probabilily,
    e.g the value are the utility. The sampled points are far apart, with a
    minimal distance between them. """
    def sample_objective(self):
        apply_scale = self.map.length_meter2pix

        self.points = sampled_points( self.map, self.sampling, \
                min_dist = apply_scale( MIN_SAMPLING_DIST ) )

    """ Sample accessible positions each robot of the team """
    def sample_all_positions(self):
        for robot in self.team:
            robot.sample_positions( self.map, self.points, self.period )

    """ Solve (using glpk) """
    def solve(self, milp_formulation = 'Perception-based TSP'):

        available_milp_formulation = {
            'Perception-based TSP': self.solver.solve_perception_tsp,
            'Position-based TSP'  : self.solver.solve_position_tsp,
        }

        try:
            solver = available_milp_formulation[ milp_formulation ]
        except KeyError:
            print "!ERROR! '{}' is not a known MILP formulation. Problem remains unsolved.".format(milp_formulation)
        else:
            solver()

    """ Perform one whole planning loop, <n> times. """
    def loop(self, n, DISPLAY = False):

        for i in range(n):
            self.loop_once(DISPLAY)

    """ Perform one whole planning loop. """
    def loop_once(self, DISPLAY = False):

        self.loop_step += 1
        print "Patrolling -- loop #{}".format(self.loop_step)

        with Timer('Updating poses and utility map'):
            self.update_poses()
            self.update_map()

        with Timer('Sampling observable points'):
            self.sample_objective()
        with Timer('Sampling positions'):
            self.sample_all_positions()

        with Timer('Solving'):
            self.solve()

        if DISPLAY:
            for robot in self.team:
                robot.display_weighted_map()
            self.display_situation()

    """ Update the robots poses according to <p>, a vector of positions. If no
    <p> is provided, then use the current robot's plan and set the pose to the
    last position of this plan."""
{'fraction_non_alphanumeric': 0.09315531000613873, 'fraction_numerical': 0.002148557397176182, 'mean_word_length': 2.4390501319261215, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5160382', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1805, 'n_words': 585}