pub trait Constraint<P>:
Debug
+ Send
+ Syncwhere
P: Phenotype,{
// Required method
fn check(&self, phenotype: &P) -> Vec<ConstraintViolation>;
// Provided methods
fn repair(&self, _phenotype: &mut P) -> bool { ... }
fn repair_with_rng(
&self,
_phenotype: &mut P,
_rng: &mut RandomNumberGenerator,
) -> bool { ... }
fn penalty_score(&self, violations: &[ConstraintViolation]) -> f64 { ... }
}Expand description
Trait for defining constraints that can be applied to phenotypes.
Constraints check whether a phenotype satisfies certain requirements and optionally provide mechanisms to repair invalid phenotypes.
Required Methods§
Sourcefn check(&self, phenotype: &P) -> Vec<ConstraintViolation>
fn check(&self, phenotype: &P) -> Vec<ConstraintViolation>
Checks if the phenotype satisfies this constraint.
Returns a vector of constraint violations. An empty vector indicates that the phenotype satisfies the constraint.
Provided Methods§
Sourcefn repair(&self, _phenotype: &mut P) -> bool
fn repair(&self, _phenotype: &mut P) -> bool
Attempts to repair the phenotype to satisfy this constraint.
Returns true if the phenotype was successfully repaired, false otherwise.
The default implementation does not perform any repair.
Sourcefn repair_with_rng(
&self,
_phenotype: &mut P,
_rng: &mut RandomNumberGenerator,
) -> bool
fn repair_with_rng( &self, _phenotype: &mut P, _rng: &mut RandomNumberGenerator, ) -> bool
Attempts to repair the phenotype using a random number generator.
This method is useful for repair strategies that require randomness.
Returns true if the phenotype was successfully repaired, false otherwise.
The default implementation does not perform any repair.
Sourcefn penalty_score(&self, violations: &[ConstraintViolation]) -> f64
fn penalty_score(&self, violations: &[ConstraintViolation]) -> f64
Returns a penalty score for the given constraint violations.
This score can be used to adjust the fitness of phenotypes that violate the constraint. Higher values indicate more severe violations.
The default implementation sums the severity of all violations, or counts the number of violations if severity is not specified.