def smoke_test_3opt(self): N, points, _, _, D, _, _ = generate_CVRP(50, 40, 10, 5) current_sol = range(N) while True: sol = self.move_op(current_sol, D) if sol: current_sol = sol else: break
def _random_cvrp(): size = randint(5, 8) C = 50 K = randint(2, max(3, int(size / 3))) muC = K * C / size sdC = randint(2, max(3, int(C / K))) N, points, _, d, D, C, _ = generate_CVRP(size, C, muC, sdC) if randint(1, 5) != 1: d = [int(e) for e in d] if randint(1, 2) != 1: D = D.astype(int) return N, points, d, D, C
def setUp(self): N = TestRandomStressOn3OptStarSolutionOperator.problem_size problem = generate_CVRP(N, 50, 10, 5) aN,pts,d,D,C = (problem.size, problem.coordinate_points, problem.customer_demands, problem.distance_matrix, problem.capacity_constraint) pts = None d = [int(dv) for dv in d] D = D.astype(int) problem = aN,pts,d,D,C self.naive_sol = routes2sol( [[n] for n in range(1,aN+1)] ) self.nn_sol = nearest_neighbor_init(D, d, C) self.L = max( calculate_objective(r,D) for r in sol2routes(self.nn_sol) ) self.N = aN self.problem = (aN,pts,d,D,C)
def cli(init_name, init_desc, init_f): ## Simple command line interface single = False # ask to run only single iteration of the algorithm measure_time = False verbosity = DEFAULT_DEBUG_VERBOSITY minimize_K = False output_logfilepath = None best_of_n = 1 interrupted = False for i in range(0, len(sys.argv) - 1): if sys.argv[i] == "-v" and sys.argv[i + 1].isdigit(): verbosity = int(sys.argv[i + 1]) if sys.argv[i] == "-n" and sys.argv[i + 1].isdigit(): best_of_n = int(sys.argv[i + 1]) if sys.argv[i] == "-1": single = True if sys.argv[i] == "-t": measure_time = True if sys.argv[i] == "-l": output_logfilepath = sys.argv[i + 1] if sys.argv[i] == "-b": otarget = sys.argv[i + 1].lower() if otarget == "cost" or otarget == "c": minimize_K = False elif otarget == "vehicles" or otarget == "k": minimize_K = True else: print("WARNING: Ignoring unknown optimization target %s" % otarget) if verbosity >= 0: set_logger_level(verbosity, logfile=output_logfilepath) if sys.argv[-1].isdigit(): N = int(sys.argv[-1]) problem_name = "random " + str(N) + " point problem" N, points, _, d, D, C, _ = cvrp_io.generate_CVRP(N, 100, 20, 5) d = [int(de) for de in d] D_c = D L, st = None, None wtt = "EXACT_2D" best_sol = None best_f = float('inf') best_K = len(D) for i in range(best_of_n): sol, sol_f, sol_K = None, float('inf'), float('inf') try: sol = init_f(points, D_c, d, C, L, st, wtt, single, minimize_K) except KeyboardInterrupt as e: print("WARNING: Solving was interrupted, returning " + "intermediate solution", file=sys.stderr) interrupted = True # if interrupted on initial sol gen, return the best of those if len(e.args) > 0 and type(e.args[0]) is list: sol = e.args[0] if sol: sol = cvrp_ops.normalize_solution(sol) sol_f = objf(sol, D_c) sol_K = sol.count(0) - 1 if is_better_sol(best_f, best_K, sol_f, sol_K, minimize_K): best_sol = sol best_f = sol_f best_K = sol_K if interrupted: break print_solution_statistics(best_sol, D, D_c, d, C, L, st, verbosity=verbosity) problem_file_list = get_a_problem_file_list([sys.argv[-1]]) if not problem_file_list or "-h" in sys.argv or "--help" in sys.argv: print ("Please give a TSPLIB file to solve with "+\ init_name+\ " OR give N (integer) to generate a random problem of N customers."+\ " OR give a path to a folder with .vrp files."+\ "\n\nOptions (before the file name):\n"+\ " -v <int> to set the verbosity level (default %d)\n"%DEFAULT_DEBUG_VERBOSITY+\ " -n <int> run the algorithm this many times and return only the best solution\n"+\ " -1 to run only one iteration (if applicable)\n"+\ " -t to print elapsed wall time\n"+\ " -l <file_path> to store the debug output to a file\n"+\ " -b <'cost'|'vehicles'> or <c|K> sets the primary optimization oBjective (default is cost)", file=sys.stderr) elif problem_file_list: for problem_path in problem_file_list: problem_name = path.basename(problem_path) print("Solve", problem_name, "with", init_name) read_and_solve_a_problem(problem_path, init_f, minimize_K, best_of_n, verbosity, single, measure_time)