예제 #1
0
def robot_faire_prochain_mouvement_test():
    grille = Grille(10, 10)
    point_montage = PointMontage(5, 5)
    point_montage_bis = PointMontage(6, 6)
    grille.add_point_montage(point_montage)
    grille.add_point_montage(point_montage_bis)

    etape = Etape(5, 6)
    tache = Tache(15, 0)
    tache.add_etape(etape)

    robot = Robot()
    robot.point_montage = point_montage
    robot.add_tache(tache, grille)
    robot.mouvements.append(Mouvement.HAUT)
    robot.mouvements.append(Mouvement.DROITE)
    grille.robots.append(robot)

    # tout va bien
    robot.faire_prochain_mouvement(grille)
    assert len(grille.cases[6][5]) == 1 and isinstance(grille.cases[6][5][0],
                                                       Bras)
    assert len(robot.bras) == 1
    assert len(robot.mouvements) == 1

    assert grille.points == 15
    assert not len(robot.taches)

    # collision avec point de montage bis
    try:
        robot.faire_prochain_mouvement(grille)
    except ConnectionError:
        pass
    else:
        assert False
예제 #2
0
def robot_coordonnees_pince_test():
    grille = Grille(10, 10)
    point_montage = PointMontage(5, 5)
    grille.add_point_montage(point_montage)

    robot = Robot()
    robot.point_montage = point_montage
    assert robot.coordonnees_pince() == robot.point_montage

    # simulation mouvement haut
    bras = Bras(5, 6)
    robot.bras.append(bras)
    grille.cases[6][5].append(bras)
    assert robot.coordonnees_pince() == bras
예제 #3
0
def robot_add_tache_test():
    grille = Grille(10, 10)
    point_montage = PointMontage(5, 5)
    etape_1 = Etape(5, 6)
    etape_2 = Etape(5, 7)
    tache = Tache(15, 0)
    tache.add_etape(etape_1)
    tache.add_etape(etape_2)
    grille.add_tache(tache)
    robot = Robot()
    robot.point_montage = point_montage
    robot.add_tache(tache, grille)

    assert len(robot.taches)
    assert not len(grille.taches)
예제 #4
0
def robot_get_plus_proche_tache_test():
    grille = Grille(10, 10)
    point_montage = PointMontage(5, 5)
    etape_1 = Etape(5, 6)
    etape_2 = Etape(5, 7)
    tache = Tache(15, 0)
    tache.add_etape(etape_1)
    tache.add_etape(etape_2)
    grille.add_tache(tache)
    robot = Robot()
    robot.point_montage = point_montage
    robot.add_tache(robot.get_plus_proche_tache(grille), grille)
    assert not robot.get_plus_proche_tache(grille)
예제 #5
0
def grille_start_simulation_test():
    grille = Grille(10, 10)
    grille.step_simulation = 2
    point_montage = PointMontage(5, 5)
    point_montage_bis = PointMontage(6, 6)
    grille.add_point_montage(point_montage)
    grille.add_point_montage(point_montage_bis)

    etape_1 = Etape(5, 6)
    etape_2 = Etape(5, 7)
    tache = Tache(15, 0)
    tache.add_etape(etape_1)
    tache.add_etape(etape_2)
    grille.add_tache(tache)

    robot = Robot()
    robot.point_montage = point_montage
    robot.add_tache(tache, grille)
    robot.mouvements.append(Mouvement.HAUT)
    robot.mouvements.append(Mouvement.HAUT)
    grille.robots.append(robot)

    grille.start_simulation()
    print(grille.points)
예제 #6
0
def robot_faire_prochain_mouvement_retractation_test():
    grille = Grille(10, 10)
    point_montage = PointMontage(5, 5)
    grille.add_point_montage(point_montage)
    robot = Robot()
    robot.point_montage = point_montage
    robot.mouvements.append(Mouvement.DROITE)
    robot.mouvements.append(Mouvement.BAS)

    assert not robot.faire_prochain_mouvement_retractation(grille)

    # simulation mouvement haut
    bras = Bras(5, 6)
    robot.bras.append(bras)
    grille.cases[6][5].append(bras)

    # pas de rétractation car mouvement a droit et non vers le bas
    assert not robot.faire_prochain_mouvement_retractation(grille)
    robot.mouvements.pop(0)

    assert robot.faire_prochain_mouvement_retractation(grille)
    assert len(robot.bras) == 0
    for item in grille.cases[6][5]:
        assert not isinstance(item, Bras)
예제 #7
0
    def to_matrix(self,
                  x1: int,
                  y1: int,
                  x2: int,
                  y2: int,
                  robot: Robot = None) -> List:
        """Renvoie la grille sous forme d'une matrice.

        0 pour un point de montage
        -1 pour un bras d'autre robots (avec ou sans etape)
        1 pour les bras du robot actuel (si lieu)
        100 pour le vide
        150 pour une etape

        :param robot: Le point de vue du robot actuel (notamment pour la rétractation)
        :type: Robot

        :param x1: coordonée x
        :param y1: coordonée y
        :type: int

        :param x2: coordonée x
        :param y2: coordonée y
        :type: int

        :return : La carte en deux dimenssions
        :rtype : List
        """
        assert x1 <= x2
        assert y1 <= y2

        map: List = []
        if robot is not None:
            pince = robot.coordonnees_pince()

        for y in range(y1, y2 + 1):
            map.append([])
            for x in range(x1, x2 + 1):
                if x < 0 or x >= self.longueur or y < 0 or y >= self.hauteur:
                    map[y - y1].append(-2)
                else:
                    if len(self.cases[y][x]) == 1:
                        if isinstance(self.cases[y][x][0], PointMontage):
                            # il s'agit d'un point de montage
                            map[y - y1].append(0)

                        elif isinstance(self.cases[y][x][0], Bras):
                            # le bras du robot ?
                            ok = False
                            if robot is not None \
                                    and (pince == self.cases[y][x][0] or
                                         (len(robot.bras) > 2 and self.cases[y][x][0] == robot.bras[-2]) or
                                         (len(robot.bras) == 1 and self.cases[y][x][0] == robot.point_montage)):
                                ok = True
                                # c'est une pince de robot, la dernière case d'un bras
                                map[y - y1].append(1)

                            if not ok:
                                # c'est le bras d'un robot
                                map[y - y1].append(-1)

                        elif isinstance(self.cases[y][x][0], Etape):
                            # il s'agit d'une étape
                            map[y - y1].append(150)

                    elif len(self.cases[y][x]) == 2:
                        ok = False
                        if robot is not None \
                                and (pince == self.cases[y][x][0] or
                                  (len(robot.bras) > 2 and self.cases[y][x][0] == robot.bras[-2]) or
                                  (len(robot.bras) == 1 and self.cases[y][x][0] == robot.point_montage)):
                            ok = True
                            #c'est une pince de robot, la dernière case d'un bras
                            map[y - y1].append(1)
                        if not ok:
                            #c'est un bras de robot
                            map[y - y1].append(-1)
                    else:
                        #c'est une case vide
                        map[y - y1].append(100)
        return map
예제 #8
0
    def parse(self, file_path: str) -> Grille:
        """parse le fichier google et retourne la Grille correspondante

        :rtype: Grille
        """

        # tests si file_path est un fichier
        assert os.path.isfile(file_path)

        with open(file_path, 'r') as file:
            index: int = 0

            # récupère toutes les lignes du fichiers
            lines: List = file.readlines()

            # Transformation des lignes en liste d'entiers
            for index_line in range(len(lines)):
                lines[index_line] = lines[index_line].split(' ')
                for index_val in range(len(lines[index_line])):
                    lines[index_line][index_val] = int(
                        lines[index_line][index_val])

            # crée un instance de Grille
            grille = Grille(lines[0][0], lines[0][1])

            # instancie dans grille le nombre de robot correspondant
            # crée les robots
            for idx_robot in range(lines[0][2]):
                grille.robots.append(Robot())

            # Crée les points de montage, et les place dans la grille
            for idx_point_montage in range(lines[0][3]):
                index += 1
                grille.add_point_montage(
                    PointMontage(lines[index][0], lines[index][1]))

            # Récupère le nombre de tour d'horloge autorisé
            grille.step_simulation = lines[0][5]

            # Récupére les informations de chaque tâche
            # instancier dans grille les tâches correspondantes
            # si une étape (assembly point) n'est pas encore créée dans la grille aux cordonnées correspondantes,
            # l'instancier et la mettre dans la grille (et ne pas oublier de l'associer à la tâche)

            # Crée les instances Taches et Etapes
            for index_tache in range(lines[0][4]):
                index += 1
                tache_tampon: Tache = Tache(lines[index][0], index_tache)
                index += 1

                g_x = 0
                g_y = 0
                for index_etape in range(lines[index - 1][1]):
                    #ajoute les étapes
                    etape = Etape(lines[index][index_etape * 2 + 0],
                                  lines[index][index_etape * 2 + 1])
                    tache_tampon.add_etape(etape)
                    g_x += (etape.x - g_x) / len(tache_tampon.etapes)
                    g_y += (etape.y - g_y) / len(tache_tampon.etapes)
                #ajoute les paramètres dans la classe tache
                tache_tampon.centre_gravite = ItemCase(int(g_x), int(g_y))
                tache_tampon.distance_centre_gravite = max(tache_tampon.etapes,
                                                           key=lambda etape: tache_tampon.centre_gravite.distance(etape)) \
                    .distance(tache_tampon.centre_gravite)
                grille.add_tache(tache_tampon)

                # calcul la distance et la surface aproximative entre chaque étape
                for etape_from, etape_to in zip(tache_tampon.etapes[0::1],
                                                tache_tampon.etapes[1::1]):
                    tache_tampon.distance += etape_from.distance(etape_to)
                    tache_tampon.surface += etape_from.distance(etape_to)

            return grille