This commit is contained in:
Niko Abeler 2022-11-18 20:57:46 +01:00
parent 633a9261ae
commit e83d63eb85
2 changed files with 20 additions and 22 deletions

View File

@ -63,17 +63,17 @@ fn read_graph(file_name: &str) -> (usize, EdgeMatrix) {
}
fn main() -> std::io::Result<()> {
const ITER: usize = 200;
const ITER: usize = 50;
const THREADS: usize = 8;
// let edges = connection_matrix(size);
let (size, edges): (usize, EdgeMatrix) = read_graph("../debug_graph.bin");
let (size, edges): (usize, EdgeMatrix) = read_graph("../graph.bin");
println!("Size: {}", size);
let nodes = nodes_list(size);
let nodes_next = nodes_list(size);
let mut nodes = nodes_list(size);
let mut nodes_next = nodes_list(size);
// let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, size)));
let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, size)));
let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, size, ITER)));
let chunks = utils::gen_chunks(size, THREADS);
for epoch in 0..ITER {
@ -98,14 +98,12 @@ fn main() -> std::io::Result<()> {
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();
node.x = node_next.x;
node.y = node_next.y;
}
// swap nodes and nodes_next
let tmp = nodes.clone();
nodes = nodes_next.clone();
nodes_next = tmp.clone();
let mut file = File::create(format!("result/{:04}.txt", epoch))?;
for i in 0..size {
let node = nodes[i].read().unwrap();

View File

@ -1,23 +1,23 @@
use crate::graph::{EdgeMatrix, NodeVector, Node};
const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
pub struct MyModel {
edges: EdgeMatrix,
size: usize,
opt_dist: f32,
c: f32,
dc: f32,
}
impl MyModel {
pub fn new(edges: EdgeMatrix, size: usize) -> MyModel {
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel {
let opt_dist = 1.0 / (size as f32).sqrt();
MyModel{ edges, size, opt_dist }
let c = 0.1;
MyModel{ edges, size, opt_dist, c: c , dc: c / ((iterations + 1) as f32) }
}
pub fn prepare(& mut self, nodes: &NodeVector) {
pub fn prepare(& mut self, _nodes: &NodeVector) {
self.c -= self.dc;
}
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
@ -48,14 +48,14 @@ impl MyModel {
let edge = edges[i_node][o].weight;
if edge == 0.0 {
let f_rep = (C_REP / (dist).powi(2)).min(C_REP);
let f_rep = (self.c / (dist).powi(2)).min(self.c);
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 * 0.5 * (dist - self.opt_dist);
let f_spring = self.c * 0.5 * (dist - self.opt_dist);
let f_spring_x = f_spring * unit_x;
let f_spring_y = f_spring * unit_y;
node_x += f_spring_x;