You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def evaluate(self, state: np.ndarray) -> float:
"""Evaluate the fitness of a state vector.
Parameters
----------
state : np.ndarray
State array for evaluation. Must be the same length as the weights
and values arrays.
Returns
-------
float
Value of fitness function.
Raises
------
ValueError
If `state` is not the same size as the weights and values arrays.
TypeError
If `state` is not an instance of `np.ndarray`.
"""
if not isinstance(state, np.ndarray):
raise TypeError(f"Expected state_vector to be np.ndarray, got {type(state).__name__} instead.")
if len(state) != len(self.weights):
raise ValueError("The state_vector must be the same size as the weights and values arrays.")
total_weight = np.sum(state * self.weights)
total_value = np.sum(state * self.values)
if total_weight <= self._w:
return float(total_value)
return 0.0
The final line return 0.0 should probably something like return 1e-6 to avoid a 0.0.
The text was updated successfully, but these errors were encountered:
The final line return 0.0 should probably something like return 1e-6 to avoid a 0.0.
The text was updated successfully, but these errors were encountered: