Skip to content

Commit

Permalink
reformatted using black
Browse files Browse the repository at this point in the history
  • Loading branch information
driesmarzougui committed Feb 9, 2024
1 parent dd30780 commit 1b21c4f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 222 deletions.
4 changes: 1 addition & 3 deletions fprs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
fprs_random_state = np.random.RandomState(seed=seed)


def set_random_state(
seed_value: int
) -> None:
def set_random_state(seed_value: int) -> None:
global seed, fprs_random_state
seed = seed_value
fprs_random_state.seed(seed_value)
104 changes: 25 additions & 79 deletions fprs/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,25 @@ class Parameter(metaclass=abc.ABCMeta):
Abstract base class for parameters.
"""

def __init__(
self,
value: T
) -> None:
def __init__(self, value: T) -> None:
self._value = value

@property
@abc.abstractmethod
def value(
self
) -> T:
def value(self) -> T:
raise NotImplementedError

def __eq__(
self,
other: Parameter
) -> bool:
def __eq__(self, other: Parameter) -> bool:
eq = self.value == other.value
if isinstance(eq, Iterable):
eq = all(eq)
return eq

@value.setter
def value(
self,
value
) -> None:
def value(self, value) -> None:
self._value = value

def set_random_value(
self
) -> None:
def set_random_value(self) -> None:
raise NotImplementedError


Expand All @@ -55,32 +42,21 @@ class FixedParameter(Parameter):
Parameter that never changes its value.
"""

def __init__(
self,
value: T
) -> None:
def __init__(self, value: T) -> None:
super().__init__(value)

@property
def value(
self
) -> T:
def value(self) -> T:
return self._value

def __eq__(
self,
other: Parameter
) -> bool:
def __eq__(self, other: Parameter) -> bool:
eq = self.value == other.value
if isinstance(eq, Iterable):
eq = all(eq)
return eq

@value.setter
def value(
self,
value
) -> None:
def value(self, value) -> None:
raise TypeError("Cannot change the value of a FixedParameter.")


Expand All @@ -89,33 +65,22 @@ class SynchronizedParameter(FixedParameter):
Parameter that maintains the value of another parameter.
"""

def __init__(
self,
linked_parameter: Parameter
) -> None:
def __init__(self, linked_parameter: Parameter) -> None:
super().__init__(linked_parameter.value)
self._linked_parameter = linked_parameter

@property
def value(
self
) -> T:
def value(self) -> T:
return self._linked_parameter.value

def __eq__(
self,
other: Parameter
) -> bool:
def __eq__(self, other: Parameter) -> bool:
eq = self.value == other.value
if isinstance(eq, Iterable):
eq = all(eq)
return eq

@value.setter
def value(
self,
value
) -> None:
def value(self, value) -> None:
raise TypeError("Cannot set the value of a SynchronizedParameter directly.")


Expand All @@ -124,66 +89,47 @@ class ContinuousParameter(Parameter):
boundaries will be generated."""

def __init__(
self,
low: float = -1.0,
high: float = 1.0,
value: Optional[float] = None
) -> None:
self, low: float = -1.0, high: float = 1.0, value: Optional[float] = None
) -> None:
super(ContinuousParameter, self).__init__(value=value)
self.low = low
self.high = high

@property
def value(
self
) -> float:
def value(self) -> float:
if self._value is None:
self.set_random_value()

self._value = np.clip(self._value, self.low, self.high)
return self._value

@value.setter
def value(
self,
value
) -> None:
def value(self, value) -> None:
self._value = value

def set_random_value(
self
) -> None:
def set_random_value(self) -> None:
self._value = fprs_random_state.uniform(low=self.low, high=self.high)


class DiscreteParameter(Parameter):
"""Parameter representing a discrete value from a given list of options."""

def __init__(
self,
options: List[T],
value: Optional[T] = None
) -> None:
def __init__(self, options: List[T], value: Optional[T] = None) -> None:
super(DiscreteParameter, self).__init__(value)
self.options = options

@property
def value(
self
) -> T:
def value(self) -> T:
if self._value is None:
self.set_random_value()
return self._value

@value.setter
def value(
self,
value: T
) -> None:
assert value in self.options, f"[DiscreteParameter] Given value '{value}' is not in options '{self.options}'"
def value(self, value: T) -> None:
assert (
value in self.options
), f"[DiscreteParameter] Given value '{value}' is not in options '{self.options}'"
self._value = value

def set_random_value(
self
) -> None:
def set_random_value(self) -> None:
self._value = fprs_random_state.choice(a=self.options)
59 changes: 17 additions & 42 deletions fprs/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

import numpy as np

from fprs.specification import ControllerSpecification, MorphologySpecification, RobotSpecification
from fprs.specification import (
ControllerSpecification,
MorphologySpecification,
RobotSpecification,
)

Observations = TypeVar("Observations")
Actions = TypeVar("Actions")
Expand All @@ -15,91 +19,62 @@
class Robot(ABC):
"""Abstract base class for a robot, which expresses the combination of a Morphology and a Controller."""

def __init__(
self,
specification: RobotSpecification
) -> None:
def __init__(self, specification: RobotSpecification) -> None:
self._specification = specification
self._morphology = None
self._controller = None

@property
def morphology(
self
) -> Morphology:
def morphology(self) -> Morphology:
if self._morphology is None:
self._morphology = self._build_morphology()
return self._morphology

@property
def controller(
self
) -> Controller:
def controller(self) -> Controller:
if self._controller is None:
self._controller = self._build_controller()
return self._controller

@abc.abstractmethod
def _build_morphology(
self
) -> Morphology:
def _build_morphology(self) -> Morphology:
raise NotImplementedError

@abc.abstractmethod
def _build_controller(
self
) -> Controller:
def _build_controller(self) -> Controller:
raise NotImplementedError

@property
def robot_specification(
self
) -> RobotSpecification:
def robot_specification(self) -> RobotSpecification:
return self._specification

def __call__(
self,
observations: Observations
) -> Actions:
def __call__(self, observations: Observations) -> Actions:
return self.controller(observations)


class Morphology(ABC):
"""Abstract base class for a robot's Morphology."""

def __init__(
self,
specification: MorphologySpecification
) -> None:
def __init__(self, specification: MorphologySpecification) -> None:
self._specification = specification

@property
def morphology_specification(
self
) -> Morphology:
def morphology_specification(self) -> Morphology:
return self._specification


class Controller(ABC):
"""Abstract base class for a robot's Controller."""

def __init__(
self,
specification: ControllerSpecification
) -> None:
def __init__(self, specification: ControllerSpecification) -> None:
self._specification = specification

@property
def controller_specification(
self
) -> ControllerSpecification:
def controller_specification(self) -> ControllerSpecification:
return self._specification

@abc.abstractmethod
def __call__(
self,
observations: np.ndarray
) -> np.ndarray:
def __call__(self, observations: np.ndarray) -> np.ndarray:
"""
Returns actions based on given observations.
Expand Down
Loading

0 comments on commit 1b21c4f

Please sign in to comment.