Exemplo n.º 1
0
def run_aggr(l, n, px, selection_func):
    # Initialization
    pop = uniform(n, l)
    health = l - hamming_distance(pop)

    # Start loop
    last_mean_health = health.mean()
    final_iter_num = N_IT - 1
    last_counter = 0

    for i in range(1, N_IT):
        pop = selection_func(pop, health, n)
        pop = mutate(pop, px)
        health = l - hamming_distance(pop)
        mean_health = health.mean()

        if abs(last_mean_health - mean_health) < EPS:
            last_counter += 1
        else:
            last_counter = 0
        if last_counter >= 10:
            final_iter_num = i
            break
        last_mean_health = mean_health

    return final_iter_num, last_mean_health
Exemplo n.º 2
0
def run(run_id, l, n, px):
    hist_file = get_empty_histogram(20000, l)
    mean_health_ar = np.zeros(N_IT)

    pop = uniform(n, l)
    health = l - hamming_distance(pop)
    mean_health_ar[0] = health.mean()
    append_to_histogram(hist_file, pop, mean_health_ar[0], health.max(), 0)
    stop = N_IT
    for i in range(1, N_IT):
        pop = roulette(pop, health, n)
        pop = mutate(pop, px)
        health = l - hamming_distance(pop)
        mean_health = health.mean()
        append_to_histogram(hist_file, pop, mean_health, health.max(), i)

        mean_health_ar[i] = mean_health
        if converges(mean_health_ar, i):
            stop = i
            break

        if i % 100 == 0:
            print('Iteration:', i)

    df = pd.DataFrame(hist_file[:stop+2], columns=get_cols(l))
    name = f'task1\\task1_id_{run_id}_L_{l}_N_{n}_px_{px:.4f}'

    print('saving')
    df.to_sql(name, engine)
    print('to_db')
    write_file(name, df)
    print('to_file')
Exemplo n.º 3
0
def run(cursor, conn, run_id, l, n, px, sql_script, estim, init, sel_type,
        size_pop_type):
    cursor.execute(
        f"SELECT good_locuses, bad_locuses, lethal_locuses  FROM locus_helper WHERE l={l}"
    )
    row = cursor.fetchone()
    kwargs = {
        'good': np.array(row[0], dtype=np.int8),
        'bad': np.array(row[1], dtype=np.int8),
        'lethal': np.array(row[2], dtype=np.int8)
    }

    estimation = ESTIM_MAP[estim]
    initialization = INIT_MAP[init]
    selection = SELECTION_MAP[sel_type]
    size_pop = SIZE_POP[size_pop_type]
    pop = initialization(size_pop(0, 1), l, **kwargs)
    health = estimation(pop, **kwargs)

    store_in_db(cursor, conn, sql_script, run_id, pop, health, health.mean(),
                0, init, estim, sel_type, size_pop_type, **kwargs)
    succ = False
    for i in range(1, N_IT):
        if i % 50 == 0:
            print(i)
        pop = selection(pop, health, size_pop(i, len(pop)))
        pop = mutate(pop, px)
        health = estimation(pop, **kwargs)
        mean_health = health.mean()
        store_in_db(cursor, conn, sql_script, run_id, pop, health, mean_health,
                    i, init, estim, sel_type, size_pop_type, **kwargs)

    return succ
Exemplo n.º 4
0
def run_aggr(init_func, estimation_func, selection_func, l, n, px, cursor):
    kwargs = {}
    if 'locuses' in estimation_func.__name__ or 'locuses' in selection_func.__name__:
        cursor.execute(
            f"SELECT good_locuses, bad_locuses, lethal_locuses  FROM locus_helper WHERE l={l}"
        )
        row = cursor.fetchone()

        kwargs.update({
            'good': np.array(row[0], dtype=np.int8),
            'bad': np.array(row[1], dtype=np.int8),
            'lethal': np.array(row[2], dtype=np.int8)
        })

    pop = init_func(n, l, **kwargs)
    health = estimation_func(pop, **kwargs)

    # Start loop
    last_mean_health = health.mean()
    final_iter_num = N_IT - 1
    last_counter = 0
    poly_d1 = []
    poly_d2 = []
    mean_health_ar = []

    for i in range(0, N_IT):
        pop = selection_func(pop, health, n)
        pop = mutate(pop, px)
        health = estimation_func(pop, **kwargs)
        mean_health = health.mean()
        mean_health_ar.append(mean_health)

        if abs(last_mean_health - mean_health) < EPS:
            last_counter += 1
        else:
            last_counter = 0
        if last_counter >= 10:
            final_iter_num = i
            break
        last_mean_health = mean_health
        if 'good' in kwargs:
            poly_d1.append(locus_roles_polymorphous(pop, **kwargs))
            poly_d2.append(locus_roles_polymorphous(pop, v=1, **kwargs))
        else:
            poly_d1.append(simple_polymorphous(pop))
            poly_d2.append(simple_polymorphous(pop, v=1))

    return final_iter_num, mean_health_ar, poly_d1, poly_d2
Exemplo n.º 5
0
def run_one_simulation(
    init_func: callable,
    estimation_func: callable,
    selection_func: callable,
    l: int,
    n: int,
    px: float,
    stop_count=STOP_COUNT,
):
    kwargs = {}

    pop = init_func(n, l, **kwargs)
    health = estimation_func(pop, **kwargs)

    # Start loop
    last_mean_health = health.mean()
    final_iter_num = N_IT - 1
    last_counter = 0
    poly_d1 = []
    mean_health_ar = []

    for i in range(0, N_IT):
        pop = selection_func(pop, health, n)
        pop = mutate(pop, px)
        health = estimation_func(pop, **kwargs)
        mean_health = health.mean()
        mean_health_ar.append(mean_health)

        if abs(last_mean_health - mean_health) < EPS:
            last_counter += 1
        else:
            last_counter = 0
        if last_counter >= stop_count:
            final_iter_num = i
            break

        last_mean_health = mean_health
        if 'good' in kwargs:
            poly_d1.append(locus_roles_polymorphous(pop, **kwargs))
        else:
            poly_d1.append(simple_polymorphous(pop))

    return final_iter_num, mean_health_ar, poly_d1
Exemplo n.º 6
0
def process_single_run(
    param_set: ParamSet,
    pmax: float,
    run_num: int,
    test_suite_id=None,
    run_set_id=None,
):
    run = Run(number=run_num)
    init_pop = InitPopulation.get(
        run_number=run_num,
        init=param_set.init,
        L=param_set.L,
        N=param_set.N,
        dim_n=param_set.func_case.func_param.dim_n,
        accuracy_decimals=param_set.func_case.func_param.accuracy_decimals,
    )
    run.init_population = init_pop.id
    init_pop_seed = init_pop.seed
    # TODO init disrt
    run.init_distr_health = None

    # Helping vars
    coder_info = param_set.encoding
    coder_info['a'] = param_set.func_case.func_param.interval_a
    coder_info['b'] = param_set.func_case.func_param.interval_b
    f_init = mappers.INIT_MAP[param_set.init]
    f_select = mappers.SEL_TYPE_MAP[param_set.sel_type]
    f_estim = get_estimation_function(param_set)
    STEPS_BACK, MAX_NFE, EPS = get_stop_cond_params(param_set)
    N = param_set.N
    L = param_set.L

    logger.info('Run taken into work',
                extra={
                    'data': {
                        'param_set_id': param_set.id,
                        'run_num': run_num,
                        'run_set_id': run_set_id,
                        'test_suite_id': test_suite_id,
                    }
                })

    # Initialization
    # pop: population with decimal values
    # pop_encoded: population represented as 1s and 0s
    pop_encoded = f_init(N, L, init_pop_seed)
    pop = decode(pop_encoded, L, coder_info)
    health = f_estim(pop)
    nfe = N
    iter_num = 0
    mean_health_ar = [health.mean()]
    max_health_ar = [health.max()]
    step = 0
    prev_mean_health = health.mean()

    while nfe < MAX_NFE and not step > STEPS_BACK:
        pop_encoded = f_select(pop_encoded, health, N)
        pop_encoded = crossover(pop_encoded, param_set.crossover_pc)
        pop_encoded = mutate(pop_encoded, pmax)
        pop = decode(pop_encoded, L, coder_info)
        health = f_estim(pop)
        nfe += N
        iter_num += 1
        mean_health = health.mean()
        mean_health_ar.append(mean_health)
        max_health_ar.append(health.max())

        if abs(mean_health - prev_mean_health) < EPS:
            step += 1
        else:
            step = 0
        prev_mean_health = mean_health

    # Last values
    last_pop = pop
    last_health = health
    last_pop_encoded = pop_encoded

    run.is_succ = nfe < MAX_NFE
    run.NFE = nfe
    run.iter_num = iter_num

    if run.is_succ:
        # Helping vars
        optimal_val = np.array([param_set.func_case.extremums[0]
                                ])  # optimal dec X for the function
        optimal_encoded = encode(
            optimal_val, L, coder_info)  # optimal bin/gray X for this function
        f_opt = f_estim(optimal_val)[0]  # value of function in extremum
        avg = last_health.mean()  # average value of health
        maxp = last_health.max()
        best_index = last_health.argmax()
        maxp_encoded = last_pop_encoded[best_index]

        run.avg_health = avg
        run.max_health = last_health.max()
        run.std_health = last_health.std()

        run.avg_health_deviation_to_opt_abs = abs(avg - f_opt)
        run.avg_health_deviation_to_opt_rel = abs((avg - f_opt) / f_opt)
        run.best_health_deviation_to_opt_abs = abs(maxp - f_opt)
        run.best_health_deviation_to_opt_rel = abs((maxp - f_opt) / f_opt)

        run.eucl_d_to_extremum = abs(maxp - f_opt)
        run.hamm_d_to_extremum = hamming_distance_between(
            maxp_encoded, optimal_encoded)

        run.best_ind = last_pop[best_index]
        run.best_ind_n = len(np.argwhere(last_pop == last_pop[best_index]))

        #
        run.final_distr_hamm = target_hamming_distribution(
            optimal_encoded, pop_encoded)
        run.final_distr_health = None
        run.final_distr_pairwise = pairwise_hamming_distribution(pop_encoded)

        run.max_avg_health_arr = max_health_ar
        run.avg_avg_health_arr = mean_health_ar

        if test_suite_id is not None:
            run.test_suite = test_suite_id
        else:
            run.run_set = run_set_id

    run.save()
    logger.info('Run completed successfully',
                extra={'data': {
                    'run_id': run.id
                }})

    return run