Beispiel #1
0
class BulkVisualizer(Visualizer):
    """Draws consecutive visualisations for consecuetively created areas"""
    def __init__(self, area, algorithm, runs):
        """Initiate all elements necessary to run an algorithm consecutively
        and create consecutive visualizations for them.

        Keyword arguments:
        area      -- the area that should be visualised
        algorithm -- the algorithm by which the given area is filled
        runs      -- the amount of times the algorithm should be run and
                     the amount of visualizations to be made
        """

        super().__init__(area, algorithm)
        self.originalArea = copy.deepcopy(area)
        self.originalAlgorithm = copy.copy(algorithm)
        self.runs = 0
        self.allTimeHigh = 0
        self.dataHelper = DataHelper()
        self.maxRuns = runs

    def on_render(self):
        """Runs and visualizes the algorithm"""

        while self.runs < self.maxRuns:
            # continue running until algorithm is done
            super().on_render()

            # track highest found area value
            if self.area.price > self.allTimeHigh:
                self.allTimeHigh = self.area.price

            # when a run is finished...
            if self.algorithm.isDone is True and self.runs < self.maxRuns:
                print('🎉🎉Run {} is complete! 🎉🎉'.format(self.runs))
                # ...save values to csv file
                self.dataHelper.writeArea(self.area)

                # ...restore to a fresh state (empty area)
                self.area = copy.deepcopy(self.originalArea)
                self.algorithm = copy.copy(self.originalAlgorithm)
                self.algorithm.area = self.area
                self.runs += 1

            if self.runs == self.maxRuns:
                print('✨✨ I succesfully ran {} times! ✨✨'.format(
                    self.runs))
Beispiel #2
0
class NoDrawBulkVisualizer:
    """Runs an algorithm several times without visualization"""
    def __init__(self, area, algorithm, runs):
        """Initiate all elements necessary to run an algorithm consecutively
        without visualizations.

        Keyword arguments:
        area      -- the area that should be visualised
        algorithm -- the algorithm by which the given area is filled
        runs      -- the amount of times the algorithm should run
        """
        self.area = area
        self.algorithm = algorithm
        self.originalArea = copy.deepcopy(area)
        self.originalAlgorithm = copy.copy(algorithm)
        self.runs = 0
        self.dataHelper = DataHelper()
        self.maxRuns = runs

    def on_execute(self):
        """Executes an algorithm several times"""

        while self.runs < self.maxRuns:
            # continue running until algorithm is done
            while self.algorithm.isDone is False:
                self.algorithm.execute()
                self.dataHelper.writeCSVLine(self.area)

            # if algorithm completes one run, reset state
            if self.algorithm.isDone is True and self.runs < self.maxRuns:
                print('Run {} is complete! 🎉'.format(self.runs))
                # save area to csv
                self.dataHelper.writeArea(self.area)
                self.dataHelper = DataHelper()

                # reset area and algorithm
                self.area = copy.deepcopy(self.originalArea)
                self.algorithm = copy.copy(self.originalAlgorithm)
                self.algorithm.area = self.area
                self.runs += 1

            if self.runs == self.maxRuns:
                print('✨✨ I succesfully ran {}'
                      'times! ✨✨'.format(self.runs))
Beispiel #3
0
class NoDrawVisualizer:
    """Runs an algorithm without visualization"""
    def __init__(self, area, algorithm):
        """Initiate all elements necessary to run an algorithm
        without visualization

        Keyword arguments:
        area      -- the area that should be used in the algoritm
        algorithm -- the algorithm by which the given area is filled
        """

        self.area = area
        self.algorithm = algorithm
        self.dataHelper = DataHelper()

    def on_execute(self):
        """Starts and executes the algorithm"""

        # while algorithm is not done, run it
        while self.algorithm.isDone is False:
            self.algorithm.execute()

            # save area state between every step
            self.dataHelper.writeArea(self.area)