own model

This commit is contained in:
Niko Abeler 2022-11-18 19:47:20 +01:00
parent 22a46b5c68
commit 633a9261ae
2 changed files with 73 additions and 1 deletions

View File

@ -1,5 +1,6 @@
mod utils;
mod spring_model;
mod my_model;
mod graph;
use rand::Rng;
@ -71,7 +72,8 @@ fn main() -> std::io::Result<()> {
let nodes = nodes_list(size);
let 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 chunks = utils::gen_chunks(size, THREADS);
for epoch in 0..ITER {

70
src/my_model.rs Normal file
View File

@ -0,0 +1,70 @@
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,
}
impl MyModel {
pub fn new(edges: EdgeMatrix, size: usize) -> MyModel {
let opt_dist = 1.0 / (size as f32).sqrt();
MyModel{ edges, size, opt_dist }
}
pub fn prepare(& mut self, nodes: &NodeVector) {
}
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
let node = nodes[i_node].read().unwrap();
let edges = self.edges.read().unwrap();
let mut node_x = node.x;
let mut node_y = node.y;
for o in 0..self.size {
if o == i_node {
continue;
}
let o_x: f32;
let o_y: f32;
{
let other = nodes[o].read().unwrap();
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();
let unit_x = d_x / dist;
let unit_y = d_y / dist;
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_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_x = f_spring * unit_x;
let f_spring_y = f_spring * unit_y;
node_x += f_spring_x;
node_y += f_spring_y;
}
}
Node {
x: node_x,
y: node_y,
}
}
}