Skip to content

Commit b53fe23

Browse files
authored
changes Strategies to a RATEnum (#65)
* changed Strategies to a RATEnum * Update enums.py * added integer input * throws error for bad indices
1 parent 8707f61 commit b53fe23

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

RATapi/inputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def make_controls(input_controls: RATapi.Controls, checks: Checks) -> Control:
451451
controls.populationSize = input_controls.populationSize
452452
controls.fWeight = input_controls.fWeight
453453
controls.crossoverProbability = input_controls.crossoverProbability
454-
controls.strategy = input_controls.strategy.value # Required for the IntEnum
454+
controls.strategy = int(input_controls.strategy) # RAT core expects strategy as an integer
455455
controls.targetValue = input_controls.targetValue
456456
controls.numGenerations = input_controls.numGenerations
457457
# NS

RATapi/utils/enums.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from enum import Enum
1+
from typing import Union
22

33
try:
44
from enum import StrEnum
@@ -48,15 +48,28 @@ class Display(RATEnum):
4848
Final = "final"
4949

5050

51-
class Strategies(Enum):
52-
"""Defines the available options for strategies"""
51+
class Strategies(RATEnum):
52+
"""Defines the available options for differential evolution strategies"""
5353

54-
Random = 1
55-
LocalToBest = 2
56-
BestWithJitter = 3
57-
RandomWithPerVectorDither = 4
58-
RandomWithPerGenerationDither = 5
59-
RandomEitherOrAlgorithm = 6
54+
Random = "random"
55+
LocalToBest = "local to best"
56+
BestWithJitter = "best jitter"
57+
RandomWithPerVectorDither = "vector dither"
58+
RandomWithPerGenerationDither = "generation dither"
59+
RandomEitherOrAlgorithm = "either or"
60+
61+
@classmethod
62+
def _missing_(cls, value: Union[int, str]):
63+
# legacy compatibility with strategies being 1-indexed ints under the hood
64+
if isinstance(value, int):
65+
if value < 1 or value > 6:
66+
raise IndexError("Strategy indices must be between 1 and 6.")
67+
return list(cls)[value - 1]
68+
return super()._missing_(value)
69+
70+
def __int__(self):
71+
# as RAT core expects strategy as an integer
72+
return list(Strategies).index(self) + 1
6073

6174

6275
class BoundHandling(RATEnum):

tests/test_controls.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,23 @@ def setup_class(self):
374374
@pytest.fixture
375375
def table_str(self):
376376
table_str = (
377-
"+----------------------+--------------------------------------+\n"
378-
"| Property | Value |\n"
379-
"+----------------------+--------------------------------------+\n"
380-
"| procedure | de |\n"
381-
"| parallel | single |\n"
382-
"| calcSldDuringFit | False |\n"
383-
"| resampleParams | [0.9, 50] |\n"
384-
"| display | iter |\n"
385-
"| populationSize | 20 |\n"
386-
"| fWeight | 0.5 |\n"
387-
"| crossoverProbability | 0.8 |\n"
388-
"| strategy | Strategies.RandomWithPerVectorDither |\n"
389-
"| targetValue | 1.0 |\n"
390-
"| numGenerations | 500 |\n"
391-
"| updateFreq | 1 |\n"
392-
"| updatePlotFreq | 20 |\n"
393-
"+----------------------+--------------------------------------+"
377+
"+----------------------+---------------+\n"
378+
"| Property | Value |\n"
379+
"+----------------------+---------------+\n"
380+
"| procedure | de |\n"
381+
"| parallel | single |\n"
382+
"| calcSldDuringFit | False |\n"
383+
"| resampleParams | [0.9, 50] |\n"
384+
"| display | iter |\n"
385+
"| populationSize | 20 |\n"
386+
"| fWeight | 0.5 |\n"
387+
"| crossoverProbability | 0.8 |\n"
388+
"| strategy | vector dither |\n"
389+
"| targetValue | 1.0 |\n"
390+
"| numGenerations | 500 |\n"
391+
"| updateFreq | 1 |\n"
392+
"| updatePlotFreq | 20 |\n"
393+
"+----------------------+---------------+"
394394
)
395395

396396
return table_str

tests/test_enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Priors,
1818
Procedures,
1919
RATEnum,
20+
Strategies,
2021
TypeOptions,
2122
)
2223

@@ -98,3 +99,8 @@ def test_alternative_spellings(test_enum: Callable, test_str: str, expected_valu
9899
assert test_enum(test_str) == expected_value
99100
assert test_enum(test_str.upper()) == expected_value
100101
assert test_enum(test_str.title()) == expected_value
102+
103+
104+
def test_integer_strategies():
105+
"""Test that strategies can be input as integers."""
106+
assert [Strategies(i) for i in range(1, len(Strategies) + 1)] == list(Strategies)

0 commit comments

Comments
 (0)