Exemple #1
0
 def do_square(self, arg):
     """Draw Square: square 100"""
     self.results.writeToFile("Drawing a square")
     self.parse(arg)
     directions = [0, 90, 180, 270]
     for i in directions:
         TurtleDrawer.draw_line(self, i, self.data)
Exemple #2
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)
Exemple #3
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)
Exemple #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)
Exemple #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)
Exemple #6
0
 def do_circle(self, arg):
     """Draw Circle: circle 50"""
     self.results.writeToFile("Drawing a circle")
     self.parse(arg)
     TurtleDrawer.draw_circle(self, self.data)
Exemple #7
0
 def do_N(self, arg):
     """Draw line 0 degrees : N 100"""
     self.results.writeToFile("Draw line 0 degrees : N", arg)
     self.parse(arg)
     TurtleDrawer.draw_line(self, 0, self.data)
Exemple #8
0
 def do_X(self, arg):
     """Go Along : X 100"""
     self.results.writeToFile("Go Along : X ", arg)
     self.parse(arg)
     TurtleDrawer.go_along(self, self.data)
Exemple #9
0
 def do_Y(self, arg):
     """Go Down : Y 100"""
     self.results.writeToFile("Go Along : Y", arg)
     self.parse(arg)
     TurtleDrawer.go_down(self, self.data)
Exemple #10
0
 def do_D(self, arg):
     """Pen Down : D"""
     self.results.writeToFile("Pen is down", arg)
     self.parse(arg)
     TurtleDrawer.pen_down(self.command)
Exemple #11
0
 def do_U(self, arg):
     """Pen Up : U"""
     self.results.writeToFile("Pen is up", arg)
     self.parse(arg)
     TurtleDrawer.pen_up(self.command)
 def setUp(self):
     self.Reader = ArgumentSourceReader(ArgumentParser(TurtleDrawer()))
     self.source = ['-e', '-t', '-k', '-g']
 def setUp(self):
     self.tkinterParser = ArgumentParser(TurtleDrawer())
     self.source = [
         '-c', '-e', '-t', '-k', '-g', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', '0'
     ]
Exemple #14
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')