def main(): # parse command line arguments and parse function parsed = parse_args() func = make_func(parsed.func) # optimize 2d parameter vector v of function path = optimize(func, parsed.start, parsed.num_iter, parsed.lr, parsed.beta_g, parsed.beta_h, parsed.hessian_pow, parsed.num_samples) # plot function and steps of optimizer plot_function(func, parsed.window) plot_path(path) show_plot()
print "running: " + revised_command print subprocess.check_output(revised_command.split()) # Get distributions distributions = [] for f in files: with open(f) as json_data: d = json.load(json_data) distributions.append(d) print len(distributions) # Plot if args.save_plot or args.show_plot: print args.type if args.type is None or args.type == 'path': plot.plot_path(distributions, labels, args.show_plot, args.save_plot, args.title) elif args.type == 'time': plot.plot_time(distributions, labels, args.show_plot, args.save_plot, args.title) elif args.type == 'time_full': plot.plot_time_full(distributions, labels, args.show_plot, args.save_plot, args.title) elif args.type == 'time_gif': plot.plot_time_gif(distributions, labels, args.show_plot, args.save_plot, args.title) elif args.type == 'realization': plot.plot_realization(distributions, labels, args.show_plot, args.save_plot, args.title) # Delete the files if keep=True if not args.keep: for f in files: try: os.remove(f)
def test(num_of_node, side_length=100, plot=False): network = Network(num_of_node, side_length) edges_set = [] cost_set = [] run_time_set = [] ### heuristic 1 - greedy start = time.time() edges, cost = greedy(network) run_time = time.time() - start edges_set.append(edges) cost_set.append(cost) run_time_set.append(run_time) print("greedy heuristic has cost of {:.2f} using {:.4f}s".format( cost, run_time)) ### heuristic 2 - two opt start = time.time() edges, cost = two_opt(network) run_time = time.time() - start edges_set.append(edges) cost_set.append(cost) run_time_set.append(run_time) print("two-opt heuristic has cost of {:.2f} using {:.4f}s".format( cost, run_time)) ### mcts 1 - random start = time.time() random_mcts = RandomMCTS(network) edges, cost = random_mcts.run( 50, 10, 1000) # run takes (number to expand, number to simulate, ## and constant C) as input run_time = time.time() - start edges_set.append(edges) cost_set.append(cost) run_time_set.append(run_time) print("random mcts has cost of {:.2f} using {:.4f}s".format( cost, run_time)) ### mcts 2 - greedy start = time.time() greedy_mcts = GreedyMCTS(network, 0.2) edges, cost = greedy_mcts.run( 50, 10, 100) # run takes (number to expand, number to simulate, ## and constant C) as input run_time = time.time() - start edges_set.append(edges) cost_set.append(cost) run_time_set.append(run_time) print("greedy mcts has cost of {:.2f} using {:.4f}s".format( cost, run_time)) if plot == True: fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8)) fig.suptitle( "Path Found by Different Models (num_of_node={:d}, side_length={:d})" .format(num_of_node, side_length)) model_names = [ 'greedy heuristic', '2-opt heuristic', 'random mcts', 'greedy mcts' ] for i in range(4): plot_path(axs[int(i / 2), i % 2], model_names[i], cost_set[i], run_time_set[i], network.graph.nodes, edges_set[i], network.positions) plt.show()
#Regularization Lasso, Ridge from sklearn.linear_model import Lasso, Ridge alphas = [.01, .1, .5] for alpha in alphas: clf = Ridge(alpha=alpha) clf, train_score, test_score = train_model(clf, cv, X, y) print("Ridge, alpha = {} :\n Train Accuracy: {}\n Test Accuracy: {}". format(alpha, train_score, test_score)) clf = Lasso(alpha=alpha, normalize=False) clf, train_score, test_score = train_model(clf, cv, X, y) print("Lasso, alpha = {} :\n Train Accuracy: {}\n Test Accuracy: {}". format(alpha, train_score, test_score)) #no significant improvement in R2 scores #Visualizing lasso path alphas = np.logspace(-3, 2, 100) lasso = Lasso() alphas, coefs, _ = lasso.path(X, y, alphas=alphas) plot_path(alphas, coefs.T, "Lasso path(Coefficient weights vs Alpha)") #visualizing ridge path alphas = np.logspace(-3, 5, 100) ridge = Ridge() coefs = [] for alpha in alphas: ridge.set_params(alpha=alpha) ridge.fit(X, y) coefs.append(ridge.coef_) plot_path(alphas, coefs, "Ridge path(Coefficient weights vs Alpha)")
############################################################################### ## Plotting method_dirs = ['curvature', 'compromise', 'laptime', 'sectors', 'estimated'] plot_dir = os.path.join( os.path.dirname(__file__), '..', 'data', 'plots', track.name, method_dirs[args.method] ) if not os.path.exists(plot_dir): os.makedirs(plot_dir) if args.plot_corners or args.plot_all: plot_corners( os.path.join(plot_dir, "corners." + args.ext), track.left, track.right, track.mid.position(trajectory.s), track.corners(trajectory.s, K_MIN, PROXIMITY, LENGTH)[1] ) if args.plot_path or args.plot_all: plot_path( os.path.join(plot_dir, "path." + args.ext), track.left, track.right, trajectory.path.position(trajectory.s), trajectory.path.controls ) if args.plot_trajectory or args.plot_all: plot_trajectory( os.path.join(plot_dir, "trajectory." + args.ext), track.left, track.right, trajectory.path.position(trajectory.s), trajectory.velocity.v )
path_quality.to_csv() # Modelling and soliving models using Gurobi slover and Heurestic arc_orders = minimise_expected_time(test_data) arc_orders2 = maximise_probability(test_data) arc_orders3 = greedy1(test_data) # Plotting plot_coordinates(xc, yc) plot_path(xc, yc, arc_orders) plot_path(xc, yc, arc_orders2) plot_path(xc, yc, arc_orders3) expected_time_find_gas(arc_orders, test_data) expected_time_travel(arc_orders, test_data) probability(arc_orders, test_data) # Random data testing n = [5] seeds = [x for x in range(1,2)] data = [[i, j, random_data(i,j)] for i in n for j in seeds] for x in data: