Esempio n. 1
0
def get_fitness(pop):
    #get score for each gene
    for p in pop:
        score = 0
        for q in pop:
            if GA._is_dominate(q, p):
                score += 1
        p.update_flag({'score': score})

    #get raw fitness for each gene
    for p in pop:
        raw = 0
        for q in pop:
            if GA._is_dominate(q, p):
                q_score = q.get_flag("score")
                raw += q_score
        p.update_flag({"raw": raw})

    #get density and fitness for each gene
    N = math.ceil(math.sqrt(GLOB.POP + GLOB.MAX_ARCHIVE_SPEA2))
    for p in pop:
        dens = 0
        for q in pop:  #get distance from others
            dist = np.linalg.norm(p.get_eval() - q.get_eval())
            q.update_flag({'dist': dist})
        sorted_pop = sorted(pop, key=lambda gene: gene.get_flag('dist'))
        dens = sorted_pop[N + 1].get_flag('dist')
        fitness = dens + p.get_flag('raw')
        p.update_flag({'dense': dens, 'fitness': fitness})
Esempio n. 2
0
def collect_non_dominated(pop, CA, DA):
    def distance(eval1, eval2):
        return np.linalg.norm(eval1 - eval2)

    new_CA = CA[:]
    new_DA = DA[:]

    #step1: update CA and DA
    for i in range(len(pop)):
        p = pop[i]

        #check if non dominated
        p.update_flag({"non-dominated": True, "dominate-other": False})
        for archive in new_CA + new_DA:
            if GA._is_dominate(archive, p):
                p.update_flag({"non-dominated": False})
                break
        if not p.get_flag("non-dominated"):  #skip if not non-dominated
            continue

        #check if it dominated other
        for archive in new_CA:
            if GA._is_dominate(p, archive):
                new_CA.remove(archive)
                p.update_flag({"dominate-other": True})
        for archive in new_DA:
            if GA._is_dominate(p, archive):
                new_DA.remove(archive)
                p.update_flag({"dominate-other": True})

        if p.get_flag("dominate-other"):  #dominate other, add at CA
            new_CA.append(p)
        else:  #not dominate other, add at DA
            new_DA.append(p)

    #step2: limit the size of CA and DA
    if len(new_CA + new_DA) > GLOB.MAX_ARCHIVE_TAEA:
        if len(new_CA) > GLOB.MAX_ARCHIVE_TAEA:
            new_DA = []
            return
        for pop_da in new_DA:
            pop_da.update_flag({"len": GLOB.LARGE})
            for pop_ca in new_CA:
                dist = distance(pop_da.get_eval(), pop_ca.get_eval())
                if dist < pop_da.get_flag("len"):
                    pop_da.update_flag({"len": dist})

        #remove some population in DA
        new_DA = sorted(new_DA, key=lambda pop: pop.get_flag("len"))
        new_DA = new_DA[-GLOB.MAX_ARCHIVE_TAEA + len(new_CA):]

    return new_CA, new_DA