|
| 1 | +use snk_grid::{ |
| 2 | + direction::Direction, |
| 3 | + grid::{Color, Grid, iter_rectangle_fill}, |
| 4 | + snake::{Snake, Snake4}, |
| 5 | +}; |
| 6 | + |
| 7 | +pub struct SolutionFitness { |
| 8 | + pub remaining_color_count: u32, |
| 9 | + pub stack_fitness: u32, |
| 10 | + pub solution_fitness: u32, |
| 11 | +} |
| 12 | + |
| 13 | +pub fn get_solution_fitness( |
| 14 | + grid: &Grid<Color>, |
| 15 | + snake: Snake4, |
| 16 | + solution: Vec<Direction>, |
| 17 | +) -> SolutionFitness { |
| 18 | + let mut grid = grid.clone(); |
| 19 | + let mut snake = snake.clone(); |
| 20 | + let mut stack = Vec::new(); |
| 21 | + |
| 22 | + let solution_length = solution.len() as u32; |
| 23 | + |
| 24 | + for direction in solution { |
| 25 | + step(&mut grid, &mut snake, &mut stack, direction); |
| 26 | + } |
| 27 | + |
| 28 | + let remaining_color_count = iter_rectangle_fill(grid.width as i8, grid.height as i8) |
| 29 | + .fold(0, |sum, p| { |
| 30 | + sum + if grid.get_color(p).is_empty() { 1 } else { 0 } |
| 31 | + }); |
| 32 | + let stack_fitness = get_stack_fitness(&stack); |
| 33 | + let solution_fitness = remaining_color_count * 100_000 + stack_fitness * 1000 + solution_length; |
| 34 | + |
| 35 | + SolutionFitness { |
| 36 | + remaining_color_count, |
| 37 | + stack_fitness, |
| 38 | + solution_fitness, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// a fitness that count the number of permutation to order the stack |
| 43 | +// 0 = perfect |
| 44 | +// the bigger fitness is, the worse the stack |
| 45 | +pub fn get_stack_fitness(stack: &Vec<Color>) -> u32 { |
| 46 | + let mut stack = stack.clone(); |
| 47 | + let mut fitness = 0; |
| 48 | + |
| 49 | + stack.push(Color::Color4); |
| 50 | + |
| 51 | + for i in 0..(stack.len() - 1) { |
| 52 | + while stack[i] > stack[i + 1] { |
| 53 | + let mut j = 0; |
| 54 | + while stack[i + j] > stack[i + j + 1] { |
| 55 | + let tmp = stack[i + j + 1]; |
| 56 | + stack[i + j + 1] = stack[i + j]; |
| 57 | + stack[i + j] = tmp; |
| 58 | + j += 1; |
| 59 | + fitness += 1; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fitness |
| 65 | +} |
| 66 | + |
| 67 | +pub fn step( |
| 68 | + grid: &mut Grid<Color>, |
| 69 | + snake: &mut Snake4, |
| 70 | + stack: &mut Vec<Color>, |
| 71 | + direction: Direction, |
| 72 | +) -> () { |
| 73 | + snake.move_snake(direction); |
| 74 | + |
| 75 | + let head = snake.get_head(); |
| 76 | + |
| 77 | + let c = grid.get_color(head); |
| 78 | + |
| 79 | + if !c.is_empty() { |
| 80 | + grid.set(head, Color::Empty); |
| 81 | + stack.push(c); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn it_should_compute_stack_fitness_for_empty_stack() { |
| 87 | + let stack = vec![]; |
| 88 | + assert_eq!(get_stack_fitness(&stack), 0); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn it_should_compute_stack_fitness_for_orderer_stack() { |
| 93 | + let stack = vec![Color::Color1, Color::Color2, Color::Color2, Color::Color4]; |
| 94 | + assert_eq!(get_stack_fitness(&stack), 0); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn it_should_compute_stack_fitness_for_unperfect_stack() { |
| 99 | + let stack = vec![Color::Color1, Color::Color2, Color::Color1]; |
| 100 | + assert_eq!(get_stack_fitness(&stack), 1); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn it_should_compute_stack_fitness_for_unperfect_stack_2() { |
| 105 | + let stack = vec![ |
| 106 | + Color::Color1, |
| 107 | + Color::Color2, |
| 108 | + Color::Color1, |
| 109 | + Color::Color1, |
| 110 | + Color::Color3, |
| 111 | + ]; |
| 112 | + assert_eq!(get_stack_fitness(&stack), 2); |
| 113 | + |
| 114 | + let stack = vec![ |
| 115 | + Color::Color1, |
| 116 | + Color::Color3, |
| 117 | + Color::Color2, |
| 118 | + Color::Color1, |
| 119 | + Color::Color1, |
| 120 | + ]; |
| 121 | + assert_eq!(get_stack_fitness(&stack), 5); |
| 122 | +} |
0 commit comments