Trait Problem

Source
pub trait Problem {
    // Required method
    fn compute<T: Individual>(&self, ind: &mut T) -> f32;

    // Provided methods
    fn is_solution(&self, value: f32) -> bool { ... }
    fn get_random_individual<T: Individual, R: Rng>(
        &self,
        size: usize,
        rng: &mut R,
    ) -> T { ... }
}
Expand description

Represents baseline interface for the objective function.

By default solution is represented by a vector of real-numbers.

§Example: Custom optimization problem

extern crate revonet;
extern crate rand;

use rand::{Rng, SeedableRng, StdRng};

use revonet::ea::*;
use revonet::problem::*;

// Dummy problem returning random fitness.
pub struct DummyProblem;

impl Problem for DummyProblem {
    // Function to evaluate a specific individual.
    fn compute<T: Individual>(&self, ind: &mut T) -> f32 {
        // use `to_vec` to get real-coded representation of an individual.
        let v = ind.to_vec().unwrap();

        // Perform calculations as per optimization problem being implemened.
        // Here just a random value is returned.
        let mut rng: StdRng = StdRng::from_seed(&[0]);
        rng.gen::<f32>()
    }
}

fn main() {}

Required Methods§

Source

fn compute<T: Individual>(&self, ind: &mut T) -> f32

Computes fitness value for a given individual.

§Arguments:
  • ind - individual to compute a fitness for.

Provided Methods§

Source

fn is_solution(&self, value: f32) -> bool

Returns whether given fitness value is enough to be a solution.

§Arguments:
  • value - fitness value to consider.
Source

fn get_random_individual<T: Individual, R: Rng>( &self, size: usize, rng: &mut R, ) -> T

Generate random individual for the problem. Default implementation creates a real-coded individual with the number of genes equal to size

§Arguments:
  • size - number of genes.
  • rng - reference to pre-initialized RNG.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Problem for RastriginProblem

Source§

impl Problem for RosenbrockProblem

Source§

impl Problem for SphereProblem

Source§

impl<T: NeuroProblem> Problem for T

Default implementation of the Problem trait for NeuroProblem