small refinements

This commit is contained in:
Niko Abeler 2022-11-17 20:28:32 +01:00
parent 8759a58cb9
commit d91e829bff
2 changed files with 31 additions and 34 deletions

View File

@ -1,8 +1,8 @@
use std::thread;
use std::sync::{Arc, RwLock};
use rand::Rng; use rand::Rng;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::sync::{Arc, RwLock};
use std::thread;
mod utils; mod utils;
@ -34,7 +34,7 @@ fn connection_matrix(size: usize) -> EdgeMatrix {
for _ in 0..size { for _ in 0..size {
let mut row = Vec::with_capacity(size); let mut row = Vec::with_capacity(size);
for _ in 0..size { for _ in 0..size {
row.push(Edge { weight: 0.0}); row.push(Edge { weight: 0.0 });
} }
matrix.push(row); matrix.push(row);
} }
@ -69,19 +69,20 @@ fn read_graph(file_name: &str) -> (usize, EdgeMatrix) {
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
const C_REP: f32 = 0.1; const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1; const C_SPRING: f32 = 0.1;
const ITER: usize = 200; const ITER: usize = 200;
const THREADS: usize = 8; const THREADS: usize = 8;
// let edges = connection_matrix(size); // let edges = connection_matrix(size);
let (size, edges): (usize, EdgeMatrix) = read_graph("../graph.bin"); let (size, edges): (usize, EdgeMatrix) = read_graph("../graph.bin");
println!("Size: {}", size); println!("Size: {}", size);
let nodes = nodes_list(size); let nodes = nodes_list(size);
let nodes_next = nodes_list(size); let nodes_next = nodes_list(size);
let chunks = utils::chunk_borders(size, THREADS); let opt_dist = 10.0 / (size as f32).sqrt();
let chunks = utils::gen_chunks(size, THREADS);
for epoch in 0..ITER { for epoch in 0..ITER {
let mut handles = vec![]; let mut handles = vec![];
for i in 0..THREADS { for i in 0..THREADS {
@ -93,10 +94,10 @@ fn main() -> std::io::Result<()> {
for n in chunk { for n in chunk {
let node = nodes[n].read().unwrap(); let node = nodes[n].read().unwrap();
let edges = edges.read().unwrap(); let edges = edges.read().unwrap();
let mut node_x = node.x; let mut node_x = node.x;
let mut node_y = node.y; let mut node_y = node.y;
for o in 0..size { for o in 0..size {
if o == n { if o == n {
continue; continue;
@ -108,31 +109,29 @@ fn main() -> std::io::Result<()> {
o_x = other.x; o_x = other.x;
o_y = other.y; o_y = other.y;
} }
let d_x = o_x - node_x; let d_x = o_x - node_x;
let d_y = o_y - node_y; let d_y = o_y - node_y;
let dist = (d_x * d_x + d_y * d_y).sqrt().max(0.01); let dist = (d_x * d_x + d_y * d_y).sqrt().max(0.0001);
let unit_x = d_x / dist; let unit_x = d_x / dist;
let unit_y = d_y / dist; let unit_y = d_y / dist;
let f_rep = C_REP/(dist).powi(2);
let f_rep_x = f_rep * unit_x;
let f_rep_y = f_rep * unit_y;
node_x += f_rep_x;
node_y += f_rep_y;
let edge = edges[n][o].weight; let edge = edges[n][o].weight;
if edge > 0.0 {
let f_spring = C_SPRING * (dist / edge).log(2.0); if edge == 0.0 {
let f_rep = -C_REP / (dist).powi(2);
let f_rep_x = f_rep * unit_x;
let f_rep_y = f_rep * unit_y;
node_x += f_rep_x;
node_y += f_rep_y;
} else {
let f_spring = C_SPRING * (dist / opt_dist).log(2.0);
let f_spring_x = f_spring * unit_x; let f_spring_x = f_spring * unit_x;
let f_spring_y = f_spring * unit_y; let f_spring_y = f_spring * unit_y;
node_x += f_spring_x; node_x += f_spring_x;
node_y += f_spring_y; node_y += f_spring_y;
} }
} }
let mut result = nodes_next[n].write().unwrap(); let mut result = nodes_next[n].write().unwrap();
result.x = node_x; result.x = node_x;
@ -141,11 +140,11 @@ fn main() -> std::io::Result<()> {
}); });
handles.push(handle); handles.push(handle);
} }
for handle in handles { for handle in handles {
handle.join().unwrap(); handle.join().unwrap();
} }
for i in 0..size { for i in 0..size {
let mut node = nodes[i].write().unwrap(); let mut node = nodes[i].write().unwrap();
let node_next = nodes_next[i].read().unwrap(); let node_next = nodes_next[i].read().unwrap();
@ -153,7 +152,6 @@ fn main() -> std::io::Result<()> {
node.y = node_next.y; node.y = node_next.y;
} }
let mut file = File::create(format!("result/{:04}.txt", epoch))?; let mut file = File::create(format!("result/{:04}.txt", epoch))?;
for i in 0..size { for i in 0..size {
let node = nodes[i].read().unwrap(); let node = nodes[i].read().unwrap();
@ -163,5 +161,4 @@ fn main() -> std::io::Result<()> {
} }
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
use std::ops::Range; use std::ops::Range;
pub fn chunk_borders(n: usize, chunks: usize) -> Vec<Range<usize>> { pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
let mut borders = vec![]; let mut borders = vec![];
let chunk_size = n / chunks; let chunk_size = n / chunks;
let mut start = 0; let mut start = 0;
@ -20,20 +20,20 @@ mod test {
use super::*; use super::*;
#[test] #[test]
fn test_chunk_borders() { fn test_gen_chunks() {
let borders = chunk_borders(10, 3); let borders = gen_chunks(10, 3);
assert_eq!(borders, vec![(0..3), (3..6), (6..10)]); assert_eq!(borders, vec![(0..3), (3..6), (6..10)]);
} }
#[test] #[test]
fn test_chunk_borders2() { fn test_gen_chunks2() {
let borders = chunk_borders(10, 2); let borders = gen_chunks(10, 2);
assert_eq!(borders, vec![(0..5), (5..10)]); assert_eq!(borders, vec![(0..5), (5..10)]);
} }
#[test] #[test]
fn test_chunk_borders3() { fn test_gen_chunks3() {
let borders = chunk_borders(10, 1); let borders = gen_chunks(10, 1);
assert_eq!(borders, vec![(0..10)]); assert_eq!(borders, vec![(0..10)]);
} }