Example #1
0
class Startpage(tk.PanedWindow):
    def __init__(self, parent, core):
        super().__init__(parent,
                         borderwidth=0,
                         sashwidth=4,
                         sashrelief='sunken')

        self.core = core

        #some help-widgets (not so important)
        leftpane = tk.PanedWindow(self,
                                  orient='vertical',
                                  borderwidth=0,
                                  sashwidth=4,
                                  sashrelief='sunken')
        self.add(leftpane)
        rightbox = tk.Frame(self)
        self.add(rightbox)
        scrollbox = tk.Frame(rightbox)
        scrollbox.pack(side='top', fill='both', expand=True)
        scrolly = tk.Scrollbar(scrollbox, orient='vertical')
        scrollx = tk.Scrollbar(scrollbox, orient='horizontal')
        scrolly.pack(side='right', fill='y')
        scrollx.pack(side='bottom', fill='x')
        scroll_canvas = tk.Canvas(scrollbox,
                                  yscrollcommand=scrolly.set,
                                  xscrollcommand=scrollx.set)
        scroll_canvas.pack(side='top', fill='both', expand=True)

        #the basic layout
        settings_box = tk.Frame(scroll_canvas)
        important_box = tk.Frame(rightbox)  #box on the bottom right
        shape_box = tk.Frame(leftpane)
        plot_box = tk.Frame(leftpane)

        settings_box.pack(side='top', fill='both', expand=True)
        important_box.pack(side='bottom', fill='x')
        leftpane.add(shape_box)
        leftpane.add(plot_box)

        #important box
        mentat_button = ttk.Button(
            important_box,
            text='MarcMentat instances',
            command=lambda: parent.master.show_frame('marcMentat'))
        mentat_button.pack(side='right', padx=4, pady=4)
        self.start_optimization_button = ttk.Button(
            important_box,
            text='start optimaization',
            command=self.optimization_button_pressed)
        self.start_optimization_button.pack(side='left', padx=4, pady=4)

        #shape box
        f = matplotlib.figure.Figure()
        self.shape_plot = f.add_subplot(111)
        self.shape_plot.set_aspect(
            'equal', adjustable='datalim')  #scale of x- and y- axis equal
        #to hide numbers and ticks of the axes
        #self.shape_plot.xaxis.set_major_locator(matplotlib.pyplot.NullLocator())
        #self.shape_plot.yaxis.set_major_locator(matplotlib.pyplot.NullLocator())
        self.shape_canvas = FigureCanvasTkAgg(f, shape_box)
        self.shape_canvas.draw()
        toolbar = NavigationToolbar2Tk(self.shape_canvas, shape_box)
        toolbar.update()
        self.shape_canvas.get_tk_widget().pack(side='top',
                                               fill='both',
                                               expand=True)

        #plot box
        f = matplotlib.figure.Figure()
        self.graph_plot = f.add_subplot(111)
        self.plot_canvas = FigureCanvasTkAgg(f, plot_box)
        self.plot_canvas.draw()
        toolbar = NavigationToolbar2Tk(self.plot_canvas, plot_box)
        toolbar.update()
        self.plot_canvas.get_tk_widget().pack(side='top',
                                              fill='both',
                                              expand=True)

        #settings box
        container_mutation_algorithms = ToggledFrameContainer(
            settings_box, 'mutation algorithm')
        container_mutation_algorithms.pack(fill='x', pady=3)
        for algo in self.core.mutation_algorithms:
            ToggledFrameAlgorithm(container_mutation_algorithms.sub_frame,
                                  algo).pack(fill='x', pady=3)

        container_pairing_algorithms = ToggledFrameContainer(
            settings_box, 'pairing algorithm')
        container_pairing_algorithms.pack(fill='x', pady=3)
        for algo in self.core.pairing_algorithms:
            ToggledFrameAlgorithm(container_pairing_algorithms.sub_frame,
                                  algo).pack(fill='x', pady=3)

        container_read_in_algorithms = ToggledFrameContainer(
            settings_box, 'read in method')
        container_read_in_algorithms.pack(fill='x', pady=3)
        all_read_in_algorithms = list(core.all_read_in_algorithms.keys())
        read_in_list = ttk.Combobox(container_read_in_algorithms.sub_frame,
                                    values=all_read_in_algorithms,
                                    state='readonly')
        read_in_list.bind(
            '<<ComboboxSelected>>',
            lambda _: self.core.set_read_in_algorithm(read_in_list.get()))
        if len(core.all_read_in_algorithms) > 0:
            read_in_list.set(core.read_in_algorithm)
        read_in_list.pack()

        container_evaluation_algorithms = ToggledFrameContainer(
            settings_box, 'calculation of fittness value')
        container_evaluation_algorithms.pack(fill='x', pady=3)
        all_evaluation_algorithms = list(core.all_evaluation_algorithms.keys())
        evaluation_algo_list = ttk.Combobox(
            container_evaluation_algorithms.sub_frame,
            values=all_evaluation_algorithms,
            state='readonly')
        evaluation_algo_list.bind(
            '<<ComboboxSelected>>', lambda _: self.core.
            set_evaluation_algorithm(evaluation_algo_list.get()))
        if len(core.all_evaluation_algorithms) > 0:
            evaluation_algo_list.set(core.evaluation_algorithm)
        evaluation_algo_list.pack()

        #....
        container_tests = ToggledFrameContainer(settings_box, 'tests')
        container_tests.pack(fill='x', pady=3)
        test_shape = ttk.Button(container_tests.sub_frame,
                                text='test',
                                command=self.master.master._test)
        test_shape.pack()


#         self.mentat_commandlist = Mentat_commandlist(marcMentat_commands)
#         self.mentat_commandlist.pack(fill=tk.BOTH, expand=True)

    def update_progress(self):
        while self.core.optimization_running:
            time.sleep(1)
            self.show_improvement_history(self.core.improvement_history())
            self.shape_canvas.clear()
            self.draw_shape_foreground(self.core.find_best_shape())
            self.shape_canvas.draw()

    def optimization_button_pressed(self):
        #self.core.set_optimization_running(not self.core.get_optimization_running())
        if not self.core.get_optimization_running():
            thread1 = threading.Thread(target=self.core.start_optimization)
            thread2 = threading.Thread(
                target=lambda: self.listen_for_optimization_loop_terminating(
                    thread1))
            thread1.start()
            thread2.start()

            # thread3 = threading.Thread(target=self.update_progress)
            # thread3.start()
            self.start_optimization_button.config(text='stop optimization')
        else:
            self.core.terminate_optimization()
            self.start_optimization_button.config(
                state='disabled', text='stopping optimization...')
            self.show_improvement_history(self.core.improvement_history)
            self.shape_canvas.clear()
            self.draw_shape_foreground(self.core.find_best_shape())
            self.shape_canvas.draw()
            #self.start_optimization_button.config(text='start optimization')

    def listen_for_optimization_loop_terminating(self, loop_thread):
        loop_thread.join()
        self.start_optimization_button.config(text='start optimization',
                                              state='normal')

    def draw_shape_background(self, shp, markers=False, color='red'):
        self.shape_plot.plot(*shp.exterior.xy, color=color)
        if markers:
            self.shape_plot.scatter(*shp.exterior.xy, color=color)
        for interior in shp.interiors:
            self.shape_plot.plot(*interior.xy, color=color)

    def draw_shape_foreground(self, shp, fill_color='black'):
        self.shape_plot.fill(*shp.exterior.xy, color=fill_color, alpha=0.1)
        self.shape_plot.plot(*shp.exterior.xy, color='black')
        self.shape_plot.scatter(*shp.exterior.xy,
                                color=self.restrictions_to_markercolors(shp))

        for interior in shp.interiors:
            self.shape_plot.fill(*interior.xy, color='white')
            self.shape_plot.plot(*interior.xy, marker='o', color='black')

    def restrictions_to_markercolors(self, shp):
        markercolors = []
        for restriction in shp.move_restrictions:
            if restriction:
                if type(restriction) is tuple:
                    markercolors.append('yellow')
                else:
                    markercolors.append('red')
            else:
                markercolors.append('black')
        markercolors.append(markercolors[0])
        return markercolors

    def show_improvement_history(self, improvement_history):
        inital_fittness = self.core.inital_shape.fittness
        generation_nums = [line[0] for line in improvement_history]
        fittness_means = [line[2] for line in improvement_history]
        fittness_min = [line[4] for line in improvement_history]
        fittness_max = [line[3] for line in improvement_history]
        self.graph_plot.clear()
        self.graph_plot.hlines(inital_fittness, 0,
                               len(self.core.generations) - 1)
        self.graph_plot.plot(generation_nums, fittness_means)
        self.graph_plot.fill_between(generation_nums,
                                     fittness_min,
                                     fittness_max,
                                     color='blue',
                                     alpha=0.2)
        self.plot_canvas.draw()

    def draw_shape_comparison(self, shp, comparison_shape, autoscale):

        self.shape_plot.clear()
        self.shape_plot.autoscale(autoscale)  #funktioniert noch nicht

        #um die Zahlen an den Achsen unsichtbar zu machen
        ##        self.plot.xaxis.set_major_locator(matplotlib.pyplot.NullLocator())
        ##        self.plot.yaxis.set_major_locator(matplotlib.pyplot.NullLocator())

        if comparison_shape:
            self.draw_shape_background(comparison_shape)

        self.draw_shape_foreground(shp)

        self.shape_canvas.draw()

    def draw_shape_pairing(self, shp1, shp2, merged_shape, autoscale):
        self.shape_plot.clear()
        self.shape_plot.autoscale(autoscale)
        if merged_shape is not None:
            self.draw_shape_background(merged_shape, markers=True)
        self.draw_shape_foreground(shp1, fill_color='yellow')
        self.draw_shape_foreground(shp2, fill_color='green')
        self.shape_canvas.draw()