Skip to content

Commit

Permalink
LOGIC Implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
devroopsaha744 authored Sep 8, 2024
1 parent 6bcb170 commit d847c7c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 61 deletions.
31 changes: 20 additions & 11 deletions logic/clook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ def calculate_metrics(path, start):
def CLOOK(requests, start, end, initial_position):
requests = sorted(requests)
path = []

if initial_position <= start:
path = [start] + requests
# Move upwards to handle all requests
path = [initial_position] + requests
elif initial_position >= end:
path = requests + [end]
# Handle all requests from the top
path = [initial_position] + requests
else:
path = [r for r in requests if r >= initial_position] + [r for r in requests if r < initial_position]
path = [initial_position] + path
# Split requests into those above and below the initial position
above = [r for r in requests if r >= initial_position]
below = [r for r in requests if r < initial_position]
# Move upwards, then jump to the lowest request and continue upwards
path = [initial_position] + above + below

total_distance, avg_seek_time = calculate_metrics(path, initial_position)
return path, total_distance, avg_seek_time



if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
initial_position = 5
start = 0
end = 19
end = 10

path, total_distance, avg_seek_time = CLOOK(requests, start, end, initial_position)
print("CLOOK Path:", path)
print("Total Distance:", total_distance)
print("Average Seek Time:", avg_seek_time)

#Results
'''
CLOOK Path: [5, 6, 8, 10, 12, 15, 18, 2, 3]
Total Distance: 30
Average Seek Time: 3.3333333333333335
'''
'''
CLOOK Path: [5, 6, 7, 9, 1, 3, 4]
Total Distance: 15
Average Seek Time: 2.142857142857143
'''


14 changes: 7 additions & 7 deletions logic/cscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ def CSCAN(requests, start, end, initial_position):
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
initial_position = 5
start = 0
end = 19

end = 10
'''
path, total_distance, avg_seek_time = CSCAN(requests, start, end, initial_position)
print("CSCAN Path:", path)
print("Total Distance:", total_distance)
print("Average Seek Time:", avg_seek_time)
#Results
'''
CSCAN Path: [5, 6, 8, 10, 12, 15, 18, 19, 0, 2, 3]
Total Distance: 36
Average Seek Time: 3.272727272727273
CSCAN Path: [5, 6, 7, 9, 10, 0, 1, 3, 4]
Total Distance: 19
Average Seek Time: 2.111111111111111
'''
33 changes: 4 additions & 29 deletions logic/fcfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def FCFS(requests, initial_position):
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
initial_position = 5

path, total_distance, avg_seek_time = FCFS(requests, initial_position)
Expand All @@ -19,32 +19,7 @@ def FCFS(requests, initial_position):

#Results
'''
FCFS Path: [5, 3, 15, 6, 18, 2, 10, 8, 12]
Total Distance: 65
Average Seek Time: 7.222222222222222
FCFS Path: [5, 1, 9, 4, 7, 3, 6]
Total Distance: 27
Average Seek Time: 3.857142857142857
'''
def calculate_metrics(path, start):
total_distance = sum(abs(path[i] - path[i - 1]) for i in range(1, len(path)))
avg_seek_time = total_distance / len(path)
return total_distance, avg_seek_time

def FCFS(requests, initial_position):
path = [initial_position] + requests
total_distance, avg_seek_time = calculate_metrics(path, initial_position)
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
initial_position = 5

path, total_distance, avg_seek_time = FCFS(requests, initial_position)
print("FCFS Path:", path)
print("Total Distance:", total_distance)
print("Average Seek Time:", avg_seek_time)

#Results
'''
FCFS Path: [5, 3, 15, 6, 18, 2, 10, 8, 12]
Total Distance: 65
Average Seek Time: 7.222222222222222
'''
10 changes: 5 additions & 5 deletions logic/look.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def LOOK(requests, start, end, initial_position):
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
initial_position = 5
start = 0
end = 19
end = 10

path, total_distance, avg_seek_time = LOOK(requests, start, end, initial_position)
print("LOOK Path:", path)
Expand All @@ -29,8 +29,8 @@ def LOOK(requests, start, end, initial_position):

#Results
'''
LOOK Path: [5, 6, 8, 10, 12, 15, 18, 3, 2]
Total Distance: 29
Average Seek Time: 3.2222222222222223
LOOK Path: [5, 6, 7, 9, 4, 3, 1]
Total Distance: 12
Average Seek Time: 1.7142857142857142
'''
10 changes: 5 additions & 5 deletions logic/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def SCAN(requests, start, end, initial_position):
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
start = 0
end = 19
end = 10
initial_position = 5

path, total_distance, avg_seek_time = SCAN(requests, start, end, initial_position)
Expand All @@ -29,7 +29,7 @@ def SCAN(requests, start, end, initial_position):

#Results
'''
SCAN Path: [5, 6, 8, 10, 12, 15, 18, 19, 3, 2]
Total Distance: 31
Average Seek Time: 3.1
SCAN Path: [5, 6, 7, 9, 10, 4, 3, 1]
Total Distance: 14
Average Seek Time: 1.75
'''
8 changes: 4 additions & 4 deletions logic/sstf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def SSTF(requests, initial_position):
return path, total_distance, avg_seek_time

if __name__ == "__main__":
requests = [3, 15, 6, 18, 2, 10, 8, 12]
requests = [1, 9, 4, 7, 3, 6]
initial_position = 5

path, total_distance, avg_seek_time = SSTF(requests, initial_position)
Expand All @@ -23,9 +23,9 @@ def SSTF(requests, initial_position):

#Results
'''
SSTF Path: [5, 6, 8, 10, 12, 15, 18, 3, 2]
Total Distance: 29
Average Seek Time: 3.2222222222222223
SSTF Path: [5, 4, 3, 1, 6, 7, 9]
Total Distance: 12
Average Seek Time: 1.7142857142857142
'''


0 comments on commit d847c7c

Please sign in to comment.