changes Strategies to a `RATEnum` by alexhroom · Pull Request #65 · RascalSoftware/python-RAT · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RATapi/inputs.py
31 changes: 22 additions & 9 deletions RATapi/utils/enums.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import Enum
from typing import Union

try:
from enum import StrEnum
Expand Down Expand Up @@ -48,15 +48,28 @@ class Display(RATEnum):
Final = "final"


class Strategies(Enum):
"""Defines the available options for strategies"""
class Strategies(RATEnum):
"""Defines the available options for differential evolution strategies"""

Random = 1
LocalToBest = 2
BestWithJitter = 3
RandomWithPerVectorDither = 4
RandomWithPerGenerationDither = 5
RandomEitherOrAlgorithm = 6
Random = "random"
LocalToBest = "local to best"
BestWithJitter = "best jitter"
RandomWithPerVectorDither = "vector dither"
RandomWithPerGenerationDither = "generation dither"
RandomEitherOrAlgorithm = "either or"

@classmethod
def _missing_(cls, value: Union[int, str]):
# legacy compatibility with strategies being 1-indexed ints under the hood
if isinstance(value, int):
if value < 1 or value > 6:
raise IndexError("Strategy indices must be between 1 and 6.")
return list(cls)[value - 1]
return super()._missing_(value)
Comment thread
alexhroom marked this conversation as resolved.

def __int__(self):
# as RAT core expects strategy as an integer
return list(Strategies).index(self) + 1


class BoundHandling(RATEnum):
Expand Down
34 changes: 17 additions & 17 deletions tests/test_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,23 @@ def setup_class(self):
@pytest.fixture
def table_str(self):
table_str = (
"+----------------------+--------------------------------------+\n"
"| Property | Value |\n"
"+----------------------+--------------------------------------+\n"
"| procedure | de |\n"
"| parallel | single |\n"
"| calcSldDuringFit | False |\n"
"| resampleParams | [0.9, 50] |\n"
"| display | iter |\n"
"| populationSize | 20 |\n"
"| fWeight | 0.5 |\n"
"| crossoverProbability | 0.8 |\n"
"| strategy | Strategies.RandomWithPerVectorDither |\n"
"| targetValue | 1.0 |\n"
"| numGenerations | 500 |\n"
"| updateFreq | 1 |\n"
"| updatePlotFreq | 20 |\n"
"+----------------------+--------------------------------------+"
"+----------------------+---------------+\n"
"| Property | Value |\n"
"+----------------------+---------------+\n"
"| procedure | de |\n"
"| parallel | single |\n"
"| calcSldDuringFit | False |\n"
"| resampleParams | [0.9, 50] |\n"
"| display | iter |\n"
"| populationSize | 20 |\n"
"| fWeight | 0.5 |\n"
"| crossoverProbability | 0.8 |\n"
"| strategy | vector dither |\n"
"| targetValue | 1.0 |\n"
"| numGenerations | 500 |\n"
"| updateFreq | 1 |\n"
"| updatePlotFreq | 20 |\n"
"+----------------------+---------------+"
)

return table_str
Expand Down
6 changes: 6 additions & 0 deletions tests/test_enums.py