Skip to content

Commit

Permalink
Merge pull request Wandalen#1095 from YuliaProkopovych/tsp-results
Browse files Browse the repository at this point in the history
NOT READY: TSP results
  • Loading branch information
Wandalen authored Feb 1, 2024
2 parents d8e0c95 + 022152d commit 0aada54
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 232 deletions.
52 changes: 52 additions & 0 deletions module/move/optimization_tools/src/optimization/diagram.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```mermaid
flowchart TB
id1([Begin])
-->
id2["`Initialize values:
*ga_max_stale_iterations*,
*sa_mutations_per_dynasty_limit*,
*reset_limit*,
*crossover_rate*,
elite_selection_rate,
mutation_rate,
fitness_recalculation,
dynasties_limit,
population_size,
population_percent`"]
-->
id3["`Initialize operators:
*crossover_operator*,
*mutation_operator*,
*selection_operator*,
*temperature_scheduler*,
*population_seeder*;`"]
-->
id4["`Calculate initial values for main loop:
*population* = initial_population,
*dynasties_number* = 1,
*stale_generations* = 0,
*prev_best_fitness = initial_population best individual fitness*,
*temperature* = initial_temperature`"]
-- enter main loop -->
id5{{"`*dynasties_number* > *dynasties_limit* ?`"}}
id5 -->|No| id6{{ population has solution ? }}
id6 -->|No| id7{{"`*stale_populations* > *stale_population_limit* ?`"}}
id7 -->|No| id8{{"`*population_best_fitness* == *prev_best_fitness* ?`"}}
id7 -->|Yes| id9[ Reseed population, reset temperature ]
id9 --> id8
id8 -->|"No ( There's progress ) "| id10["`*prev_best_fitness* = *population_best_fitness*, *stale_populations* = 0;`"]
id8 -->|"Yes (Stale population) "| id11["`*stale_population* += 1`"]
id10 --> id12[ Initialaize new_population ]
id11 --> id12
id12 --> id13["`*new_population* = *population.evolve*`"]
id13 -->|check new_population length| id14{{"`*new_population_len* < *population_len*`"}}
id14 -->|Yes| id15[ Clone individuals from old population ]
id14 -->|No| id16["`Update temperature with *temperature_scheduler*,
*population* = *new_population*,
*dynasties_number* += 1;`"]
id15 --> id16
id16 --> id5
id6 -->|Yes| id20([End optimization])
id5 -->|Yes| id20
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
```mermaid
flowchart TB
id1([Begin])
-->
id2["`Values initialized in optimizer:
*elite_selection_rate*,
*mutation_rate* = 1,
*crossover_rate* = 0`"]
-->
id4["`Values passed as parameters:
*person*,
*population*,
*tamperature*`"]
-- start -->
id5{{"`*person* is in elites ?`"}}
id5 -->|Yes| id10[ clone person ]
id5 -->|No| id6{{"`rand > *mutation_rate* ?`"}}
id6 -->|No| id7["`use *crossover_operator* `"]
id6 -->|Yes| id8["`use *mutation_operator* `"]
id7 -->id9["`select parents with *selection_operator*`"]
id9 -->id11["`perform crossover`"]
id8 -->id12["`Initialize values
*n_mutations* = 0;
*expected_number_of_mutations* = 4;`"]
id12 -- Enter loop --> id13{{"`*n_mutations* > *mutations_per_dynasty_limit* ?`"}}
id13 -->|Yes| id14["End loop"]
id13 -->|No| id15["`Create *expected_number_of_mutations* candidate persons`"]
id15 -->id17["`Mutate candidates with *mutation_operator*`"]
id17 -->id18{{"` Candidates contain vital candidate ?`"}}
id18 -->|Yes| id14["End loop"]
id18 -->|No| id19["`*n_mutations* += 1;
expected_number_of_mutations += 4;`"]
id19 --> id13
id11 --> id16([End])
id14 --> id16
id10 --> id16
```
28 changes: 8 additions & 20 deletions module/move/optimization_tools/src/optimization/gen_alg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
//!
//! New population is modified by appling mutation to some individuals in the population. Individual's likelihood of being mutated id determined by mutation_rate value.
//!
//! Termination: process is stopped if sudoku solution is found or if max_generation_number value is exseeded.
//! Termination: process is stopped if sudoku solution is found or if max_dynasties_number value is exseeded.
//!
use std::fmt::Debug;
use deterministic_rand::Hrng;

use crate::optimization::*;

/// Functionality of crossover genetic operator.
pub trait CrossoverOperator : Debug
{
///
/// Type that represents solution that crossover is performed on.
type Person : Individual + Clone;

/// Produce new Individual using genetic matherial of two selected Individuals.
Expand Down Expand Up @@ -73,29 +71,19 @@ pub trait MutationOperator : Debug
fn mutate( &self, hrng : Hrng, person : &mut Self::Person, context : &Self::Problem );
}

/// Fuctionality of operator responsible for creation of initial solutions generation.
/// Fuctionality of operator responsible for creation of initial solutions population.
pub trait InitialProblem
{
/// Type that represents generation of solutions in optimization process.
type Person : Individual + Clone + PartialEq + Send + Sync;
/// Type that represents Individual in population of solutions in optimization process.
type Person : Individual + Clone + PartialEq + Send + Sync + Debug;
// type Context : Sync;

/// Create the initial generation for the optimization algorithm.
fn initial_generation( &self, hrng : Hrng, size : usize ) -> Vec< Self::Person >;
/// Create the initial population for the optimization algorithm.
fn initial_population( &self, hrng : Hrng, size : usize ) -> Vec< Self::Person >;

/// Create the initial generation for the optimization algorithm.
fn initial_temperature( &self, hrng : Hrng ) -> Temperature;
fn get_random_person( &self, hrng : Hrng ) -> Self::Person;

/// Evaluate fitness of provided solution.
fn evaluate( &self, person : &Self::Person ) -> f64;

// fn context( &self ) -> &Self::Context;
}

/// Functionality of generation of solutions for optimization.
pub trait Generation
{
/// Check if current generation contains optimal solution.
fn is_good_enough( &self ) -> bool;
}

Loading

0 comments on commit 0aada54

Please sign in to comment.