Struct Routes

Source
pub struct Routes { /* private fields */ }
Expand description

The Population is your current pools of routes that you would to improve by evolving them.

Implementations§

Source§

impl Routes

Source

pub fn random(n_routes: usize, route_length: usize) -> Self

Create a new Population of routes by creating random invidiual routes.

§Arguments
  • n_routse - The number of routes your population of routes should contain.
  • route_length - The length of an individual route.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;

let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
Source

pub fn add_vec_route(self, routes: Vec<Route>) -> Self

Add new routes to a Routes-object and create a new Routes-object

§Arguments
  • routes - A vector of Routes that should be added.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;

let current_routes = Routes::from(vec![Route::new(vec![1]), Route::new(vec![2])]);
let extended_routes = current_routes.add_vec_route(vec![Route::new(vec![3]), Route::new(vec![4])]);
Source

pub fn combine_routes(self, other_routes: Routes) -> Self

Combine two routes objects.

§Arguments
  • routes - A vector of Routes that should be added.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;

let current_routes = Routes::from(vec![Route::new(vec![1]), Route::new(vec![2])]);
let other_routes = Routes::from(vec![Route::new(vec![3]), Route::new(vec![4])]);
println!("{}", current_routes.combine_routes(other_routes));
Source

pub fn get_n_nodes(&self) -> usize

Get the number of nodes for the Route’s in this Routes-object.

§Examples
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_tsp::routes::Routes;

let routes_with_three_nodes = Routes::from(vec![Route::new(vec![1,2,3,]), Route::new(vec![4,5,6])]);
println!("The route have {} nodes", routes_with_three_nodes.get_n_nodes());
Source

pub fn add_n_random_nodes(self, n_random_nodes: usize) -> Self

Add n random nodes to your current pool.

§Arguments:

n_random_nodes: The number of random nodes that should be added.

§Examples
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_tsp::routes::Routes;

let a_single_route = Routes::from(vec![Route::new(vec![0,1,2])]);
println!("{}", a_single_route.add_n_random_nodes(1));

Trait Implementations§

Source§

impl Clone for Routes

Source§

fn clone(&self) -> Routes

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Routes

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Routes

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Vec<Route>> for Routes

Source§

fn from(routes: Vec<Route>) -> Self

Create a new Population of Routse from a vector of routes.

§Arguments
  • routes - The routes you collected so far and would like to put into your routes.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;

let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
Source§

impl PartialEq for Routes

Source§

fn eq(&self, other: &Routes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Population<'a> for Routes

Source§

fn get_fittest_population(&self, n: usize, distance_mat: &DistanceMat) -> Routes

Given your pool of current routes, compute the fitness of your individuals to solve the problem at hand.

§Arguments
  • distance_mat - The distances between nodes that is neccessary to computes how well the route work in terms of the TSP
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_tsp::distance_mat::DistanceMat;
use genetic_algorithm_traits::Population;

let distance_matrix = DistanceMat::new(vec![vec![0.0,1.0,2.0], vec![1.0,0.0,3.0], vec![2.0,3.0,0.0]]);
let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
println!("Your routes's fitnesses: {:?}", routes.fitnesses(&distance_matrix));

Get the n fittest individuals in your routes as new routes object. This is typically used to select the top n inidividuals, before continuing to evolve the routes further.

§Arguments
  • n - The number of individuals you would like to have.
  • distance_mat - The distance matrix the fitness should be evaluated on.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_tsp::distance_mat::DistanceMat;
use genetic_algorithm_traits::Population;

let distance_matrix = DistanceMat::new(vec![vec![0.0,1.0,2.0], vec![1.0,0.0,3.0], vec![2.0,3.0,0.0]]);
let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
let my_fittest_routes = routes.get_fittest_population(2, &distance_matrix);
Source§

fn evolve(&self, mutate_prob: f32) -> Routes

Evolve your population.

The evolution consists of the following stages:

  1. crossover between all 1,…,n routes excluding the route itself.
  2. mutate is applied to all individuals.
§Arguments
  • mutate_prob - The probabilty of an inviduals beeing mutated. Is applied via individuals.mutate.
§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_traits::Population;
use genetic_algorithm_tsp::distance_mat::DistanceMat;

let distance_matrix = DistanceMat::new(vec![vec![0.0,1.0,2.0], vec![1.0,0.0,3.0], vec![2.0,3.0,0.0]]);
let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
let evolved_routes = routes.evolve(0.5);
Source§

fn iter(&'a self) -> Iter<'_, Route>

Iterate over the individuals of your population.

§Examples
use genetic_algorithm_tsp::routes::Routes;
use genetic_algorithm_tsp::route::Route;
use genetic_algorithm_traits::Population;

let routes = Routes::from(vec![Route::new(vec![0,1,2]), Route::new(vec![1,0,2])]);
for route in routes.iter(){
    println!("{:?}", route);
}
Source§

type Individual = Route

The Type of individuals your population should consist of.
Source§

type IndividualCollection = Iter<'a, Route>

The iteratore type if you iterate over your individuals. It depends on the data container you use to store individuals in your implementation of Population.
Source§

fn fitnesses( &'a self, cost_data: &'a <Self::Individual as Individual<'a>>::IndividualCost, ) -> Vec<(f64, &'a Self::Individual)>

Given the pool of current individuals, compute the fitness of your individuals to solve the problem at hand. Read more
Source§

fn get_n_fittest( &'a self, n: usize, cost_data: &'a <Self::Individual as Individual<'a>>::IndividualCost, ) -> Vec<Self::Individual>

Get the n fittest individuals in your population. Read more
Source§

fn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual>

Evolve your population. Read more
Source§

impl StructuralPartialEq for Routes

Auto Trait Implementations§

§

impl Freeze for Routes

§

impl RefUnwindSafe for Routes

§

impl Send for Routes

§

impl Sync for Routes

§

impl Unpin for Routes

§

impl UnwindSafe for Routes

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V