Exemple #1
0
    def test_build(self):
        """Assert the Opt. model is built correctly"""

        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        problem = self.problem
        model = model_builder.build(problem)
        self.assertTrue(model, msg='Opt. model built incorrectly.')
        self.assertEqual(model.manager.GetNumberOfVehicles(),
                         len(self.vehicles),
                         msg='Number of vehicles in manager is incorrect.')
        self.assertEqual(model.manager.GetNumberOfIndices(),
                         len(self.vehicles) * 2 + len(self.stops) -
                         len(problem.depots),
                         msg='Number of indices in manager is incorrect.')
        self.assertTrue(model.solver, msg='Solver could not be instantiated.')
        self.assertTrue(model.search_parameters,
                        msg='Search params could not be built.')
        self.assertEqual(model.search_parameters.time_limit,
                         Duration(seconds=self.params.SEARCH_TIME_LIMIT),
                         msg='Time limit is incorrect in the search params.')
        self.assertEqual(
            model.search_parameters.solution_limit,
            self.params.SEARCH_SOLUTIONS_LIMIT,
            msg='Solutions limit is incorrect in the search params.')
        self.assertEqual(
            model.search_parameters.first_solution_strategy,
            FIRST_SOLUTION_STRATEGY[self.params.FIRST_SOLUTION_STRATEGY],
            msg='First solution strategy is incorrect in the search params.')
        self.assertEqual(
            model.search_parameters.local_search_metaheuristic,
            LOCAL_SEARCH_METAHEURISTIC[self.params.SEARCH_METAHEURISTIC],
            msg='Search metaheuristic is incorrect in the search params.')
        self.assertTrue(model.solver.HasDimension('capacity_constraint'),
                        msg='Capacity constraint not added.')
Exemple #2
0
    def test_solve(self):
        """Asserts an Optimization Model is correctly solved"""

        riders = parse_models(model_dicts=test_riders, cls=Rider)
        vehicles = parse_models(model_dicts=test_vehicles, cls=Vehicle)
        depots = parse_models(model_dicts=test_depots, cls=Depot)
        params = get_params()
        estimator = LinearEstimator()
        problem_builder = ProblemBuilder(params=params, estimator=estimator)
        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        problem = problem_builder.build(riders, vehicles, depots)
        model = model_builder.build(problem)
        solution = model.solve()

        self.assertTrue(solution, msg='Model could not be solved.')
        self.assertEqual(
            len(solution),
            len(vehicles),
            msg='Number of routes do not match number of vehicles.')
        stops_in_solution = [stop for route in solution for stop in route]
        for stop_ix in range(len(problem.stops)):
            self.assertIn(stop_ix,
                          stops_in_solution,
                          msg=f'Stop {stop_ix} not in solution.')
Exemple #3
0
    def test_set_objective_function(self):
        """Asserts the objective function is added to the solver"""

        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        problem = self.problem
        manager = RoutingIndexManager(
            len(problem.stops),  # Number of locations
            len(problem.vehicles),  # Number of vehicles
            problem.starts,  # Start list of Vehicles
            problem.ends  # End list of Vehicles
        )
        solver = RoutingModel(manager)
        model_builder._set_objective_function(problem, manager, solver)
        self.assertTrue(solver, msg='Objective function set incorrectly.')
Exemple #4
0
    def test_apply_constraints_capacity_constraint(self):
        """Asserts constraints are read correctly by the solver"""

        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        problem = self.problem
        manager = RoutingIndexManager(
            len(problem.stops),  # Number of locations
            len(problem.vehicles),  # Number of vehicles
            problem.starts,  # Start list of Vehicles
            problem.ends  # End list of Vehicles
        )
        solver = RoutingModel(manager)
        model_builder._apply_constraints(problem, manager, solver)
        self.assertTrue(solver, msg='Constraints added incorrectly.')
        self.assertTrue(solver.HasDimension('capacity_constraint'),
                        msg='Capacity constraint not added.')
Exemple #5
0
    def test_route(self):
        """Asserts the main routing method works correctly"""

        params = get_params()
        estimator = LinearEstimator()
        problem_builder = ProblemBuilder(params=params, estimator=estimator)
        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        router = Router(problem_builder=problem_builder,
                        optimization_model_builder=model_builder)
        riders = parse_models(model_dicts=test_riders, cls=Rider)
        vehicles = parse_models(model_dicts=test_vehicles, cls=Vehicle)
        depots = parse_models(model_dicts=test_depots, cls=Depot)
        routes = router.route(riders, vehicles, depots)
        self.assertTrue(routes, msg='Routes could not be built.')

        for route in routes:
            self.assertTrue(route['vehicle_id'], msg='Route without vehicle.')
            self.assertTrue(len(route['stops']) > 1,
                            msg='Route with single stop.')
Exemple #6
0
    def test_parse_routes(self):
        """Asserts that Routes are correctly parsed from the Opt. solution"""

        params = get_params()
        estimator = LinearEstimator()
        problem_builder = ProblemBuilder(params=params, estimator=estimator)
        model_builder = OptimizationModelBuilder(
            constraints=[CapacityConstraint()])
        riders = parse_models(model_dicts=test_riders, cls=Rider)
        vehicles = parse_models(model_dicts=test_vehicles, cls=Vehicle)
        depots = parse_models(model_dicts=test_depots, cls=Depot)
        problem = problem_builder.build(riders, vehicles, depots)
        model = model_builder.build(problem)
        solution = model.solve()
        routes = Router._parse_routes(problem, solution)
        self.assertTrue(routes, msg='Routes could not be built.')

        for route in routes:
            self.assertTrue(route['vehicle_id'], msg='Route without vehicle.')
            self.assertTrue(len(route['stops']) > 1,
                            msg='Route with single stop.')
Exemple #7
0
from utils.logging_utils import configure_logs
"""Main method to execute the Router"""

# CLI parsing
parser = argparse.ArgumentParser(description='Route some school buses.')
parser.add_argument('--input-dir',
                    type=str,
                    help='Directory for reading the Input. Default is ./input',
                    default='./input')
parser.add_argument(
    '--output-dir',
    type=str,
    help='Directory for reading the Output. Default is ./output',
    default='./output',
)
args = parser.parse_args()
input_dir = args.input_dir
output_dir = args.output_dir

# Method execution
configure_logs()
riders, vehicles, depots, params = read_entities(input_dir)
estimator = LinearEstimator()
problem_builder = ProblemBuilder(params=params, estimator=estimator)
optimization_model_builder = OptimizationModelBuilder(
    constraints=[CapacityConstraint()])
router = Router(problem_builder=problem_builder,
                optimization_model_builder=optimization_model_builder)
routes = router.route(riders, vehicles, depots)
write_routes(output_dir, routes)