[lab3] Extracted edges and vertices vectors to separate module
This commit is contained in:
parent
177c11e67c
commit
da01fc5bcb
@ -5,6 +5,7 @@ members = [
|
|||||||
"lab3",
|
"lab3",
|
||||||
"lab4",
|
"lab4",
|
||||||
"lab5",
|
"lab5",
|
||||||
|
"tsp-utility"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ workspace = true
|
|||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] }
|
eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] }
|
||||||
egui_extras = { version = "0.33.3" }
|
egui_extras = { version = "0.33.3" }
|
||||||
|
tsp-utility = { path = "../tsp-utility" }
|
||||||
|
|
||||||
[profile.dev.package.eframe]
|
[profile.dev.package.eframe]
|
||||||
opt-level = 2
|
opt-level = 2
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
mod edges;
|
|
||||||
mod state;
|
mod state;
|
||||||
mod vertices;
|
|
||||||
mod _preserve_index_vec;
|
|
||||||
|
|
||||||
pub use edges::EdgesVec;
|
pub use tsp_utility::graph::{VerticesVec};
|
||||||
pub use vertices::VerticesVec;
|
use tsp_utility::graph::{Edge as _BaseEdge, EdgesVec as _BaseEdgesVec};
|
||||||
use _preserve_index_vec::_PreserveIndexVec;
|
|
||||||
|
|
||||||
|
pub struct EdgeExtraData {
|
||||||
|
pub(crate) ferment_intensity: f64
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Edge = _BaseEdge<EdgeExtraData>;
|
||||||
|
pub type EdgesVec = _BaseEdgesVec<EdgeExtraData>;
|
||||||
|
|
||||||
pub struct AntsSimulationConfig {
|
pub struct AntsSimulationConfig {
|
||||||
pub ferment_weight: f64,
|
pub ferment_weight: f64,
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::algo::EdgesVec;
|
use super::EdgesVec;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ pub fn updateState<Rng: rand::Rng>(
|
|||||||
Some(newOffset) => *offset = newOffset,
|
Some(newOffset) => *offset = newOffset,
|
||||||
None => {
|
None => {
|
||||||
let edge = &mut state.edges[*edge_index];
|
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;
|
let vertex_index;
|
||||||
match direction {
|
match direction {
|
||||||
AntDirection::ToSecond => vertex_index = edge.vertex2_index,
|
AntDirection::ToSecond => vertex_index = edge.vertex2_index,
|
||||||
@ -75,7 +75,7 @@ pub fn updateState<Rng: rand::Rng>(
|
|||||||
return (
|
return (
|
||||||
e.0,
|
e.0,
|
||||||
(e.2, e.3),
|
(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),
|
* (1.0 / e.1.length).powf(heuristic_coefficient),
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
@ -97,7 +97,7 @@ pub fn updateState<Rng: rand::Rng>(
|
|||||||
ant.location = AntLocation::OnEdge {
|
ant.location = AntLocation::OnEdge {
|
||||||
edge_index: *ei,
|
edge_index: *ei,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
direction: *dir
|
direction: *dir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -105,5 +105,5 @@ pub fn updateState<Rng: rand::Rng>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return finished_ants_count == state.ants.len()
|
return finished_ants_count == state.ants.len();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
mod algo;
|
mod algo;
|
||||||
|
|
||||||
use crate::algo::{AntsSimulationConfig, EdgesVec, VerticesVec};
|
use crate::algo::{AntsSimulationConfig, EdgeExtraData, EdgesVec, VerticesVec};
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use eframe::egui::{Frame, ScrollArea, Ui};
|
use eframe::egui::{Frame, ScrollArea, Ui};
|
||||||
use eframe::emath::Numeric;
|
use eframe::emath::Numeric;
|
||||||
@ -98,7 +98,14 @@ impl eframe::App for MyApp {
|
|||||||
if (vi == new_vi) {
|
if (vi == new_vi) {
|
||||||
continue;
|
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);
|
newEdgesSet.insert(ei);
|
||||||
v.insert(ei);
|
v.insert(ei);
|
||||||
}
|
}
|
||||||
@ -246,8 +253,6 @@ fn visualization_panel(data: &mut MyApp, ui: &mut Ui) {
|
|||||||
ui.label("");
|
ui.label("");
|
||||||
_slider(ui, "Ant speed", &mut data.speed, 1..=10, 1.0);
|
_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) {}
|
||||||
}
|
|
||||||
|
|||||||
15
tsp-utility/Cargo.toml
Normal file
15
tsp-utility/Cargo.toml
Normal file
@ -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
|
||||||
@ -5,7 +5,7 @@ enum Cell<T> {
|
|||||||
Value { value: T },
|
Value { value: T },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) struct _PreserveIndexVec<T> {
|
pub struct _PreserveIndexVec<T> {
|
||||||
next_free: Option<usize>,
|
next_free: Option<usize>,
|
||||||
buffer: Vec<Cell<T>>,
|
buffer: Vec<Cell<T>>,
|
||||||
len: usize,
|
len: usize,
|
||||||
@ -1,14 +1,14 @@
|
|||||||
use super::_PreserveIndexVec;
|
use super::_PreserveIndexVec;
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
pub struct Edge {
|
pub struct Edge<D> {
|
||||||
pub vertex1_index: usize,
|
pub vertex1_index: usize,
|
||||||
pub vertex2_index: usize,
|
pub vertex2_index: usize,
|
||||||
pub length: f64,
|
pub length: f64,
|
||||||
pub ferment_intensity: f64,
|
pub extra: D,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Edge {
|
impl<D> Edge<D> {
|
||||||
pub fn another(&self, current: usize) -> usize {
|
pub fn another(&self, current: usize) -> usize {
|
||||||
if self.vertex1_index == current {
|
if self.vertex1_index == current {
|
||||||
return self.vertex2_index;
|
return self.vertex2_index;
|
||||||
@ -20,23 +20,23 @@ impl Edge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EdgesVec {
|
pub struct EdgesVec<D> {
|
||||||
data: _PreserveIndexVec<Edge>,
|
data: _PreserveIndexVec<Edge<D>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EdgesVec {
|
impl<D> EdgesVec<D> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
return Self {
|
return Self {
|
||||||
data: _PreserveIndexVec::new(),
|
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 {
|
let data = Edge {
|
||||||
vertex1_index: vertex1,
|
vertex1_index: vertex1,
|
||||||
vertex2_index: vertex2,
|
vertex2_index: vertex2,
|
||||||
length,
|
length,
|
||||||
ferment_intensity: 0.0,
|
extra
|
||||||
};
|
};
|
||||||
return self.data.add(data);
|
return self.data.add(data);
|
||||||
}
|
}
|
||||||
@ -44,24 +44,24 @@ impl EdgesVec {
|
|||||||
pub fn remove(&mut self, edge_index: usize) {
|
pub fn remove(&mut self, edge_index: usize) {
|
||||||
self.data.remove(edge_index)
|
self.data.remove(edge_index)
|
||||||
}
|
}
|
||||||
pub fn iter(&self) -> impl Iterator<Item=&Edge>{
|
pub fn iter(&self) -> impl Iterator<Item = &Edge<D>> {
|
||||||
return self.data.iter()
|
return self.data.iter();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_mut(&mut self) -> impl Iterator<Item=&mut Edge>{
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Edge<D>> {
|
||||||
return self.data.iter_mut()
|
return self.data.iter_mut();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index<usize> for EdgesVec {
|
impl <D> Index<usize> for EdgesVec<D> {
|
||||||
type Output = Edge;
|
type Output = Edge<D>;
|
||||||
|
|
||||||
fn index(&self, index: usize) -> &Self::Output {
|
fn index(&self, index: usize) -> &Self::Output {
|
||||||
return self.data.index(index);
|
return self.data.index(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexMut<usize> for EdgesVec {
|
impl <D> IndexMut<usize> for EdgesVec<D> {
|
||||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||||
return self.data.index_mut(index);
|
return self.data.index_mut(index);
|
||||||
}
|
}
|
||||||
7
tsp-utility/src/graph/mod.rs
Normal file
7
tsp-utility/src/graph/mod.rs
Normal file
@ -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;
|
||||||
1
tsp-utility/src/lib.rs
Normal file
1
tsp-utility/src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod graph;
|
||||||
Loading…
Reference in New Issue
Block a user