Exemplo n.º 1
0
def createToolbox(MINCOMP, MAXCOMP, GENSIZE, CONSTRAINTPENALTY, TOURNMENTSIZE,
                  GENECROSSOVERPROB, GENEMUTPROB):

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMin)

    toolbox = base.Toolbox()

    toolbox.register("attr_int", random.randint, MINCOMP, MAXCOMP)
    toolbox.register("individual",
                     tools.initRepeat,
                     creator.Individual,
                     toolbox.attr_int,
                     n=GENSIZE)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", evaluateIndividual)
    toolbox.decorate(
        "evaluate",
        tools.DeltaPenalty(sufficientNumberOfFormers, CONSTRAINTPENALTY,
                           distance_sufficientNumberOfFormers))
    toolbox.decorate(
        "evaluate",
        tools.DeltaPenalty(individualInsideDomain, CONSTRAINTPENALTY,
                           distance_individualInsideDomain))

    toolbox.register("mutate",
                     tools.mutUniformInt,
                     low=MINCOMP,
                     up=MAXCOMP,
                     indpb=GENEMUTPROB)
    toolbox.register("select", tools.selTournament, tournsize=TOURNMENTSIZE)
    toolbox.register("mate", tools.cxUniform, indpb=GENECROSSOVERPROB)

    return toolbox
Exemplo n.º 2
0
def init_opti():
    ### setup NSGA3 with deap (minimize the first two goals returned by the evaluate function and maximize the third one)
    creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, 1.0))
    creator.create("Individual", list, fitness=creator.FitnessMulti)

    ref_points = tools.uniform_reference_points(nobj=3, p=12)

    #initial individual and pop
    toolbox.register("initial_indi", initial_indi)
    toolbox.register("individual",
                     tools.initRepeat,
                     creator.Individual,
                     toolbox.initial_indi,
                     n=1)

    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    #evaluation and constraints
    toolbox.register("evaluate", evaluate)

    ##assign the feasibility of solutions and if not feasible a large number for the minimization tasks and a small number for the maximization task
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible,
                                                    (10e20, 10e20, 0)))

    #mate, mutate and select to perform crossover
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
    toolbox.register("select", tools.selNSGA3, ref_points=ref_points)
Exemplo n.º 3
0
    def setup(self,
              fitness_function_handle,
              constraint=None,
              GENOME_LENGTH=20,
              POPULATION_SIZE=100,
              P_CROSSOVER=0.5,
              P_MUTATION=0.2):
        '''Set up the parameters'''
        # Set the main variables
        self.GENOME_LENGTH = GENOME_LENGTH
        self.POPULATION_SIZE = POPULATION_SIZE
        self.P_CROSSOVER = P_CROSSOVER
        self.P_MUTATION = P_MUTATION

        # Set the lower level parameters
        self.toolbox = base.Toolbox()
        self.toolbox.register("attr_float", random.random)
        self.toolbox.register("individual", tools.initRepeat,
                              creator.Individual, self.toolbox.attr_float,
                              self.GENOME_LENGTH)
        self.toolbox.register("population", tools.initRepeat, list,
                              self.toolbox.individual)
        self.toolbox.register("evaluate", fitness_function_handle)
        self.toolbox.register("mate", tools.cxTwoPoint)  # Mating method
        self.toolbox.register("mutate", tools.mutFlipBit,
                              indpb=0.05)  # Mutation method
        self.toolbox.register("select", tools.selTournament,
                              tournsize=3)  # Selection method
        if constraint is not None:
            self.toolbox.decorate("evaluate",
                                  tools.DeltaPenalty(constraint, 7))
        self.stats = []  # Initialize stats vector
Exemplo n.º 4
0
def configuracionAlgoritmo_Experimento2():
    toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.2)
    toolbox.register("select", tools.selBest)
    toolbox.register("evaluate", evaluate)
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 1000000,
                                                    distance))
Exemplo n.º 5
0
def configuracionAlgoritmo_Experimento3():
    toolbox.register("mate", tools.cxUniform, indpb=0.2)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.2)
    toolbox.register("select", tools.selTournament, tournsize=3)
    toolbox.register("evaluate", evaluate)
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 1000000,
                                                    distance))
Exemplo n.º 6
0
    def __init__(self, heroClass, initialSelection, useLibrary):
        self.heroClass = heroClass
        self.cardPool = getAvailableCardIdsForConstruction(heroClass) if not useLibrary else getLibraryCardIdsForConstruction(heroClass)
        self.initialSelection = initialSelection
        self.toolbox = base.Toolbox()

        creator.create("FitnessMax", base.Fitness, weights=(1.0,))
        creator.create("Individual", list, fitness=creator.FitnessMax)
        self.toolbox.register("attr_gen", random.randint, 0, len(self.cardPool) - 1)
        self.toolbox.register("individual", tools.initRepeat, creator.Individual, self.toolbox.attr_gen, 30 - len(self.initialSelection))
        self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
        #----------
        # Operator registration
        #----------
        # register the goal / fitness function
        self.toolbox.register("evaluate", self.evalFct)
        self.toolbox.decorate("evaluate", tools.DeltaPenalty(self.feasible, -100))

        # register the crossover operator
        self.toolbox.register("mate", tools.cxTwoPoint)

        # register a mutation operator with a probability to
        # flip each attribute/gene of 0.05
        self.toolbox.register("mutate", tools.mutUniformInt, low=0, up=len(self.cardPool) - 1)

        # operator for selecting individuals for breeding the next
        # generation: each individual of the current generation
        # is replaced by the 'fittest' (best) of three individuals
        # drawn randomly from the current generation.
        self.toolbox.register("select", tools.selTournament)
Exemplo n.º 7
0
def get_toolbox(target_func, weights):

    # collect the atomic building blocks of the individuals
    pset = gp.PrimitiveSet("MAIN", 1)
    pset.addPrimitive(operator.add, 2)
    pset.addPrimitive(operator.sub, 2)
    pset.addPrimitive(operator.mul, 2)
    pset.addPrimitive(protectedDiv, 2)
    pset.addPrimitive(operator.neg, 1)
    pset.addPrimitive(protectedPow, 2)
    pset.addPrimitive(math.cos, 1)
    pset.addPrimitive(sinX, 1)
    pset.addEphemeralConstant("rand101", lambda: random.randint(-1, 1))
    pset.addEphemeralConstant("rand10010", lambda: random.randint(-10, 10))
    pset.addTerminal(math.pi)

    # define the general form of an individual
    creator.create("FitnessMulti", base.Fitness, weights=weights)
    creator.create("Individual",
                   gp.PrimitiveTree,
                   fitness=creator.FitnessMulti,
                   target_func=target_func)

    toolbox = base.Toolbox()

    toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)
    toolbox.register("individual", tools.initIterate, creator.Individual,
                     toolbox.expr)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("compile", gp.compile, pset=pset)

    eval_range = [x / 20. * math.pi
                  for x in range(-20, 20)] + [x for x in range(-20, 20)]
    toolbox.register("evaluate",
                     get_fitness,
                     target_func=target_func,
                     toolbox=toolbox,
                     points=eval_range)

    # add filters for unwanted behaviour
    for filter_ in filters:
        toolbox.decorate('evaluate', tools.DeltaPenalty(filter_, [1000., 0.]))

    toolbox.register("select", tools.selSPEA2)
    toolbox.register("mate", gp.cxOnePoint)
    toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
    toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)

    toolbox.decorate(
        "mate", gp.staticLimit(key=operator.attrgetter("height"),
                               max_value=17))
    toolbox.decorate("mate", gp.staticLimit(key=len, max_value=20))
    toolbox.decorate(
        "mutate",
        gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
    toolbox.decorate("mutate", gp.staticLimit(key=len, max_value=20))
    return toolbox
Exemplo n.º 8
0
def configuracionAlgoritmo(toolbox):
    # Se seleccionan procedimiento standard para cruce, mutacion y seleccio
    toolbox.register("mate", tools.cxOrdered)
    toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.2)
    toolbox.register("select", tools.selTournament, tournsize=3)
    # Se define cómo se evaluará cada individuo
    # En este caso, se hará uso de la función de evaluación que se ha definido en el modulo Evaluacion.py
    toolbox.register("evaluate", eval)
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 0, distance))
Exemplo n.º 9
0
    def setup(self,
              fitness_function_handle,
              constraint=None,
              GENOME_LENGTH=20,
              POPULATION_SIZE=100,
              P_CROSSOVER=0.5,
              P_MUTATION=0.2,
              vartype="float"):
        '''Set up the standard parameters'''

        # Set the main variables
        self.GENOME_LENGTH = GENOME_LENGTH
        self.POPULATION_SIZE = POPULATION_SIZE
        self.P_CROSSOVER = P_CROSSOVER
        self.P_MUTATION = P_MUTATION

        # Set the lower level parameters
        self.toolbox = base.Toolbox()

        # Boolean or float evolution
        if vartype == "boolean":
            self.toolbox.register("attr_bool", random.randint, 0, 1)
            self.toolbox.register("individual", tools.initRepeat,
                                  creator.Individual, self.toolbox.attr_bool,
                                  GENOME_LENGTH)
            self.toolbox.register("mutate",
                                  tools.mutUniformInt,
                                  low=0,
                                  up=1,
                                  indpb=0.05)
        elif vartype == "float":
            self.toolbox.register("attr_float", random.random)
            self.toolbox.register("individual", tools.initRepeat,
                                  creator.Individual, self.toolbox.attr_float,
                                  self.GENOME_LENGTH)

            self.toolbox.register("mutate",
                                  tools.mutPolynomialBounded,
                                  eta=0.1,
                                  low=0.0,
                                  up=1.0,
                                  indpb=0.1)

        self.toolbox.register("population", tools.initRepeat, list,
                              self.toolbox.individual)

        self.toolbox.register("evaluate", fitness_function_handle)

        self.toolbox.register("mate", tools.cxUniform, indpb=0.1)

        self.toolbox.register("select", tools.selTournament, tournsize=3)

        if constraint is not None:
            self.toolbox.decorate(
                "evaluate", tools.DeltaPenalty(constraint, 0, self.distance))

        self.stats = []  # Initialize stats vector
Exemplo n.º 10
0
def prepare_toolbox(dataset: Dataset):
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMin)

    toolbox = base.Toolbox()

    # typ i zakres genu osobnika
    rand_initialier = RandInitializer(NCLUSTERS,
                                      dataset.ncolumns,
                                      min=0.,
                                      max=1.)
    toolbox.register("attr_float", rand_initialier.get_rand_val)
    toolbox.register("individual", tools.initRepeat, creator.Individual,
                     toolbox.attr_float, NCLUSTERS * dataset.ncolumns)

    # populacja to lista osobników
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    # wybór funkcji przystosowania
    toolbox.register("evaluate", dataset.evaluate)

    # funkcja kary
    arg_min = 0
    arg_max = 1

    def feasible(individual):
        for coord in individual:
            if coord < arg_min or coord > arg_max:
                return False
        return True

    def distance(individual):
        # tuple to float
        fit_val = dataset.evaluate(individual)[0]

        for x in individual:
            if x < arg_min:
                fit_val += 10**(1 + (arg_min - x))
            if x > arg_max:
                fit_val += 10**(1 + (x - arg_max))

        return fit_val,

    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 0.0, distance))

    # wybór metody krzyżowania
    toolbox.register("mate", tools.cxUniform, indpb=0.2)

    #wybór metody mutacji
    toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.05, indpb=0.3)

    # wybór metody selekcji
    toolbox.register("select", tools.selTournament, tournsize=2)

    return toolbox
Exemplo n.º 11
0
    def generate_toolbox(self, size, nmin, nmax, smin, smax, lmin, lmax, phi1, phi2):
        toolbox = base.Toolbox()
        toolbox.register("particle", ParticleSwarmOptimizer.generate, size=size,
                         nmin=nmin, nmax=nmax, smin=smin, smax=smax, lmin=lmin, lmax=lmax)
        toolbox.register("population", tools.initRepeat,
                         list, toolbox.particle)
        toolbox.register(
            "update", ParticleSwarmOptimizer.updateParticle, phi1=phi1, phi2=phi2)

        toolbox.register("evaluate", TransferMatrixMethod.evaluate_solution)
        toolbox.decorate("evaluate", tools.DeltaPenalty(
            ParticleSwarmOptimizer.feasible, -1.0))
        return toolbox
Exemplo n.º 12
0
 def register(self):
     """REGISTERING GENES"""
     creator.create('FitnessMin', base.Fitness, weights=(-1.0,))
     creator.create('Individual', list, fitness=creator.FitnessMin)
     self.toolbox.register('position', random.choice, self.possible)
     self.toolbox.register('first', lambda x: x, self.startpoint)
     self.toolbox.register('last', lambda x: x, self.endpoint)
     """INDIVIDUAL"""
     self.toolbox.register('individual', tools.initCycle, creator.Individual,
                           (self.toolbox.first, self.toolbox.position, self.toolbox.position,
                            self.toolbox.position, self.toolbox.position, self.toolbox.last), n=1)
     """POPULATION"""
     self.toolbox.register('population', tools.initRepeat, list, self.toolbox.individual)
     self.toolbox.register('evaluate', self.distance)
     self.toolbox.decorate('evaluate', tools.DeltaPenalty(self.validate, inf))
     self.toolbox.register('mate', tools.cxTwoPoint)  # crossover operator
     self.toolbox.register('mutate', tools.mutShuffleIndexes, indpb=0.5)  # mutation operator
     self.toolbox.register('select', tools.selTournament, tournsize=3)  # selecting 3 best individuals from batch
Exemplo n.º 13
0
def create_toolbox(r_lower, r_upper):
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("IndividualContainer", list, fitness=creator.FitnessMin)
    toolbox = base.Toolbox()
    #2 or 2
    toolbox.register("InitialValue1", random.uniform, r_lower, r_upper)
    toolbox.register("indiv", tools.initRepeat, creator.IndividualContainer,
                     toolbox.InitialValue1, 1)
    #toolbox.register( "InitialValue2",random.randint, N_lower, N_upper)
    #toolbox.register( "indiv", tools.initCycle, creator.IndividualContainer, (toolbox.InitialValue1, toolbox.InitialValue2), n=1)
    toolbox.register("population", tools.initRepeat, list, toolbox.indiv)
    toolbox.register("evaluate", FuncEvaluation)
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 100, distance))

    ### examplar evolving functions
    toolbox.register("mate", tools.cxBlend, alpha=0.4)
    toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.8)
    toolbox.register("select", tools.selTournament, tournsize=5)
    return toolbox
def main(target_function=target_fun, penalty=True, **globals_dict):
    initialize_globals(**globals_dict)
    '''registering object and function in environment'''
    toolbox.register("individual", generate_ev_s, creator.Individual,
                     creator.Strategy, IND_SIZE, EVO_STRATEGY, ATTR_RANGE)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", evaluate, target_function=target_function)
    if penalty:
        toolbox.decorate("evaluate",
                         tools.DeltaPenalty(feasible, 1000,
                                            distance))  # out of region penalty
    toolbox.register("mate", tools.cxESTwoPoint)
    toolbox.decorate("mate", check_strategy(EVO_STRATEGY))
    # toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=MUT_PROB) #mutESLogNormal has low efficiency
    toolbox.register("mutate", custom_mutation, mu=0, sigma=1)
    toolbox.decorate("mutate", check_strategy(EVO_STRATEGY))
    toolbox.register("select", tools.selTournament,
                     tournsize=T_SIZE)  # selBest as an alternative?

    pop = toolbox.population(n=POP_SIZE)
    hof = tools.HallOfFame(1)
    fitness_stats = tools.Statistics(lambda ind: ind.fitness.values)
    strategy_stats = tools.Statistics(lambda ind: ind.strategy)
    stats = tools.MultiStatistics(fitness=fitness_stats,
                                  strategy=strategy_stats)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    result, log = algorithms.eaMuPlusLambda(pop,
                                            toolbox,
                                            POP_SIZE,
                                            POP_SIZE,
                                            MUT_PROB,
                                            MAT_PROB,
                                            ngen=EVO_CYCLES,
                                            stats=stats,
                                            halloffame=hof,
                                            verbose=False)
    result.sort(key=lambda x: x.fitness.values)
    return (result, log, hof)
Exemplo n.º 15
0
def optim(MU, NGEN, path):

    # load the shp of the scenario
    #in_pts = "D:/04_PROJECTS/2001_WIND_OPTIM/WIND_OPTIM_git/intermediate_steps/3_wp/NSGA3_RES/in/B3.shp"
    #load in memory
    #all_pts = r"in_memory/inMemoryFeatureClass"
    all_pts = path
    #arcpy.CopyFeatures_management(in_pts, all_pts)

    #transform it to numpy array
    na = arcpy.da.TableToNumPyArray(all_pts, ['WT_ID', 'ENER_DENS', 'prod_MW'])

    # CXPB  is the probability with which two individuals
    #       are crossed
    # MUTPB is the probability for mutating an individual
    CXPB, MUTPB = 0.7, 0.4
    #MU,NGEN =20, 10
    enertarg = 4300000
    #some parameters to define the random individual
    nBITS = len(na)

    #production of energy
    sum_MW = np.sum(na['prod_MW'])

    low_targ = enertarg
    up_targ = enertarg * 1.07

    #the function to determine the initial random population which might reach the energy target
    def initial_indi():
        # relative to the total energy production to build the initial vector
        bound_up = (1.0 * up_targ / sum_MW)
        bound_low = (1.0 * low_targ / sum_MW)
        x3 = random.uniform(bound_low, bound_up)
        return np.random.choice([1, 0], size=(nBITS, ), p=[x3, 1 - x3])

    #some lists for the evaluation function
    enerd = list(na['ENER_DENS'])
    prod = list(na['prod_MW'])
    id = np.array(na['WT_ID'])

    #the evaluation function, taking the individual vector as input

    def evaluate(individual):
        individual = individual[0]
        #first check if the production of the seleced WT's is in the range between 4.31 and 4.29 TWH
        # goal 1
        mean_enerdsel = sum(
            x * y for x, y in zip(enerd, individual)) / sum(individual)
        # goal 2
        count_WTsel = sum(individual)
        # goal 3 (subset the input points by the WT_IDs which are in the ini pop (=1)
        WT_pop = np.column_stack((id, individual))
        WT_sel = WT_pop[WT_pop[:, [1]] == 1]
        WT_sel = WT_sel.astype(int)
        qry = '"WT_ID" IN ' + str(tuple(WT_sel))
        subset = arcpy.MakeFeatureLayer_management(all_pts, "tmp", qry)
        nn_output = arcpy.AverageNearestNeighbor_stats(subset,
                                                       "EUCLIDEAN_DISTANCE",
                                                       "NO_REPORT",
                                                       "41290790000")
        clus = float(nn_output.getOutput(0))
        res = (clus, count_WTsel, mean_enerdsel)
        ## delete the feature tmp since otherwise it will not work in a loop
        arcpy.Delete_management("tmp")
        arcpy.Delete_management("subset")
        return (res)

    def feasible(individual):
        individual = individual[0]
        prod_MWsel = sum(x * y for x, y in zip(prod, individual))
        if (prod_MWsel <= up_targ and prod_MWsel >= low_targ):
            return True
        return False

    ### setup NSGA3 with deap (minimize the first two goals returned by the evaluate function and maximize the third one)
    creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, 1.0))
    creator.create("Individual", list, fitness=creator.FitnessMulti)

    ref_points = tools.uniform_reference_points(nobj=3, p=12)
    ##setup the optim toolbox I do not understand that totally
    toolbox = base.Toolbox()

    #initial individual and pop
    toolbox.register("initial_indi", initial_indi)
    toolbox.register("individual",
                     tools.initRepeat,
                     creator.Individual,
                     toolbox.initial_indi,
                     n=1)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    #evaluation and constraints
    toolbox.register("evaluate", evaluate)

    ##assign the feasibility of solutions and if not feasible a large number for the minimization tasks and a small number for the maximization task
    toolbox.decorate("evaluate", tools.DeltaPenalty(feasible,
                                                    (10e20, 10e20, 0)))

    #mate, mutate and select to perform crossover
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
    toolbox.register("select", tools.selNSGA3, ref_points=ref_points)

    # initialize pareto front
    pareto = tools.ParetoFront(similar=np.array_equal)

    first_stats = tools.Statistics(key=lambda ind: ind.fitness.values[0])
    second_stats = tools.Statistics(key=lambda ind: ind.fitness.values[1])
    third_stats = tools.Statistics(key=lambda ind: ind.fitness.values[2])

    first_stats.register("min_clus", np.min, axis=0)
    second_stats.register("min_WT", np.min, axis=0)
    third_stats.register("max_enerd", np.max, axis=0)

    logbook1 = tools.Logbook()
    logbook2 = tools.Logbook()
    logbook3 = tools.Logbook()
    logbook1.header = "gen", "evals", "TIME", "min_clus"
    logbook2.header = "gen", "evals", "min_WT"
    logbook2.header = "gen", "evals", "max_enerd"

    pop = toolbox.population(n=MU)
    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    # invalid_ind = pop
    start_time = time.time()
    fitnesses = list(toolbox.map(toolbox.evaluate, invalid_ind))
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit
    end_time = time.time()
    delt_time = end_time - start_time

    ## Hyper volume
    # hv.hypervolume()

    # Compile statistics about the population
    record1 = first_stats.compile(pop)
    logbook1.record(gen=0, evals=len(invalid_ind), TIME=delt_time, **record1)

    record2 = second_stats.compile(pop)
    logbook2.record(gen=0, evals=len(invalid_ind), **record2)

    record3 = third_stats.compile(pop)
    logbook3.record(gen=0, evals=len(invalid_ind), **record3)

    # Begin the evolution with NGEN repetitions
    for gen in range(1, NGEN):
        print("-- Generation %i --" % gen)
        start_time = time.time()
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))

        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1[0], child2[0])
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant[0])
                del mutant.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]

        fitnesses = list(toolbox.map(toolbox.evaluate, invalid_ind))
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        end_time = time.time()
        delt_time = end_time - start_time
        #select the next generation with NSGA3 from pop and offspring of size MU
        pop = toolbox.select(pop + offspring, MU)

        pareto.update(pop)

        # Compile statistics about the new population
        record1 = first_stats.compile(invalid_ind)
        logbook1.record(gen=gen,
                        evals=len(invalid_ind),
                        TIME=delt_time,
                        **record1)

        record2 = second_stats.compile(invalid_ind)
        logbook2.record(gen=gen, evals=len(invalid_ind), **record2)

        record3 = third_stats.compile(invalid_ind)
        logbook3.record(gen=gen, evals=len(invalid_ind), **record3)
        print("--- %s seconds ---" % delt_time)

    # pareto fitnes values
    fitness_pareto = toolbox.map(toolbox.evaluate, pareto)
    fitness_pareto = np.array(fitness_pareto)
    fitness_pareto = {
        'CLUS': fitness_pareto[:, 0],
        'N_WT': fitness_pareto[:, 1],
        'ENERDENS': fitness_pareto[:, 2]
    }

    #pareto items and robustness
    par_items = np.array(pareto.items)
    par_rob = np.array(1.0 * sum(par_items[1:len(par_items)]) / len(par_items))
    par_rob = par_rob.ravel()
    par_rob_mat = np.column_stack((id, par_rob))
    par_rob_mat = {'WT_ID2': par_rob_mat[:, 0], 'par_rob': par_rob_mat[:, 1]}

    #last items and robustness
    last_rob = np.array(invalid_ind)
    last_rob = np.array(1.0 * sum(last_rob[1:len(last_rob)]) / len(last_rob))
    last_rob = last_rob.ravel()
    last_rob_mat = np.column_stack((id, last_rob))
    last_rob_mat = {
        'WT_ID2': last_rob_mat[:, 0],
        'last_rob': last_rob_mat[:, 1]
    }

    #logbook
    gen = np.array(logbook1.select('gen'))
    TIME = np.array(logbook1.select('TIME'))
    WT = np.array(logbook2.select('min_WT'))
    clus = np.array(logbook1.select('min_clus'))
    enerd = np.array(logbook3.select('max_enerd'))
    logbook = np.column_stack((gen, TIME, WT, clus, enerd))
    logbook = {
        'GENERATION': logbook[:, 0],
        'TIME': logbook[:, 1],
        'N_WT': logbook[:, 2],
        'CLUS': logbook[:, 3],
        'ENERDENS': logbook[:, 4]
    }
    arcpy.Delete_management("all_pts")
    arcpy.Delete_management("in_pts")
    arcpy.Delete_management("na")
    arcpy.Delete_management("in_memory/inMemoryFeatureClass")
    return par_rob_mat, last_rob_mat, fitness_pareto, logbook
Exemplo n.º 16
0
    elif len(individual) > 5:
        # delete a track
        del individual[random.choice(range(0, len(individual)))]
    return creator.Individual(individual),

NUM_SONGS = 20

toolbox = base.Toolbox()
toolbox.register("tracks", random.sample, all_tracks, NUM_SONGS)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.tracks)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalPlaylist, desired_play_time=120)
invalidPlaylistScore = (sys.float_info.max, sys.float_info.max, sys.float_info.max,
                        sys.float_info.max, sys.float_info.max, sys.float_info.max,
                        sys.float_info.max, 0.0, 0.0, 0.0)
toolbox.decorate("evaluate", tools.DeltaPenalty(validPlaylist, invalidPlaylistScore))
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("mutate", mutatePlaylist)
toolbox.register("select", tools.selNSGA2)

# Simulation parameters:
# Number of generations
NGEN = 5000
# The number of individuals to select for the next generation (eliminate bad ones).
MU = 500
# The number of children to produce at each generation.
LAMBDA = 50
# The probability that an offspring is produced by crossover.
CXPB = 0.5
# The probability that an offspring is produced by mutation.
MUTPB = 0.5
Exemplo n.º 17
0
    def fit(self):
        """
        Setting up the DEAP - genetic algorithm parameters and functions.

        Notes
        -----
        Must call this function before calling any algorithm.
        """
        creator.create("FitnessMin", base.Fitness, weights=self.Weights)
        creator.create("Individual", list, fitness=creator.FitnessMin)
        self.toolbox = base.Toolbox()
        self.toolbox.register("individual", tools.initIterate,
                              creator.Individual, self.chromosome_generator)
        self.toolbox.register("population", tools.initRepeat, list,
                              self.toolbox.individual)

        if self.crossover_type == "OnePoint":
            self.toolbox.register("mate", tools.cxOnePoint)
        elif self.crossover_type == "TwoPoint":
            self.toolbox.register("mate", tools.cxTwoPoint)
        elif self.crossover_type == "Uniform":
            self.toolbox.register("mate",
                                  tools.cxUniform,
                                  indpb=self.crossover_prob)
        elif self.crossover_type == "Blend":
            self.toolbox.register("mate", tools.cxBlend, alpha=0.5)

        self.toolbox.register("selectTournament",
                              tools.selTournament,
                              tournsize=30)
        self.toolbox.register("selectRoulette", tools.selRoulette)

        def feasibility(indi):
            for x, i in zip(indi, range(self.chromosome_length)):
                if self.chromosome_length == 1:
                    if self.bit_limits[0] <= x <= self.bit_limits[1]:
                        continue
                    else:
                        return False
                else:
                    if self.bit_limits[i][0] <= x <= self.bit_limits[i][1]:
                        continue
                    else:
                        return False
            return True

        def distance(indi):
            s = 0
            for x, i in zip(indi, range(self.chromosome_length)):
                if self.chromosome_length == 1:
                    low = self.bit_limits[0]
                    up = self.bit_limits[1]
                else:
                    low = self.bit_limits[i][0]
                    up = self.bit_limits[i][1]
                if x < low:
                    s += ((x - low) * 100)**2
                elif x > up:
                    s += ((x - up) * 100)**2
            return s

        self.toolbox.register("evaluate", self.evaluate)
        self.toolbox.decorate(
            "evaluate",
            tools.DeltaPenalty(feasibility, -100.0 * self.Weights[0],
                               distance))
Exemplo n.º 18
0
    logbook.chapters['runtime'].header = 'start',

    classifier = GaussianNB()
    # classifier = MLPClassifier(solver='lbfgs', activation='relu', alpha=1e-5, max_iter=1000, hidden_layer_sizes=(100,60), random_state=RANDOMSTATE)
    cv = StratifiedShuffleSplit(n_splits=NSPLITS,
                                test_size=TESTSIZE,
                                random_state=RANDOMSTATE)
    #cv = StratifiedKFold(n_splits=NSPLITS, shuffle=True, random_state=RANDOMSTATE)

    tb.register('evaluate',
                evaluate_ml_classifier,
                data=test_X,
                target=test_y,
                classifier=classifier,
                cross_val=cv)
    tb.decorate('evaluate', tools.DeltaPenalty(feasible, (0, -60)))
    # Initial population
    pop = tb.population(n=POPSIZE)
    invalid = evaluate_population_fitness(tb, pop)
    pop = tb.select(pop, len(pop))

    np.set_printoptions(precision=2)
    record = mstats.compile(pop)
    logbook.record(gen=0, evals=invalid, **record)
    print(logbook.stream)

    # Iterative mode on
    plt.ion()
    fig, ax = plt.subplots(dpi=150)
    ax.set_xlim([0, 1])
    ax.set_ylim([-60, 0])
Exemplo n.º 19
0
toolbox = base.Toolbox()

#initial individual and pop
toolbox.register("initial_indi", initial_indi)
toolbox.register("individual",
                 tools.initRepeat,
                 creator.Individual,
                 toolbox.initial_indi,
                 n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

#evaluation and constraints
toolbox.register("evaluate", evaluate)

##assign the feasibility of solutions and if not feasible a large number for the minimization tasks and a small number for the maximization task
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, (10e20, 10e20, 0)))

#mate, mutate and select to perform crossover
toolbox.register("mate", tools.cxTwoPoint)
#toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("mutate",
                 tools.mutPolynomialBounded,
                 low=0,
                 up=1,
                 eta=20,
                 indpb=0.05)
toolbox.register("select", tools.selNSGA3, ref_points=ref_points)

## the optimization (n seems to be the number of individuals aka population size MU
#def main():
# initialize pareto front
Exemplo n.º 20
0
toolbox.register('mutate', tools.mutFlipBit, indpb=0.6)
toolbox.register('select', tools.selNSGA2)
toolbox.register('evaluate', J)

listCons = [
    geom_cons_1, geom_cons_2, geom_cons_3, geom_cons_4, geom_cons_5,
    geom_cons_6, geom_cons_7, geom_cons_9, func_cons_1, func_cons_2,
    func_cons_3, func_cons_4
]
listDist = [
    geom_cons_1_dist, geom_cons_2_dist, geom_cons_3_dist, geom_cons_4_dist,
    geom_cons_5_dist, geom_cons_6_dist, geom_cons_7_dist, geom_cons_9_dist,
    func_cons_1_dist, func_cons_2_dist, func_cons_3_dist, func_cons_4_dist
]
for i in range(len(listCons)):
    toolbox.decorate("evaluate", tools.DeltaPenalty(listCons[i], 12.0))

# pareto=tools.ParetoFront()
population = toolbox.population(n=population_size)
# fits=toolbox.map(toolbox.evaluate,population)
# for fit,ind in zip(fits,population):
#     ind.fitness.values=fit
# pareto.update(population)
# counter = 0
# for gen in range(num_generations):
#     offspring=algorithms.varAnd(population,toolbox,cxpb=1.0,mutpb=0.1)
#     fits=toolbox.map(toolbox.evaluate,population)
#     for fit,ind in zip(fits,offspring):
#         ind.fitness.values=fit
#     population=toolbox.select(offspring+population,k=population_size)
#     pareto.update(population)
Exemplo n.º 21
0
 def registerEvaluate(self):
     evaluate = lambda ind: (self.fitnessUtils.computeFMeasure(ind), )
     self.toolbox.register("evaluate", evaluate)
     self.toolbox.decorate("evaluate",
                           tools.DeltaPenalty(self.feasible, 0.0))
        individual.update([random.choice(ITEMS_NAME)])
    else:
        val = random.choice(ITEMS_NAME)
        individual.subtract([val])
        if individual[val] < 0:
            del individual[val]
    return individual,

import sys

# register the functions, some with extra fixed arguments (like target_price)
toolbox.register("evaluate", evalXKCD, target_price=15.05)
# severely penalize individuals that are over budget ($15.05))
# return a fixed terrible fitness
toolbox.decorate("evaluate", tools.DeltaPenalty(lambda ind: evalXKCD(ind, 15.05)[0] <= 0,
                                               (-sys.float_info.max,
                                                sys.float_info.max,
                                                -sys.float_info.max)))
toolbox.register("mate", cxCounter, indpb=0.5)
toolbox.register("mutate", mutCounter)
toolbox.register("select", tools.selNSGA2)


# Simulation parameters:
# Number of generations = 100
NGEN = 100
# The number of individuals to select for the next generation (eliminate bad ones).
MU = 50
# The number of children to produce at each generation.
LAMBDA = 20
# The probability that an offspring is produced by crossover.
CXPB = 0.3
            and (engine_design_point_pressure >= 33000
                 and engine_design_point_pressure <= 43000) and
        (engine_design_point_mach >= 0.74 and engine_design_point_mach <= 0.82)
            and (engine_position >= 0 and engine_position <= 1)
            and (winglet_presence >= 0 and winglet_presence <= 1)
            and (slat_precense >= 0 and slat_precense <= 1)
            and (attr_horizontal_tail_position >= 0
                 and attr_horizontal_tail_position <= 1)):

        return True
    return False


toolbox.register("evaluate", evavl)
toolbox.decorate("evaluate", tools.DeltaPenalty(feaseGeom, [
    1.0,
]))


def main():
    pop = toolbox.population(n=10)
    hof = tools.HallOfFame(5)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    pop, log = algorithms.eaSimple(pop,
                                   toolbox,
                                   cxpb=0.5,
toolbox.register("evaluate", simionescu)


# define the valid input domain using the cosntraints:
def feasible(individual):
    """Feasibility function for the individual.
    Returns True if feasible, False otherwise.
    """
    x = individual[0]
    y = individual[1]
    return x**2 + y**2 <= (1 + 0.2 * math.cos(8.0 * math.atan2(x, y)))**2


# decorate the fitness function with the delta penalty function:
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, PENALTY_VALUE))

# genetic operators:
toolbox.register("select", tools.selTournament, tournsize=2)
toolbox.register("mate",
                 tools.cxSimulatedBinaryBounded,
                 low=BOUND_LOW,
                 up=BOUND_UP,
                 eta=CROWDING_FACTOR)
toolbox.register("mutate",
                 tools.mutPolynomialBounded,
                 low=BOUND_LOW,
                 up=BOUND_UP,
                 eta=CROWDING_FACTOR,
                 indpb=1.0 / DIMENSIONS)
Exemplo n.º 25
0
    otherwise."""
    if -0.5 < individual[0] < 0.5:
        return True
    return False


# from https://deap.readthedocs.io/en/master/tutorials/advanced/constraints.html
def distance(individual):
    """A distance function to the feasibility region."""
    return (individual[0] - 0.0)**2


toolbox.register("evaluate", evalOneMax)
# Here we apply a feasible constraint:
# https://deap.readthedocs.io/en/master/tutorials/advanced/constraints.html
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 1.0, distance))
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

##This is different!#################################
toolbox.register("map", ray_deap_map, creator_setup=creator_setup)
######################################################

if __name__ == "__main__":

    random.seed(64)

    pop = toolbox.population(n=300)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
Exemplo n.º 26
0
creator.create("FitnessMax", d_base.Fitness, weights=(1.0, ))
creator.create("Individual", list, fitness=creator.FitnessMax)

toolbox = d_base.Toolbox()

toolbox.register("indices", routeInit)
toolbox.register("individual", tools.initIterate, creator.Individual,
                 toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.1)  # mutation
toolbox.register("select", tools.selTournament, tournsize=3)  # selection
toolbox.register("evaluate", evalTSP)  # evaluation
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, -20,
                                                routeQ))  # penalization

#%%


def main():
    random.seed(80)

    #   As the algorithm converge rapidly to a solution and tends to stay around it,
    #   the population is set to a higher value, and number of generations to a lower value
    #   This will provide the algorithm with a better ability to match with the optimal solution
    CXPB, MUTPB, NIND, NGEN = 0.8, 0.1, 500, 40
    pop = toolbox.population(NIND)

    #   Save best individual and register data
    hof = tools.HallOfFame(1)
Exemplo n.º 27
0
def ejecucion_ga(conn, ga_params):
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individuo", list, fitness=creator.FitnessMax)

    toolbox = base.Toolbox()

    toolbox.register("coordenada", crear_coordenadas, mapa=ga_params.get('barrios_caba_gdf'),
                     poligono=ga_params.get('poligonos_caba'))
    toolbox.register("individuo", tools.initRepeat, creator.Individuo, toolbox.coordenada,
                     n=ga_params.get('q_coord'))
    toolbox.register("poblacion", tools.initRepeat, list, toolbox.individuo)
    toolbox.register("mate", cxBlend_custom)
    toolbox.register("mutate", mutGaussian_custom, indpb=ga_params.get('mut_prob_alelo'))

    if ga_params.get('tipo_seleccion') == 1:
        toolbox.register("select", tools.selRoulette)
    else:
        toolbox.register("select", tools.selTournament, tournsize=ga_params.get('torneo_tam'))

    toolbox.register("evaluate", funcion_objetivo, Q_COORD = ga_params.get('q_coord'), area=ga_params.get('area'))
    toolbox.register("map", futures.map)

    toolbox.decorate("evaluate", tools.DeltaPenalty(validar_individuo, 0))

    print('Ejecutando experimento {}'.format(ga_params.get('tipo_seleccion')))

    '''
    config_id = base_de_datos.insert_config(conn, ga_params.get('q_ind'), ga_params.get('q_coord'),
                                            ga_params.get('mut_prob'), ga_params.get('cross_prob'),
                                            ga_params.get('mut_prob_alelo'), ga_params.get('tipo_seleccion'),
                                            ga_params.get('torneo_tam'), ga_params.get('mu_x'), ga_params.get('mu_y'),
                                            ga_params.get('sigma_x'), ga_params.get('sigma_y'),
                                            ga_params.get('generacion_parada'), ga_params.get('alpha'))
    '''
    pop = toolbox.poblacion(n=ga_params.get('q_ind'))

    fitnesses = list(map(toolbox.evaluate, pop))

    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    fits = [ind.fitness.values[0] for ind in pop]

    g = 0
    print('Generacion {} - Max Fit: {} Avg Fit {}'.format(g, max(fits), np.average(fits)))

    # generacion_id = base_de_datos.insert_generacion(conn, g, config_id)

    '''
    for i, ind in enumerate(pop):
        individuo_id = base_de_datos.insert_individuo(conn, i, generacion_id, ind.fitness.values[0])
        list_puntos = zip(count(start=1), repeat(individuo_id), [x.wkt for x in ind])
        base_de_datos.insert_punto(conn, list(list_puntos))
    '''
    # Begin the evolution
    while max(fits) < 2.15 and g < 300:#ga_params.get('generacion_parada'):
        # A new generation
        g = g + 1
        print('Generacion {} - Max Fit: {} Avg Fit {}'.format(g, max(fits), np.average(fits)))

        #generacion_id = base_de_datos.insert_generacion(conn, g, config_id)

        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))

        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))

        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < ga_params.get('cross_prob'):
                toolbox.mate(child1, child2, ga_params.get('alpha'))  # random.random())
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < ga_params.get('mut_prob'):
                toolbox.mutate(mutant, ga_params.get('mu_x'), ga_params.get('sigma_x'),
                               ga_params.get('mu_y'), ga_params.get('sigma_y'))
                del mutant.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop[:] = offspring

        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]

        '''
        for i, ind in enumerate(pop):
            individuo_id = base_de_datos.insert_individuo(conn, i, generacion_id, ind.fitness.values[0])

            list_puntos = zip(count(start=1), repeat(individuo_id), [x.wkt for x in ind])

            base_de_datos.insert_punto(conn, list(list_puntos))

    base_de_datos.update_config(conn, config_id)
    '''
    '''
        Termina la ejecución y limpieza de variables, clases y funciones
    '''

    toolbox.unregister('select')
    toolbox.unregister('poblacion')
    toolbox.unregister('individuo')

    del creator.Individuo
    del creator.FitnessMax
Exemplo n.º 28
0
def distance(individual):
    """A distance function to the feasibility region."""
    return (individual[0] - 5.0)**2


#register individuals
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, 0.90, 1.10)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, 5)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", objective)
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, -100.0))


CXPB, MUTPB = 0.5, 1.0

restart = True
restart_file = "ga_restart.pkl"
save_file = "ga_restart3.pkl"


#restart from file
if restart:
    with open(restart_file, "r") as cp_file:
        cp = pickle.load(cp_file)
    population = cp["population"]
    start_gen = cp["generation"]
Exemplo n.º 29
0
def feasible(individual):

    #global jobFile, macroFile
    """Feasibility function for the individual. Returns True if feasible False
    otherwise."""
    stress, disp = analyseStructure(jobFile, macroFile,
                                    individual)  # need to make this function

    if compStressLim < stress < tensStressLim and dispLimLow < disp < dispLimUp:
        return True
    return False


toolbox.register("evaluate", fitFunc)
toolbox.decorate("evaluate", tools.DeltaPenalty(
    feasible, 10.0))  # applies penalty of 10 to result if it is unfeasible.
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

jobFile = 'Job-1.inp'
macroFile = r'C:\Users\pqb18127\OneDrive\PhD\Training\NAFEMS Homework 1\dataOut.py'
simResults = 'data.csv'


def main():

    pop = toolbox.population(n=300)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
Exemplo n.º 30
0
HALL_OF_FAME_SIZE = 30

# Fitness
creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
creator.create('Individual', list, fitness=creator.FitnessMin)
# God
toolbox = base.Toolbox()
toolbox.register('random_float', random.uniform, BOUND_LOW, BOUND_HIGH)
toolbox.register('individual_creator', tools.initRepeat, creator.Individual,
                 toolbox.random_float, DIMENSIONS)
toolbox.register('population_creator', tools.initRepeat, list,
                 toolbox.individual_creator)

# Evaluate
toolbox.register('evaluate', simionescu)
toolbox.decorate('evaluate', tools.DeltaPenalty(feasibility,
                                                CONSTRAINT_PENALTY))

# Genetic operators
toolbox.register('select', tools.selTournament, tournsize=TOURNAMENT_SIZE)
toolbox.register('mate',
                 tools.cxSimulatedBinaryBounded,
                 low=BOUND_LOW,
                 up=BOUND_HIGH,
                 eta=CROWDING_FACTOR)
toolbox.register('mutate',
                 tools.mutPolynomialBounded,
                 low=BOUND_LOW,
                 up=BOUND_HIGH,
                 eta=CROWDING_FACTOR,
                 indpb=1.0 / DIMENSIONS)