5"""Search a weighted directed graph for one short route per endpoint pair."""
7from argparse
import ArgumentParser
9from time
import perf_counter
12from scipy.sparse
import csr_matrix
13from scipy.sparse.csgraph
import dijkstra
18EDGES = tuple((node, (node + step) % SIZE, 1 + (node * 3 + step * step) % 9)
19 for node
in range(SIZE)
for step
in (1, -1, 4))
22def comparison(budget, seed):
23 """Score discovered routes against exact achievable route costs."""
24 rows, columns, weights = zip(*EDGES)
25 graph = csr_matrix((weights, (rows, columns)), shape=(SIZE, SIZE))
26 optimal = dijkstra(graph, directed=
True)
27 reachable = int(np.isfinite(optimal).sum()) - SIZE
28 for name, probability, mode
in [(
"qd-ea", 0,
"uniform"), (
"qd-ga-uniform", 0.5,
"uniform"),
29 (
"qd-ga-feasible", 0.5,
"feasible")]:
30 start = perf_counter()
31 result =
graph_path_qd(EDGES, nodes=range(SIZE), budget=budget, crossover_probability=probability,
32 crossover=mode, seed=seed)
33 seconds = perf_counter() - start
34 ratios = [route.cost / optimal[pair]
for pair, route
in result.routes.items()]
35 yield dict(method=name, seed=seed, attempts=result.attempts, valid_offspring=result.evaluations,
36 initial_routes=result.initial_routes, coverage=len(ratios) / reachable,
37 optimal_pair_fraction=sum(ratio == 1
for ratio
in ratios) / reachable,
38 mean_stretch=float(np.mean(ratios)), seconds=seconds)
41if __name__ ==
"__main__":
42 parser = ArgumentParser(description=__doc__)
43 parser.add_argument(
"--budget", type=int, default=1024)
44 parser.add_argument(
"--seeds", type=int, nargs=
"+", default=[11, 29, 47])
45 args = parser.parse_args()
46 for seed
in args.seeds:
47 for row
in comparison(args.budget, seed):
48 print(json.dumps(row, sort_keys=
True, allow_nan=
False))