small refinements
This commit is contained in:
parent
8759a58cb9
commit
d91e829bff
37
src/main.rs
37
src/main.rs
|
@ -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,7 +69,6 @@ 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;
|
||||||
|
@ -81,7 +80,9 @@ fn main() -> std::io::Result<()> {
|
||||||
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 {
|
||||||
|
@ -111,28 +112,26 @@ fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
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;
|
||||||
|
@ -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(())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
14
src/utils.rs
14
src/utils.rs
|
@ -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)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue