Skip to content

Commit

Permalink
sort propagators based on time complexity for speeding up filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
yangeorget committed Sep 20, 2024
1 parent a12bcb7 commit cb1ea8c
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 16 deletions.
1 change: 0 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
- choice points pruning
- backtrackable propagator __state__
- implement decision variables
- try other strategies for computing fix point in BC

# Numba
- use prange with parallel=True
Expand Down
4 changes: 3 additions & 1 deletion nucs/problems/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
new_shr_domains_propagators,
new_triggered_propagators,
)
from nucs.propagators.propagators import GET_TRIGGERS_FCTS
from nucs.propagators.propagators import GET_COMPLEXITY_FCTS, GET_TRIGGERS_FCTS
from nucs.statistics import STATS_PROBLEM_PROPAGATOR_NB, STATS_PROBLEM_VARIABLE_NB


Expand Down Expand Up @@ -119,6 +119,8 @@ def init_problem(self, statistics: Optional[NDArray] = None) -> None:
self.shr_domains_arr = new_shr_domains_by_values(self.shr_domains_lst)
self.dom_indices_arr = new_dom_indices_by_values(self.dom_indices_lst)
self.dom_offsets_arr = new_dom_offsets_by_values(self.dom_offsets_lst)
# Sort the propagators based on their estimated amortized complexities.
self.propagators.sort(key=lambda prop: GET_COMPLEXITY_FCTS[prop[1]](len(prop[0]), prop[2]))
# Propagator initialization
self.propagator_nb = len(self.propagators)
# This is where the triggered propagators will be stored,
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/affine_eq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_affine_eq(n: int, data: NDArray) -> float:
return 5 * n


def get_triggers_affine_eq(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/affine_geq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from nucs.propagators.affine_eq_propagator import compute_domain_sum_max, compute_domain_sum_min


def get_complexity_affine_geq(n: int, data: NDArray) -> float:
return 5 * n


def get_triggers_affine_geq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/affine_leq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from nucs.propagators.affine_eq_propagator import compute_domain_sum_max, compute_domain_sum_min


def get_complexity_affine_leq(n: int, data: NDArray) -> float:
return 5 * n


def get_triggers_affine_leq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
6 changes: 6 additions & 0 deletions nucs/propagators/alldifferent_propagator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

import numpy as np
from numba import njit # type: ignore
from numpy.typing import NDArray
Expand All @@ -6,6 +8,10 @@
from nucs.numpy import new_triggers


def get_complexity_alldifferent(n: int, data: NDArray) -> float:
return 2 * n * math.log(n) + 5 * n


def get_triggers_alldifferent(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/count_eq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_count_eq(n: int, data: NDArray) -> float:
return 2 * n


def get_triggers_count_eq(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/dummy_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from nucs.numpy import new_triggers


def get_complexity_dummy(n: int, data: NDArray) -> float:
return 0


def get_triggers_dummy(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/element_lic_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from nucs.numpy import new_triggers


def get_complexity_element_lic(n: int, data: NDArray) -> float:
return n


def get_triggers_element_lic(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/element_liv_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from nucs.numpy import new_triggers


def get_complexity_element_liv(n: int, data: NDArray) -> float:
return n


def get_triggers_element_liv(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/exactly_eq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_exactly_eq(n: int, data: NDArray) -> float:
return 2* n


def get_triggers_exactly_eq(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/lexicographic_leq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from nucs.numpy import new_triggers


def get_complexity_lexicographic_leq(n: int, data: NDArray) -> float:
return 4 * n


def get_triggers_lexicographic_leq(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/max_eq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_max_eq(n: int, data: NDArray) -> float:
return 3 * n


def get_triggers_max_eq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/max_leq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_max_leq(n: int, data: NDArray) -> float:
return 3 * n


def get_triggers_max_leq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/min_eq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_min_eq(n: int, data: NDArray) -> float:
return 3 * n


def get_triggers_min_eq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/min_geq_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from nucs.numpy import new_triggers


def get_complexity_min_geq(n: int, data: NDArray) -> float:
return 3 * n


def get_triggers_min_geq(n: int, data: NDArray) -> NDArray:
"""
Returns the triggers for this propagator.
Expand Down
83 changes: 69 additions & 14 deletions nucs/propagators/propagators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,61 @@
from numpy.typing import NDArray

from nucs.numba import NUMBA_DISABLE_JIT, build_function_address_list
from nucs.propagators.affine_eq_propagator import compute_domains_affine_eq, get_triggers_affine_eq
from nucs.propagators.affine_geq_propagator import compute_domains_affine_geq, get_triggers_affine_geq
from nucs.propagators.affine_leq_propagator import compute_domains_affine_leq, get_triggers_affine_leq
from nucs.propagators.alldifferent_propagator import compute_domains_alldifferent, get_triggers_alldifferent
from nucs.propagators.count_eq_propagator import compute_domains_count_eq, get_triggers_count_eq
from nucs.propagators.dummy_propagator import compute_domains_dummy, get_triggers_dummy
from nucs.propagators.element_lic_propagator import compute_domains_element_lic, get_triggers_element_lic
from nucs.propagators.element_liv_propagator import compute_domains_element_liv, get_triggers_element_liv
from nucs.propagators.exactly_eq_propagator import compute_domains_exactly_eq, get_triggers_exactly_eq
from nucs.propagators.affine_eq_propagator import (
compute_domains_affine_eq,
get_complexity_affine_eq,
get_triggers_affine_eq,
)
from nucs.propagators.affine_geq_propagator import (
compute_domains_affine_geq,
get_complexity_affine_geq,
get_triggers_affine_geq,
)
from nucs.propagators.affine_leq_propagator import (
compute_domains_affine_leq,
get_complexity_affine_leq,
get_triggers_affine_leq,
)
from nucs.propagators.alldifferent_propagator import (
compute_domains_alldifferent,
get_complexity_alldifferent,
get_triggers_alldifferent,
)
from nucs.propagators.count_eq_propagator import (
compute_domains_count_eq,
get_complexity_count_eq,
get_triggers_count_eq,
)
from nucs.propagators.dummy_propagator import compute_domains_dummy, get_complexity_dummy, get_triggers_dummy
from nucs.propagators.element_lic_propagator import (
compute_domains_element_lic,
get_complexity_element_lic,
get_triggers_element_lic,
)
from nucs.propagators.element_liv_propagator import (
compute_domains_element_liv,
get_complexity_element_liv,
get_triggers_element_liv,
)
from nucs.propagators.exactly_eq_propagator import (
compute_domains_exactly_eq,
get_complexity_exactly_eq,
get_triggers_exactly_eq,
)
from nucs.propagators.lexicographic_leq_propagator import (
compute_domains_lexicographic_leq,
get_complexity_lexicographic_leq,
get_triggers_lexicographic_leq,
)
from nucs.propagators.max_eq_propagator import compute_domains_max_eq, get_triggers_max_eq
from nucs.propagators.max_leq_propagator import compute_domains_max_leq, get_triggers_max_leq
from nucs.propagators.min_eq_propagator import compute_domains_min_eq, get_triggers_min_eq
from nucs.propagators.min_geq_propagator import compute_domains_min_geq, get_triggers_min_geq
from nucs.propagators.relation_propagator import compute_domains_relation, get_triggers_relation
from nucs.propagators.max_eq_propagator import compute_domains_max_eq, get_complexity_max_eq, get_triggers_max_eq
from nucs.propagators.max_leq_propagator import compute_domains_max_leq, get_complexity_max_leq, get_triggers_max_leq
from nucs.propagators.min_eq_propagator import compute_domains_min_eq, get_complexity_min_eq, get_triggers_min_eq
from nucs.propagators.min_geq_propagator import compute_domains_min_geq, get_complexity_min_geq, get_triggers_min_geq
from nucs.propagators.relation_propagator import (
compute_domains_relation,
get_complexity_relation,
get_triggers_relation,
)

# The ordinals of the algorithms for all propagators (sorted by alphabetical ordering).
(
Expand Down Expand Up @@ -60,6 +97,24 @@
get_triggers_relation,
]

GET_COMPLEXITY_FCTS = [
get_complexity_affine_eq,
get_complexity_affine_geq,
get_complexity_affine_leq,
get_complexity_alldifferent,
get_complexity_count_eq,
get_complexity_dummy,
get_complexity_element_liv,
get_complexity_element_lic,
get_complexity_exactly_eq,
get_complexity_lexicographic_leq,
get_complexity_max_eq,
get_complexity_max_leq,
get_complexity_min_eq,
get_complexity_min_geq,
get_complexity_relation,
]


COMPUTE_DOMAINS_FCTS = [
compute_domains_affine_eq,
Expand Down
4 changes: 4 additions & 0 deletions nucs/propagators/relation_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from nucs.numpy import new_triggers


def get_complexity_relation(n: int, data: NDArray) -> float:
return 3 * len(data)


def get_triggers_relation(n: int, data: NDArray) -> NDArray:
"""
This propagator is triggered whenever there is a change in the domain of a variable.
Expand Down

0 comments on commit cb1ea8c

Please sign in to comment.