def solve(self, data, vehicles, timeout): """Solves the CVRP problem using Monte Carlo Savings method (OLIVEIRA, 2014) Parameters: data: CVRPData instance vehicles: Vehicles number timeout: max processing time in seconds Returns a solution (MonteCarloSavingsSolution class)) """ start = time.time() time_found = None best = MonteCarloSavingsSolution(data, vehicles) best_feasible = best savings_lists = self.compute_list_of_savings_list(data) solution_lengths = 0 processed_count = 0 for savings_list in savings_lists: solution = MonteCarloSavingsSolution(data, vehicles) for i, j in savings_list[:]: if solution.is_complete(): break if solution.can_process((i, j)): solution, inserted = solution.process((i, j)) if time.time() - start > timeout: break if time.time() - start > timeout: break if solution.is_complete() and not best_feasible.is_complete(): best_feasible = solution elif solution.is_complete() and solution.length() < best.length(): best_feasible = solution if solution.length() < best.length(): best = solution time_found = time.time() - start processed_count = processed_count + 1 solution_lengths = solution_lengths + solution.length() print 'best solution found on {}'.format(time_found) if processed_count: print 'average solution lengths: {}'.format(solution_lengths / float(processed_count)) if not best.is_complete(): from project import util print 'Best solution not feasible, printing best feasible found' util.print_solution(best_feasible) return best
def solve(self, data, vehicles, timeout): """Solves the CVRP problem using Monte Carlo Savings method (OLIVEIRA, 2014) Parameters: data: CVRPData instance vehicles: Vehicles number timeout: max processing time in seconds Returns a solution (MonteCarloSavingsSolution class)) """ start = time.time() time_found = None best = MonteCarloSavingsSolution(data, vehicles) best_feasible = best savings_lists = self.compute_list_of_savings_list(data) solution_lengths = 0 processed_count = 0 for savings_list in savings_lists: solution = MonteCarloSavingsSolution(data, vehicles) for i, j in savings_list[:]: if solution.is_complete(): break if solution.can_process((i, j)): solution, inserted = solution.process((i, j)) if time.time() - start > timeout: break if time.time() - start > timeout: break if solution.is_complete() and not best_feasible.is_complete(): best_feasible = solution elif solution.is_complete() and solution.length() < best.length(): best_feasible = solution best = solution time_found = time.time() - start processed_count = processed_count + 1 solution_lengths = solution_lengths + solution.length() print('best solution found after {} seconds'.format(time_found)) if processed_count: print('average solution lengths: {}'.format( solution_lengths / float(processed_count))) if not best.is_complete(): from project import util print('Best solution not feasible, printing best feasible found') util.print_solution(best_feasible) return best
def main(): if len(sys.argv) < 3: # python main.py <file> <vehicles_number> [<p>] return usage() input_file = sys.argv[1] data = data_input.read_file(input_file) vehicles = int(sys.argv[2]) lambda_p = None if sys.argv == 4: lambda_p = float(sys.argv[3]) clarke_wright_solver = clarke_wright.ClarkeWrightSolver() monte_carlo_savings_solver = monte_carlo_savings.MonteCarloSavingsSolver( lambda_p) binary_mcscws_solver = binary_mcscws.BinaryMCSCWSSolver() timeout = 300 algorithms = [(clarke_wright_solver, 'ClarkeWrightSolver'), (monte_carlo_savings_solver, 'MonteCarloSavingsSolver'), (binary_mcscws_solver, 'BinaryMCSCWSSolver')] best_algorithm = None best_solution = None for solver, algorithm in algorithms: print("=== Starting {} algorithm ===\n".format(algorithm)) start = time.time() solution = solver.solve(data, vehicles, timeout) elapsed = time.time() - start if not solution.is_complete(): print('Solution from algorithm {} not a complete solution'.format( algorithm)) print('{} solution:'.format(algorithm)) util.print_solution(solution) print('Elapsed time (seconds): {}'.format(elapsed)) if best_algorithm is None: best_algorithm = algorithm if best_solution is None: best_solution = solution if solution.length() < best_solution.length(): best_solution = solution best_algorithm = algorithm print("") print("=== Finished {} algorithm ===\n".format(algorithm)) print('Best solution for \"{}\" problem was from algorithm {}'.format( input_file, best_algorithm))
def main(): if len(sys.argv) < 3: # python main.py <file> <vehicles_number> [<p>] return usage() data = data_input.read_file(sys.argv[1]) vehicles = int(sys.argv[2]) lambda_p = None if sys.argv == 4: lambda_p = float(sys.argv[3]) clarke_wright_solver = clarke_wright.ClarkeWrightSolver() monte_carlo_savings_solver = monte_carlo_savings.MonteCarloSavingsSolver(lambda_p) binary_mcscws_solver = binary_mcscws.BinaryMCSCWSSolver() timeout = 300 algorithms = [ (clarke_wright_solver, 'ClarkeWrightSolver'), (monte_carlo_savings_solver, 'MonteCarloSavingsSolver'), (binary_mcscws_solver, 'BinaryMCSCWSSolver') ] best_algorithm = None best_solution = None for solver, algorithm in algorithms: start = time.time() solution = solver.solve(data, vehicles, timeout) elapsed = time.time() - start if not solution.is_complete(): print 'Solution from algorithm {} not a complete solution'.format(algorithm) print '{} solution:'.format(algorithm) util.print_solution(solution) print 'Elapsed time (seconds): {}'.format(elapsed) if best_algorithm is None: best_algorithm = algorithm if best_solution is None: best_solution = solution if solution.length() < best_solution.length(): best_solution = solution best_algorithm = algorithm print print 'Best: {}'.format(best_algorithm)
def main(): if len(sys.argv) < 2: # python main.py <file> <vehicles_number> return usage() data = data_input.read_file(sys.argv[1]) vehicles = 99 timeout = 300 #clarke_wright_solver = clarke_wright.ClarkeWrightSolver() binary_mcscws_solver = binary_mcscws.BinaryMCSCWSSolver() start = time.time() solution = binary_mcscws_solver.solve(data, vehicles, timeout) elapsed = time.time() - start routeList = util.print_solution(solution) print routeList print 'Elapsed time (seconds): {}'.format(elapsed)