def setup(self): Agent.id = 1 GA_World.individual_class = TSP_Individual GA_World.chromosome_class = TSP_Chromosome.factory # The following GUI elements are defined in ga.py. # We can't set their default values in this file's GUI. # The only way to do it is explicitly. gui_set('Max generations', value=float('inf')) gui_set('prob_random_parent', value=20) # Don't display the following standard GA features. for label in ['Discrep:', 'discrepancy', 'Gens:', 'generations']: gui_set(label, visible=False) TSP_Agent.show_labels = gui_get('show_labels') (SimEngine.event, SimEngine.values) = gui.WINDOW.read(timeout=10) SimEngine.draw_world() # auto_setup is initially True (by default). The next line aborts setup in that case. # The purpose is to allow the system to create a display but not run gen_population, # which is run when the user clicks setup. That works because auto_setup is set to # False by SimEngine after setup run, which happens automatically. So, if auto_setup # is False, we will complete setup and generate the initial population. if not SimEngine.auto_setup: self.msp_links = None if gui_get('Animate construction'): print( f'\nGenerating the initial population of {gui_get("pop_size")} paths.' ) super().setup()
def draw_links(links, sleep_time=0.6): cached_world_links_set = World.links World.links = set() gui_set(gui.GOSTOP, text='pause', button_color=('white', 'red'), enabled=True) gui_set(gui.GO_ONCE, enabled=False) gui_set(SimEngine.simple_gui.SETUP, enabled=False) gui_set(SimEngine.simple_gui.EXIT, enabled=False) paused = False while links: (SimEngine.event, SimEngine.values) = gui.WINDOW.read(timeout=10) if SimEngine.event == gui.GOSTOP: gui_set(gui.GOSTOP, text='pause' if paused else 'continue', button_color=('white', 'red' if paused else 'green')) paused = not paused SimEngine.draw_world() if not paused: lnk = links.pop(0) if lnk in World.links: World.links.remove(lnk) World.links.add(lnk) SimEngine.draw_world() if sleep_time: sleep(sleep_time) gui_set(SimEngine.simple_gui.EXIT, enabled=True) gui_set(gui.GOSTOP, text='stop', button_color=('white', 'red'), enabled=True) World.links = cached_world_links_set