Example #1
0
def NSGA2(name):
    model = DimacsModel(name)
    toolbox = model.toolbox
    toolbox.register('mutate', model.bit_flip_mutate)
    toolbox.register('select', tools.selNSGA2)
    toolbox.register('mate', model.cxTwoPoint)
    # get the initial generation from the result of sway
    # and evaluate them
    pop = load_sway_results(model)
    toolbox.map(model.eval_ind, pop)

    # the parameters for NSGA-II as follows
    MU = 100
    NGEN = 100
    CXPB = 0.9

    # start the NSGA2 algorithms
    for _ in range(100-len(pop)):
        pop.append(random.choice(pop))
    random.shuffle(pop)

    for gen in range(1, NGEN):
        # vary the population
        tools.emo.assignCrowdingDist(pop)
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        map(model.eval, offspring)
        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)

    valid_pop = [i for i in pop if i.fitness.values[0] <=0]
    hv, spread, _, size, _ = stat_basing_on_pop(valid_pop, False)

    print(hv, spread, size)
    return hv, spread, size
Example #2
0
def IBEA(name):
    model = DimacsModel(name)
    toolbox = model.toolbox
    toolbox.register('mutate', model.bit_flip_mutate)
    toolbox.register('select', tools.selIBEA)
    toolbox.register('mate', model.cxTwoPoint)
    toolbox.register('clone', copy.deepcopy)
    # get the initial generation from the result of sway
    # and evaluate them
    pop = load_sway_results(model)
    toolbox.map(model.eval_ind, pop)

    # the parameters for  IBEA as follows
    MU = 100
    NGEN = 100
    CXPB = 0.9

    # start the NSGA2 algorithms
    for _ in range(100-len(pop)):
        pop.append(random.choice(pop))
    random.shuffle(pop)

    parents = pop[:]
    for gen in range(1, NGEN):
        # Vary the parents
        offspring = varAnd(parents, toolbox, CXPB, 0.2)
        pop[:] = parents + offspring

        for p in pop:
            if not p.fitness.valid:
                model.eval(p)

        # Select the next generation parents
        parents[:] = toolbox.select(pop, MU)

    # pdb.set_trace()
    valid_pop = [i for i in pop if i.fitness.values[0] <=0]
    hv, spread, _, size, _ = stat_basing_on_pop(valid_pop, False)

    print(hv, spread, size)
    return hv, spread, size