Skip to content

Commit

Permalink
Use more sophisticated random seed (#199)
Browse files Browse the repository at this point in the history
* Use more sophisticated random seed

* Drop very verbose logging of all destinations when loading the distribution

* Add test case for random seeds
  • Loading branch information
1kastner authored Sep 26, 2023
1 parent 9970eef commit f9fde69
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
4 changes: 3 additions & 1 deletion conflowgen/api/container_flow_generation_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import typing

from conflowgen.application.models.container_flow_generation_properties import ContainerFlowGenerationProperties
from conflowgen.data_summaries.data_summaries_cache import DataSummariesCache
from conflowgen.application.repositories.container_flow_generation_properties_repository import \
ContainerFlowGenerationPropertiesRepository
Expand Down Expand Up @@ -59,7 +60,8 @@ def get_properties(self) -> typing.Dict[str, typing.Union[str, datetime.date, fl
Returns:
The properties of the container flow.
"""
properties = self.container_flow_generation_properties_repository.get_container_flow_generation_properties()
properties: ContainerFlowGenerationProperties = (self.container_flow_generation_properties_repository.
get_container_flow_generation_properties())
return {
'name': properties.name,
'start_date': properties.start_date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class RandomSeedStoreRepository:

random_seed_offset = 0

def __init__(self):
self.logger = logging.getLogger("conflowgen")

Expand All @@ -24,7 +26,9 @@ def get_random_seed(self, seed_name: str, log_loading_process: bool = False) ->
random_seed_store.random_seed = random_seed
random_seed_store.save()
if log_loading_process:
self.logger.debug(f"Overwrite seed {previous_seed} with {random_seed} for '{seed_name}'")
self.logger.debug(
f"Replace seed {previous_seed} with {random_seed} for '{seed_name}' for the new round."
)
else:
# there is a previous seed and we should re-use it
random_seed = random_seed_store.random_seed
Expand All @@ -42,9 +46,10 @@ def get_random_seed(self, seed_name: str, log_loading_process: bool = False) ->
self.logger.debug(f"Randomly set seed {random_seed} for '{seed_name}'")
return random_seed

@staticmethod
def _get_random_seed() -> int:
return int(time.time())
@classmethod
def _get_random_seed(cls) -> int:
cls.random_seed_offset += 1
return int(time.time_ns() + cls.random_seed_offset)

def fix_random_seed(
self, seed_name: str, random_seed: typing.Optional[int], log_loading_process: bool = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def reload_distributions(self):
self.logger.debug("Loading destination distribution...")
for schedule, distribution_for_schedule in self.distribution.items():
self.logger.debug(f"Load destination distribution for service '{schedule.service_name}' by "
f"{schedule.vehicle_type}")
for destination, fraction in distribution_for_schedule.items():
self.logger.debug(f"Destination '{destination.destination_name}' is frequented by {100*fraction:.2f}% "
f"of the containers and is number {destination.sequence_id}")
f"{schedule.vehicle_type} with {len(distribution_for_schedule)} destinations")

def assign(self) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ def test_reuse_existing_entry(self):
RandomSeedStore.create(
name="reuse_existing",
random_seed=seed,
is_random=True
is_random=False
)
random_seed = self.repository.get_random_seed("reuse_existing", False)
self.assertEqual(random_seed, seed)

def test_do_not_reuse_existing_random_entry(self):
seed = int(time.time())
RandomSeedStore.create(
name="reuse_existing",
random_seed=seed,
is_random=True
)
random_seed = self.repository.get_random_seed("reuse_existing", False)
self.assertNotEqual(random_seed, seed)

def test_fix_and_reuse_journey(self):
for _ in range(10):
seed = int(time.time())
Expand Down
2 changes: 1 addition & 1 deletion run_ci_light.bat
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ IF "%CONDA_PREFIX%" NEQ "" (
)

ECHO.It seems like you are not in an isolated development environment. In a later step, the current version of
ECHO.ConFlowgen will be installed as a library. Please abort if you do not want to clutter your Python installation.
ECHO.ConFlowGen will be installed as a library. Please abort if you do not want to clutter your Python installation.
ECHO.If you actually are in an isolated development environment, feel free to improve this check.

:AGAIN
Expand Down

0 comments on commit f9fde69

Please sign in to comment.