def __init__(self, root, forbidden): self.root = Node(root, None) self.forbidden = forbidden self.currentNode = self.root self.root.setChildren(self.forbidden) self.appendList.append(self.currentNode) for i in self.currentNode.children: i.setChildren(self.forbidden) self.fringe+= self.currentNode.children
def input_bfs(batteries, houses, command): """ Only neighbourhood 4 should be tried, because the state space of the other neighbourhoods is too big. """ node = Node() best = Node() dist, distdict, lowbprice = distancearr(batteries, houses) best.price = 100000 best = bfs(node, batteries, houses, distdict, best) nodetoclasses(batteries, houses, best) price = price_calc(batteries, distdict) print(f"Best price found: {price}") visualize(batteries, houses, argv[1], command)
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)
class Tree: root = None currentNode = None fringe = [] appendList = [] forbidden = [] def __init__(self, root, forbidden): self.root = Node(root, None) self.forbidden = forbidden self.currentNode = self.root self.root.setChildren(self.forbidden) self.appendList.append(self.currentNode) for i in self.currentNode.children: i.setChildren(self.forbidden) self.fringe+= self.currentNode.children def generateChildren(self): if len(self.currentNode.children) == 0: self.currentNode.setChildren(self.forbidden) self.fringe+= self.currentNode.children for i in self.currentNode.children: i.setChildren(self.forbidden) def move(self, node): self.currentNode = node self.fringe.remove(self.currentNode) self.appendList.append(self.currentNode) self.generateChildren() def checkExpand(self, node): if node.number in self.getNumber(self.appendList): if self.checkSimilar(node, self.appendList): return True return False def moveToParent(self): self.currentNode = self.currentNode.parent def checkSimilar(self, node, nodeList): for i in nodeList: if node.number == i.number and self.checkParent(node, i): return False return True def checkParent(self, node, node2): if node.parent is None and node2.parent is None: return True if node.parent is None or node2.parent is None: return False num1 = [abs(int(node.number[0]) - int(node.parent.number[0])), abs(int(node.number[1]) - int(node.parent.number[1])),abs(int(node.number[2]) - int(node.parent.number[2]))] num2 = [abs(int(node2.number[0]) - int(node2.parent.number[0])), abs(int(node2.number[1]) - int(node2.parent.number[1])),abs(int(node2.number[2]) - int(node2.parent.number[2]))] if num1 == num2: return True else: return False def getNumber(self, numList): result = [] for i in numList: result.append(i.number) return result