def random_neighbor(self, distance: int):
        mask_list1 = General.get_mask_list(self.solution.n, distance, climb=False)
        (value_list1, weight_list1) = General.parse_item_list_data(self.item_list)

        solution_binary = "".join([str(item) for item in self.solution.item_list])
        solution_number = int(solution_binary, 2)

        mask_list = deepcopy(mask_list1)
        random.shuffle(mask_list)

        for mask in mask_list:
            masked_number = solution_number ^ int(mask, 2)
            masked_binary = bin(masked_number)[2:].zfill(self.solution.n)
            neighbor = [int(digit) for digit in masked_binary]
            neighbor_weight_list = [a*b for a,b in zip(neighbor, weight_list1)]

            if sum(neighbor_weight_list) <= self.solution.capacity:
                neighbor_value_list = [a*b for a,b in zip(neighbor, value_list1)]
                self.solution.value = sum(neighbor_value_list)
                self.solution.weight =sum(neighbor_weight_list)
                self.solution.item_list = deepcopy(neighbor)
                return True

        return False
        
Ejemplo n.º 2
0
    def run(self, tabu_list: list) -> None:
        #print(f"ic| Executing Tabu Search with distance {self.distance}")

        if self.counter == 0:
            self.output_file.write_line(
                self.output_filename.replace('TEMP-', ''))
            self.output_file.write_line(str(self.solution.optimum))
            self.output_file.write_line(
                f"{self.counter} {self.solution.value}")

        mask_list = General.get_mask_list(self.solution.n,
                                          self.distance,
                                          climb=True)
        (value_list,
         weight_list) = General.parse_item_list_data(self.item_list)

        teste = self.evaluate_neighborhood_tabu(self.solution, mask_list,
                                                value_list, weight_list,
                                                tabu_list)
        if teste:
            self.counter += 1
            #self.solution.print_solution()
            #ic(f"{self.counter} {self.solution.value}")
            self.output_file.write_line(
                f"{self.counter} {self.solution.value}")
Ejemplo n.º 3
0
    def run_vns(self, solution: Solution, item_list: list, max_iterations: int,
                neighborhood_size: int, output_filename: str):
        print(f"ic| run_vns: Executing Variable Neighbourhood Search")
        counter = 0
        output_file = FileWriter(file_name=output_filename)

        output_file.write_line(output_filename.replace('TEMP-', ''))
        output_file.write_line(str(solution.optimum))
        output_file.write_line(f"{counter} {solution.value}")

        (value_list,
         weight_list) = General.parse_item_list_data(self.item_list)

        for i in range(max_iterations):
            initial_solution = solution
            k = 1
            while k <= neighborhood_size:
                mask_list = General.get_mask_list(solution.n, k, climb=False)
                initial_solution = self.random_neighbor(
                    solution, k, value_list, weight_list)
                if not initial_solution:
                    continue
                best_neighbor = self.evaluate_neighborhood(
                    initial_solution, mask_list, value_list, weight_list)

                if best_neighbor and best_neighbor.value > solution.value:
                    counter += 1
                    solution = deepcopy(best_neighbor)
                    #solution.print_solution()
                    #ic(f"{counter} {solution.value}")
                    output_file.write_line(f"{counter} {solution.value}")
                else:
                    k += 1
    def run_local_search_improved(self):
        print(
            f"ic| run_local_search_improved: Executing Local Search with distance {self.distance}"
        )
        counter = 0
        output_file = FileWriter(file_name=self.output_filename)

        output_file.write_line(self.output_filename.replace('TEMP-', ''))
        output_file.write_line(str(self.solution.optimum))
        output_file.write_line(f"{counter} {self.solution.value}")

        mask_list = General.get_mask_list(self.solution.n,
                                          self.distance,
                                          climb=True)
        (value_list,
         weight_list) = General.parse_item_list_data(self.item_list)
        while self.evaluate_neighborhood_improved(self.solution, mask_list,
                                                  value_list, weight_list):
            counter += 1
            #self.solution.print_solution()
            ic(f"{counter} {self.solution.value}")
            output_file.write_line(f"{counter} {self.solution.value}")

            # prevent looping indefinitely
            if counter >= 100:
                return
    def run(self, counter):
        #print(f"ic| Executing Iterated Local Search with distance {self.distance}")

        if counter == 0:
            self.output_file.write_line(
                self.output_filename.replace('TEMP-', ''))
            self.output_file.write_line(str(self.solution.optimum))
            self.output_file.write_line(f"{counter} {self.solution.value}")

        mask_list = General.get_mask_list(self.solution.n,
                                          self.distance,
                                          climb=True)
        (value_list,
         weight_list) = General.parse_item_list_data(self.item_list)

        while self.evaluate_neighborhood(self.solution, mask_list, value_list,
                                         weight_list):
            #self.solution.print_solution()
            #ic(f"{self.counter} {self.solution.value}")
            pass
    def run(self, counter):
        #print(f"ic| Executing Multi Start Local Search with distance {self.distance}")

        if counter == 0:
            self.output_file.write_line(
                self.output_filename.replace('TEMP-', ''))
            self.output_file.write_line(str(self.solution.optimum))
            self.output_file.write_line(f"{counter} {self.solution.value}")

        mask_list = General.get_mask_list(self.solution.n,
                                          self.distance,
                                          climb=True)
        (value_list,
         weight_list) = General.parse_item_list_data(self.item_list)

        counter_stop = 0

        while self.evaluate_neighborhood(self.solution, mask_list, value_list,
                                         weight_list):
            counter_stop += 1
            # prevent from looping indefinitely
            if counter_stop >= 100:
                return