-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_ordering.py
More file actions
166 lines (141 loc) · 5.11 KB
/
tool_ordering.py
File metadata and controls
166 lines (141 loc) · 5.11 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import itertools
import random
import math
from collections import Counter
from typing import Any, Dict, List, Tuple
Edge = Tuple[Any, Any, float]
def circ_dist(i: int, j: int, M: int) -> int:
"""Circular distance between i and j on a ring of M slots."""
d = abs(i - j)
return min(d, M - d)
def cost_mapping(
mapping: Dict[Any, int],
edges: List[Edge],
M: int
) -> float:
"""
Given an injective map node→position, compute total weighted
circular distance cost.
"""
total = 0.0
for u, v, w in edges:
pi, pj = mapping[u], mapping[v]
total += w * circ_dist(pi, pj, M)
return total
def brute_force_circular_positions(
nodes: List[Any],
edges: List[Edge],
M: int
) -> Tuple[Dict[Any,int], float]:
"""
Exact minimum: try every choice of k positions out of M,
and every assignment of nodes to them.
Warning: O( (M choose k) * k! ) → only for k<=8, M small.
"""
k = len(nodes)
best_map = {}
best_cost = float('inf')
for pos_subset in itertools.combinations(range(M), k):
# for rotational symmetry you could fix nodes[0] at pos_subset[0]
for perm in itertools.permutations(nodes):
mapping = {node: pos for node, pos in zip(perm, pos_subset)}
c = cost_mapping(mapping, edges, M)
if c < best_cost:
best_cost, best_map = c, mapping.copy()
return best_map, best_cost
def simulated_annealing_circular_positions(
nodes: List[Any],
edges: List[Edge],
M: int,
iterations: int = 100_000,
t0: float = 1.0,
alpha: float = 0.9999
) -> Tuple[Dict[Any,int], float]:
"""
Heuristic: simulated annealing over injective node→position maps.
Neighbors either swap two nodes’ positions or move one node into
a free slot.
"""
# initial random injective mapping
all_positions = list(range(M))
chosen = random.sample(all_positions, len(nodes))
mapping = {node: pos for node, pos in zip(nodes, chosen)}
best_map = mapping.copy()
current_cost = cost_mapping(mapping, edges, M)
best_cost = current_cost
T = t0
for _ in range(iterations):
# pick a neighbor
if random.random() < 0.5:
# swap positions of two random nodes
u, v = random.sample(nodes, 2)
mapping[u], mapping[v] = mapping[v], mapping[u]
new_cost = cost_mapping(mapping, edges, M)
Δ = new_cost - current_cost
if Δ < 0 or random.random() < math.exp(-Δ / T):
current_cost = new_cost
if new_cost < best_cost:
best_cost, best_map = new_cost, mapping.copy()
else:
# revert swap
mapping[u], mapping[v] = mapping[v], mapping[u]
else:
# move one node into a random free slot
u = random.choice(nodes)
occupied = set(mapping.values())
free = [p for p in all_positions if p not in occupied]
if not free:
continue
old = mapping[u]
newp = random.choice(free)
mapping[u] = newp
new_cost = cost_mapping(mapping, edges, M)
Δ = new_cost - current_cost
if Δ < 0 or random.random() < math.exp(-Δ / T):
current_cost = new_cost
if new_cost < best_cost:
best_cost, best_map = new_cost, mapping.copy()
else:
mapping[u] = old
T *= alpha
return best_map, best_cost
def make_nodes_edges(sequence):
seq = sequence
nodes = []
seen = set()
for x in seq:
if x not in seen:
seen.add(x)
nodes.append(x)
rot = seq[1:] + seq[:1]
pairs = list(zip(seq, rot))
print("pairs: ", pairs)
canon = [(u,v) if u<=v else (v,u) for u,v in pairs]
counts = Counter(canon)
edges = [(u, v, w) for (u, v), w in counts.items()]
return nodes, edges
def sa_solve(tool_sequence, M, verbose=True):
nodes, edges = make_nodes_edges(tool_sequence)
# exact (if len(nodes)<=8):
if len(nodes) <= 8:
bf_map, bf_cost = brute_force_circular_positions(nodes, edges, M)
if verbose:
print("EXACT mapping:", bf_map, "cost:", bf_cost, "edges: ", edges)
sa_map, sa_cost = simulated_annealing_circular_positions(nodes, edges, M)
# derive a circular order by sorting nodes by their assigned position:
circ_order = sorted(sa_map.items(), key=lambda kv: kv[1])
# print("Approx mapping:", sa_map)
sa_tool_order = [node for node, _ in circ_order]
if verbose:
print("Cost :", sa_cost)
print("Order around ring:", sa_tool_order)
return sa_cost, sa_tool_order
# --- example usage ---
if __name__ == "__main__":
#######################################
# Edit this to match your tool numbers
#######################################
# TOOL_SQUENCE = [1, 13, 1, 35, 17, 33, 31, 29, 34, 1, 37, 13, 30, 8, 1, 13, 8, 15]
TOOL_SQUENCE = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 15, 16]
M = 28 # Number of pockets you have
sa_solve(TOOL_SQUENCE, M)