From 2765d7d5721ff9da865fee15e6b20ee7cad25c0c Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 10 Jul 2024 12:01:45 +0200 Subject: [PATCH 1/3] Set the random seed before potentially calling initialize() --- ramp/core/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ramp/core/core.py b/ramp/core/core.py index 1578c93..1eaa957 100644 --- a/ramp/core/core.py +++ b/ramp/core/core.py @@ -105,13 +105,14 @@ def __init__( self.add_user(users) self.collect_appliances_from_users() - if self.date_start is not None and self.date_end is not None: - self.initialize() # Set global random seed if it is specified if self.random_seed: random.seed(self.random_seed) + if self.date_start is not None and self.date_end is not None: + self.initialize() + @property def date_start(self): """Start date of the daily profiles generated by the UseCase instance""" From e8a25e62b5113dd6c795f71fb8c057c06db667ac Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 10 Jul 2024 12:03:34 +0200 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 739dc52..72e78c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Release History 0.5.3 (dev) ----------- +**|fixed|** Random seed behavior if both date_start and date_end are provided to UseCase instance (issue #156) + + 0.5.2 (2024-06-07) ------------------ From 303522674c6f126b384bf2e9272912f374a8db87 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 10 Jul 2024 12:13:59 +0200 Subject: [PATCH 3/3] Add test for random seed initialization --- tests/test_object_creation.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_object_creation.py b/tests/test_object_creation.py index 309ef56..592013a 100644 --- a/tests/test_object_creation.py +++ b/tests/test_object_creation.py @@ -1,6 +1,8 @@ import pytest -from ramp import User, Appliance +from ramp import UseCase, User, Appliance +from ramp.example.input_file_1 import User_list +from copy import deepcopy @pytest.fixture @@ -42,3 +44,25 @@ def test_skip_add_existing_appliances_to_user(test_user): test_user.add_appliance(appliance1) assert len(test_user.App_list) == 1 + + +def test_random_seed_initialization(): + # Build use case 2 and fixed random seed + uc_1 = UseCase( + users=deepcopy(User_list), + random_seed=1, + date_start="2020-01-01", + date_end="2020-01-01", + ) + # Initialize and generate load profile + uc_1.initialize(peak_enlarge=0.15, num_days=1) + uc_1_lp = uc_1.generate_daily_load_profiles() + + # Build use case 2 and same fixed random seed as uc_1 + uc_2 = UseCase(users=deepcopy(User_list), random_seed=1) + + # Initialize and generate load profile + uc_2.initialize(peak_enlarge=0.15, num_days=1) + uc_2_lp = uc_2.generate_daily_load_profiles() + + assert (uc_1_lp - uc_1_lp == 0).all()