Exemplo n.º 1
0
def crs3(A_shared, function_number, vec_len):

    A = A_shared[:]
    rn.seed(1)
    iterations = 100000

    final_result = list()

    for iteration in range(iterations):

        i_W = np.argmax(np.array(A).T[-1])
        W = A[i_W]

        A_exclude_W = A.copy()
        A_exclude_W.remove(W)

        R_chosen = rn.sample(A_exclude_W, vec_len)

        i_S = np.argmax(np.array(A_exclude_W).T[-1])
        S = A_exclude_W[i_S]

        centroid = get_G(R_chosen)

        P = get_P(centroid, W)
        Q = get_Q(centroid, W)
        R = get_R(centroid, W)

        if f.is_point_in_domain(P):

            P[-1] = eval('f.function_' + str(function_number) + '(P[:-1])')

            if P[-1] < S[-1]:
                if not f.is_point_in_domain(R):
                    W = P
                    A[i_W] = P
                    final_result.append(P[-1])
                else:
                    R[-1] = eval('f.function_' + str(function_number) +
                                 '(R[:-1])')
                    if R[-1] < S[-1]:
                        W = R
                        A[i_W] = R
                        final_result.append(R[-1])
                    else:
                        W = P
                        A[i_W] = P
                        final_result.append(P[-1])
        else:
            if not f.is_point_in_domain(Q):
                continue
            else:
                Q[-1] = eval('f.function_' + str(function_number) + '(Q[:-1])')
                if Q[-1] < S[-1]:
                    W = Q
                    A[i_W] = Q
                    final_result.append(Q[-1])
                else:
                    continue

    return final_result[-1]
Exemplo n.º 2
0
def crs2(A_shared, function_number, vec_len):
    A = A_shared[:]
    rn.seed(1)
    iterations = 100000

    final_result = list()

    for iteration in range(iterations):

        i_M = np.argmax(np.array(A).T[-1])
        i_L = np.argmin(np.array(A).T[-1])
        M = A[i_M]
        L = A[i_L]

        A_exclude_L = A.copy()
        A_exclude_L.remove(L)

        R_chosen = rn.sample(A_exclude_L, vec_len)
        R_n_plus_1 = R_chosen.pop()
        centroid = get_centroid(R_chosen)
        P = get_P(centroid, R_n_plus_1)

        if f.is_point_in_domain(P):

            P[-1] = eval('f.function_' + str(function_number) + '(P[:-1])')

            if P[-1] < M[-1]:
                M = P
                A[i_M] = P
                final_result.append(P[-1])

    return P[-1]
Exemplo n.º 3
0
def crs2(A, return_list, function_number, vec_len):

    rn.seed(1)
    iterations = 20000
    final_result = list()

    CLOSE_ENOUGH = 10e-4
    WANTED_RESULT = 0
    last_P_value = 10e4

    for iteration in range(iterations):
        # while abs(last_P_value - WANTED_RESULT) > CLOSE_ENOUGH:

        i_M = np.argmax(np.array(A).T[-1])
        i_L = np.argmin(np.array(A).T[-1])
        M = A[i_M]
        L = A[i_L]

        A_exclude_L = A.copy()
        A_exclude_L.remove(L)

        R_chosen = rn.sample(A_exclude_L, vec_len)
        R_n_plus_1 = R_chosen.pop()
        centroid = get_centroid(R_chosen)
        P = get_P(centroid, R_n_plus_1)

        if f.is_point_in_domain(P):

            P[-1] = eval('f.function_' + str(function_number) + '(P[:-1])')

            if P[-1] < M[-1]:
                M = P
                A[i_M] = P
                final_result.append(P[-1])

                last_P_value = P[-1]

    return_list.append(P[-1])
def crs3(A, return_list, function_number, vec_len):

    rn.seed(1)
    iterations = 20000

    final_result = list()

    CLOSE_ENOUGH = 10e-4
    WANTED_RESULT = 0
    last_P_value = 10e4

    # while abs(last_P_value - WANTED_RESULT) > CLOSE_ENOUGH:

    for iteration in range(iterations):

        i_W = np.argmax(np.array(A).T[-1])
        W = A[i_W]

        A_exclude_W = A.copy()
        A_exclude_W.remove(W)

        R_chosen = rn.sample(A_exclude_W, vec_len)

        i_S = np.argmax(np.array(A_exclude_W).T[-1])
        S = A_exclude_W[i_S]

        centroid = get_G(R_chosen)

        P = get_P(centroid, W)
        Q = get_Q(centroid, W)
        R = get_R(centroid, W)

        if f.is_point_in_domain(P):

            P[-1] = eval('f.function_' + str(function_number) + '(P[:-1])')

            if P[-1] < S[-1]:
                if not f.is_point_in_domain(R):
                    W = P
                    A[i_W] = P
                    final_result.append(P[-1])
                    last_P_value = P[-1]
                else:
                    R[-1] = eval('f.function_' + str(function_number) +
                                 '(R[:-1])')
                    if R[-1] < S[-1]:
                        W = R
                        A[i_W] = R
                        final_result.append(R[-1])
                        last_P_value = P[-1]
                    else:
                        W = P
                        A[i_W] = P
                        final_result.append(P[-1])
                        last_P_value = P[-1]
        else:
            if not f.is_point_in_domain(Q):
                continue
            else:
                Q[-1] = eval('f.function_' + str(function_number) + '(Q[:-1])')
                if Q[-1] < S[-1]:
                    W = Q
                    A[i_W] = Q
                    final_result.append(Q[-1])
                    last_P_value = P[-1]
                else:
                    continue
    #
    #     if iteration % 100 == 0:
    #         print(str(iteration) + ' Kolejna wartość P' + str(P))
    #
    #         if iteration//100 < 10:
    #             f_name = 'results/CRS2_00{}'.format(iteration//100)
    #         elif iteration//100 < 100:
    #             f_name = 'results/CRS2_0{}'.format(iteration//100)
    #         else:
    #             f_name = 'results/CRS2_{}'.format(iteration//100)
    #
    #         hp.plot_3d_scatter(whole_set=A,
    #                            centroid=centroid,
    #                            r_last=W,
    #                            optimum=P,
    #                            f_name=f_name,
    #                            func_number=FUNCTION_NUMBER)
    #
    # hp.plot_convergence(final_result)

    return_list.append(final_result[-1])
Exemplo n.º 5
0
def CRS3(FUNCTION_NUMBER, vec_len):
    final_result = list()
    rn.seed(1)
    N = 800
    iterations = 20000
    start_rand = time.time()
    A = eval('f.generate_points_func_' + str(FUNCTION_NUMBER) + '(N, vec_len)')
    start_iter = time.time()
    for iteration in range(iterations):

        i_W = np.argmax(np.array(A).T[-1])
        W = A[i_W]

        A_exclude_W = A.copy()
        A_exclude_W.remove(W)

        R_chosen = rn.sample(A_exclude_W, vec_len)

        i_S = np.argmax(np.array(A_exclude_W).T[-1])
        S = A_exclude_W[i_S]

        centroid = get_G(R_chosen)

        P = get_P(centroid, W)
        Q = get_Q(centroid, W)
        R = get_R(centroid, W)

        if f.is_point_in_domain(P):

            P[-1] = eval('f.function_' + str(FUNCTION_NUMBER) + '(P[:-1])')

            if P[-1] < S[-1]:
                if not f.is_point_in_domain(R):
                    W = P
                    A[i_W] = P
                    final_result.append(P[-1])
                else:
                    R[-1] = eval('f.function_' + str(FUNCTION_NUMBER) +
                                 '(R[:-1])')
                    if R[-1] < S[-1]:
                        W = R
                        A[i_W] = R
                        final_result.append(R[-1])
                    else:
                        W = P
                        A[i_W] = P
                        final_result.append(P[-1])
        else:
            if not f.is_point_in_domain(Q):
                continue
            else:
                Q[-1] = eval('f.function_' + str(FUNCTION_NUMBER) + '(Q[:-1])')
                if Q[-1] < S[-1]:
                    W = Q
                    A[i_W] = Q
                    final_result.append(Q[-1])
                else:
                    continue
        if iteration % 100 == 0:
            print(str(iteration) + ' Kolejna wartość P' + str(P))

            if iteration // 100 < 10:
                f_name = 'results/CRS3_00{}'.format(iteration // 100)
            elif iteration // 100 < 100:
                f_name = 'results/CRS3_0{}'.format(iteration // 100)
            else:
                f_name = 'results/CRS3_{}'.format(iteration // 100)

            hp.plot_3d_scatter(whole_set=A,
                               centroid=centroid,
                               r_last=W,
                               optimum=P,
                               f_name=f_name,
                               func_number=FUNCTION_NUMBER)

    hp.plot_convergence(final_result)
    end = time.time()
    iter_time = end - start_iter
    rand_time = start_iter - start_rand
    full_time = end - start_rand

    return (iter_time, rand_time, full_time, final_result[-1])