diff --git a/Cargo.toml b/Cargo.toml index 22fe193..f2e0b6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "lab3", "lab4", "lab5", + "tsp-utility" ] diff --git a/lab3/Cargo.toml b/lab3/Cargo.toml index 35b983c..1811de1 100644 --- a/lab3/Cargo.toml +++ b/lab3/Cargo.toml @@ -9,6 +9,7 @@ workspace = true rand = "0.9.2" eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] } egui_extras = { version = "0.33.3" } +tsp-utility = { path = "../tsp-utility" } [profile.dev.package.eframe] opt-level = 2 diff --git a/lab3/src/algo/mod.rs b/lab3/src/algo/mod.rs index c168195..ca75a6a 100644 --- a/lab3/src/algo/mod.rs +++ b/lab3/src/algo/mod.rs @@ -1,12 +1,14 @@ -mod edges; mod state; -mod vertices; -mod _preserve_index_vec; -pub use edges::EdgesVec; -pub use vertices::VerticesVec; -use _preserve_index_vec::_PreserveIndexVec; +pub use tsp_utility::graph::{VerticesVec}; +use tsp_utility::graph::{Edge as _BaseEdge, EdgesVec as _BaseEdgesVec}; +pub struct EdgeExtraData { + pub(crate) ferment_intensity: f64 +} + +pub type Edge = _BaseEdge; +pub type EdgesVec = _BaseEdgesVec; pub struct AntsSimulationConfig { pub ferment_weight: f64, diff --git a/lab3/src/algo/state.rs b/lab3/src/algo/state.rs index 9eb6a67..0139271 100644 --- a/lab3/src/algo/state.rs +++ b/lab3/src/algo/state.rs @@ -1,4 +1,4 @@ -use crate::algo::EdgesVec; +use super::EdgesVec; use std::collections::HashSet; use std::ops::Mul; @@ -50,7 +50,7 @@ pub fn updateState( Some(newOffset) => *offset = newOffset, None => { let edge = &mut state.edges[*edge_index]; - edge.ferment_intensity += (q / edge.length) * r; + edge.extra.ferment_intensity += (q / edge.length) * r; let vertex_index; match direction { AntDirection::ToSecond => vertex_index = edge.vertex2_index, @@ -75,7 +75,7 @@ pub fn updateState( return ( e.0, (e.2, e.3), - e.1.ferment_intensity.powf(ferment_weight) + e.1.extra.ferment_intensity.powf(ferment_weight) * (1.0 / e.1.length).powf(heuristic_coefficient), ); }) @@ -97,7 +97,7 @@ pub fn updateState( ant.location = AntLocation::OnEdge { edge_index: *ei, offset: 0, - direction: *dir + direction: *dir, } } }; @@ -105,5 +105,5 @@ pub fn updateState( } } - return finished_ants_count == state.ants.len() + return finished_ants_count == state.ants.len(); } diff --git a/lab3/src/main.rs b/lab3/src/main.rs index c15b77e..8706dad 100644 --- a/lab3/src/main.rs +++ b/lab3/src/main.rs @@ -2,7 +2,7 @@ mod algo; -use crate::algo::{AntsSimulationConfig, EdgesVec, VerticesVec}; +use crate::algo::{AntsSimulationConfig, EdgeExtraData, EdgesVec, VerticesVec}; use eframe::egui; use eframe::egui::{Frame, ScrollArea, Ui}; use eframe::emath::Numeric; @@ -98,7 +98,14 @@ impl eframe::App for MyApp { if (vi == new_vi) { continue; } - let ei = self.edges.add(new_vi, vi, 1.0); + let ei = self.edges.add( + new_vi, + vi, + 1.0, + EdgeExtraData { + ferment_intensity: 0.0, + }, + ); newEdgesSet.insert(ei); v.insert(ei); } @@ -246,8 +253,6 @@ fn visualization_panel(data: &mut MyApp, ui: &mut Ui) { ui.label(""); _slider(ui, "Ant speed", &mut data.speed, 1..=10, 1.0); }); - } -fn draw_ants(data: &mut MyApp, ui: &mut Ui) { -} +fn draw_ants(data: &mut MyApp, ui: &mut Ui) {} diff --git a/tsp-utility/Cargo.toml b/tsp-utility/Cargo.toml new file mode 100644 index 0000000..bc38a8d --- /dev/null +++ b/tsp-utility/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "tsp-utility" +edition = "2024" + +[lints] +workspace = true + +[dependencies] +rand = "0.9.2" +eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] } +egui_extras = { version = "0.33.3" } + +[profile.dev.package.eframe] +opt-level = 2 +debug = true \ No newline at end of file diff --git a/lab3/src/algo/_preserve_index_vec.rs b/tsp-utility/src/graph/_preserve_index_vec.rs similarity index 98% rename from lab3/src/algo/_preserve_index_vec.rs rename to tsp-utility/src/graph/_preserve_index_vec.rs index b17401b..99f2098 100644 --- a/lab3/src/algo/_preserve_index_vec.rs +++ b/tsp-utility/src/graph/_preserve_index_vec.rs @@ -5,7 +5,7 @@ enum Cell { Value { value: T }, } -pub(super) struct _PreserveIndexVec { +pub struct _PreserveIndexVec { next_free: Option, buffer: Vec>, len: usize, diff --git a/lab3/src/algo/edges.rs b/tsp-utility/src/graph/edges.rs similarity index 69% rename from lab3/src/algo/edges.rs rename to tsp-utility/src/graph/edges.rs index 108b606..e71c255 100644 --- a/lab3/src/algo/edges.rs +++ b/tsp-utility/src/graph/edges.rs @@ -1,14 +1,14 @@ use super::_PreserveIndexVec; use std::ops::{Index, IndexMut}; -pub struct Edge { +pub struct Edge { pub vertex1_index: usize, pub vertex2_index: usize, pub length: f64, - pub ferment_intensity: f64, + pub extra: D, } -impl Edge { +impl Edge { pub fn another(&self, current: usize) -> usize { if self.vertex1_index == current { return self.vertex2_index; @@ -20,23 +20,23 @@ impl Edge { } } -pub struct EdgesVec { - data: _PreserveIndexVec, +pub struct EdgesVec { + data: _PreserveIndexVec>, } -impl EdgesVec { +impl EdgesVec { pub fn new() -> Self { return Self { data: _PreserveIndexVec::new(), }; } - pub fn add(&mut self, vertex1: usize, vertex2: usize, length: f64) -> usize { + pub fn add(&mut self, vertex1: usize, vertex2: usize, length: f64, extra: D) -> usize { let data = Edge { vertex1_index: vertex1, vertex2_index: vertex2, length, - ferment_intensity: 0.0, + extra }; return self.data.add(data); } @@ -44,24 +44,24 @@ impl EdgesVec { pub fn remove(&mut self, edge_index: usize) { self.data.remove(edge_index) } - pub fn iter(&self) -> impl Iterator{ - return self.data.iter() + pub fn iter(&self) -> impl Iterator> { + return self.data.iter(); } - - pub fn iter_mut(&mut self) -> impl Iterator{ - return self.data.iter_mut() + + pub fn iter_mut(&mut self) -> impl Iterator> { + return self.data.iter_mut(); } } -impl Index for EdgesVec { - type Output = Edge; +impl Index for EdgesVec { + type Output = Edge; fn index(&self, index: usize) -> &Self::Output { return self.data.index(index); } } -impl IndexMut for EdgesVec { +impl IndexMut for EdgesVec { fn index_mut(&mut self, index: usize) -> &mut Self::Output { return self.data.index_mut(index); } diff --git a/tsp-utility/src/graph/mod.rs b/tsp-utility/src/graph/mod.rs new file mode 100644 index 0000000..b42a450 --- /dev/null +++ b/tsp-utility/src/graph/mod.rs @@ -0,0 +1,7 @@ +mod _preserve_index_vec; +pub mod edges; +pub mod vertices; + +use _preserve_index_vec::_PreserveIndexVec; +pub use edges::{Edge, EdgesVec}; +pub use vertices::VerticesVec; diff --git a/lab3/src/algo/vertices.rs b/tsp-utility/src/graph/vertices.rs similarity index 100% rename from lab3/src/algo/vertices.rs rename to tsp-utility/src/graph/vertices.rs diff --git a/tsp-utility/src/lib.rs b/tsp-utility/src/lib.rs new file mode 100644 index 0000000..5ebfe64 --- /dev/null +++ b/tsp-utility/src/lib.rs @@ -0,0 +1 @@ +pub mod graph; \ No newline at end of file