# 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 ))
scorer = L2DistanceCost(roads) solver = GreedyStochasticSolver(roads, mapAstar, scorer, Consts.STOCH_INITIAL_TEMPERATURE, Consts.STOCH_TEMPERATURE_DECAY_FUNCTION, Consts.STOCH_TOP_SCORES_TO_CONSIDER) REPEATS = 200 results = [solver.solve(prob).getDistance() / 1000 for _ in range(REPEATS)] print("Stochastic ({} repetitions): {}km".format(REPEATS, min(results))) ############ A* solver ############ # Run A* with the zero heuristic busAstar = AStar(NullHeuristic(), cost=ActualDistanceCost(roads, mapAstar)) _, gBus, hVal, developed = busAstar.run(prob) print( "A* (null heuristic):\tg(G)={:.2f}km, h(I)={:.2f}km, developed: {} states". format(gBus / 1000, hVal / 1000, developed)) # Run A* with the custom heuristic customH = TSPCustomHeuristic(roads, prob.initialState) busAstar = AStar(customH, cost=ActualDistanceCost(roads, mapAstar)) _, gBus, hVal, developed = busAstar.run(prob) print( "A* (Custom heuristic):\tg(G)={:.2f}km, h(I)={:.2f}km, developed: {} states" .format(gBus / 1000, hVal / 1000, developed)) # Run A* with the MST heuristic tspH = MSTHeuristic(roads, prob.initialState, ActualDistanceCost(roads, mapAstar))