def hill_climber(area, hill_climber_name): """ Runs the specific hill climber with the given area. """ if hill_climber_name == "hill_climber_steps": hill_climber_steps(area) elif hill_climber_name == "hill_climber_random": hill_climber_random(area) elif hill_climber_name == "hill_climber_random_random": hill_climber_random_random(area) elif hill_climber_name == "simulated_annealing": simulated_annealing(area)
def random_greedy(self, rounds): from algorithms.simulated_annealing import simulated_annealing ''' Distributes the houses using a random greedy algorithm. This randomly distributes the houses under the batteries and than reshuffles the houses by randomly choosing one of the batteries and deciding to change battery if that battery is at closer proximity than the current battery. Random greedy algorithms includes an aspect of randomness into a greedy model. A greedy algorithm will always choose the best option for a problem without considering the effects further down the line. Input: the amount of times the houses will be moved between batteries. ''' # distributing houses randomly amoung batteries self.functions_in_sa_class = simulated_annealing(self.district) self.functions_in_sa_class.random_house_distribution() # attempts to move a house rounds amount of times into a random battery, choice proximity based self.to_cal_distance = Cable(self.district) self.rounds_done = 0 while self.rounds_done < rounds: for self.a_house in self.district.all_houses: self.ran_chosen_bat = random.choice( self.district.all_batteries) if self.to_cal_distance.calculate_distance(self.a_house, self.a_house.battery) > \ self.to_cal_distance.calculate_distance(self.a_house, self.ran_chosen_bat): self.a_house.battery.houses_in_battery.remove(self.a_house) self.a_house.battery = self.ran_chosen_bat self.ran_chosen_bat.houses_in_battery.append(self.a_house) self.rounds_done += 1
def simulated_annealing_house_manhatten_cable(self, cable_sharing): ''' Combines a simulated annealing algorithm with a huristic based on proximity of a house to a battery and the fullness of a battery compared to other batteries to devide the houses amoung the batteries. Than uses the manhatten distance to lay cables between thouse houses and their batteries. Finally, prints the total costs of the solution. ''' self.sa_distribution = simulated_annealing(self.the_district) self.sa_distribution.creating_starting_possition() self.sa_distribution.running_simulated_annealing(cable_sharing) self.astar_cable2 = Cable(self.the_district) self.astar_cable2.cable_list_batteries(self.the_district.all_batteries, cable_sharing) self.the_district.refresh_config() return self.sa_distribution.district.cs50_check(cable_sharing)
def annealing_alg(solution, cooling, temperature, route_iterations, connection_iterations): """ Simulated Annealing solution. Args: solution: Empty instance of solution object. cooling: Integer representing the cooling scheme chosen. temperature: Integer representing the start temperature of the algorithm. route_iterations: The number of route iterations to do. connection_iterations: The number of connection iterations to do. Returns: A Greedy solution. """ return an.simulated_annealing(solution, cooling, temperature, \ route_iterations, connection_iterations)
def annealing_hill_climber(self, step_num, cable_sharing): ''' This method first applies the simulating annealing algorithm to sort all the houses in the batteries. Then it applies the hill climber algorithm to modify this solution to an even better one. It takes step_num steps in the hill climber algorithm. ''' self.annealing_house_devide1 = simulated_annealing(self.the_district) self.annealing_house_devide1.creating_starting_possition() self.annealing_house_devide1.running_simulated_annealing(cable_sharing) self.astar_cable2 = Cable(self.the_district) self.astar_cable2.cable_list_batteries(self.the_district.all_batteries, False) self.hill = Hill_climber(self.the_district) self.hill.climb_the_hill(step_num) self.the_district.refresh_config() return self.annealing_house_devide1.district.cs50_check(cable_sharing)