-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp8_DFS_Modifed.py
78 lines (62 loc) · 2.43 KB
/
App8_DFS_Modifed.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
# this is a modified version of DFS
# but it is not working properly (incomplete)
import random
def euclidean_distance(x1, y1, x2, y2):
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
def dfs_randomized(input_file, output_file):
# Read input from the file
with open(input_file, 'r') as file:
lines = file.readlines()
# Parse the input
Tmax, P = map(int, lines[0].split())
points = []
for line in lines[1:]:
x, y, score = map(float, line.split())
points.append((x, y, score))
# Initialize variables
n = len(points)
visited = [False] * n
path = [0] # Starting from the first point (starting point)
current_time = 0
current_score = 0
best_path = None
best_score = 0
# Randomized DFS
def dfs(node):
nonlocal current_time, current_score, best_path, best_score
# Update current time and score
current_time += Tmax
current_score += points[node][2]
visited[node] = True
# Check if the current score is the best so far
if current_score > best_score:
best_path = path.copy()
best_score = current_score
# Randomly shuffle the unvisited neighbors
neighbors = [i for i in range(1, n) if not visited[i]]
random.shuffle(neighbors)
# Explore the neighbors
for neighbor in neighbors:
distance = euclidean_distance(points[node][0], points[node][1], points[neighbor][0], points[neighbor][1])
# Check if the remaining time is sufficient and the profit is good
if current_time + distance <= Tmax and points[neighbor][2] / distance > 1.5:
path.append(neighbor)
dfs(neighbor)
path.pop()
# Undo the changes
current_time -= Tmax
current_score -= points[node][2]
visited[node] = False
# Start the DFS from the first point (starting point)
dfs(0)
# Write the output to the file
with open(output_file, 'w') as file:
if best_path is not None:
file.write(f"Path: {', '.join(map(str, best_path))}\n")
file.write(f"Total Profit: {best_score}")
else:
file.write("No valid path found.")
# Usage example
input_file = 'Dataset/set_64_1_80.txt'
output_file = 'Results/set_64_1_80_App8.txt'
dfs_randomized(input_file, output_file)