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
impl Routes
Sourcepub fn random(n_routes: usize, route_length: usize) -> Self
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])]);
Sourcepub fn add_vec_route(self, routes: Vec<Route>) -> Self
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 ofRoute
s 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])]);
Sourcepub fn combine_routes(self, other_routes: Routes) -> Self
pub fn combine_routes(self, other_routes: Routes) -> Self
Combine two routes objects.
§Arguments
routes
- A vector ofRoute
s 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));
Sourcepub fn get_n_nodes(&self) -> usize
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());
Sourcepub fn add_n_random_nodes(self, n_random_nodes: usize) -> Self
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 From<Vec<Route>> for Routes
impl From<Vec<Route>> for Routes
Source§fn from(routes: Vec<Route>) -> Self
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<'a> Population<'a> for Routes
impl<'a> Population<'a> for Routes
Source§fn get_fittest_population(&self, n: usize, distance_mat: &DistanceMat) -> Routes
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
fn evolve(&self, mutate_prob: f32) -> Routes
Evolve your population.
The evolution consists of the following stages:
crossover
between all 1,…,n routes excluding the route itself.mutate
is applied to all individuals.
§Arguments
mutate_prob
- The probabilty of an inviduals beeing mutated. Is applied viaindividuals.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>
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
type Individual = Route
Source§type IndividualCollection = Iter<'a, Route>
type IndividualCollection = Iter<'a, Route>
Population
.