fleetmanager.model.qampo package
Submodules
fleetmanager.model.qampo.classes module
This file defines the different classes used in the solution.
- class fleetmanager.model.qampo.classes.AlgorithmParameters(*, time_limit_in_seconds: int = None)[source]
Bases:
BaseModel
- time_limit_in_seconds: Optional[int]
Defines for how long, an algorithm is allowed to be run.
- class fleetmanager.model.qampo.classes.AlgorithmType(value)[source]
Bases:
Enum
What type of algorithm to be used.
- EXACT_CP = 'exact_cp'
Exact constraint programming algorithm.
- EXACT_MIP = 'exact_mip'
Exact mixed-integer programming algorithm.
- GREEDY = 'greedy'
Greedy algorithm.
- class fleetmanager.model.qampo.classes.Assignment(*, vehicle: BaseVehicle, route: Trips, variable_cost: float, co2_emission_in_tons: float)[source]
Bases:
BaseModel
An assignment is trips assigned to a specific vehicle.
- co2_emission_in_tons: float
The CO2 emission of the route measured in tons.
- route: Trips
Contains a list of trips. A new trip can start immediately, after the previous trip ends, thus, ending at 08:00 means, the next step can start at 08:00.
- variable_cost: float
The variable cost for the route.
- vehicle: BaseVehicle
The vehicle, a route is assigned to.
- class fleetmanager.model.qampo.classes.BaseVehicle(*, variable_cost_per_kilometer: float, co2_emission_gram_per_kilometer: float)[source]
Bases:
BaseModel
A base vehicle consists of a variable cost of kilometers and an amount of CO2 emission per kilometer measured in grams.
- co2_emission_gram_per_kilometer: float
The CO2 emission per kilometer measured in grams.
- variable_cost_per_kilometer: float
The variable cost per kilometer.
- class fleetmanager.model.qampo.classes.Fleet(*, vehicles: list[fleetmanager.model.qampo.classes.Vehicle], employee_car: BaseVehicle, emission_cost_per_ton_co2: float = None)[source]
Bases:
BaseModel
A tradeoff between C02 and money for a list of vehicles.
Official documents say, somewhere between 1500 and 5000 dkk/ton is an optional list for possible parameters for scenarios, otherwise, default values are set.
- emission_cost_per_ton_co2: Optional[float]
The CO2 emission cost per ton. Default value will be 1000 dkk.
- employee_car: BaseVehicle
An employee car.
- vehicles: list[fleetmanager.model.qampo.classes.Vehicle]
A list of vehicles.
- class fleetmanager.model.qampo.classes.RoutePlan(*, assignments: list[fleetmanager.model.qampo.classes.Assignment], employee_car: Assignment, total_cost: float, total_co2_emission_in_tons: float)[source]
Bases:
BaseModel
A route plan consists of a list of assignments, total cost and total CO2 emission.
- assignments: list[fleetmanager.model.qampo.classes.Assignment]
A list of assignments.
- employee_car: Assignment
The assignment for the employee car(s)
- total_co2_emission_in_tons: float
The total amount of CO2 emission of the assignments measured in tons.
- total_cost: float
The total cost of the routes.
- class fleetmanager.model.qampo.classes.RoutingAlgorithm[source]
Bases:
ABC
An ABC for doing a single day routing assignment.
- abstract optimize_single_day(trips: list[fleetmanager.model.qampo.classes.Trip], vehicles: list[fleetmanager.model.qampo.classes.Vehicle], employee_car: BaseVehicle, emission_cost_per_ton_co2: float = 1500, time_limit_in_seconds: int = 60) RoutePlan [source]
- class fleetmanager.model.qampo.classes.Trip(*, id: int, start_time: datetime, end_time: datetime, length_in_kilometers: float)[source]
Bases:
BaseModel
A trip consist of a start time, an end time and the length of the trip measured in kilometers.
The starting location and ending location are assumed to be identical for this application.
- end_time: datetime
The time for which the trip ends.
- get_trip_length_in_minutes() int [source]
Get the trip duration in minutes (rounded up) of the trip @:return duration in number of minutes (rounded up)
- id: int
A unique id for the trip.
- length_in_kilometers: float
The length of the trip measured in kilometers.
- start_time: datetime
The time for which the trip begins.
- class fleetmanager.model.qampo.classes.Trips(*, trips: list[fleetmanager.model.qampo.classes.Trip])[source]
Bases:
BaseModel
A list of trips.
- trips: list[fleetmanager.model.qampo.classes.Trip]
A list of trips.
- class fleetmanager.model.qampo.classes.Vehicle(*, variable_cost_per_kilometer: float, co2_emission_gram_per_kilometer: float, id: int, range_in_kilometers: float, maximum_driving_in_minutes: int, name: str = '')[source]
Bases:
BaseVehicle
A vehicle consists of a range of kilometers, a variable cost of kilometers, an amount of CO2 emission per kilometer measured in grams, and a name.
- id: int
A unique id for the vehicle.
- maximum_driving_in_minutes: int
The number of minutes the vehicle can drive. Used to control e.g. electric cars recharge time in lieu of a proper battery management implementation e.g. an uptime of 16*60 minutes could model an 8 hour recharging time.
- name: str
The name of the car, e.g. Toyota Yaris No. 1. Not used in the algorithm, but for easier debugging and information.
- range_in_kilometers: float
The range in kilometers.
fleetmanager.model.qampo.cost_calculator module
This file defines functions for calculating the CO2 emission and costs for a route plan.
- fleetmanager.model.qampo.cost_calculator.calculate_co2_emission_cost_per_kilometer_for_vehicle(vehicle: BaseVehicle, emission_cost_per_ton_co2: float) float [source]
Calculates the CO2 emission cost per kilometer for a given vehicle.
- fleetmanager.model.qampo.cost_calculator.calculate_co2_emission_gram_of_trips(vehicle: BaseVehicle, trips: list[fleetmanager.model.qampo.classes.Trip]) float [source]
Calculates the CO2 emission (grams) of all trips combined.
- fleetmanager.model.qampo.cost_calculator.calculate_co2_emission_ton_of_trips(vehicle: BaseVehicle, trips: list[fleetmanager.model.qampo.classes.Trip]) float [source]
Calculates the CO2 emission (tons) of all trips combined.
- fleetmanager.model.qampo.cost_calculator.calculate_total_length_of_trips(trips: list[fleetmanager.model.qampo.classes.Trip]) float [source]
Calculates the length of all trips combined.
- fleetmanager.model.qampo.cost_calculator.calculate_variable_cost_of_trips(vehicle: BaseVehicle, trips: list[fleetmanager.model.qampo.classes.Trip]) float [source]
Calculates the cost of all trips combined.
fleetmanager.model.qampo.exceptions module
This file defines a number of custom exceptions.
- exception fleetmanager.model.qampo.exceptions.MultipleDaysNotSupported[source]
Bases:
Exception
Thrown, when multiple days are not supported (usually in the optimization).
- exception fleetmanager.model.qampo.exceptions.NoSolutionFoundException[source]
Bases:
Exception
Thrown, when no solution can be found.
- exception fleetmanager.model.qampo.exceptions.NumberOfTripsPlannedMismatchException[source]
Bases:
Exception
Thrown, when number of trips does not match number of trips assigned.
fleetmanager.model.qampo.helper_functions module
This file defines various helper functions used throughout the code.
- fleetmanager.model.qampo.helper_functions.prioritize_vehicles_according_to_weighted_variable_costs(vehicles: list[fleetmanager.model.qampo.classes.BaseVehicle], emission_cost_per_ton_co2: float) list[fleetmanager.model.qampo.classes.BaseVehicle] [source]
This function weights the variable costs and CO2 emission of the vehicles and returns them in a sorted list in ascending order. :param vehicles: Vehicles to be listed. :param emission_cost_per_ton_co2: The cost of CO2 emission in dkk (or at least same currency as the variable cost on the vehicles). :return: Prioritized list of vehicles with the cheapest vehicle in position 0, second cheapest in position 1, etc.
fleetmanager.model.qampo.instance_generator module
This file defines functions for generating random test instances.
- fleetmanager.model.qampo.instance_generator.generate_trips(earliest_start_hour: int, latest_start_hour: int, number_of_trips: int, minimum_length_in_kilometers: float, maximum_length_in_kilometers: float) Trips [source]
Generates the specified number of trips with a starting time within the earliest start hour and latest start hour within the specified range in kilometers.
In general a uniform distribution is applied to the different fields.
The trips will be generated to start at minute 00/60, 15, 30 or 45.
- Parameters
earliest_start_hour – E.g. 6 meaning, the earliest start time would be 6:00. Must be >= 0.
latest_start_hour – E.g. 23 meaning, the latest start time would be 23:00. Must be <= 23.
number_of_trips – E.g. 100 meaning, 100 trips are generated.
minimum_length_in_kilometers – The minimum length of a trip.
maximum_length_in_kilometers – The maximum length of a trip.
- Returns
A class of Trips.
fleetmanager.model.qampo.main module
This file defines the main functionality of the repository - a webservice that can be called to optimize routes a day/a fleet over a couple of days. FastAPI is used for handling different HTTP requests, for instance to post an optimization.
- async fleetmanager.model.qampo.main.optimize_single_day(fleet: Fleet, trips: list[fleetmanager.model.qampo.classes.Trip], algorithm_type: AlgorithmType, algorithm_parameters: Optional[AlgorithmParameters] = AlgorithmParameters(time_limit_in_seconds=60))[source]
fleetmanager.model.qampo.qampo_simulation module
- fleetmanager.model.qampo.qampo_simulation.optimize_single_day(fleet: Fleet, trips: list[fleetmanager.model.qampo.classes.Trip], algorithm_type: AlgorithmType, algorithm_parameters: Optional[AlgorithmParameters] = AlgorithmParameters(time_limit_in_seconds=60))[source]
fleetmanager.model.qampo.routeplan_factory module
This file defines route plan factory to be used for classes.py.
- fleetmanager.model.qampo.routeplan_factory.route_plan_from_vehicle_trip_map(vehicle_assignments: {list[fleetmanager.model.qampo.classes.Trip], <class 'fleetmanager.model.qampo.classes.Vehicle'>}) RoutePlan [source]
Creates a route plan from a dictionary of vehicle as key and list of trips as value. :param vehicle_assignments: Assignments for vehicle. :return: Route plan.
fleetmanager.model.qampo.routeplan_tester module
This file holds basic information for testing a solution for the small trips and 3 vehicles problem.
- fleetmanager.model.qampo.routeplan_tester.check_solution_for_problem(number_of_trips: int, solution: RoutePlan) None [source]
This function performs a number of checks to find possible issues and raises exceptions if any exists. @param number_of_trips Number of trips. @param solution The solution to be checked for issues.
fleetmanager.model.qampo.routing_cp module
This file contains code, that optimizes a single day routing given a fixed fleet in an optimal manner using the Constraint programming solver from ortools.
- class fleetmanager.model.qampo.routing_cp.RoutingCp[source]
Bases:
RoutingAlgorithm
- optimize_single_day(trips: list[fleetmanager.model.qampo.classes.Trip], vehicles: list[fleetmanager.model.qampo.classes.Vehicle], employee_car: BaseVehicle, emission_cost_per_ton_co2: float = 1500, time_limit_in_seconds: int = 60) RoutePlan [source]
This is an exact SAT algorithm that assigns trips to vehicles based on a weight of the variable cost per kilometer and the CO2 emission. time_limit_in_seconds specifies for how long, the algorithm is allowed to be run. :param trips: List of trips in the route plan. :param vehicles: List of vehicles in the route plan. :param employee_car: Employee car a trip can be assigned to. :param emission_cost_per_ton_co2: CO2 emission cost per ton for the entire route plan. :param time_limit_in_seconds: Time limit for the running time of the algorithm. :return Routing plan created after optimization has been performed.
fleetmanager.model.qampo.routing_greedy module
This file contains code that optimizes a single day routing given a fixed fleet in a greedy manner. This is conceptually the same as the algorithm in the overall project (IFFK) that simulates car assignment previously.
- class fleetmanager.model.qampo.routing_greedy.RoutingGreedy[source]
Bases:
RoutingAlgorithm
- optimize_single_day(trips: list[fleetmanager.model.qampo.classes.Trip], vehicles: list[fleetmanager.model.qampo.classes.Vehicle], employee_car: BaseVehicle, emission_cost_per_ton_co2: float = 1500, time_limit_in_seconds: int = 60) RoutePlan [source]
This is a simple greedy algorithm that assigns trips to vehicles based on a weight of the variable cost per kilometer and the CO2 emission. Greedily means that earliest trips are assigned first to the cheapest vehicle. time_limit_in_seconds specifies for how long, the algorithm is allowed to be run. :param trips: List of trips in the route plan. :param vehicles: List of vehicles in the route plan. :param employee_car: Employee car a trip can be assigned to. :param emission_cost_per_ton_co2: CO2 emission cost per ton for the entire route plan. :param time_limit_in_seconds: Time limit for the running time of the algorithm. :return Routing plan created after optimization has been performed.
fleetmanager.model.qampo.routing_mip module
This file contains code that optimizes a single day routing given a fixed fleet in an optimal manner using the MIP solver from ortools (utilizing SCIP).
- class fleetmanager.model.qampo.routing_mip.RoutingMip[source]
Bases:
RoutingAlgorithm
- optimize_single_day(trips: list[fleetmanager.model.qampo.classes.Trip], vehicles: list[fleetmanager.model.qampo.classes.Vehicle], employee_car: BaseVehicle, emission_cost_per_ton_co2: float = 1500, time_limit_in_seconds: int = 60) RoutePlan [source]
An exact MIP algorithm that will assign trips to vehicles based on a weight of the variable cost per kilometer and the CO2 emission. time_limit_in_seconds specifies for how long, the algorithm is allowed to be run. :param trips: List of trips in the route plan. :param vehicles: List of vehicles in the route plan. :param employee_car: Employee car a trip can be assigned to. :param emission_cost_per_ton_co2: CO2 emission cost per ton for the entire route plan. :param time_limit_in_seconds: Time limit for the running time of the algorithm. :return Routing plan created after optimization has been performed.
fleetmanager.model.qampo.validation module
This file has functions for validating input data etc.
- fleetmanager.model.qampo.validation.check_trips_only_has_single_date(trips: list[fleetmanager.model.qampo.classes.Trip]) None [source]
Checks if only one single start date is present in the list, or else raises an MultipleDaysNotSupported. :param trips: Trips.