Skip to content

Commit

Permalink
Actually make the new method work
Browse files Browse the repository at this point in the history
  • Loading branch information
leo848 committed Apr 27, 2024
1 parent 2d05655 commit d5208d4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod creation;
pub mod improve;
pub mod improvement;

use std::fmt::Debug;
use std::ops::Range;

use itertools::Itertools;
Expand All @@ -19,7 +20,7 @@ use crate::{
const PESSIMAL: bool = false;

pub trait PathContext {
type Path: PartialEq + Clone;
type Path: Debug + PartialEq + Clone;

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> Self::Path;
fn len(&self) -> usize;
Expand Down
66 changes: 55 additions & 11 deletions src/path/improve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,65 @@ pub fn inner_rotate<C: ImproveContext>(ctx: C) -> C::Path {
ctx.path_from_indices(path.iter())
}

pub fn two_opt_and_inner_rotate<C: ImproveContext>(mut ctx: C) -> C::Path {
let mut path = ctx.path_from_indices(ctx.start_path().iter());
let mut prev_path = None;

while prev_path.as_ref() != Some(&path) {
prev_path = Some(path.clone());
pub fn two_opt_and_inner_rotate<C: ImproveContext>(ctx: C) -> C::Path {
fn two_opt_swap(path: &mut graph::Path, v1: usize, v2: usize) {
let path = path.as_mut();
path[v1 + 1..v2].reverse();
}

ctx = ctx.with_start_path(path);
path = two_opt(ctx.clone());
let mut improvement = true;
let mut path = ctx.start_path();
let mut best_cost = ctx.cost(&path);

ctx = ctx.with_start_path(path);
path = inner_rotate(ctx.clone());
'improvin: while improvement {
improvement = false;
for i in 0..path.len() - 1 {
for j in i + 1..path.len() {
two_opt_swap(&mut path, i, j);
let new_cost = ctx.cost(&path);
if new_cost < best_cost {
ctx.send_path(
path.iter(),
Some((i * path.len() + j) as f32 / ((path.len()) * path.len()) as f32),
);
}
if new_cost < best_cost {
if !ctx.prefer_step() {
improvement = true;
}
best_cost = new_cost;
continue 'improvin;
}
two_opt_swap(&mut path, i, j);
}
}
for start in 0..path.len() {
ctx.send_path_for_reactivity(path.iter(), Some(start as f32 / path.len() as f32));
for end in start + 1..path.len() {
for amount in 1..end - start {
path.as_mut()[start..end].rotate_left(amount);
let new_cost = ctx.cost(&path);
if new_cost < best_cost {
ctx.send_path(
path.iter(),
Some(
(start * path.len() + end) as f32
/ ((path.len() * path.len()) as f32),
),
);
best_cost = new_cost;
if !ctx.prefer_step() {
improvement = true;
}
continue 'improvin;
}
path.as_mut()[start..end].rotate_right(amount);
}
}
}
}

path
ctx.path_from_indices(path.iter())
}

pub fn simulated_annealing<C: ImproveContext>(ctx: C) -> C::Path {
Expand Down

0 comments on commit d5208d4

Please sign in to comment.