diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a25760..dc4c544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log -## Python Contraint +## Python Constraint All notable changes to this code base will be documented in this file, for every released major and minor version. diff --git a/constraint/solvers.py b/constraint/solvers.py index e583064..0a78f09 100644 --- a/constraint/solvers.py +++ b/constraint/solvers.py @@ -544,24 +544,29 @@ class MinConflictsSolver(Solver): NotImplementedError: MinConflictsSolver doesn't provide iteration """ - def __init__(self, steps=1000): + def __init__(self, steps=1000, rand=None): """Initialization method. Args: steps (int): Maximum number of steps to perform before giving up when looking for a solution (default is 1000) + rand (Random): Optional random.Random instance to use for + repeatability. """ self._steps = steps + self._rand = rand - def getSolution(self, domains: dict, constraints: list[tuple], vconstraints: dict): # noqa: D102 + def getSolution(self, domains: dict, constraints: list[tuple], vconstraints: dict): # noqa: D102 + choice = self._rand.choice if self._rand is not None else random.choice + shuffle = self._rand.shuffle if self._rand is not None else random.shuffle assignments = {} # Initial assignment for variable in domains: - assignments[variable] = random.choice(domains[variable]) + assignments[variable] = choice(domains[variable]) for _ in range(self._steps): conflicted = False lst = list(domains.keys()) - random.shuffle(lst) + shuffle(lst) for variable in lst: # Check if variable is not in conflict for constraint, variables in vconstraints[variable]: @@ -585,7 +590,7 @@ def getSolution(self, domains: dict, constraints: list[tuple], vconstraints: dic del minvalues[:] minvalues.append(value) # Pick a random one from these values. - assignments[variable] = random.choice(minvalues) + assignments[variable] = choice(minvalues) conflicted = True if not conflicted: return assignments diff --git a/poetry.lock b/poetry.lock index 01d53f9..ed3a5dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -29,18 +29,18 @@ test = ["coverage", "mypy", "pexpect", "ruff", "wheel"] [[package]] name = "babel" -version = "2.16.0" +version = "2.17.0" description = "Internationalization utilities" optional = false python-versions = ">=3.8" groups = ["docs"] files = [ - {file = "babel-2.16.0-py3-none-any.whl", hash = "sha256:368b5b98b37c06b7daf6696391c3240c938b37767d4584413e8438c5c435fa8b"}, - {file = "babel-2.16.0.tar.gz", hash = "sha256:d1f3554ca26605fe173f3de0c65f750f5a42f924499bf134de6423582298e316"}, + {file = "babel-2.17.0-py3-none-any.whl", hash = "sha256:4d0b53093fdfb4b21c92b5213dba5a1b23885afa8383709427046b21c366e5f2"}, + {file = "babel-2.17.0.tar.gz", hash = "sha256:0c54cffb19f690cdcc52a3b50bcbf71e07a808d1c80d549f2459b9d2cf0afb9d"}, ] [package.extras] -dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] +dev = ["backports.zoneinfo", "freezegun (>=1.0,<2.0)", "jinja2 (>=3.0)", "pytest (>=6.0)", "pytest-cov", "pytz", "setuptools", "tzdata"] [[package]] name = "certifi" diff --git a/pyproject.toml b/pyproject.toml index 1facdaa..353c3c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,9 +4,13 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "python-constraint2" # when set back to "python-constraint", don't forget to remove '2' in other places (e.g. README) -packages = [{ include = "constraint", from = "." }] +packages = [ + { include = "constraint", from = "." }, + { include = "tests", format = "sdist" } +] +include = [{ path = "constraint/*.so", format = "wheel" }] description = "python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains." -version = "2.0.1" # adhere to PEP440 versioning: https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#id55 +version = "2.0.2" # adhere to PEP440 versioning: https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#id55 authors = [ "Gustavo Niemeyer ", "Sébastien Celles ",