This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
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.
- Loading branch information
0 parents
commit 20ed7e6
Showing
7 changed files
with
943 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
Cargo.lock | ||
src/.#* |
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,13 @@ | ||
[package] | ||
name = "constraint" | ||
version = "0.1.0" | ||
authors = ["Duane Bailey <bailey.d.r@gmail.com>"] | ||
|
||
|
||
[lib] | ||
name = "constraint" | ||
path = "src/lib.rs" | ||
|
||
[[bin]] | ||
name = "rig" | ||
path = "src/bin.rs" |
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,160 @@ | ||
use expr::*; | ||
|
||
use std::fmt::{Display, Error, Formatter}; | ||
|
||
///LinearInequality, LinearEquality combine LinearExpression with an operation. | ||
pub struct LinearEquality { | ||
// normalized to equal zero | ||
expr: LinearExpression | ||
} | ||
|
||
impl LinearEquality { | ||
pub fn new() -> LinearEquality { | ||
LinearEquality{ | ||
expr: LinearExpression::new() | ||
} | ||
} | ||
|
||
pub fn zero(expr: &LinearExpression) -> LinearEquality { | ||
LinearEquality{ | ||
expr: expr.clone() | ||
} | ||
} | ||
|
||
pub fn equals(lhs: &LinearExpression, rhs: &LinearExpression) -> LinearEquality { | ||
LinearEquality{ | ||
expr: rhs.minus(lhs) | ||
} | ||
} | ||
} | ||
|
||
impl Display for LinearEquality { | ||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { | ||
fmt.write_str(format!("0 {} {}", Relation::EQ, self.expr).as_ref() as &str) | ||
} | ||
} | ||
|
||
pub struct LinearInequality { | ||
op: Relation, | ||
expr: LinearExpression | ||
} | ||
|
||
impl LinearInequality { | ||
pub fn new(expr1: &LinearExpression, op: Relation, expr2: &LinearExpression) -> LinearInequality { | ||
match op { | ||
Relation::GEQ | Relation::GT => { | ||
LinearInequality{ | ||
op: op, | ||
expr: expr1.minus(expr2) | ||
} | ||
} | ||
Relation::LEQ | Relation::LT => { | ||
LinearInequality{ | ||
op: op, | ||
expr: expr2.minus(expr1) | ||
} | ||
} | ||
_ => panic!("not a valid inequality: {:?}", op) | ||
} | ||
} | ||
} | ||
|
||
impl Display for LinearInequality { | ||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { | ||
fmt.write_str(format!("0 {} {}", self.op, self.expr).as_ref() as &str) | ||
} | ||
} | ||
|
||
pub struct LinearBounds { | ||
variable: Variable, | ||
lower: Option<Scalar>, | ||
upper: Option<Scalar> | ||
} | ||
|
||
impl LinearBounds { | ||
pub fn free(var: Variable) -> LinearBounds { | ||
LinearBounds{ | ||
variable: var, | ||
lower: None, | ||
upper: None | ||
} | ||
} | ||
|
||
pub fn lower(var: Variable, lower: Scalar) -> LinearBounds { | ||
LinearBounds{ | ||
variable: var, | ||
lower: Some(lower), | ||
upper: None | ||
} | ||
} | ||
|
||
pub fn upper(var: Variable, upper: Scalar) -> LinearBounds { | ||
LinearBounds{ | ||
variable: var, | ||
lower: None, | ||
upper: Some(upper) | ||
} | ||
} | ||
|
||
pub fn double(var: Variable, lower: Scalar, upper: Scalar) -> LinearBounds { | ||
assert!(lower <= upper); | ||
LinearBounds{ | ||
variable: var, | ||
lower: Some(lower), | ||
upper: Some(upper) | ||
} | ||
} | ||
|
||
pub fn fixed(var: Variable, constant: Scalar) -> LinearBounds { | ||
LinearBounds{ | ||
variable: var, | ||
lower: Some(constant), | ||
upper: Some(constant) | ||
} | ||
} | ||
} | ||
|
||
impl Display for LinearBounds { | ||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { | ||
let inf = String::from("\u{221E}"); | ||
match (self.lower, self.upper) { | ||
(Some(lower), Some(upper)) => { | ||
if approx_eq(lower, upper) { | ||
fmt.write_str(format!("{} = {}", self.variable, lower).as_ref() as &str) | ||
} else { | ||
fmt.write_str(format!("{} {} {} {} {}", | ||
lower, | ||
Relation::LEQ, | ||
self.variable, | ||
Relation::LEQ, | ||
upper).as_ref() as &str) | ||
} | ||
} | ||
(None, Some(upper)) => { | ||
fmt.write_str(format!("-{} {} {} {} {}", | ||
inf, | ||
Relation::LT, | ||
self.variable, | ||
Relation::LEQ, | ||
upper).as_ref() as &str) | ||
} | ||
(Some(lower), None) => { | ||
fmt.write_str(format!("{} {} {} {} {}", | ||
lower, | ||
Relation::LEQ, | ||
self.variable, | ||
Relation::LT, | ||
inf).as_ref() as &str) | ||
} | ||
(None, None) => { | ||
fmt.write_str(format!("-{} {} {} {} {}", | ||
inf, | ||
Relation::LT, | ||
self.variable, | ||
Relation::LT, | ||
inf).as_ref() as &str) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.