Skip to content

Commit

Permalink
refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yangeorget committed Sep 24, 2024
1 parent 91d163e commit 3fa8b38
Show file tree
Hide file tree
Showing 37 changed files with 145 additions and 132 deletions.
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
copyright = '2024, Yan Georget'
author = 'Yan Georget'

release = '0.8'
version = '0.8.1'
release = '0.9'
version = '0.9.0'

# -- General configuration

Expand Down
4 changes: 2 additions & 2 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ Let's find all solutions to the `12-queens problem <https://www.csplib.org/Probl

.. code-block:: bash
NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python tests/examples/test_queens.py -n 12
NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python nucs/examples/queens/queens_problem.py -n 12
Let's find the optimal solution to the `Golomb ruler problem <https://www.csplib.org/Problems/prob006>`_ with 10 marks:

.. code-block:: bash
NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python tests/examples/test_golomb.py -n 10
NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python nucs/examples/golomb/golomb_problem.py -n 10
Expand Down
Empty file added nucs/examples/alpha/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from pprint import pprint
from typing import List

from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_AFFINE_EQ, ALG_ALLDIFFERENT
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import min_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import get_statistics

A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z = tuple(range(26))

Expand Down Expand Up @@ -81,3 +85,13 @@ def pretty_print_solution(self, solution: List[int]) -> None:
"Z": solution[Z],
}
)


if __name__ == "__main__":
problem = AlphaProblem()
solver = BacktrackSolver(
problem, var_heuristic=smallest_domain_var_heuristic, dom_heuristic=min_value_dom_heuristic
)
solutions = solver.find_all()
pprint(get_statistics(solver.statistics))
print(solutions[0])
Empty file added nucs/examples/bibd/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from pprint import pprint

from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_EXACTLY_EQ, ALG_LEXICOGRAPHIC_LEQ, ALG_MIN_EQ
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.statistics import get_statistics


class BIBDProblem(Problem):
Expand Down Expand Up @@ -36,3 +40,10 @@ def __init__(self, v: int, b: int, r: int, k: int, l: int):
# lexleq on columns
for j in range(0, b - 1):
self.add_propagator((list(range(j, v * b, b)) + list(range(j + 1, v * b, b)), ALG_LEXICOGRAPHIC_LEQ, []))


if __name__ == "__main__":
problem = BIBDProblem(8, 14, 7, 4, 3)
solver = BacktrackSolver(problem)
solver.solve_all()
pprint(get_statistics(solver.statistics))
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from pprint import pprint
from typing import List

from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_AFFINE_EQ, ALG_ALLDIFFERENT
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import min_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import get_statistics

A, B, D, E, G, L, N, O, R, T = tuple(range(10))

Expand Down Expand Up @@ -40,3 +44,13 @@ def pretty_print_solution(self, solution: List[int]) -> None:
"T": solution[T],
}
)


if __name__ == "__main__":
problem = DonaldProblem()
solver = BacktrackSolver(
problem, var_heuristic=smallest_domain_var_heuristic, dom_heuristic=min_value_dom_heuristic
)
for solution in solver.solve():
problem.pretty_print_solution(solution)
pprint(get_statistics(solver.statistics))
Empty file.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import argparse
from pprint import pprint

import numpy as np
from numpy.typing import NDArray

from nucs.constants import MAX, MIN
from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_AFFINE_EQ, ALG_AFFINE_LEQ, ALG_ALLDIFFERENT
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.consistency_algorithms import bound_consistency_algorithm
from nucs.solvers.heuristics import first_not_instantiated_var_heuristic
from nucs.statistics import get_statistics

GOLOMB_LENGTHS = [0, 0, 1, 3, 6, 11, 17, 25, 34, 44, 55, 72, 85, 106, 127]

Expand Down Expand Up @@ -114,3 +119,15 @@ def golomb_consistency_algorithm(statistics: NDArray, problem: GolombProblem) ->
problem.set_min_value(var_idx, new_min)
# then apply BC
return bound_consistency_algorithm(statistics, problem)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=10)
args = parser.parse_args()
problem = GolombProblem(args.n)
solver = BacktrackSolver(problem, consistency_algorithm=golomb_consistency_algorithm)
solution = solver.minimize(problem.length_idx)
pprint(get_statistics(solver.statistics))
print(solution)
print(solution[problem.length_idx]) # type: ignore
Empty file.
35 changes: 35 additions & 0 deletions nucs/examples/knapsack/knapsack_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pprint import pprint
from typing import List

from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_AFFINE_EQ, ALG_AFFINE_LEQ
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import first_not_instantiated_var_heuristic, max_value_dom_heuristic
from nucs.statistics import get_statistics


class KnapsackProblem(Problem):
"""
CSPLIB problem #133 - https://www.csplib.org/Problems/prob133/
"""

def __init__(self, weights: List[int], volumes: List[int], capacity: int) -> None:
n = len(weights)
super().__init__([(0, 1)] * n + [(0, sum(weights))])
self.add_propagator((list(range(n)), ALG_AFFINE_LEQ, [*volumes, capacity]))
self.add_propagator((list(range(n + 1)), ALG_AFFINE_EQ, [*weights, -1, 0]))
self.weight = n


if __name__ == "__main__":
problem = KnapsackProblem(
[40, 40, 38, 38, 36, 36, 34, 34, 32, 32, 30, 30, 28, 28, 26, 26, 24, 24, 22, 22],
[40, 40, 38, 38, 36, 36, 34, 34, 32, 32, 30, 30, 28, 28, 26, 26, 24, 24, 22, 22],
55,
)
solver = BacktrackSolver(
problem, var_heuristic=first_not_instantiated_var_heuristic, dom_heuristic=max_value_dom_heuristic
)
solution = solver.maximize(problem.weight)
pprint(get_statistics(solver.statistics))
print(solution)
17 changes: 0 additions & 17 deletions nucs/examples/knapsack_problem.py

This file was deleted.

Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import argparse
from pprint import pprint

from nucs.problems.latin_square_problem import M_COLOR, M_COLUMN, M_ROW, LatinSquareRCProblem
from nucs.propagators.propagators import ALG_ELEMENT_LIV
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import min_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import get_statistics


class QuasigroupProblem(LatinSquareRCProblem):
Expand Down Expand Up @@ -40,3 +46,15 @@ def __init__(self, n: int):
),
0,
)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=10)
args = parser.parse_args()
problem = Quasigroup5Problem(args.n)
solver = BacktrackSolver(
problem, var_heuristic=smallest_domain_var_heuristic, dom_heuristic=min_value_dom_heuristic
)
solver.solve_all()
pprint(get_statistics(solver.statistics))
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import argparse
from pprint import pprint

from nucs.problems.problem import Problem
from nucs.propagators.propagators import ALG_ALLDIFFERENT
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.statistics import get_statistics


class QueensProblem(Problem):
Expand All @@ -18,3 +23,13 @@ def __init__(self, n: int):
self.add_propagator((list(range(n)), ALG_ALLDIFFERENT, []))
self.add_propagator((list(range(n, 2 * n)), ALG_ALLDIFFERENT, []))
self.add_propagator((list(range(2 * n, 3 * n)), ALG_ALLDIFFERENT, []))


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=12)
args = parser.parse_args()
problem = QueensProblem(args.n)
solver = BacktrackSolver(problem)
solver.solve_all()
pprint(get_statistics(solver.statistics))
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name="NUCS"
version="0.8.1"
version="0.9.0"
authors = [
{ name="Yan Georget", email="[email protected]" },
]
Expand Down
16 changes: 2 additions & 14 deletions tests/examples/test_alpha.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pprint import pprint

from nucs.examples.alpha_problem import AlphaProblem
from nucs.examples.alpha.alpha_problem import AlphaProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import min_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import STATS_SOLVER_SOLUTION_NB, get_statistics
from nucs.statistics import STATS_SOLVER_SOLUTION_NB


class TestAlpha:
Expand Down Expand Up @@ -42,13 +40,3 @@ def test_alpha(self) -> None:
14,
18,
]


if __name__ == "__main__":
problem = AlphaProblem()
solver = BacktrackSolver(
problem, var_heuristic=smallest_domain_var_heuristic, dom_heuristic=min_value_dom_heuristic
)
solutions = solver.find_all()
pprint(get_statistics(solver.statistics))
print(solutions[0])
13 changes: 2 additions & 11 deletions tests/examples/test_bibd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from pprint import pprint

from nucs.examples.bibd_problem import BIBDProblem
from nucs.examples.bibd.bibd_problem import BIBDProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.statistics import STATS_SOLVER_SOLUTION_NB, get_statistics
from nucs.statistics import STATS_SOLVER_SOLUTION_NB


class TestBIBD:
Expand All @@ -17,10 +15,3 @@ def test_8_14_7_4_3(self) -> None:
solver = BacktrackSolver(problem)
solver.solve_all()
assert solver.statistics[STATS_SOLVER_SOLUTION_NB] == 92


if __name__ == "__main__":
problem = BIBDProblem(8, 14, 7, 4, 3)
solver = BacktrackSolver(problem)
solver.solve_all()
pprint(get_statistics(solver.statistics))
16 changes: 2 additions & 14 deletions tests/examples/test_donald.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pprint import pprint

from nucs.examples.donald_problem import DonaldProblem
from nucs.examples.donald.donald_problem import DonaldProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import min_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import STATS_SOLVER_SOLUTION_NB, get_statistics
from nucs.statistics import STATS_SOLVER_SOLUTION_NB


class TestDonald:
Expand All @@ -15,13 +13,3 @@ def test_donald(self) -> None:
solutions = solver.find_all()
assert solver.statistics[STATS_SOLVER_SOLUTION_NB] == 1
assert solutions[0] == [4, 3, 5, 9, 1, 8, 6, 2, 7, 0]


if __name__ == "__main__":
problem = DonaldProblem()
solver = BacktrackSolver(
problem, var_heuristic=smallest_domain_var_heuristic, dom_heuristic=min_value_dom_heuristic
)
for solution in solver.solve():
problem.pretty_print_solution(solution)
pprint(get_statistics(solver.statistics))
18 changes: 1 addition & 17 deletions tests/examples/test_golomb.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import argparse
from pprint import pprint

import pytest

from nucs.constants import MIN
from nucs.examples.golomb_problem import GolombProblem, golomb_consistency_algorithm, index, init_domains
from nucs.examples.golomb.golomb_problem import GolombProblem, golomb_consistency_algorithm, index, init_domains
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.statistics import get_statistics


class TestGolomb:
Expand All @@ -28,15 +24,3 @@ def test_golomb(self, mark_nb: int, solution_nb: int) -> None:
solution = solver.minimize(problem.length_idx)
assert solution
assert solution[problem.length_idx] == solution_nb


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=10)
args = parser.parse_args()
problem = GolombProblem(args.n)
solver = BacktrackSolver(problem, consistency_algorithm=golomb_consistency_algorithm)
solution = solver.minimize(problem.length_idx)
pprint(get_statistics(solver.statistics))
print(solution)
print(solution[problem.length_idx]) # type: ignore
19 changes: 1 addition & 18 deletions tests/examples/test_knapsack.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from pprint import pprint

from nucs.examples.knapsack_problem import KnapsackProblem
from nucs.examples.knapsack.knapsack_problem import KnapsackProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import first_not_instantiated_var_heuristic, max_value_dom_heuristic
from nucs.statistics import get_statistics


class TestKnapsack:
Expand All @@ -19,17 +16,3 @@ def test_knapsack(self) -> None:
solution = solver.maximize(problem.weight)
assert solution
assert solution[problem.weight] == 54


if __name__ == "__main__":
problem = KnapsackProblem(
[40, 40, 38, 38, 36, 36, 34, 34, 32, 32, 30, 30, 28, 28, 26, 26, 24, 24, 22, 22],
[40, 40, 38, 38, 36, 36, 34, 34, 32, 32, 30, 30, 28, 28, 26, 26, 24, 24, 22, 22],
55,
)
solver = BacktrackSolver(
problem, var_heuristic=first_not_instantiated_var_heuristic, dom_heuristic=max_value_dom_heuristic
)
solution = solver.maximize(problem.weight)
pprint(get_statistics(solver.statistics))
print(solution)
2 changes: 1 addition & 1 deletion tests/examples/test_magic_sequence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from nucs.examples.magic_sequence_problem import MagicSequenceProblem
from nucs.examples.magic_sequence.magic_sequence_problem import MagicSequenceProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import last_not_instantiated_var_heuristic, min_value_dom_heuristic
from nucs.statistics import STATS_SOLVER_SOLUTION_NB
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/test_magic_square.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from nucs.examples.magic_square_problem import MagicSquareProblem
from nucs.examples.magic_square.magic_square_problem import MagicSquareProblem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.heuristics import max_value_dom_heuristic, smallest_domain_var_heuristic
from nucs.statistics import STATS_SOLVER_SOLUTION_NB
Expand Down
Loading

0 comments on commit 3fa8b38

Please sign in to comment.