-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AcyclicEnforcer tracks trajectory, rejects action if already attempte…
…d from current state to mitigate flip-flopping
- Loading branch information
1 parent
8dd849b
commit 97751ad
Showing
3 changed files
with
92 additions
and
17 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
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,29 @@ | ||
from typing import Any, Set | ||
|
||
import numpy as np | ||
|
||
|
||
class StateAction: | ||
def __init__(self, position: np.ndarray, action: Any): | ||
self.position = position | ||
self.action = action | ||
|
||
def __eq__(self, other: "StateAction") -> bool: | ||
return self.__hash__() == other.__hash__() | ||
|
||
def __hash__(self) -> int: | ||
string_repr = f"{self.position}_{self.action}" | ||
return hash(string_repr) | ||
|
||
|
||
class AcyclicEnforcer: | ||
history: Set[StateAction] = set() | ||
|
||
def check_cyclic(self, position: np.ndarray, action: Any) -> bool: | ||
state_action = StateAction(position, action) | ||
cyclic = state_action in self.history | ||
return cyclic | ||
|
||
def add_state_action(self, position: np.ndarray, action: Any): | ||
state_action = StateAction(position, action) | ||
self.history.add(state_action) |