From 99af660e1d6f68b5d3b3ee1fc2c3909f481066dc Mon Sep 17 00:00:00 2001 From: Yan Georget Date: Fri, 20 Dec 2024 14:22:53 +0100 Subject: [PATCH] clean up --- nucs/examples/tsp/__main__.py | 1 + nucs/examples/tsp/tsp_problem.py | 35 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/nucs/examples/tsp/__main__.py b/nucs/examples/tsp/__main__.py index 519664e..5b17fe8 100644 --- a/nucs/examples/tsp/__main__.py +++ b/nucs/examples/tsp/__main__.py @@ -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 diff --git a/nucs/examples/tsp/tsp_problem.py b/nucs/examples/tsp/tsp_problem.py index 04b97a9..b388ef8 100644 --- a/nucs/examples/tsp/tsp_problem.py +++ b/nucs/examples/tsp/tsp_problem.py @@ -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))