-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yan Georget
committed
Dec 16, 2024
1 parent
16cdbfa
commit 076cd81
Showing
15 changed files
with
295 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
############################################################################### | ||
# __ _ _____ _____ | ||
# | \ | | / ____| / ____| | ||
# | \| | _ _ | | | (___ | ||
# | . ` | | | | | | | \___ \ | ||
# | |\ | | |_| | | |____ ____) | | ||
# |_| \_| \__,_| \_____| |_____/ | ||
# | ||
# Fast constraint solving in Python - https://github.com/yangeorget/nucs | ||
# | ||
# Copyright 2024 - Yan Georget | ||
############################################################################### | ||
import sys | ||
|
||
from numba import njit # type: ignore | ||
from numpy.typing import NDArray | ||
|
||
from nucs.constants import MAX, MIN | ||
from nucs.heuristics.max_regret_var_heuristic import regret | ||
|
||
|
||
@njit(cache=True) | ||
def tsp_var_heuristic( | ||
decision_domains: NDArray, shr_domains_stack: NDArray, stacks_top: NDArray, params: NDArray | ||
) -> int: | ||
""" | ||
:param decision_domains: the indices of a subset of the shared domains | ||
:param shr_domains_stack: the stack of shared domains | ||
:param stacks_top: the index of the top of the stacks as a Numpy array | ||
:param params: a two-dimensional (first dimension correspond to variables, second to values) costs array | ||
:return: the index of the shared domain | ||
""" | ||
best_score = -sys.maxsize | ||
best_idx = -1 | ||
top = stacks_top[0] | ||
for dom_idx in decision_domains: | ||
shr_domain = shr_domains_stack[top, dom_idx] | ||
if 0 < shr_domain[MAX] - shr_domain[MIN]: | ||
score = compute_score(shr_domain, dom_idx, params) | ||
if best_score < score: | ||
best_idx = dom_idx | ||
best_score = score | ||
return best_idx | ||
|
||
|
||
@njit(cache=True) | ||
def compute_score(shr_domain: NDArray, dom_idx: int, params: NDArray) -> int: | ||
""" | ||
Minimize [min(5, size(X)), -regret(X)] for lexicographic order. | ||
""" | ||
size = min(5, 1 + shr_domain[MAX] - shr_domain[MIN]) | ||
return -size * 1024 + regret(shr_domain, dom_idx, params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
############################################################################### | ||
# __ _ _____ _____ | ||
# | \ | | / ____| / ____| | ||
# | \| | _ _ | | | (___ | ||
# | . ` | | | | | | | \___ \ | ||
# | |\ | | |_| | | |____ ____) | | ||
# |_| \_| \__,_| \_____| |_____/ | ||
# | ||
# Fast constraint solving in Python - https://github.com/yangeorget/nucs | ||
# | ||
# Copyright 2024 - Yan Georget | ||
############################################################################### | ||
from nucs.problems.problem import Problem | ||
from nucs.propagators.propagators import ALG_ALLDIFFERENT, ALG_PERMUTATION_AUX | ||
|
||
|
||
class PermutationProblem(Problem): | ||
""" | ||
A model for permutation. | ||
""" | ||
|
||
def __init__(self, n: int): | ||
""" | ||
Inits the permutation problem. | ||
:param n: the number variables/values | ||
""" | ||
self.n = n | ||
shr_domains = [(0, n - 1)] * 2 * n | ||
super().__init__(shr_domains) | ||
for i in range(n): | ||
self.add_propagator((list(range(n)) + [n + i], ALG_PERMUTATION_AUX, [i])) | ||
self.add_propagator((list(range(n, 2 * n)) + [i], ALG_PERMUTATION_AUX, [i])) | ||
self.add_propagator((list(range(n)), ALG_ALLDIFFERENT, [])) | ||
self.add_propagator((list(range(n, 2 * n)), ALG_ALLDIFFERENT, [])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.