From 6b9d1474e886333106df71d7cafb7ebf7c7bcbd9 Mon Sep 17 00:00:00 2001 From: Derek Groen Date: Sun, 27 Aug 2023 08:17:38 +0200 Subject: [PATCH] DFlee Flood rules for move chances implemented. #81 --- flee/SimulationSettings.py | 9 ++++++++- flee/moving.py | 7 +++++++ tests/test_dflee.py | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/flee/SimulationSettings.py b/flee/SimulationSettings.py index 998551d2..a18d7d44 100644 --- a/flee/SimulationSettings.py +++ b/flee/SimulationSettings.py @@ -245,18 +245,25 @@ def ReadFromYML(ymlfile: str): for a in ["ChildrenAvoidHazards", "BoysTakeRisk", "MatchCampEthnicity", "MatchTownEthnicity", "MatchConflictEthnicity"]: SimulationSettings.move_rules[a] = bool(fetchss(dpr,a,False)) + + # DFlee Flood Location Move rules dpf = fetchss(dp, "flood_rules", None) if dpf is not None: SimulationSettings.move_rules["FloodRulesEnabled"] = True SimulationSettings.spawn_rules["MaxFloodLevel"] = int(fetchss(dpf,"max_flood_level", -1)) - # Move rules + # FloodMovechances *override* move chances when flood level is higher than 0. SimulationSettings.move_rules["FloodMovechances"] = fetchss(dps,"flood_movechances", None) # Expect an array or dict print("Flood Movechances set to:", SimulationSettings.spawn_rules["FloodMovechances"], file=sys.stderr) + + # FloodLocWeights *multiply* existing location weights when flood level is higher than 0. SimulationSettings.move_rules["FloodLocWeights"] = fetchss(dps,"flood_loc_weights", None) # Expect an array or dict print("Flood Location Weights set to:", SimulationSettings.spawn_rules["FloodLocWeights"], file=sys.stderr) + + # FloodLinkWeights *multiply* existing link weights when flood level is higher than 0. SimulationSettings.move_rules["FloodLinkWeights"] = fetchss(dps,"flood_link_weights", None) # Expect an array or dict print("Flood Link Weights set to:", SimulationSettings.spawn_rules["FloodLinkWeights"], file=sys.stderr) + print("Note: Flood Link Weights are not supported yet in this version of DFlee.") #TODO: Add verification code. diff --git a/flee/moving.py b/flee/moving.py index 18c273cf..d91c5a34 100644 --- a/flee/moving.py +++ b/flee/moving.py @@ -252,8 +252,15 @@ def calculateMoveChance(a, ForceTownMove: bool) -> float: return 1.0 else: # called first time in loop movechance = a.location.movechance + # Population-based scaling movechance *= (float(max(a.location.pop, a.location.capacity)) / SimulationSettings.move_rules["MovechancePopBase"])**SimulationSettings.move_rules["MovechancePopScaleFactor"] + # DFlee Flood Location Movechance implementation + if SimulationSettings.move_rules["FloodRulesEnabled"] is True: + flood_level = a.location.attributes.get("flood_level",0) + if flood_level > 0: + return float(SimulationSettings.move_rules["FloodMovechances"][flood_level]) + return movechance diff --git a/tests/test_dflee.py b/tests/test_dflee.py index f9457dee..70e6eb09 100644 --- a/tests/test_dflee.py +++ b/tests/test_dflee.py @@ -43,7 +43,7 @@ def test_flood_level_location_attribute(): flee.SimulationSettings.ReadFromYML("empty.yml") flee.SimulationSettings.move_rules["FloodRulesEnabled"] = True flee.SimulationSettings.move_rules["FloodLocWeights"] = [0.0,1.0,1.0,1.0,1.0] - flee.SimulationSettings.move_rules["FloodMoveChances"] = [0.0,1.0,1.0,1.0,1.0] + flee.SimulationSettings.move_rules["FloodMovechances"] = [0.0,1.0,1.0,1.0,1.0] e = flee.Ecosystem()