Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
duane committed Dec 20, 2015
0 parents commit 20ed7e6
Show file tree
Hide file tree
Showing 7 changed files with 943 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
Cargo.lock
src/.#*
13 changes: 13 additions & 0 deletions Cargo.toml
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"
160 changes: 160 additions & 0 deletions src/constraint.rs
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)
}
}
}
}
Loading

0 comments on commit 20ed7e6

Please sign in to comment.