Skip to content

Commit

Permalink
pass around ramp-up and ramp-down periods
Browse files Browse the repository at this point in the history
  • Loading branch information
1kastner committed May 23, 2024
1 parent 2440662 commit 78a558b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 10 deletions.
12 changes: 12 additions & 0 deletions conflowgen/domain_models/factories/container_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import datetime
import math
import typing
from typing import Dict, MutableSequence, Sequence, Type

from conflowgen.domain_models.container import Container
Expand Down Expand Up @@ -36,6 +38,16 @@ def __init__(self):
self.storage_requirement_distribution: dict[ContainerLength, dict[StorageRequirement, float]] | None = None
self.large_scheduled_vehicle_repository = LargeScheduledVehicleRepository()

def set_ramp_up_and_down_times(
self,
ramp_up_period_end: typing.Optional[datetime.datetime],
ramp_down_period_start: typing.Optional[datetime.datetime],
) -> None:
self.large_scheduled_vehicle_repository.set_ramp_up_and_down_times(
ramp_up_period_end=ramp_up_period_end,
ramp_down_period_start=ramp_down_period_start
)

def reload_distributions(self):
"""The user might change the distributions at any time, so reload them at a meaningful point of time!"""
self.mode_of_transportation_distribution = ModeOfTransportDistributionRepository.get_distribution()
Expand Down
11 changes: 9 additions & 2 deletions conflowgen/flow_generator/container_flow_generation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,24 @@ def _update_generation_properties_and_distributions(self):
assert -1 < self.transportation_buffer

self.large_scheduled_vehicle_for_onward_transportation_manager.reload_properties(
transportation_buffer=self.transportation_buffer
transportation_buffer=self.transportation_buffer,
ramp_up_period_end=self.ramp_up_period_end,
ramp_down_period_start=self.ramp_down_period_start,
)
self.allocate_space_for_containers_delivered_by_truck_service.reload_distribution(
transportation_buffer=self.transportation_buffer
)

self.truck_for_import_containers_manager.reload_distributions()
self.truck_for_export_containers_manager.reload_distributions()

self.large_scheduled_vehicle_creation_service.reload_properties(
container_flow_start_date=self.container_flow_start_date,
container_flow_end_date=self.container_flow_end_date
container_flow_end_date=self.container_flow_end_date,
ramp_up_period_end=self.ramp_up_period_end,
ramp_down_period_start=self.ramp_down_period_start,
)

self.assign_destination_to_container_service.reload_distributions()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import datetime
import typing
from typing import List, Type
import logging

Expand Down Expand Up @@ -29,11 +32,17 @@ def __init__(self):
def reload_properties(
self,
container_flow_start_date: datetime.date,
container_flow_end_date: datetime.date
container_flow_end_date: datetime.date,
ramp_up_period_end: typing.Optional[datetime.datetime | datetime.date],
ramp_down_period_start: typing.Optional[datetime.datetime | datetime.date],
):
assert container_flow_start_date < container_flow_end_date
self.container_flow_start_date = container_flow_start_date
self.container_flow_end_date = container_flow_end_date
self.container_factory.set_ramp_up_and_down_times(
ramp_up_period_end=ramp_up_period_end,
ramp_down_period_start=ramp_down_period_start
)
self.container_factory.reload_distributions()

def create(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def __init__(self):
def reload_properties(
self,
transportation_buffer: float,
ramp_up_period_end: Optional[datetime.datetime] = None,
ramp_down_period_start: Optional[datetime.datetime] = None,
ramp_up_period_end: Optional[datetime.datetime | datetime.date] = None,
ramp_down_period_start: Optional[datetime.datetime | datetime.date] = None,
):
assert -1 < transportation_buffer
self.schedule_repository.set_transportation_buffer(transportation_buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from conflowgen.domain_models.factories.fleet_factory import FleetFactory
from conflowgen.domain_models.data_types.mode_of_transport import ModeOfTransport
from conflowgen.domain_models.large_vehicle_schedule import Destination
from conflowgen.domain_models.vehicle import Feeder, LargeScheduledVehicle, Schedule, Truck
from conflowgen.domain_models.vehicle import Feeder, LargeScheduledVehicle, Schedule, Truck, DeepSeaVessel
from conflowgen.tests.substitute_peewee_database import setup_sqlite_in_memory_db


Expand All @@ -25,6 +25,7 @@ def setUp(self) -> None:
sqlite_db = setup_sqlite_in_memory_db()
sqlite_db.create_tables([
Feeder,
DeepSeaVessel,
LargeScheduledVehicle,
Schedule,
Container,
Expand Down Expand Up @@ -81,3 +82,69 @@ def test_create_containers_for_feeder_vessel(self) -> None:
feeder_1.large_scheduled_vehicle
)
self.assertIsNone(containers[0].delivered_by_truck)

def test_create_containers_for_single_deep_sea_vessel_during_ramp_up_period(self):
schedule = Schedule.create(
service_name="SunExpress",
vehicle_type=ModeOfTransport.deep_sea_vessel,
vehicle_arrives_at=datetime.date(2021, 7, 9),
vehicle_arrives_at_time=datetime.time(11),
average_vehicle_capacity=24000,
average_moved_capacity=3000
)
vessels = FleetFactory().create_deep_sea_vessel_fleet(
schedule=schedule,
first_at=datetime.date(2021, 7, 8),
latest_at=datetime.date(2021, 7, 10)
)
self.assertEqual(
len(vessels),
1
)
vessel = vessels[0]

self.container_factory.set_ramp_up_and_down_times(
ramp_up_period_end=datetime.datetime(2021, 7, 10),
ramp_down_period_start=None
)

# noinspection PyTypeChecker
containers = self.container_factory.create_containers_for_large_scheduled_vehicle(vessel)

container_volume = sum([c.occupied_teu for c in containers])

Check notice on line 114 in conflowgen/tests/domain_models/factories/test_container_factory__create_for_large_scheduled_vehicle.py

View check run for this annotation

codefactor.io / CodeFactor

conflowgen/tests/domain_models/factories/test_container_factory__create_for_large_scheduled_vehicle.py#L114

Consider using a generator instead 'sum(c.occupied_teu for c in containers)' (consider-using-generator)

self.assertGreater(container_volume, 2900, "A bit less than 3000 is acceptable but common!")
self.assertLess(container_volume, 3100, "A bit more than 3000 is acceptable but common!")

def test_create_containers_for_single_deep_sea_vessel_during_ramp_down_period(self):
schedule = Schedule.create(
service_name="SunExpress",
vehicle_type=ModeOfTransport.deep_sea_vessel,
vehicle_arrives_at=datetime.date(2021, 7, 9),
vehicle_arrives_at_time=datetime.time(11),
average_vehicle_capacity=24000,
average_moved_capacity=3000
)
vessels = FleetFactory().create_deep_sea_vessel_fleet(
schedule=schedule,
first_at=datetime.date(2021, 7, 8),
latest_at=datetime.date(2021, 7, 10)
)
self.assertEqual(
len(vessels),
1
)
vessel = vessels[0]

self.container_factory.set_ramp_up_and_down_times(
ramp_up_period_end=None,
ramp_down_period_start=datetime.datetime(2021, 7, 8)
)

# noinspection PyTypeChecker
containers = self.container_factory.create_containers_for_large_scheduled_vehicle(vessel)

container_volume = sum([c.occupied_teu for c in containers])

Check notice on line 147 in conflowgen/tests/domain_models/factories/test_container_factory__create_for_large_scheduled_vehicle.py

View check run for this annotation

codefactor.io / CodeFactor

conflowgen/tests/domain_models/factories/test_container_factory__create_for_large_scheduled_vehicle.py#L147

Consider using a generator instead 'sum(c.occupied_teu for c in containers)' (consider-using-generator)

self.assertGreater(container_volume, 290, "A bit less than 3000 is acceptable but common!")
self.assertLess(container_volume, 310, "A bit more than 3000 is acceptable but common!")
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def _create_feeder(
average_vehicle_capacity=300,
average_moved_capacity=300,
)
schedule.save()
feeder_lsv = LargeScheduledVehicle.create(
vehicle_name="TestFeeder1",
capacity_in_teu=schedule.average_vehicle_capacity,
Expand All @@ -82,7 +81,6 @@ def _create_feeder(
feeder = Feeder.create(
large_scheduled_vehicle=feeder_lsv
)
feeder.save()
return feeder

@staticmethod
Expand Down Expand Up @@ -318,7 +316,26 @@ def test_nothing_to_do(self):
get_vehicles_method.assert_not_called()

def test_behavior_during_ramp_up_period(self):
... # TODO!
feeder = self._create_feeder(datetime.datetime(year=2022, month=8, day=7, hour=13, minute=15))
feeder.large_scheduled_vehicle.moved_capacity = 100 # in TEU
feeder.save()
self.manager.reload_properties(
transportation_buffer=0,
ramp_up_period_end=datetime.date(2022, 8, 8)
)

# run actual function
self.manager.choose_departing_vehicle_for_containers()

containers_reloaded: Iterable[Container] = Container.select().where(
Container.picked_up_by_large_scheduled_vehicle == feeder
)
teu_loaded = 0
for container in containers_reloaded: # pylint: disable=not-an-iterable
self.assertEqual(container.picked_up_by_large_scheduled_vehicle, feeder.large_scheduled_vehicle)
teu_loaded += ContainerLength.get_teu_factor(container.length)
self.assertLessEqual(teu_loaded, 10, "Feeder must have loaded much less containers because this is the"
"ramp-up period!")

def test_behavior_during_ramp_down_period(self):
... # TODO!
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 78a558b

Please sign in to comment.