示例#1
0
文件: stochastic.py 项目: orgoaz/AI
sys.path.insert(0, os.getcwd())

from consts import Consts
from astar import AStar
from ways import load_map_from_csv
from busSolvers import GreedyBestFirstSolver, GreedyStochasticSolver
from problems import BusProblem
from costs import L2DistanceCost
from heuristics import L2DistanceHeuristic
import numpy as np

REPEATS = 150

# Load the files
roads = load_map_from_csv(Consts.getDataFilePath("israel.csv"))
prob = BusProblem.load(Consts.getDataFilePath("HAIFA_100.in"))

mapAstar = AStar(L2DistanceHeuristic(), shouldCache=True)

scorer = L2DistanceCost(roads)

# Run the greedy solver
pickingPath = GreedyBestFirstSolver(roads, mapAstar, scorer).solve(prob)
greedyDistance = pickingPath.getDistance() / 1000
print("Greedy solution: {:.2f}km".format(greedyDistance))

# Run the stochastic solver #REPATS times
solver = GreedyStochasticSolver(roads, mapAstar, scorer,
                                Consts.STOCH_INITIAL_TEMPERATURE,
                                Consts.STOCH_TEMPERATURE_DECAY_FUNCTION,
                                Consts.STOCH_TOP_SCORES_TO_CONSIDER)
示例#2
0
##########################################
from consts import Consts
from ways import load_map_from_csv
from problems import BusProblem, MapProblem
from matplotlib import pyplot as plt
from ways.draw import plotPath, plotOrders
from astar import AStar
from heuristics import L2DistanceHeuristic
from path import Path
import pickle

# Read files
roads = load_map_from_csv(Consts.getDataFilePath("israel.csv"))
#pickle.dump(roads, open( "save.p", "wb" ) )
# roads = pickle.load(open( "save.p", "rb"))
prob = BusProblem.load(Consts.getDataFilePath("TLV_5.in"))

mapAstar = AStar(L2DistanceHeuristic(), shouldCache=True)

# 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