Ejemplo n.º 1
0
 def getLength(self, poly_list):
     length = PolyListProcessor.packingLength(
         poly_list,
         self.history_index_list,
         self.history_length_list,
         self.width,
         NFPAssistant=self.NFPAssistant)
     return length
Ejemplo n.º 2
0
    def run(self):
        initial_length = PolyListProcessor.packingLength(
            self.cur_poly_list, self.history_index_list,
            self.history_length_list, self.width)

        global_lowest_length_list = []  # 记录每个温度下最最低高度,理论上会下降
        temp_lowest_length_list = []  # 每个温度下的平衡高度

        global_best_list = copy.deepcopy(self.cur_poly_list)  # 用于记录历史上最好蓄力
        global_lowest_length = initial_length  # 全局最低高度

        temp_best_list = copy.deepcopy(self.cur_poly_list)  # 局部温度下的最低
        temp_lowest_length = initial_length  # 局部温度下的最低

        unchange_times = 0

        # 开始循环寻找
        while self.temp_now > self.temp_end:
            print("当前温度:", self.temp_now)
            old_lowest_length = global_lowest_length  # 统计未更改次数

            cur_length = PolyListProcessor.packingLength(
                self.cur_poly_list,
                self.history_index_list,
                self.history_length_list,
                self.width,
                NFPAssistant=self.NFPAssistant)

            # 在某个温度下进行一定次数的寻找
            for i in range(self.loop_times):
                self.newPolyList()

                new_length = PolyListProcessor.packingLength(
                    self.new_poly_list,
                    self.history_index_list,
                    self.history_length_list,
                    self.width,
                    NFPAssistant=self.NFPAssistant)
                delta_length = new_length - cur_length

                if delta_length < 0:  # 当前温度下如果高度更低则接受
                    temp_best_list = self.cur_poly_list = copy.deepcopy(
                        self.new_poly_list)
                    temp_lowest_length = new_length  # 修改为新的高度
                    cur_length = new_length

                    if new_length < global_lowest_length:  # 如果新的高度小于最低的高度则修改最低高度
                        global_lowest_length = new_length
                        global_best_list = copy.deepcopy(self.new_poly_list)

                elif np.random.random() < np.exp(
                        -delta_length / self.temp_now):  # 按照一定概率修改,并作为下一次检索基础
                    self.poly_list = copy.deepcopy(self.new_poly_list)
                    cur_length = new_length
                else:
                    pass  # 否则不进行修改

            print("当前温度最低长度:", temp_lowest_length)
            print("最低长度:", global_lowest_length)

            if old_lowest_length == global_lowest_length:
                unchange_times += 1
                if unchange_times > 15:
                    break
            else:
                unchange_times = 0

            self.cur_poly_list = copy.deepcopy(
                temp_best_list)  # 某温度下检索结束后取该温度下最优值
            self.temp_now *= self.dec_rate  #退火
            global_lowest_length_list.append(
                global_lowest_length)  # 全局的在每个温度下的最低高度,理论上一直在降低
            temp_lowest_length_list.append(temp_lowest_length)  # 每个温度下的最低高度

        # print('结束温度的局部最优的序列:',temp_best_list)
        print('结束温度的局部最优高度:', temp_lowest_length)
        # print('最好序列:',global_best_list)
        print('最好序列高度:', global_lowest_length)

        PolyListProcessor.showPolyList(self.width, global_best_list)

        self.showBestResult(temp_lowest_length_list, global_lowest_length_list)