Example #1
0
def main():

    print('\033c', end='')

    ref = Color.from_rgb(args.color) if args.color else RandomColor()
    algo = Algo(args.population_size)

    n = 0
    ok = None
    start = datetime.now()
    while True:
        n += 1

        deltas = []
        for color in algo.population:
            delta = ref.delta_e(color)
            deltas.append(delta)
            if delta <= args.fitness:
                if ok is None or ok[1] > delta:
                    ok = (color, delta)

        dump(ref, algo, deltas)
        if ok:
            break
        if n == args.iterations:
            print('\033[1;31mFailed after {} iterations\033[0;0m'.format(n))
            exit(1)

        algo.tick(deltas)
        if args.delay:
            sleep(args.delay)

    stop = datetime.now()
    duration = (stop - start).total_seconds()
    color, delta = ok
    print('Color to find   : {} {}'.format(ref, ref.rgb))
    print('Color found     : {} {} (\u0394E = {:.3f})'.format(color,
                                                              color.rgb,
                                                              delta))
    print('Algorithm       : {}'.format(algo.__class__.__name__))
    print('Iterations      : {}'.format(n))
    print('Population size : {}'.format(args.population_size))
    print('Colors tested   : {}'.format(n * args.population_size))
    print('All that in     : {:.3f} seconds'.format(duration))