-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeuristicSearch.py
295 lines (240 loc) · 9.74 KB
/
HeuristicSearch.py
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
from typing import List, Tuple, Dict, Set, Optional
from collections import defaultdict
import itertools
import time
from scipy.optimize import linprog
class HeuristicSearch:
"""Implementation of Heuristic Search Algorithms"""
@staticmethod
def simulated_annealing(initial_state: List[int],
cost_func: callable,
neighbor_func: callable,
temp_schedule: callable,
max_iter: int = 1000) -> Tuple[List[int], float]:
"""
Simulated Annealing optimization
Args:
initial_state: Starting solution
cost_func: Function to evaluate solution cost
neighbor_func: Function to generate neighbor solution
temp_schedule: Temperature scheduling function
max_iter: Maximum iterations
"""
current_state = initial_state.copy()
current_cost = cost_func(current_state)
best_state = current_state.copy()
best_cost = current_cost
for i in range(max_iter):
T = temp_schedule(i)
if T <= 0:
break
neighbor = neighbor_func(current_state)
neighbor_cost = cost_func(neighbor)
delta_E = neighbor_cost - current_cost
if delta_E < 0 or np.random.random() < np.exp(-delta_E / T):
current_state = neighbor
current_cost = neighbor_cost
if current_cost < best_cost:
best_state = current_state.copy()
best_cost = current_cost
return best_state, best_cost
@staticmethod
def hill_climbing(initial_state: List[int],
cost_func: callable,
neighbor_func: callable,
max_iter: int = 1000) -> Tuple[List[int], float]:
"""Hill Climbing with random restarts"""
def single_climb(state):
current_state = state.copy()
current_cost = cost_func(current_state)
for _ in range(max_iter):
neighbor = neighbor_func(current_state)
neighbor_cost = cost_func(neighbor)
if neighbor_cost >= current_cost:
break
current_state = neighbor
current_cost = neighbor_cost
return current_state, current_cost
# Multiple random restarts
best_state, best_cost = single_climb(initial_state)
for _ in range(5): # Number of restarts
new_state = [np.random.randint(0, 100) for _ in range(len(initial_state))]
state, cost = single_climb(new_state)
if cost < best_cost:
best_state, best_cost = state, cost
return best_state, best_cost
class Backtracking:
"""Implementation of Backtracking Algorithms"""
@staticmethod
def n_queens(n: int) -> List[List[int]]:
"""
Solve N-Queens problem using backtracking
Returns all valid placements
"""
def is_safe(board: List[int], row: int, col: int) -> bool:
# Check previous queens placements
for prev_row in range(row):
prev_col = board[prev_row]
if (prev_col == col or
abs(prev_col - col) == abs(prev_row - row)):
return False
return True
def solve(board: List[int], row: int) -> None:
if row == n:
solutions.append(board[:])
return
for col in range(n):
if is_safe(board, row, col):
board[row] = col
solve(board, row + 1)
solutions = []
initial_board = [-1] * n
solve(initial_board, 0)
return solutions
@staticmethod
def subset_sum(numbers: List[int], target: int) -> List[List[int]]:
"""Find all subsets that sum to target"""
def backtrack(start: int, target: int, current: List[int]) -> None:
if target == 0:
solutions.append(current[:])
return
for i in range(start, len(numbers)):
if target - numbers[i] >= 0:
current.append(numbers[i])
backtrack(i + 1, target - numbers[i], current)
current.pop()
solutions = []
backtrack(0, target, [])
return solutions
class BruteForce:
"""Implementation of Brute Force Algorithms"""
@staticmethod
def traveling_salesman(distances: List[List[float]]) -> Tuple[List[int], float]:
"""
Solve TSP using brute force
Returns optimal tour and distance
"""
n = len(distances)
cities = list(range(n))
min_distance = float('inf')
best_tour = None
for tour in itertools.permutations(cities[1:]):
tour = (0,) + tour # Start from city 0
distance = 0
for i in range(n-1):
distance += distances[tour[i]][tour[i+1]]
distance += distances[tour[-1]][tour[0]] # Return to start
if distance < min_distance:
min_distance = distance
best_tour = list(tour)
return best_tour, min_distance
@staticmethod
def string_matching(text: str, pattern: str) -> List[int]:
"""Find all occurrences of pattern in text"""
n, m = len(text), len(pattern)
positions = []
for i in range(n - m + 1):
if text[i:i+m] == pattern:
positions.append(i)
return positions
class LinearProgramming:
"""Implementation of Linear Programming Solutions"""
@staticmethod
def solve_production_planning(profits: List[float],
constraints: List[List[float]],
bounds: List[float]) -> Tuple[List[float], float]:
"""
Solve production planning LP problem
Args:
profits: Profit per unit for each product
constraints: Resource constraints matrix
bounds: Resource availability bounds
"""
# Minimize negative profits (for maximization)
c = [-p for p in profits]
# Solve using scipy's linprog
result = linprog(c, A_ub=constraints, b_ub=bounds, method='simplex')
if result.success:
return result.x, -result.fun
return None, None
@staticmethod
def solve_transportation(supply: List[float],
demand: List[float],
costs: List[List[float]]) -> Tuple[List[List[float]], float]:
"""
Solve transportation problem
Args:
supply: Supply at each source
demand: Demand at each destination
costs: Transportation costs matrix
"""
m, n = len(supply), len(demand)
# Prepare for scipy.linprog format
num_vars = m * n
c = [cost for row in costs for cost in row]
# Supply constraints
A_eq = []
b_eq = []
# Each source constraint
for i in range(m):
row = [0] * num_vars
for j in range(n):
row[i*n + j] = 1
A_eq.append(row)
b_eq.append(supply[i])
# Each destination constraint
for j in range(n):
row = [0] * num_vars
for i in range(m):
row[i*n + j] = 1
A_eq.append(row)
b_eq.append(demand[j])
# Solve
result = linprog(c, A_eq=A_eq, b_eq=b_eq, method='simplex')
if result.success:
# Reshape solution into matrix
solution = np.reshape(result.x, (m, n))
return solution, result.fun
return None, None
def example_usage():
"""Demonstrate usage of different algorithmic approaches"""
# Heuristic Search Example
def cost_func(x): return sum(x) # Minimize sum
def neighbor_func(x):
x = x.copy()
i = np.random.randint(0, len(x))
x[i] += np.random.randint(-10, 11)
return x
def temp_schedule(i): return 100 * (0.95 ** i)
initial = [50] * 10
result, cost = HeuristicSearch.simulated_annealing(
initial, cost_func, neighbor_func, temp_schedule)
print("Simulated Annealing Result:", result, "Cost:", cost)
# Backtracking Example
queens_solutions = Backtracking.n_queens(4)
print("4-Queens Solutions:", queens_solutions)
numbers = [1, 2, 3, 4, 5]
subset_solutions = Backtracking.subset_sum(numbers, 7)
print("Subset Sum Solutions for target 7:", subset_solutions)
# Brute Force Example
distances = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]
tour, distance = BruteForce.traveling_salesman(distances)
print("TSP Solution:", tour, "Distance:", distance)
# Linear Programming Example
profits = [40, 30] # Profit per unit for two products
constraints = [
[2, 1], # Resource 1 usage
[1, 3] # Resource 2 usage
]
bounds = [100, 90] # Resource availability
production, total_profit = LinearProgramming.solve_production_planning(
profits, constraints, bounds)
print("Optimal Production:", production, "Total Profit:", total_profit)
if __name__ == "__main__":
example_usage()