Example #1
0
    def run(self, graph, demands):
        start_timer = time.time()
        self.logger.info("run an algorithm")

        self.current_graph = graph.copy()                  # context for current solution
        self.next_graph = graph.copy()                     # context for next solution
        self.allocator = SpectrumAllocator(self.current_graph)

        self.current_solution = self.create_first_solution(demands)
        self.allocate_solution(self.current_solution, self.current_graph)
        self.current_energy = calculate_graph_cost(self.current_graph)

        self.best_solution = copy_demands(self.current_solution)
        self.best_energy = self.current_energy

        while self.is_not_cold():
            self.logger.debug("temperature = %s", self.temperature)
            self.next_solution = self.create_new_solution(self.current_solution)
            self.allocate_solution(self.next_solution, self.next_graph)
            self.next_energy = calculate_graph_cost(self.next_graph)

            if self.next_energy < self.current_energy:
                self.logger.debug("better solution than current one founded with cost: %s", self.next_energy)
                self.accept_next()
                self.check_if_next_is_better_than_best()
            elif self.is_worse_solution_acceptable():
                self.logger.debug("worse solution than current accepted with cost: %s", self.next_energy)
                self.accept_next()
            self.cool_temperature()

        stop_timer = time.time()
        self.final_time = stop_timer - start_timer
Example #2
0
 def create_new_solution(self, demands):
     solution = copy_demands(demands)
     index = random.randint(0, len(demands) - 2)
     solution[index], solution[index+1] = solution[index+1], solution[index]
     demand = solution[index]
     if isinstance(demand, UnicastDemand):
         path_index = random.randint(0, demand.nr_of_paths - 1)
         demand.select_path(path_index)
     elif isinstance(demand, AnycastDemand):
         data_center_index = random.randint(0, demand.nr_of_data_centers - 1)
         path_up_index = random.randint(0, demand.nr_of_paths - 1)
         path_down_index = random.randint(0, demand.nr_of_paths - 1)
         demand.select_data_center_by_index(data_center_index)
         demand.select_path_up(path_up_index)
         demand.select_path_down(path_down_index)
     return solution
Example #3
0
 def create_first_solution(self, demands):
     solution = []
     size_of_demands = len(demands)
     # todo: think how to remove redundancy
     demands = copy_demands(demands)
     while size_of_demands > 0:
         selected_demand_index = random.randint(0, size_of_demands - 1)
         demand = demands.pop(selected_demand_index)
         if isinstance(demand, UnicastDemand):
             path_index = random.randint(0, demand.nr_of_paths - 1)
             demand.select_path(path_index)
             solution.append(demand)
         elif isinstance(demand, AnycastDemand):
             data_center_index = random.randint(0, demand.nr_of_data_centers - 1)
             path_up_index = random.randint(0, demand.nr_of_paths - 1)
             path_down_index = random.randint(0, demand.nr_of_paths - 1)
             demand.select_data_center_by_index(data_center_index)
             demand.select_path_up(path_up_index)
             demand.select_path_down(path_down_index)
             solution.append(demand)
         size_of_demands -= 1
     return solution
Example #4
0
 def check_if_next_is_better_than_best(self):
     if self.next_energy < self.best_energy:
         self.logger.debug("new best solution found with cost: %s", self.next_energy)
         self.best_energy = self.next_energy
         self.best_solution = copy_demands(self.next_solution)       # copy
Example #5
0
 def accept_next(self):
     self.current_energy = self.next_energy
     self.current_solution = copy_demands(self.next_solution)      # copy