AI2020_Project/route-planning.md

3.5 KiB

Report 2

General information

Costs of fields:

  • 0 - Garbage Dump
  • 1-5 - dumpsters
  • 10 - grass

This causes the garbage truck to sometimes move through already visited dumpsters to move more quickly. The garbage truck firstly visitis all houses and then visits the garbage dump

Main loop of A star

To implement A star we have used tuples and a priority queue. We start with an openlist with our starting node and cost of the node and then we add more nodes that are possible to visit to that list. If it did not find anything in openlist then it would mean that there is no target, but in our case that cannot happen. If the current node we are visiting is our goal then it means that we are done and can retrace our path. openlist represents nodes that are possible to visit.

    while  openlist!={}:
        current = openlist.get()

        if current == goal:
            path = []
            #following from the succesors to the root our starting point
            while current != start:
                path.append(current)
                current = origin[current]
            path.reverse()
            break

        # succescor function
        for succ in neighbors(current):
            #checking if didn't go out of the maze
            if(succ[0] < 0 or succ[1] < 0 or succ[0] > 19 or succ[1] > 19):
               continue

            gScore = fScore[current[0],current[1]] + checkCost(grid, current)
            if succ not in closedlist or gScore < fScore[succ[0],succ[1]]:
                closedlist.append(succ)
                origin[succ[0],succ[1]] = current
                fScore[succ[0],succ[1]] = gScore
                priority = gScore + heuristic(goal, succ)
                openlist.put(succ, priority)

Successor function

Our successor function goes through the neighbors. Firstly it checks whether the neighbor is in fact in the grid or if it is outside. If it is outside then we skip it. Then we calculate the g(the distance between our starting node and the current node). If the node has not been already visited or if it has a lower g cost then f, then we add it to the closed list. Change the predecessor, reassign fscore as gscore and calculate the priority. At the end we add it to openlist. Then we repeat for the next neighbor until all of them are visited(if possible).

#how we get nodes neighbors
def neighbors(point):
    x, y = point
    list=((x+1,y), (x,y+1), (x,y-1), (x-1,y))
    return list
    
# succescor function
        for succ in neighbors(current):
            #checking if didn't go out of the maze
            if(succ[0] < 0 or succ[1] < 0 or succ[0] > 19 or succ[1] > 19):
               continue

            gScore = fScore[current[0],current[1]] + checkCost(grid, current)
            if succ not in closedlist or gScore < fScore[succ[0],succ[1]]:
                closedlist.append(succ)
                origin[succ[0],succ[1]] = current
                fScore[succ[0],succ[1]] = gScore
                priority = gScore + heuristic(goal, succ)
                openlist.put(succ, priority)

Heuristic function

We actually have 2 heuristics. One in our main code. That we use for determining the closest dumpster and the garbage dump:

def closest(GT, D):
    point = np.column_stack((np.abs(GT[0]-D[:,0])+np.abs(GT[1]-D[:,1]),D))
    sorted_list = sorted(point, key=lambda x:x[0])
    return sorted_list[0][1:]

Second in our A star algorithm. To calculate the possible distance from the curent node to our goal:

def heuristic(xy1, xy2):
    return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])