Skip to content

Commit

Permalink
Add less-than! constraint
Browse files Browse the repository at this point in the history
relates #135
  • Loading branch information
tuturto committed Nov 8, 2015
1 parent 96b9d2e commit 307ed4d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/pyherc/solver.hy
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
(when (value? var2)
(narrow context var1 (- var1.values var2.values))))

(fact less-than!
"smaller than constraint"
(let [[maximum (max updated-variable.values)]]
(if (= updated-variable var1)
(narrow context var2 (set-comp x [x var2.values] (> x (min var1.values))))
(narrow context var1 (set-comp x [x var1.values] (< x (max var2.values)))))))

(defn solve [&rest variables]
"solve all variables"
(let [[context {:frame-pointer nil
Expand Down
24 changes: 22 additions & 2 deletions src/pyherc/test/unit/test_solver.hy
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
;; You should have received a copy of the GNU General Public License
;; along with pyherc. If not, see <http://www.gnu.org/licenses/>.

(import [pyherc.solver [Variable are-equal! are-inequal! solve solve-one value]]
[hamcrest [assert-that is- equal-to is-not :as is-not-]])
(import [pyherc.solver [Variable are-equal! are-inequal! less-than!
solve solve-one value]]
[hamcrest [assert-that is- equal-to is-not :as is-not- less-than]])

(defn context []
"create an empty context for testing"
Expand All @@ -43,6 +44,25 @@
(solve var₁ var₂)
(assert-that (value var₁) (is-not- (equal-to (value var₂))))))

(defn test-less-than-constraint []
"variable can be constrained to be less than something else"
(let [[var₁ (Variable 1 2 3 4 5)]
[var₂ (Variable 1 2 3 4 5)]]
(less-than! var₁ var₂)
(solve var₁ var₂)
(assert-that (value var₁) (is- (less-than (value var₂))))))

(defn test-triple-less-than []
"set of variables can be ordered with less-than!"
(let [[var₁ (Variable 1 2 3 4 5)]
[var₂ (Variable 1 2 3 4 5)]
[var₃ (Variable 1 2 3 4 5)]]
(less-than! var₁ var₂)
(less-than! var₂ var₃)
(solve var₁ var₂ var₃)
(assert-that (value var₁) (is- (less-than (value var₂))))
(assert-that (value var₂) (is- (less-than (value var₃))))))

(defn test-multiple-constraints []
"variables with multiple constraints can be solved"
(let [[var₁ (Variable 1 2 3 4 5)]
Expand Down

0 comments on commit 307ed4d

Please sign in to comment.