Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Georget committed Dec 20, 2024
1 parent 22a248c commit 99af660
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
1 change: 1 addition & 0 deletions nucs/examples/tsp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.solvers.consistency_algorithms import CONSISTENCY_ALG_BC, CONSISTENCY_ALG_SHAVING
from nucs.solvers.multiprocessing_solver import MultiprocessingSolver
from rich import print

# Run with the following command (the second run is much faster because the code has been compiled):
# NUMBA_CACHE_DIR=.numba/cache python -m nucs.examples.alpha
Expand Down
35 changes: 17 additions & 18 deletions nucs/examples/tsp/tsp_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,29 @@
class TSPProblem(CircuitProblem):
""" """

def __init__(self, cost_rows: List[List[int]]) -> None:
def __init__(self, costs: List[List[int]]) -> None:
"""
Inits the problem.
:param cost_rows: the costs between vertices
:param costs: the costs between vertices as a list of lists of integers
"""
n = len(cost_rows)
n = len(costs)
super().__init__(n)
max_costs = [max(cost_row) for cost_row in cost_rows]
min_costs = [min([cost for cost in cost_row if cost > 0]) for cost_row in cost_rows]
self.next_costs = self.add_variables([(min_costs[i], max_costs[i]) for i in range(n)])
self.prev_costs = self.add_variables([(min_costs[i], max_costs[i]) for i in range(n)])
max_costs = [max(cost_row) for cost_row in costs]
min_costs = [min([cost for cost in cost_row if cost > 0]) for cost_row in costs]
self.succ_costs = self.add_variables([(min_costs[i], max_costs[i]) for i in range(n)])
self.pred_costs = self.add_variables([(min_costs[i], max_costs[i]) for i in range(n)])
self.total_cost = self.add_variable((sum(min_costs), sum(max_costs))) # the total cost
for i in range(n):
self.add_propagator(([i, self.next_costs + i], ALG_ELEMENT_IV, cost_rows[i]))
self.add_propagator(([n + i, self.prev_costs + i], ALG_ELEMENT_IV, cost_rows[i]))
self.add_propagators([([i, self.succ_costs + i], ALG_ELEMENT_IV, costs[i]) for i in range(n)])
self.add_propagators([([n + i, self.pred_costs + i], ALG_ELEMENT_IV, costs[i]) for i in range(n)])
self.add_propagator(
(list(range(self.next_costs, self.next_costs + n)) + [self.total_cost], ALG_AFFINE_EQ, [1] * n + [-1, 0])
(list(range(self.succ_costs, self.succ_costs + n)) + [self.total_cost], ALG_AFFINE_EQ, [1] * n + [-1, 0])
)
self.add_propagator(
(list(range(self.prev_costs, self.prev_costs + n)) + [self.total_cost], ALG_AFFINE_EQ, [1] * n + [-1, 0])
(list(range(self.pred_costs, self.pred_costs + n)) + [self.total_cost], ALG_AFFINE_EQ, [1] * n + [-1, 0])
)
total_cost_prop_idx = register_propagator(
get_triggers_total_cost, get_complexity_total_cost, compute_domains_total_cost
)
costs = [cost for cost_row in cost_rows for cost in cost_row]
self.add_propagator((list(range(n)) + [self.total_cost], total_cost_prop_idx, costs))
self.add_propagator((list(range(n, 2 * n)) + [self.total_cost], total_cost_prop_idx, costs))
# total_cost_prop_idx = register_propagator(
# get_triggers_total_cost, get_complexity_total_cost, compute_domains_total_cost
# )
# costs = [cost for cost_row in costs for cost in cost_row]
# self.add_propagator((list(range(n)) + [self.total_cost], total_cost_prop_idx, costs))
# self.add_propagator((list(range(n, 2 * n)) + [self.total_cost], total_cost_prop_idx, costs))

0 comments on commit 99af660

Please sign in to comment.