예제 #1
0
 def do_square(self, arg):
     """Draw Square: square 100"""
     self.results.writeToFile("Drawing a square")
     self.parse(arg)
     turtle_drawer = TurtleDrawer()
     directions = [0, 90, 180, 270]
     for i in directions:
         turtle_drawer.draw_line(i, self.data)
예제 #2
0
 def do_S(self, arg):
     """Draw line 120 degrees : S 100"""
     self.results.writeToFile("Draw line 120 degrees : S", arg)
     self.parse(arg)
     turtle_drawer = TurtleDrawer()
     turtle_drawer.draw_line(180, self.data)
예제 #3
0
 def do_W(self, arg):
     """Draw line 270 degrees : W 100"""
     self.results.writeToFile("Draw line 270 degrees : W", arg)
     self.parse(arg)
     turtle_drawer = TurtleDrawer()
     turtle_drawer.draw_line(270, self.data)
예제 #4
0
 def do_E(self, arg):
     """Draw line 90 degrees : E 100"""
     self.results.writeToFile("Draw line 90 degrees : E", arg)
     self.parse(arg)
     turtle_drawer = TurtleDrawer()
     turtle_drawer.draw_line(90, self.data)
예제 #5
0
 def do_P(self, arg):
     """Select Pen:  P 10"""
     self.results.writeToFile("Selected pen", arg)
     self.parse(arg)
     turtle_drawer = TurtleDrawer()
     turtle_drawer.select_pen(self.data)
예제 #6
0
 def setUp(self):
     self.Reader = ArgumentSourceReader(ArgumentParser(TurtleDrawer()))
     self.source = ['-e', '-t', '-k', '-g']
예제 #7
0
 def setUp(self):
     self.tkinterParser = ArgumentParser(TurtleDrawer())
     self.source = [
         '-c', '-e', '-t', '-k', '-g', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', '0'
     ]
예제 #8
0
def main():
    # Generate random population of chromosomes
    population = []  # Chromosomes are appended in this

    for i in range(POPULATION_SIZE):
        # Every chromosome is a list of numbers
        chromosome = list(range(array_size))
        # ...randomized/shuffled.
        random.shuffle(chromosome)
        # Add to population
        population.append(chromosome)

    turtle_drawer = TurtleDrawer()
    plot_drawer = PlotDrawer()

    # Result arrays for plotting
    generation_indices = []
    average_results = []
    min_results = []
    max_results = []

    # Keep track of the last max_chromosome to avoid redundancy
    previous_max_chromosome = []

    for gen_count in range(NUMBER_OF_GENERATIONS):
        # Calculate fitness from distance and flow using imported function
        fitness_scores = get_fitness_scores(population, distance_array,
                                            flow_array)
        # Normalize fitness score by inverting and dividing by the total
        fitness_scores_normalized = normalise_fitness_scores(fitness_scores)

        # While it's not normalized yet, max is the worst, therefore min is the most suitable
        # Use numpy to get min, max, and average
        max_fitness = np.min(fitness_scores)
        min_fitness = np.max(fitness_scores)
        average_fitness = np.mean(fitness_scores)
        max_chromosome = population[np.argmin(fitness_scores)]
        max_chromosome = list(map(lambda value: value + 1, max_chromosome))

        # Use the imported strategy functions to select chromosomes from
        # population based on the normalized scores
        selected_chromosomes = selection_strategy.start_tournament(
            population, fitness_scores_normalized)

        # Input the selected chromosomes to the imported crossover function
        crossed_chromosomes = crossover_strategy.start_crossover(
            selected_chromosomes)
        # Apply mutation over the crossever chromosomes
        mutated_chromosomes = mutation_strategy.start_mutation(
            crossed_chromosomes)

        # Print current values to the terminal
        print(
            "Generation_Count: \t\t\t\t{}\nMean fitness.: \t\t\t{}\nMax score: \t\t\t{}\nMax chromosome.: \t\t{}\n\n"
            .format(gen_count, average_fitness, max_fitness, max_chromosome))

        # DRAWING FRAME

        # Implementing the turtle functions and iteratively drawing
        def draw_visual_frame():
            # The function takes flow and distance arrays, the best chromosome,
            # and other data and draws points and lines representing flow and distance
            turtle_drawer.draw_main_frame(max_chromosome, gen_count,
                                          max_fitness, max_chromosome,
                                          flow_array, distance_array)

            # Delay between each iteration of drawing
            time.sleep(SLEEP_TIME)

            return

        if previous_max_chromosome != max_chromosome:

            draw_visual_frame()

        # Update the max chromosome
        previous_max_chromosome = max_chromosome

        # Update the population to the one generated by the mutation strategy
        population = crossed_chromosomes

        # For plot drawing
        # ----------------
        # Add fitness scores to the results arrays, to be used in plot drawing
        max_results.append(max_fitness)
        min_results.append(min_fitness)
        average_results.append(average_fitness)
        # Add an increment to the generation indices, for the x-axis of the plot
        generation_indices.append(gen_count)

        # wait for 2 seconds between consective output generation
        # time.sleep(2)

        # Draw and save a plot at the first generation, and every 1/5ths of the total generations after that,
        # and then at the very last generation.
        if gen_count == 0 or gen_count % (
                NUMBER_OF_GENERATIONS /
                5) == 0 or gen_count == NUMBER_OF_GENERATIONS - 1:
            plot_drawer.drawPlot("Plot_For_" + 'Gen_' + str(gen_count),
                                 generation_indices,
                                 average_results,
                                 max_results,
                                 min_results,
                                 path='Graphs')