0

I tried to program an AI. I made this script for path finding with Breadth First Search but it seems to be slow. I don't how to make it faster. Please help me. By the way if you need i used this tutorial https://www.youtube.com/watch?v=hettiSrJjM4

import queue


class AIRoadMap(object):
    def __init__(self, game):
        self.game = game
        self.road_list = []
        self.initialize_road_list()

    def initialize_road_list(self):
        # This method adds all empty coordinates to list (self.road_list)
        for y_row in self.game.map:
            y_coord = self.game.map.index(y_row)
            for x in y_row:
                if x['symbol'] == '.':
                    x_coord = y_row.index(x)
                    self.road_list.append({'x' : x_coord, 'y' : y_coord})

    def check_valid_moves(self, x_from, y_from, moves):
        unreal_x = x_from
        unreal_y = y_from
        valid = False
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1
            valid = False
            for item in self.road_list:
                if item['x'] == unreal_x and item['y'] == unreal_y:
                    valid = True
            if valid is False:
                break
        return valid

    def IsPathEnd(self, x_from, y_from, to_x, to_y, moves):
        unreal_x = x_from
        unreal_y = y_from
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1

        if unreal_y == to_y and unreal_x == to_x:
            return True
        if unreal_x != to_x or unreal_y != to_y:
            return False
        if unreal_x != to_x and unreal_y != to_y:
            return False

    def generate_road(self, from_x, to_x, from_y, to_y):
        nums = queue.Queue()
        nums.put("")
        add = ""

        while not self.IsPathEnd(from_x, from_y, to_x, to_y, add):
            add = nums.get()
            # print(add)
            for j in ["L", "R", "U", "D"]:
                put = add + j
                if self.check_valid_moves(from_x, from_y, put):
                    if len(put) < 3:
                        nums.put(put)
                    else:
                        if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":
                            nums.put(put)
                    #nums.put(put)

        return add

Method initialize_road_list() adds all empty coordinates to self.road_list from self.game.map which is basically a grid

1 Answers1

0

There seems to be a lot of possible improvements to your code, I'll suggest just a few at the moment:

1) replace:

        valid = False
        for item in self.road_list:
            if item['x'] == unreal_x and item['y'] == unreal_y:
                valid = True
        if valid is False:
            break
    return valid

with

       if not any(item['x'] == unreal_x and item['y'] == unreal_y for item in self.road_list):
           return False
   return True

2) replace:

    if unreal_y == to_y and unreal_x == to_x:
        return True
    if unreal_x != to_x or unreal_y != to_y:
        return False
    if unreal_x != to_x and unreal_y != to_y:
        return False

with:

    return unreal_y == to_y and unreal_x == to_x

3) replace:

if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":

with:

if put[-2:] in frozenset(('LR', 'RL', 'UD', 'DU')):
Błotosmętek
  • 12,717
  • 19
  • 29
  • Ok I modificated the code the way you showed but third replace basically freezes it – ToggleMountain Feb 24 '20 at 15:16
  • The biggest improvement would probably come from changing the `check_valid_moves` function. At present, you're iterating through the list of `items` (I presume those are "passable squares"); but this list is generated from a grid (`self.game.map`) - checking what is at coords x, y in the grid would be much faster than looking for an item with those coordinates (no loop required). – Błotosmętek Feb 25 '20 at 08:52
  • Błotosmętek I changed the code like you did (replace 1) with `any` so technically it doesn't contain loop right? – ToggleMountain Feb 25 '20 at 13:23