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<()> { fn main() -> std::io::Result<()> {
const ITER: usize = 200; const ITER: usize = 50;
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("../debug_graph.bin"); let (size, edges): (usize, EdgeMatrix) = read_graph("../graph.bin");
println!("Size: {}", size); println!("Size: {}", size);
let nodes = nodes_list(size); let mut nodes = nodes_list(size);
let nodes_next = 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(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); let chunks = utils::gen_chunks(size, THREADS);
for epoch in 0..ITER { for epoch in 0..ITER {
@ -99,12 +99,10 @@ fn main() -> std::io::Result<()> {
handle.join().unwrap(); handle.join().unwrap();
} }
for i in 0..size { // swap nodes and nodes_next
let mut node = nodes[i].write().unwrap(); let tmp = nodes.clone();
let node_next = nodes_next[i].read().unwrap(); nodes = nodes_next.clone();
node.x = node_next.x; nodes_next = tmp.clone();
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 {

View File

@ -1,23 +1,23 @@
use crate::graph::{EdgeMatrix, NodeVector, Node}; use crate::graph::{EdgeMatrix, NodeVector, Node};
const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
pub struct MyModel { pub struct MyModel {
edges: EdgeMatrix, edges: EdgeMatrix,
size: usize, size: usize,
opt_dist: f32, opt_dist: f32,
c: f32,
dc: f32,
} }
impl MyModel { 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(); 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 { pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
@ -48,14 +48,14 @@ impl MyModel {
let edge = edges[i_node][o].weight; let edge = edges[i_node][o].weight;
if edge == 0.0 { 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_x = f_rep * unit_x;
let f_rep_y = f_rep * unit_y; let f_rep_y = f_rep * unit_y;
node_x -= f_rep_x; node_x -= f_rep_x;
node_y -= f_rep_y; node_y -= f_rep_y;
} else { } 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_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;