Пример #1
0
def main():
    g_map = GlobalMap(read_dir=read_dir)
    if not load:
        controller = Controller(nature_num=nature_num,
                                chromo_num=chromo_num,
                                g_map=g_map,
                                punish=_punish,
                                read_dir=read_dir,
                                save_dir=save_dir)
    else:
        try:
            controller: Controller = pickle_load(save_dir + '/controller.pkl')
            controller.set_punish(punish=_punish)
        except FileNotFoundError:
            print(
                'No "controller" in given direction. New "controller" will be created.'
            )
            controller = Controller(nature_num=nature_num,
                                    chromo_num=chromo_num,
                                    g_map=g_map,
                                    punish=_punish,
                                    read_dir=read_dir,
                                    save_dir=save_dir)

    for generation in range(0, generation_num):
        print('Generation {} start.'.format(generation))
        controller.operate()
        best: Chromo = controller.get_best()
        print('Best Cost: {}\tRoute Num: {}\tPunish Num: {}'.format(
            best.cost, len(best.sequence), best.has_punish_num()))
        if generation % 10 == 9:
            if save:
                pickle_dump(controller, file_path=save_dir + '/controller.pkl')
            controller.set_punish(punish=controller.punish * punish_increase)

    best_chromo: Chromo = controller.get_best()
    for route in best_chromo.sequence:
        print(route.sequence)
Пример #2
0
 def run(self) -> None:
     try:
         nature: Nature = pickle_load(self.save_dir)
         nature.set_punish_para(punish=self.punish)
     except FileNotFoundError:
         print('No "nature{}" in given direction. New "nature" will be created.'.format(self.idx))
         nature: Nature = Nature(chromo_list=[], chromo_num=self.chromo_num, g_map=GlobalMap(self.read_dir),
                                 new_chromo_num=self.new_chromo_num, punish=self.punish)
     nature.operate()
     pickle_dump(nature, self.save_dir)