From af6e56fa29faabd193672dcc1fb12a6d991a3ae6 Mon Sep 17 00:00:00 2001 From: Kamila Bobkowska Date: Fri, 1 May 2020 11:14:32 +0000 Subject: [PATCH] adding --- route-planning.md | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 route-planning.md diff --git a/route-planning.md b/route-planning.md new file mode 100644 index 0000000..db5a099 --- /dev/null +++ b/route-planning.md @@ -0,0 +1,76 @@ +### 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. + +## 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 thorugh 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 ot has a lower g cost to f then we add it to the closed list. Change the predecessor, reassign fscore as gscore and calcualte the priority. At the end we add it to `openlist`. Then we reapet for the next neighbor untill 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]) +```