Compare commits

...

5 Commits

Author SHA1 Message Date
Niko Abeler a2244ed58c rank dampening 2022-11-18 21:09:35 +01:00
Niko Abeler e83d63eb85 cooling 2022-11-18 20:57:46 +01:00
Niko Abeler 633a9261ae own model 2022-11-18 19:47:36 +01:00
Niko Abeler 22a46b5c68 move model into own struct 2022-11-18 19:32:13 +01:00
Niko Abeler d91e829bff small refinements 2022-11-17 20:28:32 +01:00
5 changed files with 212 additions and 89 deletions

12
src/graph.rs Normal file
View File

@ -0,0 +1,12 @@
use std::sync::{Arc, RwLock};
pub struct Node {
pub x: f32,
pub y: f32,
}
pub struct Edge {
pub weight: f32,
}
pub type EdgeMatrix = Arc<RwLock<Vec<Vec<Edge>>>>;
pub type NodeVector = Arc<Vec<RwLock<Node>>>;

View File

@ -1,28 +1,22 @@
use std::thread;
use std::sync::{Arc, RwLock};
mod utils;
mod spring_model;
mod my_model;
mod graph;
use rand::Rng;
use std::fs::File;
use std::io::prelude::*;
use std::sync::{Arc, RwLock};
use std::thread;
use graph::{EdgeMatrix, NodeVector, Node, Edge};
mod utils;
struct Node {
x: f32,
y: f32,
}
struct Edge {
weight: f32,
}
type EdgeMatrix = Arc<RwLock<Vec<Vec<Edge>>>>;
fn nodes_list(size: usize) -> Arc<Vec<RwLock<Node>>> {
fn nodes_list(size: usize) -> NodeVector {
let mut nodes = Vec::new();
for _ in 0..size {
let node = RwLock::new(Node {
x: rand::thread_rng().gen_range(0.0..100.0),
y: rand::thread_rng().gen_range(0.0..100.0),
x: rand::thread_rng().gen_range(-0.5..0.5),
y: rand::thread_rng().gen_range(-0.5..0.5),
});
nodes.push(node);
}
@ -34,7 +28,7 @@ fn connection_matrix(size: usize) -> EdgeMatrix {
for _ in 0..size {
let mut row = Vec::with_capacity(size);
for _ in 0..size {
row.push(Edge { weight: 0.0});
row.push(Edge { weight: 0.0 });
}
matrix.push(row);
}
@ -69,91 +63,47 @@ fn read_graph(file_name: &str) -> (usize, EdgeMatrix) {
}
fn main() -> std::io::Result<()> {
const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
const ITER: usize = 200;
const ITER: usize = 50;
const THREADS: usize = 8;
// let edges = connection_matrix(size);
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 chunks = utils::chunk_borders(size, THREADS);
// let model = Arc::new(RwLock::new(spring_model::InitialModel::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 {
model.write().unwrap().prepare(&nodes);
let mut handles = vec![];
for i in 0..THREADS {
let nodes = nodes.clone();
let nodes_next = nodes_next.clone();
let edges = edges.clone();
let chunk = chunks[i].clone();
let model = model.clone();
let handle = thread::spawn(move || {
for n in chunk {
let node = nodes[n].read().unwrap();
let edges = edges.read().unwrap();
let mut node_x = node.x;
let mut node_y = node.y;
for o in 0..size {
if o == n {
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().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;
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 update = model.read().unwrap().step(&nodes,n);
let mut result = nodes_next[n].write().unwrap();
result.x = node_x;
result.y = node_y;
result.x = update.x;
result.y = update.y;
}
});
handles.push(handle);
}
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();
@ -163,5 +113,4 @@ fn main() -> std::io::Result<()> {
}
Ok(())
}

86
src/my_model.rs Normal file
View File

@ -0,0 +1,86 @@
use crate::graph::{EdgeMatrix, NodeVector, Node};
pub struct MyModel {
edges: EdgeMatrix,
ranks: Vec<f32>,
size: usize,
opt_dist: f32,
c: f32,
dc: f32,
}
impl MyModel {
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel {
let opt_dist = 1.0 / (size as f32).sqrt();
let c = 0.1;
let mut ranks = vec![0.0; size];
{
let edges = edges.read().unwrap();
for i in 0..size {
ranks[i] = edges[i].iter().map(|e| e.weight).sum();
}
}
MyModel{
edges,
ranks,
size,
opt_dist,
c: c ,
dc: c / ((iterations + 1) as f32)
}
}
pub fn prepare(& mut self, _nodes: &NodeVector) {
self.c -= self.dc;
}
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 = (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 = self.c * 0.5 * (dist - self.opt_dist) / self.ranks[i_node];
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,
}
}
}

View File

@ -0,0 +1,76 @@
use crate::graph::{EdgeMatrix, NodeVector, Node};
const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
pub struct InitialModel {
edges: EdgeMatrix,
size: usize,
opt_dist: f32,
t: f32,
}
impl InitialModel {
pub fn new(edges: EdgeMatrix, size: usize) -> InitialModel {
let opt_dist = 1.0 / (size as f32).sqrt();
InitialModel{ edges, size, opt_dist, t: 0.1 }
}
pub fn prepare(& mut self, nodes: &NodeVector) {
self.t = 0.1 * f32::max(
nodes.iter().map(|n| n.read().unwrap().x.abs()).reduce(|a, b| a.max(b)).unwrap(),
nodes.iter().map(|n| n.read().unwrap().y.abs()).reduce(|a, b| a.max(b)).unwrap()
);
}
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().max(0.01);
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(self.t);
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 * (dist / self.opt_dist).log(2.0)).min(self.t);
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,
}
}
}

View File

@ -1,6 +1,6 @@
use std::ops::Range;
pub fn chunk_borders(n: usize, chunks: usize) -> Vec<Range<usize>> {
pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
let mut borders = vec![];
let chunk_size = n / chunks;
let mut start = 0;
@ -20,20 +20,20 @@ mod test {
use super::*;
#[test]
fn test_chunk_borders() {
let borders = chunk_borders(10, 3);
fn test_gen_chunks() {
let borders = gen_chunks(10, 3);
assert_eq!(borders, vec![(0..3), (3..6), (6..10)]);
}
#[test]
fn test_chunk_borders2() {
let borders = chunk_borders(10, 2);
fn test_gen_chunks2() {
let borders = gen_chunks(10, 2);
assert_eq!(borders, vec![(0..5), (5..10)]);
}
#[test]
fn test_chunk_borders3() {
let borders = chunk_borders(10, 1);
fn test_gen_chunks3() {
let borders = gen_chunks(10, 1);
assert_eq!(borders, vec![(0..10)]);
}