示例#1
0
def GA(max_epochs, N, fitness, seed=None):

    if seed is not None:
        np.random.seed(seed)

    dimension = fitness.dim()

    cross_prob = 0.65
    mut_prob = 0.05

    # create population
    population = Population(N,
                            dimension,
                            fitness,
                            crossover_prob=cross_prob,
                            mutation_prob=mut_prob)

    for epoch in range(max_epochs):

        population.run_generation()

        if epoch % 10 == 0 and epoch > 1:
            size = population.size
            best_error = population.best_error
            print("Generation = " + str(epoch) +
                  " best error = %.3f" % best_error)
            print_position(population.best_position)
            print("Size: {}".format(size))

    print("")

    return population.best_position
示例#2
0
    print("Capturing frames")
    num_frames = 0

    # Start time
    start = time.time()

    # Grab a few frames
    fps_show = 0

    while True:
        frame_start = time.time()
        ret, frame = video.read()
        if ret == True:
            h, w, _ = np.shape(frame)
            position = detect(frame)
            print_position(h, w, position,
                           int(1 / (time.time() - frame_start)))
            num_frames += 1
        if frame_start - start > time_to_run:
            break

    # End time
    end = time.time()

    # Time elapsed
    seconds = end - start
    print("Time taken : {0} seconds".format(seconds))

    # Calculate frames per second
    fps = num_frames / seconds
    print("Estimated fps : {0}".format(fps))
示例#3
0
                            fitness,
                            crossover_prob=cross_prob,
                            mutation_prob=mut_prob)

    for epoch in range(max_epochs):

        population.run_generation()

        if epoch % 10 == 0 and epoch > 1:
            size = population.size
            best_error = population.best_error
            print("Generation = " + str(epoch) +
                  " best error = %.3f" % best_error)
            print_position(population.best_position)
            print("Size: {}".format(size))

    print("")

    return population.best_position


if __name__ == "__main__":

    fitness = F.Schwefel(dim=2)
    epochs = 500

    best_position = GA(epochs, 50, fitness)

    print("Solution found after {} epochs".format(epochs))
    print_position(best_position)
    print("Best error: {:.3f}".format(fitness(best_position)))
示例#4
0
from utils import (start_position, update_position, create_view,
                   print_position)
import engine
from tqdm import tqdm
from time_limit import time_limit

ended = False
current_position = start_position
possible = frozenset({start_position})

while not ended:

    # show the current view
    view = create_view(current_position, False)
    print_position(view)

    # take user input
    inp = input("select piece\n")
    begin = tuple(int(i) for i in inp.split(","))
    inp = input("select target\n")
    end = tuple(int(i) for i in inp.split(","))
    user_move = [begin, end]

    # play the user move
    current_position = update_position(current_position, user_move, False)

    # show the current view
    view = create_view(current_position, False)
    print_position(view)

    # update the information of the computer