forked from microsoft/HeurAgenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.template.py
More file actions
65 lines (50 loc) · 2.08 KB
/
components.template.py
File metadata and controls
65 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from src.problems.base.components import BaseSolution, BaseOperator
class Solution(BaseSolution):
"""The solution of xxx."""
def __init__(self, **kwargs):
# Init the solution.
pass
def __str__(self) -> str:
# str to print solution.
pass
class Operator1(BaseOperator):
def __init__(self, **kwargs):
# Init the operator, but not run.
pass
def run(self, solution: Solution) -> Solution:
# Run the operator and return a new solution.
pass
class Operator2(BaseOperator):
def __init__(self, **kwargs):
# Init the operator, but not run.
pass
def run(self, solution: Solution) -> Solution:
# Run the operator and return a new solution.
pass
# This is an example from TSP.
# class Solution(BaseSolution):
# def __init__(self, tour: list[int]):
# self.tour = tour
# def __str__(self) -> str:
# return "tour: " + "->".join(map(str, self.tour + [self.tour[0]]))
# class AppendOperator(BaseOperator):
# """Append the node at the end of the solution."""
# def __init__(self, node: int):
# self.node = node
# def run(self, solution: Solution) -> Solution:
# new_tour = solution.tour + [self.node]
# return Solution(new_tour)
# class SwapOperator(BaseOperator):
# """Swap two nodes in the solution. swap_node_pairs is a list of tuples, each containing the two nodes to swap."""
# def __init__(self, swap_node_pairs: list[tuple[int, int]]):
# self.swap_node_pairs = swap_node_pairs
# def run(self, solution: Solution) -> Solution:
# node_to_index = {node: index for index, node in enumerate(solution.tour)}
# new_tour = solution.tour.copy()
# for node_a, node_b in self.swap_node_pairs:
# index_a = node_to_index.get(node_a)
# index_b = node_to_index.get(node_b)
# assert index_a is not None
# assert index_b is not None
# new_tour[index_a], new_tour[index_b] = new_tour[index_b], new_tour[index_a]
# return Solution(new_tour)