Beispiel #1
0
    def compute(self, fromState, toState):
        from problems import MapProblem

        mapSubProblem = MapProblem(self.roads, fromState.junctionIdx,
                                   toState.junctionIdx)
        _, l, _, _ = self.astar.run(mapSubProblem)
        return l
Beispiel #2
0
    def solve(self, problem):
        pickingOrder = self._findPickingOrder(problem)

        pickingPath = [problem.initialState.junctionIdx]

        # Build the picking path itself using A* between every two locations in the picking order
        for source, target in zip(pickingOrder[:-1], pickingOrder[1:]):
            mapSubProblem = MapProblem(self.roads, source, target)
            statesSubpath = self.astar.run(mapSubProblem)[0]

            delta = [s.junctionIdx for s in statesSubpath[1:]]

            pickingPath = pickingPath + delta

        from path import Path
        return Path(self.roads, pickingPath)
# Plot the orders
plotOrders(roads, prob.orders)
plt.title("Showing orders. Click to show path")
plt.title("Showing orders. Click to show path")
plt.show(block=False)
plt.waitforbuttonpress()

colors = ['red', 'blue', 'green', 'orange', 'grey']

plt.title("Showing paths")

totalDistance = 0
# Plot each order at a time
for i,order in enumerate(prob.orders):
    # Create a sub-problem to solve with A* (getting the optimal path for each order separately)
    subProblem = MapProblem(roads, order[0], order[1])

    subPath, distance, _, _ = mapAstar.run(subProblem)

    print("Shortest distance for order from #{} to #{}: {:.2f}km".format(order[0], order[1], distance / 1000))
    totalDistance += distance

    # Plot the path
    plotPath(Path(roads, [s.junctionIdx for s in subPath]), color=colors[i])
    plt.show(block=False)
    plt.waitforbuttonpress()

print("Total distance: {:.2f}km".format(totalDistance / 1000    ))