コード例 #1
0
ファイル: ga.py プロジェクト: msu-coinlab/pymoo
def comp_by_cv_and_fitness(pop, P, **kwargs):
    S = np.full(P.shape[0], np.nan)

    for i in range(P.shape[0]):
        a, b = P[i, 0], P[i, 1]

        # if at least one solution is infeasible
        if pop[a].CV > 0.0 or pop[b].CV > 0.0:
            S[i] = compare(a,
                           pop[a].CV,
                           b,
                           pop[b].CV,
                           method='smaller_is_better',
                           return_random_if_equal=True)

        # both solutions are feasible just set random
        else:
            S[i] = compare(a,
                           pop[a].F,
                           b,
                           pop[b].F,
                           method='smaller_is_better',
                           return_random_if_equal=True)

    return S[:, None].astype(int)
コード例 #2
0
def binary_tournament(pop, P, algorithm, **kwargs):
    n_tournaments, n_parents = P.shape

    if n_parents != 2:
        raise ValueError("Only implemented for binary tournament!")

    tournament_type = algorithm.tournament_type
    S = np.full(n_tournaments, np.nan)

    for i in range(n_tournaments):

        a, b = P[i, 0], P[i, 1]
        a_cv, a_f, b_cv, b_f, = pop[a].CV[0], pop[a].F, pop[b].CV[0], pop[b].F
        rank_a, cd_a = pop[a].get("rank", "crowding")
        rank_b, cd_b = pop[b].get("rank", "crowding")

        # if at least one solution is infeasible
        if a_cv > 0.0 or b_cv > 0.0:
            S[i] = compare(a,
                           a_cv,
                           b,
                           b_cv,
                           method='smaller_is_better',
                           return_random_if_equal=True)

        # both solutions are feasible
        else:

            if tournament_type == 'comp_by_dom_and_crowding':
                rel = Dominator.get_relation(a_f, b_f)
                if rel == 1:
                    S[i] = a
                elif rel == -1:
                    S[i] = b

            elif tournament_type == 'comp_by_rank_and_crowding':
                S[i] = compare(a,
                               rank_a,
                               b,
                               rank_b,
                               method='smaller_is_better')

            else:
                raise Exception("Unknown tournament type.")

            # if rank or domination relation didn't make a decision compare by crowding
            if np.isnan(S[i]):
                S[i] = compare(a,
                               cd_a,
                               b,
                               cd_b,
                               method='larger_is_better',
                               return_random_if_equal=True)

    return S[:, None].astype(int, copy=False)
コード例 #3
0
def constr_binary_tournament(pop, P, algorithm, **kwargs):
    n_tournaments, n_parents = P.shape

    if n_parents != 2:
        raise ValueError("Only implemented for binary tournament!")

    S = np.full(n_tournaments, np.nan)

    for i in range(n_tournaments):

        a, b = P[i, 0], P[i, 1]
        a_cv, a_f, b_cv, b_f, = pop[a].CV[0], pop[a].F, pop[b].CV[0], pop[b].F
        a_below, b_below = pop[a].get("below"), pop[b].get("below")

        if a_cv > 0.0 and b_cv > 0.0:
            if a_below and b_below:
                S[i] = compare(a,
                               a_cv,
                               b,
                               b_cv,
                               method='smaller_is_better',
                               return_random_if_equal=True)
            elif not a_below and not b_below:
                S[i] = compare(a,
                               a_f,
                               b,
                               b_f,
                               method='smaller_is_better',
                               return_random_if_equal=True)
            elif a_below and not b_below:
                S[i] = a
            elif not a_below and b_below:
                S[i] = b

        elif a_cv == 0.0 and b_cv == 0.0:
            S[i] = np.random.choice([a, b])
            # S[i] = compare(a, a_f, b, b_f, method='smaller_is_better', return_random_if_equal=True)
        else:
            S[i] = np.random.choice([a, b])

    return S[:, None].astype(int, copy=False)
コード例 #4
0
ファイル: ga_niching.py プロジェクト: msu-coinlab/pymoo
def comp_by_cv_and_clearing_fitness(pop, P, **kwargs):
    S = np.full(P.shape[0], np.nan)

    for i in range(P.shape[0]):
        a, b = P[i, 0], P[i, 1]

        # if at least one solution is infeasible
        if pop[a].CV[0] > 0.0 or pop[b].CV[0] > 0.0:
            S[i] = compare(a, pop[a].CV, b, pop[b].CV,
                           method='smaller_is_better',
                           return_random_if_equal=True)

        # first compare by the round the individual was selected
        else:
            S[i] = compare(a, pop[a].get("iter"), b, pop[b].get("iter"), method='smaller_is_better')

            # if it was the same round - then use the rank of the fitness directly
            if np.isnan(S[i]):
                S[i] = compare(a, pop[a].get("rank"), b, pop[b].get("rank"),
                               method='smaller_is_better', return_random_if_equal=True)

    return S[:, None].astype(int)
コード例 #5
0
ファイル: unsga3.py プロジェクト: msu-coinlab/pymoo
def comp_by_rank_and_ref_line_dist(pop, P, **kwargs):
    S = np.full(P.shape[0], np.nan)

    for i in range(P.shape[0]):
        a, b = P[i, 0], P[i, 1]

        # if at least one solution is infeasible
        if pop[a].CV > 0.0 or pop[b].CV > 0.0:
            S[i] = compare(a,
                           pop[a].CV,
                           b,
                           pop[b].CV,
                           method='smaller_is_better',
                           return_random_if_equal=True)

        # both solutions are feasible
        else:

            # if in the same niche select by rank
            if pop[a].get("niche") == pop[b].get("niche"):

                if pop[a].get("rank") != pop[b].get("rank"):
                    S[i] = compare(a,
                                   pop[a].get("rank"),
                                   b,
                                   pop[b].get("rank"),
                                   method='smaller_is_better')

                else:
                    S[i] = compare(a,
                                   pop[a].get("dist_to_niche"),
                                   b,
                                   pop[b].get("dist_to_niche"),
                                   method='smaller_is_better')

        if np.isnan(S[i]):
            S[i] = np.random.choice([a, b])

    return S[:, None].astype(int)