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

View File

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