Compare commits
7 Commits
f534521410
...
b63b5b08b6
Author | SHA1 | Date |
---|---|---|
coolneng | b63b5b08b6 | |
coolneng | d04d0becfe | |
coolneng | a81756e93b | |
coolneng | 04c92add44 | |
coolneng | f73e28fb8a | |
coolneng | 27df20f7d1 | |
coolneng | 6a3bdc44e3 |
|
@ -1,6 +1,8 @@
|
||||||
from preprocessing import parse_file
|
from preprocessing import parse_file
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame, Series
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
from random import seed, randint
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
|
||||||
def get_first_solution(n, data):
|
def get_first_solution(n, data):
|
||||||
|
@ -11,7 +13,7 @@ def get_first_solution(n, data):
|
||||||
distance_sum = distance_sum.append(
|
distance_sum = distance_sum.append(
|
||||||
{"point": element, "distance": distance}, ignore_index=True
|
{"point": element, "distance": distance}, ignore_index=True
|
||||||
)
|
)
|
||||||
furthest_index = distance_sum["distance"].idxmax()
|
furthest_index = distance_sum["distance"].astype(float).idxmax()
|
||||||
furthest_row = distance_sum.iloc[furthest_index]
|
furthest_row = distance_sum.iloc[furthest_index]
|
||||||
furthest_row["distance"] = 0
|
furthest_row["distance"] = 0
|
||||||
return furthest_row
|
return furthest_row
|
||||||
|
@ -25,11 +27,14 @@ def get_different_element(original, row):
|
||||||
|
|
||||||
def get_furthest_element(element, data):
|
def get_furthest_element(element, data):
|
||||||
element_df = data.query(f"source == {element} or destination == {element}")
|
element_df = data.query(f"source == {element} or destination == {element}")
|
||||||
furthest_index = element_df["distance"].idxmax()
|
furthest_index = element_df["distance"].astype(float).idxmax()
|
||||||
furthest_row = data.iloc[furthest_index]
|
furthest_row = data.iloc[furthest_index]
|
||||||
furthest_point = get_different_element(original=element, row=furthest_row)
|
furthest_point = get_different_element(original=element, row=furthest_row)
|
||||||
furthest_element = {"point": furthest_point, "distance": furthest_row["distance"]}
|
return Series(data={"point": furthest_point, "distance": furthest_row["distance"]})
|
||||||
return furthest_element, furthest_index
|
|
||||||
|
|
||||||
|
def remove_solution_dataset(data, solution):
|
||||||
|
return data.query(f"source != {solution} and destination != {solution}")
|
||||||
|
|
||||||
|
|
||||||
def greedy_algorithm(n, m, data):
|
def greedy_algorithm(n, m, data):
|
||||||
|
@ -37,29 +42,64 @@ def greedy_algorithm(n, m, data):
|
||||||
first_solution = get_first_solution(n, data)
|
first_solution = get_first_solution(n, data)
|
||||||
solutions = solutions.append(first_solution, ignore_index=True)
|
solutions = solutions.append(first_solution, ignore_index=True)
|
||||||
for _ in range(m):
|
for _ in range(m):
|
||||||
last_solution = solutions["point"].tail(n=1)
|
last_solution = int(solutions["point"].tail(n=1))
|
||||||
centroid, index = get_furthest_element(element=int(last_solution), data=data)
|
centroid = get_furthest_element(element=last_solution, data=data)
|
||||||
solutions = solutions.append(dict(centroid), ignore_index=True)
|
solutions = solutions.append(centroid, ignore_index=True)
|
||||||
data = data.drop(index)
|
data = remove_solution_dataset(data=data, solution=last_solution)
|
||||||
return solutions
|
return solutions
|
||||||
|
|
||||||
|
|
||||||
# NOTE In each step, switch to the element that gives the least amount
|
def get_pseudorandom_solution(n, data):
|
||||||
def local_search():
|
seed(42)
|
||||||
|
solution = data.iloc[randint(a=0, b=n)]
|
||||||
|
return Series(data={"point": solution["destination"], "distance": 0})
|
||||||
|
|
||||||
|
|
||||||
|
def local_search(n, m, data):
|
||||||
|
solutions = DataFrame(columns=["point", "distance"])
|
||||||
|
first_solution = get_pseudorandom_solution(n=n, data=data)
|
||||||
|
solutions = solutions.append(first_solution, ignore_index=True)
|
||||||
|
for _ in range(m):
|
||||||
pass
|
pass
|
||||||
|
return solutions
|
||||||
|
|
||||||
|
|
||||||
|
def execute_algorithm(choice, n, m, data):
|
||||||
|
if choice == "greedy":
|
||||||
|
return greedy_algorithm(n, m, data)
|
||||||
|
elif choice == "local":
|
||||||
|
return local_search(n, m, data)
|
||||||
|
else:
|
||||||
|
print("The valid algorithm choices are 'greedy' and 'local'")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def show_results(solutions, time_delta):
|
||||||
|
distance_sum = solutions["distance"].sum()
|
||||||
|
duplicates = solutions.duplicated()
|
||||||
|
print(solutions)
|
||||||
|
print("Total distance: " + str(distance_sum))
|
||||||
|
if solutions[duplicates].empty:
|
||||||
|
print("No duplicates found")
|
||||||
|
print("Execution time: " + str(time_delta))
|
||||||
|
|
||||||
|
|
||||||
def usage(argv):
|
def usage(argv):
|
||||||
print(f"Usage: python {argv[0]} <file>")
|
print(f"Usage: python {argv[0]} <file> <algorithm choice>")
|
||||||
|
print("algorithm choices:")
|
||||||
|
print("greedy: greedy algorithm")
|
||||||
|
print("local: local search algorithm")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(argv) != 2:
|
if len(argv) != 3:
|
||||||
usage(argv)
|
usage(argv)
|
||||||
n, m, data = parse_file(argv[1])
|
n, m, data = parse_file(argv[1])
|
||||||
solutions = greedy_algorithm(n, m, data)
|
start_time = time()
|
||||||
print(solutions)
|
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
|
||||||
|
end_time = time()
|
||||||
|
show_results(solutions, time_delta=end_time - start_time)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue