-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement conversion from BooleanNetwork to BmaModel.
- Loading branch information
Showing
8 changed files
with
357 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::update_fn::bma_fn_tree::BmaFnUpdate; | ||
use crate::update_fn::enums::ArithOp; | ||
use biodivine_lib_param_bn::{BinaryOp, FnUpdate}; | ||
|
||
impl BmaFnUpdate { | ||
pub fn try_from_fn_update(fn_update: &FnUpdate) -> Result<Self, String> { | ||
// update functions with function symbols are not allowed | ||
if !fn_update.collect_parameters().is_empty() { | ||
return Err("Update function with function symbols can not be translated.".to_string()); | ||
} | ||
Self::try_from_fn_update_rec(fn_update) | ||
} | ||
|
||
/// Recursively converts the `FnUpdate` Boolean formula into a corresponding `BmaFnUpdate` | ||
/// real-number expression. | ||
pub fn try_from_fn_update_rec(fn_update: &FnUpdate) -> Result<BmaFnUpdate, String> { | ||
let res = match fn_update { | ||
FnUpdate::Const(val) => BmaFnUpdate::mk_constant(if *val { 1 } else { 0 }), | ||
FnUpdate::Var(var_id) => { | ||
BmaFnUpdate::mk_variable(&format!("var({})", var_id.to_index())) | ||
} | ||
FnUpdate::Not(child) => { | ||
// NOT: map !A to (1 - A) | ||
let child_expr = Self::try_from_fn_update_rec(child)?; | ||
let one_node = BmaFnUpdate::mk_constant(1); | ||
BmaFnUpdate::mk_arithmetic(one_node, child_expr, ArithOp::Minus) | ||
} | ||
FnUpdate::Binary(op, left, right) => { | ||
let left_expr = Self::try_from_fn_update_rec(left)?; | ||
let right_expr = Self::try_from_fn_update_rec(right)?; | ||
|
||
match op { | ||
// AND: map A && B to A * B | ||
BinaryOp::And => { | ||
BmaFnUpdate::mk_arithmetic(left_expr, right_expr, ArithOp::Times) | ||
} | ||
// OR: map A || B to A + B - A * B | ||
BinaryOp::Or => { | ||
let sum_expr = BmaFnUpdate::mk_arithmetic( | ||
left_expr.clone(), | ||
right_expr.clone(), | ||
ArithOp::Add, | ||
); | ||
let prod_expr = | ||
BmaFnUpdate::mk_arithmetic(left_expr, right_expr, ArithOp::Times); | ||
BmaFnUpdate::mk_arithmetic(sum_expr, prod_expr, ArithOp::Minus) | ||
} | ||
// XOR: map A ^ B to A + B - 2 * (A * B) | ||
BinaryOp::Xor => { | ||
let sum_expr = BmaFnUpdate::mk_arithmetic( | ||
left_expr.clone(), | ||
right_expr.clone(), | ||
ArithOp::Add, | ||
); | ||
let prod_expr = | ||
BmaFnUpdate::mk_arithmetic(left_expr, right_expr, ArithOp::Times); | ||
let two_prod_expr = BmaFnUpdate::mk_arithmetic( | ||
BmaFnUpdate::mk_constant(2), | ||
prod_expr, | ||
ArithOp::Times, | ||
); | ||
BmaFnUpdate::mk_arithmetic(sum_expr, two_prod_expr, ArithOp::Minus) | ||
} | ||
// IFF: map A <-> B to 1 - (A ^ B) | ||
BinaryOp::Iff => { | ||
let xor_expr = BmaFnUpdate::try_from_fn_update_rec(&FnUpdate::Binary( | ||
BinaryOp::Xor, | ||
left.clone(), | ||
right.clone(), | ||
))?; | ||
let one_node = BmaFnUpdate::mk_constant(1); | ||
BmaFnUpdate::mk_arithmetic(one_node, xor_expr, ArithOp::Minus) | ||
} | ||
// IMP: map A -> B to 1 - A + A * B | ||
BinaryOp::Imp => { | ||
let not_left_expr = BmaFnUpdate::mk_arithmetic( | ||
BmaFnUpdate::mk_constant(1), | ||
left_expr.clone(), | ||
ArithOp::Minus, | ||
); | ||
let prod_expr = | ||
BmaFnUpdate::mk_arithmetic(left_expr, right_expr, ArithOp::Times); | ||
BmaFnUpdate::mk_arithmetic(not_left_expr, prod_expr, ArithOp::Add) | ||
} | ||
} | ||
} | ||
_ => Err("Unsupported operator.")?, | ||
}; | ||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::update_fn::bma_fn_tree::BmaFnUpdate; | ||
use biodivine_lib_param_bn::FnUpdate; | ||
|
||
impl BmaFnUpdate { | ||
pub fn to_update_fn(&self) -> FnUpdate { | ||
// TODO: implementation via explicit construction of the function table | ||
todo!() | ||
} | ||
} |
Oops, something went wrong.