Skip to content

Commit

Permalink
Revert method names in .decay (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Sep 3, 2024
1 parent ae4c8a4 commit 91b6ee0
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 129 deletions.
51 changes: 21 additions & 30 deletions src/mlrose_ky/algorithms/decay/arithmetic_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,51 @@ class ArithDecay:
Parameters
----------
initial_temperature : float
init_temp : float
Initial value of the temperature parameter T. Must be greater than 0.
decay_rate : float
decay : float
Temperature decay parameter. Must be a positive value and less than or equal to 1.
minimum_temperature : float
Minimum allowable value of the temperature parameter. Must be positive and less than `initial_temperature`.
min_temp : float
Minimum allowable value of the temperature parameter. Must be positive and less than `init_temp`.
Attributes
----------
initial_temperature : float
init_temp : float
Stores the initial temperature.
decay_rate : float
decay : float
Stores the rate of temperature decay.
minimum_temperature : float
min_temp : float
Stores the minimum temperature.
Examples
--------
>>> schedule = ArithDecay(initial_temperature=10, decay_rate=0.95, minimum_temperature=1)
>>> schedule = ArithDecay(init_temp=10, decay=0.95, min_temp=1)
>>> schedule.evaluate(5)
5.25
"""

def __init__(self, initial_temperature: float = 1.0, decay_rate: float = 0.0001, minimum_temperature: float = 0.001) -> None:
self.initial_temperature: float = initial_temperature
self.decay_rate: float = decay_rate
self.minimum_temperature: float = minimum_temperature
def __init__(self, init_temp: float = 1.0, decay: float = 0.0001, min_temp: float = 0.001) -> None:
self.init_temp: float = init_temp
self.decay: float = decay
self.min_temp: float = min_temp

if self.initial_temperature <= 0:
if self.init_temp <= 0:
raise ValueError("Initial temperature must be greater than 0.")
if not (0 < self.decay_rate <= 1):
if not (0 < self.decay <= 1):
raise ValueError("Decay rate must be greater than 0 and less than or equal to 1.")
if not (0 < self.minimum_temperature < self.initial_temperature):
if not (0 < self.min_temp < self.init_temp):
raise ValueError("Minimum temperature must be greater than 0 and less than initial temperature.")

def __str__(self) -> str:
return (
f"ArithDecay(initial_temperature={self.initial_temperature}, "
f"decay_rate={self.decay_rate}, "
f"minimum_temperature={self.minimum_temperature})"
)
return f"ArithDecay(init_temp={self.init_temp}, decay={self.decay}, min_temp={self.min_temp})"

def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, ArithDecay):
return False
return (
self.initial_temperature == other.initial_temperature
and self.decay_rate == other.decay_rate
and self.minimum_temperature == other.minimum_temperature
)
return self.init_temp == other.init_temp and self.decay == other.decay and self.min_temp == other.min_temp

def evaluate(self, time: int) -> float:
"""
Expand All @@ -86,8 +78,7 @@ def evaluate(self, time: int) -> float:
float
The temperature parameter at the given time, respecting the minimum temperature.
"""
temperature = max(self.initial_temperature - (self.decay_rate * time), self.minimum_temperature)
return temperature
return max(self.init_temp - (self.decay * time), self.min_temp)

def get_info(self, time: int = None, prefix: str = "") -> dict:
"""
Expand All @@ -110,9 +101,9 @@ def get_info(self, time: int = None, prefix: str = "") -> dict:

info = {
f"{info_prefix}type": "arithmetic",
f"{info_prefix}initial_temperature": self.initial_temperature,
f"{info_prefix}decay_rate": self.decay_rate,
f"{info_prefix}minimum_temperature": self.minimum_temperature,
f"{info_prefix}init_temp": self.init_temp,
f"{info_prefix}decay": self.decay,
f"{info_prefix}min_temp": self.min_temp,
}

if time is not None:
Expand Down
28 changes: 14 additions & 14 deletions src/mlrose_ky/algorithms/decay/custom_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CustomSchedule:
Parameters
----------
decay_function : Callable[..., float]
A function with the signature `decay_function(t: int, **kwargs)` that calculates the temperature at time t.
schedule : Callable[..., float]
A function with the signature `schedule(t: int, **kwargs)` that calculates the temperature at time t.
**kwargs : dict
Additional keyword arguments to be passed to the decay function.
Expand All @@ -29,44 +29,44 @@ class CustomSchedule:
15
"""

def __init__(self, decay_function: Callable[..., float], **kwargs) -> None:
self.decay_function: Callable[..., float] = decay_function
def __init__(self, schedule: Callable[..., float], **kwargs) -> None:
self.schedule: Callable[..., float] = schedule
self.kwargs: dict = kwargs

def __str__(self) -> str:
return f"CustomSchedule(function={self.decay_function.__name__}, parameters={self.kwargs})"
return f"CustomSchedule(schedule={self.schedule.__name__}, kwargs={self.kwargs})"

def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, CustomSchedule):
return False
return self.decay_function == other.decay_function and self.kwargs == other.kwargs
return self.schedule == other.schedule and self.kwargs == other.kwargs

def evaluate(self, time: int) -> float:
def evaluate(self, t: int) -> float:
"""
Evaluate the temperature parameter at the specified time using the custom decay function.
Parameters
----------
time : int
t : int
The time at which to evaluate the temperature parameter.
Returns
-------
float
The calculated temperature at the specified time.
"""
return self.decay_function(time, **self.kwargs)
return self.schedule(t, **self.kwargs)

def get_info(self, time: int | None = None, prefix: str = "") -> dict:
def get_info__(self, t: int | None = None, prefix: str = "") -> dict:
"""
Retrieve a dictionary containing the configuration of the decay schedule and optionally the current value.
Parameters
----------
time : int | None, optional
t : int | None, optional
If provided, include the current temperature value at the given time.
prefix : str, optional
A prefix to append to each dictionary key, enhancing integration with other data structures.
Expand All @@ -80,11 +80,11 @@ def get_info(self, time: int | None = None, prefix: str = "") -> dict:

info = {
f"{info_prefix}type": "custom",
f"{info_prefix}function": self.decay_function.__name__,
f"{info_prefix}schedule": self.schedule.__name__,
**{f"{info_prefix}param_{key}": value for key, value in self.kwargs.items()},
}

if time is not None:
info[f"{info_prefix}current_value"] = self.evaluate(time)
if t is not None:
info[f"{info_prefix}current_value"] = self.evaluate(t)

return info
63 changes: 27 additions & 36 deletions src/mlrose_ky/algorithms/decay/exponential_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,84 +20,75 @@ class ExpDecay:
Parameters
----------
initial_temperature : float, default=1.0
init_temp : float, default=1.0
The initial value of the temperature parameter T. Must be greater than 0.
decay_rate : float, default=0.005
exp_const : float, default=0.005
The rate of exponential decay. Must be greater than 0.
minimum_temperature : float, default=0.001
The minimum allowable temperature. Must be greater than 0 and less than `initial_temperature`.
min_temp : float, default=0.001
The minimum allowable temperature. Must be greater than 0 and less than `init_temp`.
Attributes
----------
initial_temperature : float
init_temp : float
Stores the initial temperature.
decay_rate : float
exp_const : float
Stores the rate of exponential decay.
minimum_temperature : float
min_temp : float
Stores the minimum temperature.
Examples
--------
>>> schedule = ExpDecay(initial_temperature=10, decay_rate=0.05, minimum_temperature=1)
>>> schedule = ExpDecay(init_temp=10, exp_const=0.05, min_temp=1)
>>> print(schedule.evaluate(5))
7.788007830714049
"""

def __init__(self, initial_temperature: float = 1.0, decay_rate: float = 0.005, minimum_temperature: float = 0.001) -> None:
self.initial_temperature: float = initial_temperature
self.decay_rate: float = decay_rate
self.minimum_temperature: float = minimum_temperature
def __init__(self, init_temp: float = 1.0, exp_const: float = 0.005, min_temp: float = 0.001) -> None:
self.init_temp: float = init_temp
self.exp_const: float = exp_const
self.min_temp: float = min_temp

if self.initial_temperature <= 0:
if self.init_temp <= 0:
raise ValueError("Initial temperature must be greater than 0.")
if self.decay_rate <= 0:
if self.exp_const <= 0:
raise ValueError("Decay rate must be greater than 0 and positive.")
if not (0 < self.minimum_temperature < self.initial_temperature):
if not (0 < self.min_temp < self.init_temp):
raise ValueError("Minimum temperature must be greater than 0 and less than initial temperature.")

def __str__(self) -> str:
return (
f"ExpDecay(initial_temperature={self.initial_temperature}, "
f"decay_rate={self.decay_rate}, "
f"minimum_temperature={self.minimum_temperature})"
)
return f"ExpDecay(init_temp={self.init_temp}, exp_const={self.exp_const}, min_temp={self.min_temp})"

def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, ExpDecay):
return False
return (
self.initial_temperature == other.initial_temperature
and self.decay_rate == other.decay_rate
and self.minimum_temperature == other.minimum_temperature
)
return self.init_temp == other.init_temp and self.exp_const == other.exp_const and self.min_temp == other.min_temp

def evaluate(self, time: int) -> float:
def evaluate(self, t: int) -> float:
"""
Evaluate the temperature parameter at the specified time using exponential decay.
Parameters
----------
time : int
t : int
The time at which the temperature parameter T is evaluated.
Returns
-------
float
The temperature parameter at the given time, respecting the minimum temperature.
"""
temperature = max(self.initial_temperature * np.exp(-self.decay_rate * time), self.minimum_temperature)
return temperature
return float(max(self.init_temp * np.exp(-self.exp_const * t), self.min_temp))

def get_info(self, time: int | None = None, prefix: str = "") -> dict:
def get_info__(self, t: int | None = None, prefix: str = "") -> dict:
"""
Retrieve a dictionary containing the configuration and optionally the current value of the decay schedule.
Parameters
----------
time : int | None, optional
t : int | None, optional
If provided, include the current temperature value at the given time.
prefix : str, optional
A prefix to append to each dictionary key, enhancing integration with other data structures.
Expand All @@ -111,12 +102,12 @@ def get_info(self, time: int | None = None, prefix: str = "") -> dict:

info = {
f"{info_prefix}type": "exponential",
f"{info_prefix}initial_temperature": self.initial_temperature,
f"{info_prefix}decay_rate": self.decay_rate,
f"{info_prefix}minimum_temperature": self.minimum_temperature,
f"{info_prefix}init_temp": self.init_temp,
f"{info_prefix}exp_const": self.exp_const,
f"{info_prefix}min_temp": self.min_temp,
}

if time is not None:
info[f"{info_prefix}current_value"] = self.evaluate(time)
if t is not None:
info[f"{info_prefix}current_value"] = self.evaluate(t)

return info
Loading

0 comments on commit 91b6ee0

Please sign in to comment.