refactoring to allow different models

This commit is contained in:
Niko Abeler 2022-11-20 20:28:23 +01:00
parent 2d3572e63b
commit c3cc7f690e
4 changed files with 43 additions and 28 deletions

View File

@ -1,8 +1,11 @@
mod graph; mod graph;
mod model;
mod runner; mod runner;
mod spring_model; mod spring_model;
mod utils; mod utils;
use std::sync::{Arc, RwLock};
use pyo3::prelude::*; use pyo3::prelude::*;
/// Formats the sum of two numbers as string. /// Formats the sum of two numbers as string.
@ -13,8 +16,9 @@ fn layout_from_edge_list(
iter: usize, iter: usize,
threads: usize, threads: usize,
) -> PyResult<Vec<(f32, f32)>> { ) -> PyResult<Vec<(f32, f32)>> {
let model = Arc::new(RwLock::new(spring_model::SimpleSpringModel::new(1.0)));
let r = runner::Runner::new(iter, threads); let r = runner::Runner::new(iter, threads);
Ok(r.layout(number_of_nodes, edges)) Ok(r.layout(number_of_nodes, edges, model))
} }
/// A Python module implemented in Rust. /// A Python module implemented in Rust.

7
src/model.rs Normal file
View File

@ -0,0 +1,7 @@
use crate::graph::{EdgeMatrix, Node, NodeVector};
pub trait ForceModel {
fn init(&mut self, edges: EdgeMatrix, size: usize, iterations: usize);
fn prepare(&mut self, _nodes: &NodeVector);
fn step(&self, nodes: &NodeVector, i_node: usize) -> Node;
}

View File

@ -1,5 +1,5 @@
use crate::graph::{new_edge_matrix, new_node_vector, EdgeMatrix}; use crate::graph::{new_edge_matrix, new_node_vector, EdgeMatrix};
use crate::spring_model; use crate::model::ForceModel;
use crate::utils; use crate::utils;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::thread; use std::thread;
@ -30,22 +30,21 @@ impl Runner {
} }
} }
pub fn layout( pub fn layout<T: 'static + ForceModel + Sync + Send>(
self: &Self, self: &Self,
number_of_nodes: usize, number_of_nodes: usize,
edge_list: Vec<(u32, u32)>, edge_list: Vec<(u32, u32)>,
model: Arc<RwLock<T>>,
) -> Vec<(f32, f32)> { ) -> Vec<(f32, f32)> {
// let edges = connection_matrix(size); // let edges = connection_matrix(size);
let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list); let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list);
let mut nodes = new_node_vector(number_of_nodes); let mut nodes = new_node_vector(number_of_nodes);
let mut nodes_next = new_node_vector(number_of_nodes); let mut nodes_next = new_node_vector(number_of_nodes);
// let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, number_of_nodes))); model
let model = Arc::new(RwLock::new(spring_model::MyModel::new( .write()
edges, .unwrap()
number_of_nodes, .init(edges, number_of_nodes, self.iterations);
self.iterations,
)));
let chunks = utils::gen_chunks(number_of_nodes, self.threads); let chunks = utils::gen_chunks(number_of_nodes, self.threads);
for _epoch in 0..self.iterations { for _epoch in 0..self.iterations {

View File

@ -1,32 +1,37 @@
use crate::graph::{EdgeMatrix, Node, NodeVector}; use crate::graph::{new_edge_matrix, EdgeMatrix, Node, NodeVector};
use crate::model::ForceModel;
pub struct MyModel { pub struct SimpleSpringModel {
edges: EdgeMatrix, edges: EdgeMatrix,
size: usize, size: usize,
opt_dist: f32,
c: f32, c: f32,
dc: f32, dc: f32,
} }
impl MyModel { impl SimpleSpringModel {
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel { pub fn new(c: f32) -> Self {
let opt_dist = 1.0; SimpleSpringModel {
let c = 0.1; edges: new_edge_matrix(0),
size: 0,
MyModel { c,
edges, dc: 0.0,
size,
opt_dist,
c: c,
dc: c / ((iterations + 1) as f32),
} }
} }
}
pub fn prepare(&mut self, _nodes: &NodeVector) { impl ForceModel for SimpleSpringModel {
fn init(&mut self, edges: EdgeMatrix, size: usize, iterations: usize) {
let dc = self.c / ((iterations + 1) as f32);
self.edges = edges;
self.size = size;
self.dc = dc;
}
fn prepare(&mut self, _nodes: &NodeVector) {
self.c -= self.dc; self.c -= self.dc;
} }
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node { fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
let node = nodes[i_node].read().unwrap(); let node = nodes[i_node].read().unwrap();
let edges = self.edges.read().unwrap(); let edges = self.edges.read().unwrap();
@ -57,14 +62,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 = dist.powi(2).recip().min(self.opt_dist); let f_rep = dist.powi(2).recip().min(1.0);
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;
sum_x -= f_rep_x; sum_x -= f_rep_x;
sum_y -= f_rep_y; sum_y -= f_rep_y;
} else { } else {
let f_spring = 0.5 * (dist - self.opt_dist); let f_spring = 0.5 * (dist - 1.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;
sum_x += f_spring_x; sum_x += f_spring_x;