1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
# copyright 2002-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-constraint.
#
# logilab-constraint is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# logilab-constraint is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.
"""Unit testing for constraint propagation module"""
import unittest
import os
from logilab.common.testlib import TestCase
from logilab.constraint.propagation import Repository, ConsistencyFailure, Solver
from logilab.constraint import fd
from logilab.constraint.distributors import DefaultDistributor
class Repository_TC(TestCase):
def setUp(self):
self.domains = {}
self.variables = list("abcdef")
for v in self.variables:
self.domains[v] = fd.FiniteDomain(range(6))
self.repo = Repository(self.variables, self.domains)
def testVCGDraw(self):
for v1 in self.variables:
for v2 in self.variables:
if v1 < v2:
self.repo.addConstraint(
fd.make_expression((v1, v2), f"{v1} < {v2}")
)
try:
try:
self.repo.vcg_draw("toto.vcg")
except OSError as exc:
self.fail(
"This test cannot run in the testing environment"
"because I cannot write the file.\n"
"The error message was: \n%s" % exc
)
finally:
os.unlink("toto.vcg")
def testGetDomains(self):
doms = self.repo.getDomains()
self.assertEqual(doms, self.domains)
def testDistribute(self):
d = []
for distributed in self.repo.distribute(DefaultDistributor()):
d.append(distributed)
self.assertEqual(len(d), 2)
def testConsistencyNoConstraint(self):
self.repo.consistency()
for v, dom in self.repo.getDomains().items():
self.assertEqual(dom.size(), 6)
def testConsistency(self):
for v1 in self.variables:
for v2 in self.variables:
if v1 < v2:
self.repo.addConstraint(
fd.make_expression((v1, v2), f"{v1} < {v2}")
)
self.repo.consistency()
for v, dom in self.repo.getDomains().items():
self.assertEqual(dom.size(), 1)
def testInconsistency(self):
self.repo.addConstraint(fd.make_expression(("a", "b"), "a < b"))
self.repo.addConstraint(fd.make_expression(("a", "b"), "a > b"))
try:
self.repo.consistency()
self.fail("No ConsistencyFailure raised")
except ConsistencyFailure:
pass
class Sover_TC(TestCase):
def setUp(self):
self.solver = Solver()
self.domains = {}
self.variables = list("abcdef")
for v in self.variables:
self.domains[v] = fd.FiniteDomain(list(range(6)))
self.repo = Repository(self.variables, self.domains)
for v1 in self.variables:
for v2 in self.variables:
if v1 < v2:
self.repo.addConstraint(
fd.make_expression((v1, v2), f"{v1} < {v2}")
)
def testSolveOne(self):
solution = self.solver.solve_one(self.repo)
self.assertEqual(solution, {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5})
def testSolve(self):
solutions = self.solver.solve(self.repo)
self.assertEqual(solutions, [{"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5}])
def testSolveAll(self):
solutions = []
for s in self.solver.solve_all(self.repo):
solutions.append(s)
self.assertEqual(solutions, [{"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5}])
def testNolutionSolve(self):
self.repo.addConstraint(fd.make_expression(("a", "b"), "b < a"))
solutions = self.solver.solve(self.repo)
self.assertEqual(solutions, [])
class SolverBest_TC(TestCase):
def setUp(self):
self.solver = Solver()
self.domains = {}
self.variables = list("abc")
for v in self.variables:
self.domains[v] = fd.FiniteDomain(range(6))
self.repo = Repository(self.variables, self.domains)
for v1 in self.variables:
for v2 in self.variables:
if v1 < v2:
self.repo.addConstraint(
fd.make_expression((v1, v2), f"{v1} < {v2}")
)
def costFunc(self, a, b, c):
return -(a * a + b * b + c * c)
def testSolveBest(self):
solutions = []
for s in self.solver.solve_best(self.repo, self.costFunc):
solutions.append(s)
costs = [self.costFunc(**sol[0]) for sol in solutions]
sorted_costs = costs[:]
sorted_costs.sort()
sorted_costs.reverse()
self.assertEqual(costs, sorted_costs)
self.assertEqual(costs, [s[1] for s in solutions])
if __name__ == "__main__":
unittest.main()
|