Пример #1
0
def demo_sweep_intersections(n, out):
    for i in range(n):
        try:
            print("{}{}{}{}".format(
                Terminal.bold,
                Terminal.blue,
                i,
                Terminal.end,
            ))
            seed(i)
            xs = random_segments(15)
            (segments, points) = sweep_intersections(xs)
            ax = init_plot()
            plot_points(ax, points)
            plot_segments(ax, segments)
            export("{}/sweep_intersections_{}.png".format(out, i))
            (brute_segments, brute_points) = brute_sweep_intersections(xs)
            ax = init_plot()
            plot_points(ax, brute_points)
            plot_segments(ax, brute_segments)
            export("{}/brute_sweep_intersections_{}.png".format(out, i))
            equal = sorted(points) == sorted(brute_points)
            print("same points : {}{}{}{}\n\n".format(
                Terminal.bold,
                Terminal.green if equal else Terminal.red,
                equal,
                Terminal.end,
            ))
        except:
            pass
Пример #2
0
def main():
    """
    run the program
    """

    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option("--num-obstacles", type='int',
                      dest="num_obstacles", default=NUM_OBSTACLES,
                      help="number of obstacles")

    parser.add_option("--pop-size", type='int',
                      dest="pop_size", default=POP_SIZE,
                      help="population size")

    parser.add_option("--crossover-rate", type='float',
                      dest="crossover_rate", default=CROSSOVER_RATE,
                      help="crossover rate")

    parser.add_option("--mutation-rate", type='float',
                      dest="mutation_rate", default=MUTATION_RATE,
                      help="mutation rate")

    parser.add_option("--max-generations", type='int',
                      dest="max_generations", default=MAX_GENERATIONS,
                      help="maximum nubmer of generations")

    options, args = parser.parse_args()

    obstacles = generate_obstacles(options.num_obstacles)
    disks = generate_disks(options.pop_size)

    chromo_length = 3 # x, y, r
    alg = Algorithm(chromo_length,
                    options.pop_size, options.crossover_rate,
                    options.mutation_rate)
    init_plot(WIDTH, HEIGHT)

    while alg.generation < options.max_generations:

        for c, chromosome in enumerate(alg.chromosomes):
            disks[c].set_chromosome(chromosome)

        for i, disk in enumerate(disks):
            disk.update(obstacles)
            alg.chromosomes[i].fitness = disk.fitness

        redraw_plot(obstacles, disks)

        alg.epoch()
        print 'generation', alg.generation
Пример #3
0
def demo_convex_hull(n, out):
    for i in range(n):
        seed(i)
        points = random_points(25)
        ax = init_plot()
        plot_points(ax, points)
        plot_segments(ax, convex_hull(points))
        export("{}/convex_hull_{}.png".format(out, i))
Пример #4
0
def demo_point_of_intersection(n, out):
    for i in range(n):
        seed(i)
        segments = random_segments(2)
        point = point_of_intersection(*segments)
        ax = init_plot()
        if point:
            plot_points(ax, [point])
        plot_segments(ax, segments)
        export("{}/point_of_intersection_{}.png".format(out, i))
Пример #5
0
from copy import deepcopy

import plot
from package_simulation import simulate_packages
from scheduling import round_robin, fair_queuing

print('# =============== #')
print('# Scheduling Plot #')
print('# =============== #')
print('© Dominic Plein 11/2020')

# Plot
fig, (ax1, ax2, ax3, ax4, ax5) = plot.init_plot()

# Packages
sources, max_end_time = simulate_packages(15, 40, 10)
plot.plot_broken_barh(ax1, 'Eintreffende Pakete', sources, max_end_time)

# Round Robin (abbreviated as rr)
sources_rr, diff_rr, max_end_time_rr, data_rr = round_robin(deepcopy(sources))
plot.plot_broken_barh(ax2, 'Round-Robin', sources_rr, max_end_time_rr, diff_rr)
plot.plot_scatter(ax4, 'Round-Robin (Auswertung)', data_rr)

# Fair Queuing (abbreviated as fq)
sources_fq, diff_fq, max_end_time_fq, data_fq = fair_queuing(deepcopy(sources))
plot.plot_broken_barh(ax3, 'Fair Queuing', sources_fq, max_end_time_fq,
                      diff_fq)
plot.plot_scatter(ax5, 'Fair Queuing (Auswertung)', data_fq)

# Plot
ax1.set_xlim(0, max(max_end_time_rr, max_end_time_fq))
Пример #6
0
def main():
    """
    run the program
    """

    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option("--num-mines", type='int',
                      dest="num_mines", default=NUM_MINES,
                      help="number of mines")

    parser.add_option("--pop-size", type='int',
                      dest="pop_size", default=POP_SIZE,
                      help="population size")

    parser.add_option("--crossover-rate", type='float',
                      dest="crossover_rate", default=CROSSOVER_RATE,
                      help="crossover rate")

    parser.add_option("--mutation-rate", type='float',
                      dest="mutation_rate", default=MUTATION_RATE,
                      help="mutation rate")

    parser.add_option("--max-generations", type='int',
                      dest="max_generations", default=MAX_GENERATIONS,
                      help="maximum nubmer of generations")

    options, args = parser.parse_args()

    mines = generate_mines(options.num_mines)
    tanks = generate_tanks(options.pop_size)

    alg = Algorithm(len(tanks[0].neural_net.get_weights()),
                    options.pop_size, options.crossover_rate,
                    options.mutation_rate)

    init_plot(WIDTH, HEIGHT)

    t = 0
    while alg.generation < options.max_generations:

        if t == 0:
            for c, chromosome in enumerate(alg.chromosomes):
                tanks[c].neural_net.set_weights(chromosome.weights)
                tanks[c].fitness = 0
                print c, tanks[c]

        if t < 150:
            for i, tank in enumerate(tanks):
                closest_mine = tank.update(mines)
                vec = Vector(closest_mine.position.x, closest_mine.position.y)
                distance = vec.distance(tank.position)
                if 2 > distance:
                    tank.fitness += 10
                    closest_mine.relocate()
                alg.chromosomes[i].fitness = tank.fitness
            redraw_plot(mines, tanks)
            t += 1

        else:
            t = 0
            alg.epoch()
            print 'generation', alg.generation
Пример #7
0
def main():
    """
    run the program
    """

    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option("--num-beams",
                      type='int',
                      dest="num_beams",
                      default=NUM_BEAMS,
                      help="number of beams")

    parser.add_option("--pop-size",
                      type='int',
                      dest="pop_size",
                      default=POP_SIZE,
                      help="population size")

    parser.add_option("--crossover-rate",
                      type='float',
                      dest="crossover_rate",
                      default=CROSSOVER_RATE,
                      help="crossover rate")

    parser.add_option("--mutation-rate",
                      type='float',
                      dest="mutation_rate",
                      default=MUTATION_RATE,
                      help="mutation rate")

    parser.add_option("--max-generations",
                      type='int',
                      dest="max_generations",
                      default=MAX_GENERATIONS,
                      help="maximum nubmer of generations")

    options, args = parser.parse_args()

    beams = generate_beams(options.num_beams)
    planes = generate_planes(options.pop_size)

    alg = Algorithm(len(planes[0].neural_net.get_weights()), options.pop_size,
                    options.crossover_rate, options.mutation_rate)

    init_plot(WIDTH, HEIGHT)

    def describe():
        fitness = 0
        action = 0
        for i, plane in enumerate(planes):
            fitness += plane.fitness
            action += plane.action
        print t, 'FITNESS', fitness / len(planes)
        print t, 'ACTION', action / len(planes)

    def reset():
        for b in beams:
            b.reset()

        for p in planes:
            p.reset()

    def animate():
        # ----
        chromosome = sorted(alg.chromosomes,
                            key=lambda x: x.fitness,
                            reverse=True)[0]

        for i, plane in enumerate(planes):
            plane.neural_net.set_weights(chromosome.weights)

        for _ in range(150):
            for i, plane in enumerate(planes):
                beam = plane.update(beams)
            redraw_plot(beams, planes)

        reset()

    t = 0
    while alg.generation < options.max_generations:

        if t == 0:
            print len(alg.chromosomes)
            for c, chromosome in enumerate(alg.chromosomes):
                planes[c].neural_net.set_weights(chromosome.weights)
                planes[c].reset()
                print c, planes[c]

        if t < 300:
            for i, plane in enumerate(planes):
                beam = plane.update(beams)
                alg.chromosomes[i].fitness = plane.fitness
            # describe()


#           redraw_plot(beams, planes)
            t += 1

        else:
            t = 0
            reset()
            alg.epoch()
            print 'generation', alg.generation

            if alg.generation % 10 == 0:
                animate()
Пример #8
0
def main():
    """
    run the program
    """

    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option("--num-obstacles",
                      type='int',
                      dest="num_obstacles",
                      default=NUM_OBSTACLES,
                      help="number of obstacles")

    parser.add_option("--pop-size",
                      type='int',
                      dest="pop_size",
                      default=POP_SIZE,
                      help="population size")

    parser.add_option("--crossover-rate",
                      type='float',
                      dest="crossover_rate",
                      default=CROSSOVER_RATE,
                      help="crossover rate")

    parser.add_option("--mutation-rate",
                      type='float',
                      dest="mutation_rate",
                      default=MUTATION_RATE,
                      help="mutation rate")

    parser.add_option("--max-generations",
                      type='int',
                      dest="max_generations",
                      default=MAX_GENERATIONS,
                      help="maximum nubmer of generations")

    options, args = parser.parse_args()

    obstacles = generate_obstacles(options.num_obstacles)
    disks = generate_disks(options.pop_size)

    chromo_length = 3  # x, y, r
    alg = Algorithm(chromo_length, options.pop_size, options.crossover_rate,
                    options.mutation_rate)
    init_plot(WIDTH, HEIGHT)

    while alg.generation < options.max_generations:

        for c, chromosome in enumerate(alg.chromosomes):
            disks[c].set_chromosome(chromosome)

        for i, disk in enumerate(disks):
            disk.update(obstacles)
            alg.chromosomes[i].fitness = disk.fitness

        redraw_plot(obstacles, disks)

        alg.epoch()
        print 'generation', alg.generation