-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix inconsistency with cpp impl, improve design (#3)
- fix: RNG is skipped when volatility is 0 - RNG as an argument to higher level generators - Separate computation of available units and available power - add unit tests Signed-off-by: Sylvain Leclerc <[email protected]>
- Loading branch information
Showing
14 changed files
with
519 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
numpy==1.24.4 | ||
pandas | ||
pydantic | ||
PyYAML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
from abc import ABC, abstractmethod | ||
from enum import Enum | ||
from math import log, sqrt | ||
from typing import List | ||
|
||
import numpy as np | ||
|
||
from random_generator import RNG | ||
|
||
|
||
class ProbabilityLaw(Enum): | ||
UNIFORM = "UNIFORM" | ||
GEOMETRIC = "GEOMETRIC" | ||
|
||
|
||
class DurationGenerator(ABC): | ||
""" | ||
Logic to generate unavailability duration. | ||
""" | ||
|
||
@abstractmethod | ||
def generate_duration(self, day: int) -> int: | ||
... | ||
|
||
|
||
class GeneratorWrapper(DurationGenerator): | ||
""" | ||
Wraps another generator to skip unnecessary random number generations. | ||
Used to keep backward compat with cpp implementation. | ||
""" | ||
|
||
def __init__( | ||
self, delegate: DurationGenerator, volatility: float, expecs: List[int] | ||
) -> None: | ||
self.volatility = volatility | ||
self.expectations = expecs | ||
self.delegate = delegate | ||
|
||
def generate_duration(self, day: int) -> int: | ||
""" | ||
generation of random outage duration | ||
""" | ||
expectation = self.expectations[day] | ||
# Logic copied from cpp implementation for results preservation | ||
if self.volatility == 0 or expectation == 1: | ||
return expectation | ||
return self.delegate.generate_duration(day) | ||
|
||
|
||
class UniformDurationGenerator(DurationGenerator): | ||
def __init__(self, rng: RNG, volatility: float, expecs: List[int]) -> None: | ||
self.rng = rng | ||
self.a = np.empty(len(expecs), dtype=float) | ||
self.b = np.empty(len(expecs), dtype=float) | ||
for day, expec in enumerate(expecs): | ||
xtemp = volatility * (expec - 1) | ||
self.a[day] = expec - xtemp | ||
self.b[day] = 2 * xtemp + 1 | ||
|
||
def generate_duration(self, day: int) -> int: | ||
""" | ||
generation of random outage duration | ||
""" | ||
rnd_nb = self.rng.next() | ||
return int(self.a[day] + rnd_nb * self.b[day]) | ||
|
||
|
||
class GeometricDurationGenerator(DurationGenerator): | ||
def __init__(self, rng: RNG, volatility: float, expecs: List[int]) -> None: | ||
self.rng = rng | ||
self.a = np.empty(len(expecs), dtype=float) | ||
self.b = np.empty(len(expecs), dtype=float) | ||
for day, expec in enumerate(expecs): | ||
xtemp = volatility * volatility * expec * (expec - 1) | ||
if xtemp != 0: | ||
ytemp = (sqrt(4 * xtemp + 1) - 1) / (2 * xtemp) | ||
self.a[day] = expec - 1 / ytemp | ||
self.b[day] = 1 / log(1 - ytemp) | ||
else: | ||
self.a[day] = expec - 1 | ||
self.b[day] = 0 | ||
|
||
def generate_duration(self, day: int) -> int: | ||
""" | ||
generation of random outage duration | ||
""" | ||
rnd_nb = self.rng.next() | ||
return min(int(1 + self.a[day] + self.b[day] * log(rnd_nb)), 1999) | ||
|
||
|
||
def make_duration_generator( | ||
rng: RNG, law: ProbabilityLaw, volatility: float, expectations: List[int] | ||
) -> DurationGenerator: | ||
""" | ||
return a DurationGenerator for the given law | ||
""" | ||
base_rng: DurationGenerator | ||
if law == ProbabilityLaw.UNIFORM: | ||
base_rng = UniformDurationGenerator(rng, volatility, expectations) | ||
elif law == ProbabilityLaw.GEOMETRIC: | ||
base_rng = GeometricDurationGenerator(rng, volatility, expectations) | ||
else: | ||
raise ValueError(f"Unknown law type: {law}") | ||
return GeneratorWrapper(base_rng, volatility, expectations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
import random | ||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
from mersenne_twister import MersenneTwister | ||
|
||
|
||
class RNG(ABC): | ||
""" | ||
Random number generator interface | ||
""" | ||
|
||
@abstractmethod | ||
def next(self) -> float: | ||
... | ||
|
||
|
||
class PythonRNG(ABC): | ||
""" | ||
Native python RNG. | ||
""" | ||
|
||
def next(self) -> float: | ||
return random.random() | ||
|
||
|
||
class MersenneTwisterRNG(RNG): | ||
""" | ||
Our own RNG based on Mersenne-Twister algorithm. | ||
""" | ||
|
||
def __init__(self, seed: int = 5489): | ||
self._rng = MersenneTwister() | ||
self._rng.seed(seed) | ||
|
||
def next(self) -> float: | ||
return self._rng.next() |
Oops, something went wrong.