Skip to content

cds2.combinatorial

Discrete optimization: TSP heuristics, knapsack, assignment, LCS.

Classic discrete optimization: TSP heuristics, 0/1 knapsack, assignment and LCS.

TourResult dataclass

Closed tour (first city repeated at the end) with its total cost.

Source code in src/cds2/combinatorial.py
30
31
32
33
34
35
36
@dataclass(frozen=True)
class TourResult:
    """Closed tour (first city repeated at the end) with its total cost."""

    tour: IntArray
    cost: float
    improved_from: float | None = None

KnapsackResult dataclass

Best 0/1 knapsack packing with the ascending indices of chosen items.

Source code in src/cds2/combinatorial.py
39
40
41
42
43
44
45
@dataclass(frozen=True)
class KnapsackResult:
    """Best 0/1 knapsack packing with the ascending indices of chosen items."""

    total_value: float
    chosen_indices: IntArray
    total_weight: float

AssignmentResult dataclass

Optimal assignment cost together with the row-to-column mapping.

Source code in src/cds2/combinatorial.py
48
49
50
51
52
53
@dataclass(frozen=True)
class AssignmentResult:
    """Optimal assignment cost together with the row-to-column mapping."""

    cost: float
    row_to_col: IntArray

nearest_neighbor_tsp(distance_matrix, start=0)

Greedy nearest-neighbour heuristic for the travelling-salesman problem.

Source code in src/cds2/combinatorial.py
 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
def nearest_neighbor_tsp(
    distance_matrix: FloatArray | Sequence[Sequence[float]],
    start: int = 0,
) -> TourResult:
    """Greedy nearest-neighbour heuristic for the travelling-salesman problem."""
    matrix = _as_distance_matrix(distance_matrix)
    city_count = matrix.shape[0]
    if not 0 <= start < city_count:
        msg = "start city out of range"
        raise ValueError(msg)

    visited = np.zeros(city_count, dtype=bool)
    visited[start] = True
    order = [start]
    current = start
    total = 0.0
    for _ in range(city_count - 1):
        best_city = -1
        best_distance = math.inf
        for candidate in range(city_count):
            if visited[candidate]:
                continue
            candidate_distance = float(matrix[current, candidate])
            if candidate_distance < best_distance:
                best_distance = candidate_distance
                best_city = candidate
        visited[best_city] = True
        order.append(best_city)
        total += best_distance
        current = best_city
    total += float(matrix[order[-1], start])
    order.append(start)

    tour: IntArray = np.asarray(order, dtype=np.int64)
    return TourResult(tour=tour, cost=float(total))

two_opt(tour, distance_matrix)

Improve a closed tour with 2-opt edge exchanges until no swap helps.

Source code in src/cds2/combinatorial.py
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
def two_opt(
    tour: IntArray | Sequence[int],
    distance_matrix: FloatArray | Sequence[Sequence[float]],
) -> TourResult:
    """Improve a closed tour with 2-opt edge exchanges until no swap helps."""
    matrix = _as_distance_matrix(distance_matrix)
    city_count = matrix.shape[0]
    nodes: IntArray = np.asarray(tour, dtype=np.int64)
    if nodes[0] != nodes[-1]:
        msg = "tour must start and end at the same city"
        raise ValueError(msg)
    if int(nodes.min()) < 0 or int(nodes.max()) >= city_count:
        msg = "tour contains city indices outside the distance matrix"
        raise ValueError(msg)

    initial_cost = _tour_cost(nodes, matrix)
    best_cost = initial_cost
    improved = True
    while improved:
        improved = False
        for i in range(1, city_count - 1):
            for j in range(i + 1, city_count):
                removed = float(matrix[int(nodes[i - 1]), int(nodes[i])]) + float(
                    matrix[int(nodes[j]), int(nodes[j + 1])]
                )
                added = float(matrix[int(nodes[i - 1]), int(nodes[j])]) + float(
                    matrix[int(nodes[i]), int(nodes[j + 1])]
                )
                if added - removed < -_EPSILON:
                    nodes[i : j + 1] = nodes[i : j + 1][::-1]
                    best_cost += added - removed
                    improved = True
                    break
            if improved:
                break

    return TourResult(
        tour=nodes,
        cost=best_cost,
        improved_from=initial_cost if best_cost < initial_cost else None,
    )

knapsack_01(weights, values, capacity)

Exact 0/1 knapsack via dynamic programming over integer capacity units.

Source code in src/cds2/combinatorial.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def knapsack_01(
    weights: Sequence[float] | FloatArray,
    values: Sequence[float] | FloatArray,
    capacity: float,
) -> KnapsackResult:
    """Exact 0/1 knapsack via dynamic programming over integer capacity units."""
    weight_array = np.asarray(weights, dtype=float)
    value_array = np.asarray(values, dtype=float)
    if (
        weight_array.ndim != 1
        or value_array.ndim != 1
        or weight_array.size != value_array.size
        or weight_array.size == 0
    ):
        msg = "weights and values must be equal-length non-empty"
        raise ValueError(msg)
    capacity_value = float(capacity)
    weight_values = [float(item) for item in weight_array.tolist()]
    value_values = [float(item) for item in value_array.tolist()]
    if not all(item.is_integer() for item in (*weight_values, capacity_value)):
        msg = "weights and capacity must be integer-valued for exact DP"
        raise ValueError(msg)

    capacity_units = int(round(capacity_value))
    if capacity_units <= 0:
        msg = "capacity must be positive"
        raise ValueError(msg)

    item_count = weight_array.size
    integer_weights = [int(round(item)) for item in weight_values]
    table = np.zeros((item_count + 1, capacity_units + 1), dtype=float)
    for item in range(1, item_count + 1):
        item_weight = integer_weights[item - 1]
        item_value = value_values[item - 1]
        for remaining in range(capacity_units + 1):
            best = float(table[item - 1, remaining])
            if item_weight <= remaining:
                candidate = float(table[item - 1, remaining - item_weight]) + item_value
                if candidate > best:
                    best = candidate
            table[item, remaining] = best

    chosen: list[int] = []
    remaining = capacity_units
    for item in range(item_count, 0, -1):
        if float(table[item, remaining]) != float(table[item - 1, remaining]):
            chosen.append(item - 1)
            remaining -= integer_weights[item - 1]
    chosen.reverse()

    return KnapsackResult(
        total_value=float(table[item_count, capacity_units]),
        chosen_indices=np.asarray(chosen, dtype=np.int64),
        total_weight=float(sum(weight_values[index] for index in chosen)),
    )

assign_min_cost(cost_matrix)

Minimum-cost bipartite assignment over a (possibly rectangular) matrix.

Source code in src/cds2/combinatorial.py
214
215
216
217
218
219
220
221
222
223
224
225
def assign_min_cost(
    cost_matrix: FloatArray | Sequence[Sequence[float]],
) -> AssignmentResult:
    """Minimum-cost bipartite assignment over a (possibly rectangular) matrix."""
    matrix: FloatArray = np.asarray(cost_matrix, dtype=float)
    if matrix.ndim != 2 or min(matrix.shape) < 1:
        msg = "cost matrix must be 2-D with at least one row and one column"
        raise ValueError(msg)
    rows, columns = linear_sum_assignment(matrix)
    mapping: IntArray = np.asarray(columns, dtype=np.int64)
    total = float(np.asarray(matrix[rows, columns], dtype=float).sum())
    return AssignmentResult(cost=total, row_to_col=mapping)

longest_common_subsequence(a, b)

One longest common subsequence of a and b via dynamic programming.

Source code in src/cds2/combinatorial.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def longest_common_subsequence(a: str, b: str) -> str:
    """One longest common subsequence of ``a`` and ``b`` via dynamic programming."""
    table = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
    for i in range(1, len(a) + 1):
        for j in range(1, len(b) + 1):
            if a[i - 1] == b[j - 1]:
                table[i][j] = table[i - 1][j - 1] + 1
            else:
                table[i][j] = max(table[i - 1][j], table[i][j - 1])

    characters: list[str] = []
    i, j = len(a), len(b)
    while i > 0 and j > 0:
        if a[i - 1] == b[j - 1]:
            characters.append(a[i - 1])
            i -= 1
            j -= 1
        elif table[i - 1][j] >= table[i][j - 1]:
            i -= 1
        else:
            j -= 1
    return "".join(reversed(characters))