first implementation
This commit is contained in:
parent
f3c9abe5b5
commit
a0dda954ad
59
src/main.rs
59
src/main.rs
|
@ -1,6 +1,8 @@
|
|||
use std::thread;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use rand::Rng;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
|
||||
struct Node {
|
||||
x: f32,
|
||||
|
@ -38,9 +40,13 @@ fn connection_matrix(size: usize) -> Arc<RwLock<Vec<Vec<Edge>>>> {
|
|||
Arc::new(RwLock::new(matrix))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
let size = 24000;
|
||||
const C_REP: f32 = 0.1;
|
||||
const C_SPRING: f32 = 0.1;
|
||||
const ITER: usize = 200;
|
||||
|
||||
let size = 5000;
|
||||
let threads = 8;
|
||||
|
||||
let nodes = nodes_list(size);
|
||||
|
@ -48,8 +54,8 @@ fn main() {
|
|||
|
||||
let edges = connection_matrix(size);
|
||||
|
||||
for epoch in 0..ITER {
|
||||
let mut handles = vec![];
|
||||
|
||||
let chunks = size / threads;
|
||||
for i in 0..threads {
|
||||
let nodes = nodes.clone();
|
||||
|
@ -65,6 +71,9 @@ fn main() {
|
|||
let mut node_y = node.y;
|
||||
|
||||
for o in 0..size {
|
||||
if o == n {
|
||||
continue;
|
||||
}
|
||||
let o_x: f32;
|
||||
let o_y: f32;
|
||||
{
|
||||
|
@ -72,9 +81,31 @@ fn main() {
|
|||
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 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;
|
||||
node_x += (o_x - node.x) * edge;
|
||||
node_y += (o_y - node.y) * edge;
|
||||
if edge > 0.0 {
|
||||
let f_spring = C_SPRING * (dist / edge).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;
|
||||
|
@ -88,4 +119,22 @@ fn main() {
|
|||
handle.join().unwrap();
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
let mut node = nodes[i].write().unwrap();
|
||||
let node_next = nodes_next[i].read().unwrap();
|
||||
node.x = node_next.x;
|
||||
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();
|
||||
// println!("{} {}", node.x, node.y);
|
||||
file.write_all(format!("{} {}\n", node.x, node.y).as_bytes())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue