def input_greedy(batteries, houses, command): """ This algorithm chooses randomly a house and connects it to a battery based on best (shortest) distance. It asks for number of repeats to run the algorithm. It saves all solutions to one .csv file and it writes the best price of all repeats to another .csv file. The best price is also visualized. """ dist, distdict, lowbprice = distancearr(batteries, houses) print("run how many times?") repeats = int(input("> ")) best = Node() best.price = 100000 for i in range(0, repeats): greedy(dist, batteries, houses) price = price_calc(batteries, distdict) if price < best.price: best.batts = [[], [], [], [], []] best.fillnode(batteries, houses, price) write_to_csv(argv[1], command, price) reset(batteries, houses) savefig = bestplot(argv[1], command, best.price) print(f"Best price found: {best.price}") if savefig is True: nodetoclasses(batteries, houses, best) visualize(batteries, houses, argv[1], command, best.price)
def input_hillclimber(batteries, houses, command): """ Iterates over all houses and checks if an improvement can be made by switching the connections of 2 houses. It asks for number of repeats to run the algorithm. It saves all solutions to one .csv file and it writes the best price of all repeats to another .csv file. The best price is also visualized. """ dist, distdict, lowbprice = distancearr(batteries, houses) print("base: greedy or random") base = (input("> ")).lower() if base != "greedy" and base != "random": return False command = base + "_" + command print("run how many times?") repeats = int(input("> ")) best = Node() best.price = 100000 for i in range(0, repeats): if base == "greedy": greedy(dist, batteries, houses) elif base == "random": random_alg(distdict, batteries, houses) hillclimber(dist, distdict, batteries, houses) price = price_calc(batteries, distdict) if price < best.price: best.batts = [[], [], [], [], []] best.fillnode(batteries, houses, price) write_to_csv(argv[1], command, price) reset(batteries, houses) savefig = bestplot(argv[1], command, best.price) print(f"Best price found: {best.price}") nodetoclasses(batteries, houses, best) if savefig is True: visualize(batteries, houses, argv[1], command, best.price) battery_optimization(batteries) dist, distdict, lowbprice = distancearr(batteries, houses) price = price_calc(batteries, distdict) print(f"Best price found with optimize: {price}") visualize(batteries, houses, argv[1], command, price)
def input_randclimber(batteries, houses, command): """ A version of hillclimber which switches 2 random houses until no improvements are made for the set number of times. It asks for number of repeats to run the algorithm. It saves all solutions to one .csv file and it writes the best price of all repeats to another .csv file. The best price is also visualized. """ dist, distdict, lowbprice = distancearr(batteries, houses) print("base: greedy or random") base = (input("> ")).lower() if base != "greedy" and base != "random": return False print("repeat until no change for how many times?") repetitions = int(input("> ")) command = base + "_" + command + str(repetitions) print("run how many times?") repeats = int(input("> ")) best = Node() best.price = 100000 for i in range(0, repeats): if base == "greedy": greedy(dist, batteries, houses) elif base == "random": random_alg(distdict, batteries, houses) randclimber(repetitions, distdict, batteries, houses) price = price_calc(batteries, distdict) if price < best.price: best.batts = [[], [], [], [], []] best.fillnode(batteries, houses, price) write_to_csv(argv[1], command, price) reset(batteries, houses) savefig = bestplot(argv[1], command, best.price) print(best.price) if savefig is True: nodetoclasses(batteries, houses, best) visualize(batteries, houses, argv[1], command, best.price)