Skip to content

Commit

Permalink
Improve equation definitions for clarity in FourPeaks and SixPeaks
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Oct 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 2cd87ca commit ba7551b
Showing 2 changed files with 28 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/mlrose_ky/fitness/four_peaks.py
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@


class FourPeaks(_DiscretePeaksBase):
"""Fitness function for Four Peaks optimization problem. Evaluates the
fitness of an n-dimensional state vector `x`, given parameter T, as:
"""
Fitness function for Four Peaks optimization problem. Evaluates the
fitness of an N-dimensional state vector `x`, given parameter T, as:
.. math::
@@ -20,15 +21,19 @@ class FourPeaks(_DiscretePeaksBase):
* `tail(b, x)` is the number of trailing b's in `x`;
* `head(b, x)` is the number of leading b's in `x`;
* `R(x, T) = n`, if `tail(0, x) > T` and `head(1, x) > T`; and
* `R(x, T) = N`, if `tail(0, x) > T` and `head(1, x) > T`; and
* `R(x, T) = 0`, otherwise.
There are two global maxima for this function. They are achieved either when there are `T + 1` leading 1's followed by all 0's, or
when there are `T + 1` trailing 0's preceded by all 1's. There are also two suboptimal local maxima that occur with a string of all
1's or all 0's. For large values of `T`, this problem becomes increasingly more difficult because the basin of attraction for the
inferior local maxima becomes larger.
Parameters
----------
t_pct : float, optional, default=0.1
Threshold parameter (T) for Four Peaks fitness function, expressed as
a percentage of the state space dimension, n (i.e.
`T = threshold_pct \\times n`).
Threshold parameter (T) for Four Peaks fitness function, expressed as a percentage of the state space dimension, N
(i.e. `T = threshold_pct * N`).
Examples
--------
@@ -39,9 +44,9 @@ class FourPeaks(_DiscretePeaksBase):
References
----------
De Bonet, J., C. Isbell, and P. Viola (1997). MIMIC: Finding Optima by
De Bonet, J., Isbell, C., & Viola, P. (1997). MIMIC: Finding Optima by
Estimating Probability Densities. In *Advances in Neural Information
Processing Systems* (NIPS) 9, pp. 424–430.
Processing Systems* (NIPS) 9, 424–430.
Note
----
25 changes: 15 additions & 10 deletions src/mlrose_ky/fitness/six_peaks.py
Original file line number Diff line number Diff line change
@@ -9,27 +9,32 @@


class SixPeaks(_DiscretePeaksBase):
"""Fitness function for Six Peaks optimization problem. Evaluates the
fitness of an n-dimensional state vector `x`, given parameter T, as:
"""
Fitness function for Six Peaks optimization problem. Evaluates the
fitness of an N-dimensional state vector `x`, given parameter T, as:
.. math::
Fitness(x, T) = \\max(tail(0, x), head(1, x)) + R(x, T)
where:
The Six Peaks problem is a slight variation on :py:class:`mlrose_ky.fitness.FourPeaks` where:
* `tail(b, x)` is the number of trailing b's in `x`;
* `head(b, x)` is the number of leading b's in `x`;
* `R(x, T) = n`, if (`tail(0, x) > T` and
`head(1, x) > T`) or (`tail(1, x) > T` and
`head(0, x) > T`); and
* `R(x, T) = N`, if (`tail(0, x) > T` and `head(1, x) > T`) or
(`tail(1, x) > T` and `head(0, x) > T`); and
* `R(x, T) = 0`, otherwise.
This function has two additional global maxima where there are `T + 1` leading 0's followed by all 1's, or when there are `T + 1`
trailing 1's preceded by all 0's. In this case, it is not the values of the candidates that is important, but their structure: the
first `T + 1` positions should take on the same value, the last `T + 1` positions should take on the same value, these two groups
should take on different values, and the middle positions should take on all the same value.
Parameters
----------
t_pct : float, optional, default=0.1
Threshold parameter (T) for Six Peaks fitness function, expressed as a percentage
of the state space dimension, n (i.e. `T = threshold_pct \\times n`).
Threshold parameter (T) for Six Peaks fitness function, expressed as a percentage of the state space dimension, N
(i.e. `T = threshold_pct * N`).
Examples
--------
@@ -95,10 +100,10 @@ def evaluate(self, state: np.ndarray) -> float:
leading_ones = self.head(1, state)
trailing_ones = self.tail(1, state)

# Calculate max(tail(0, x), head(1, x))
# Calculate max(tail(0, x), head(1, x)); identical to FourPeaks
max_score = max(trailing_zeros, leading_ones)

# Calculate R(x, T)
# Calculate R(x, T); slightly differs from FourPeaks
reward = (
vector_length
if (trailing_zeros > threshold and leading_ones > threshold) or (trailing_ones > threshold and leading_zeros > threshold)

0 comments on commit ba7551b

Please sign in to comment.