Skip to content

Commit

Permalink
interface_example.py runs and prints its values.
Browse files Browse the repository at this point in the history
  • Loading branch information
knub committed Aug 4, 2014
1 parent e4a80ea commit 93aa914
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions examples/interface_example.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
from __future__ import division

import sys
sys.path.append("../src")
sys.path.append("../tests")

from skypyblue.core import ConstraintSystem
from skypyblue.models import Strength, Variable
from skypyblue.models import Constraint, Strength, Variable, Method

cs = ConstraintSystem()

v1 = Variable("v1", 15, cs)
v2 = Variable("v2", 15, cs)
v3 = Variable("v3", 15, cs)
v1 = Variable("v1", 16, cs)
v2 = Variable("v2", 16, cs)
v3 = Variable("v3", 16, cs)
v4 = Variable("v4", 10, cs)

cs.stay(v4, Strength.WEAK)
v4.stay()

# v1 + v2 = v3, v1 - v2 = v4
m12_34 = Method([v1, v2], [v3, v4],
lambda v1, v2: (v1 + v2 , v1 - v2))

m34_12 = Method([v3, v4], [v1, v2],
lambda v3, v4: (v3 + v4) / 2 , (v3 - v4) / 2)
lambda v3, v4: ((v3 + v4) / 2 , (v3 - v4) / 2))

cn1 = Constraint(
lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4,
Strength.STRONG,
[v1, v2, v3, v4],
[m12_34, m34_12])

cs.add_constraint(cn1) # sets v3 to 30 and v4 to 0
cs.add_constraint(cn1)

def print_values():
print("v1 = " + str(v1.get_value()))
print("v2 = " + str(v2.get_value()))
print("v3 = " + str(v3.get_value()))
print("v4 = " + str(v4.get_value()))
print("Constraint: v1 + v2 = v3, v1 - v2 = v4.")
print_values()

print("Set v1 to 5.")
v1.set_value(5)
print_values()

print("Set v2 to 8.")
v2.set_value(8)
print_values()

print("Set v3 to 10.")
v3.set_value(10)
print(v1.get_value()) # 5
print(v2.get_value()) # 5
print_values()

print("Set v4 to 2.")
v4.set_value(2)
print_values()

0 comments on commit 93aa914

Please sign in to comment.