Skip to content

Commit

Permalink
refactor: move types into their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
kpbaks committed Feb 1, 2024
1 parent 519444a commit ddb3d3d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 83 deletions.
4 changes: 4 additions & 0 deletions gbp-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ rust-version.workspace = true

[dependencies]
nalgebra = "0.32.3"
nutype = "0.4.0"
# probability = "0.20.3"
rayon = "1.8.1"
rstest = "0.18.2"
rand = "0.8.5"
rand_distr = "0.4.3"
10 changes: 10 additions & 0 deletions gbp-rs/examples/1d_line_fitting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Reimplementation of the 1D line fitting example from https://gaussianbp.github.io/
/// jupyter notebook: https://colab.research.google.com/drive/1-nrE95X4UC9FBLR0-cTnsIP_XhA_PZKW?usp=sharing#scrollTo=kiAOHWV4uMGY
use gbp_rs::prelude::*;

fn main() {

let mut fg = FactorGraph::new();

}
1 change: 0 additions & 1 deletion gbp-rs/src/factor.rs

This file was deleted.

26 changes: 26 additions & 0 deletions gbp-rs/src/factorgraph/factor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@



pub trait Factor {}

#[derive(Debug)]
struct DefaultFactor;
#[derive(Debug)]
struct DynamicFactor;
#[derive(Debug)]
struct InterRobotFactor;
#[derive(Debug)]
struct ObstacleFactor;

impl Factor for DefaultFactor {}
impl Factor for DynamicFactor {}
impl Factor for InterRobotFactor {}
impl Factor for ObstacleFactor {}

#[derive(Debug)]
enum FactorType {
Default(DefaultFactor),
Dynamic(DynamicFactor),
InterRobot(InterRobotFactor),
Ocstacle(ObstacleFactor),
}
39 changes: 39 additions & 0 deletions gbp-rs/src/factorgraph/factorgraph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

use crate::factorgraph::factor::Factor;
use crate::factorgraph::variable::Variable;

/// A factor graph is a bipartite graph representing the factorization of a function.
/// It is composed of two types of nodes: factors and variables.
///
pub struct FactorGraph {
factors: Vec<Box<dyn Factor>>,
variables: Vec<Variable>,
}


impl FactorGraph {
pub fn new() -> Self {
Self {
factors: Vec::new(),
variables: Vec::new(),
}
}

pub fn add_factor(&mut self, factor: Box<dyn Factor>) {
self.factors.push(factor);
}

pub fn add_variable(&mut self, variable: Variable) {
self.variables.push(variable);
}

pub fn update_all_beliefs(&mut self) {
// for variable in self.variables.iter_mut() {
// variable.update_belief();
// }
todo!()
// for factor in self.factors.iter_mut() {
// factor.update_belief();
// }
}
}
45 changes: 45 additions & 0 deletions gbp-rs/src/factorgraph/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// enum MsgPassingMode {EXTERNAL, INTERNAL};

/// Ways message passing can be performed
#[derive(Debug)]
enum MessagePassingMode {
/// Between two different robots/factorgraphs
External,
/// Within a robot/factorgraph
Internal,
}
// Eigen::VectorXd eta;
// Eigen::MatrixXd lambda;
// Eigen::VectorXd mu;

#[derive(Debug)]
pub struct Message {
pub eta: nalgebra::DVector<f64>,
pub lambda: nalgebra::DMatrix<f64>,
pub mu: nalgebra::DVector<f64>,
}

// Implement addtion and subtraction for messages
impl std::ops::Add for Message {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
eta: self.eta + other.eta,
lambda: self.lambda + other.lambda,
mu: self.mu + other.mu,
}
}
}

impl std::ops::Sub for Message {
type Output = Self;

fn sub(self, other: Self) -> Self {
Self {
eta: self.eta - other.eta,
lambda: self.lambda - other.lambda,
mu: self.mu - other.mu,
}
}
}
4 changes: 4 additions & 0 deletions gbp-rs/src/factorgraph/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod factor;
pub mod variable;
pub mod factorgraph;
pub mod message;
21 changes: 21 additions & 0 deletions gbp-rs/src/factorgraph/variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
///
// pub trait Variable {}

// struct RobotId(usize);
type RobotId = usize;
// struct NodeId(usize);
type NodeId = usize;


#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
pub struct Variable {
node_id: NodeId,
robot_id: RobotId,
}

impl Variable {
fn new(node_id: NodeId, robot_id: RobotId) -> Self {
Self { node_id, robot_id }
}
}
88 changes: 6 additions & 82 deletions gbp-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,10 @@
// struct RobotId(usize);
type RobotId = usize;
// struct NodeId(usize);
type NodeId = usize;
pub mod factorgraph;

trait Factor {}

#[derive(Debug)]
struct DefaultFactor;
#[derive(Debug)]
struct DynamicFactor;
#[derive(Debug)]
struct InterRobotFactor;
#[derive(Debug)]
struct ObstacleFactor;

impl Factor for DefaultFactor {}
impl Factor for DynamicFactor {}
impl Factor for InterRobotFactor {}
impl Factor for ObstacleFactor {}

#[derive(Debug)]
enum FactorType {
Default(DefaultFactor),
Dynamic(DynamicFactor),
InterRobot(InterRobotFactor),
Ocstacle(ObstacleFactor),
}

// enum MsgPassingMode {EXTERNAL, INTERNAL};

/// Ways message passing can be performed
#[derive(Debug)]
enum MessagePassingMode {
/// Between two different robots/factorgraphs
External,
/// Within a robot/factorgraph
Internal,
}
// Eigen::VectorXd eta;
// Eigen::MatrixXd lambda;
// Eigen::VectorXd mu;

#[derive(Debug)]
struct Message {
pub eta: nalgebra::DVector<f64>,
pub lambda: nalgebra::DMatrix<f64>,
pub mu: nalgebra::DVector<f64>,
pub mod prelude {
pub use crate::factorgraph::factor::Factor;
pub use crate::factorgraph::factorgraph::FactorGraph;
pub use crate::factorgraph::variable::Variable;
pub use crate::factorgraph::message::Message;
}

// Implement addtion and subtraction for messages
impl std::ops::Add for Message {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
eta: self.eta + other.eta,
lambda: self.lambda + other.lambda,
mu: self.mu + other.mu,
}
}
}

impl std::ops::Sub for Message {
type Output = Self;

fn sub(self, other: Self) -> Self {
Self {
eta: self.eta - other.eta,
lambda: self.lambda - other.lambda,
mu: self.mu - other.mu,
}
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
struct Variable {
node_id: NodeId,
robot_id: RobotId,
}

impl Variable {
fn new(node_id: NodeId, robot_id: RobotId) -> Self {
Self { node_id, robot_id }
}
}

0 comments on commit ddb3d3d

Please sign in to comment.