本文整理汇总了Python中deap.creator.FitnessMin方法的典型用法代码示例。如果您正苦于以下问题:Python creator.FitnessMin方法的具体用法?Python creator.FitnessMin怎么用?Python creator.FitnessMin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.creator
的用法示例。
在下文中一共展示了creator.FitnessMin方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_toolbox
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMin [as 别名]
def create_toolbox(self):
"""OptiGenAlgNsga2Deap method to create DEAP toolbox
Parameters
----------
self : OptiGenAlgNsga2Deap
Returns
-------
self : OptiGenAlgNsga2Deap
OptiGenAlgNsga2Deap with toolbox created
"""
# Create toolbox
self.toolbox = base.Toolbox()
# Create Fitness and individual
creator.create(
"FitnessMin", base.Fitness, weights=[-1 for _ in self.problem.design_var]
)
creator.create("Individual", list, typecode="d", fitness=creator.FitnessMin)
self.toolbox.register("creator", creator.Individual)
# Register individual and population
self.toolbox.register(
"individual",
create_indiv,
self.toolbox.creator,
self.problem.output,
self.problem.design_var,
)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
示例2: create_toolbox
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMin [as 别名]
def create_toolbox():
pset = gp.PrimitiveSet("MAIN", 1)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(division_operator, 2)
pset.addPrimitive(operator.neg, 1)
pset.addPrimitive(math.cos, 1)
pset.addPrimitive(math.sin, 1)
pset.addEphemeralConstant("rand101", lambda: random.randint(-1,1))
pset.renameArguments(ARG0='x')
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
toolbox.register("evaluate", eval_func, points=[x/10. for x in range(-10,10)])
toolbox.register("select", tools.selTournament, tournsize=3)
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("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
return toolbox
示例3: main
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMin [as 别名]
def main(func, dim, maxfuncevals, ftarget=None):
toolbox = base.Toolbox()
toolbox.register("update", update)
toolbox.register("evaluate", func)
toolbox.decorate("evaluate", tupleize)
# Create the desired optimal function value as a Fitness object
# for later comparison
opt = creator.FitnessMin((ftarget,))
# Interval in which to initialize the optimizer
interval = -5, 5
sigma = (interval[1] - interval[0])/2.0
alpha = 2.0**(1.0/dim)
# Initialize best randomly and worst as a place holder
best = creator.Individual(random.uniform(interval[0], interval[1]) for _ in range(dim))
worst = creator.Individual([0.0] * dim)
# Evaluate the first individual
best.fitness.values = toolbox.evaluate(best)
# Evolve until ftarget is reached or the number of evaluation
# is exausted (maxfuncevals)
for g in range(1, maxfuncevals):
toolbox.update(worst, best, sigma)
worst.fitness.values = toolbox.evaluate(worst)
if best.fitness <= worst.fitness:
# Incease mutation strength and swap the individual
sigma = sigma * alpha
best, worst = worst, best
else:
# Decrease mutation strength
sigma = sigma * alpha**(-0.25)
# Test if we reached the optimum of the function
# Remember that ">" for fitness means better (not greater)
if best.fitness > opt:
return best
return best
示例4: setUp
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMin [as 别名]
def setUp(self):
@binary.bin2float(0, 1023, 10)
def evaluate(individual):
"""Simplest evaluation function."""
return individual
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
self.toolbox = base.Toolbox()
self.toolbox.register("evaluate", evaluate)
示例5: runOptGenetic
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMin [as 别名]
def runOptGenetic():
'''
@return:
@rtype:
'''
# COULDDO parametrisation
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
IND_SIZE = num_slots * num_evs
POP_SIZE = 30
toolbox = base.Toolbox()
toolbox.register("attr_float", rd.random) # COULDDO heuristic init
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=POP_SIZE)
toolbox.register("evaluate", evaluate)
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 0.0, distance))
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.5, indpb=0.5)
toolbox.register("select", tools.selTournament, tournsize=3)
stats = tools.Statistics(key=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)
hof = tools.HallOfFame(1)
population = toolbox.population()
# if no of-the-shelf algorithm used...
# fits = toolbox.map(toolbox.evaluate, population)
# for fit, ind in zip(fits, population):
# ind.fitness.values = fit
population, logbook = algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.3, ngen=5, stats=stats, verbose=True, halloffame=hof)
sorted_pop = sorted(population, key=lambda ind: ind.fitness)
ev_schedules = np.asarray(best).reshape((num_evs, num_slots))
schedules = np.zeros((num_households, num_slots)).tolist()
for i in range(num_evs):
schedules[evs[i].position] = ev_schedules[i].tolist()
evs[i].schedule = schedules[evs[i].position]
return schedules
# *****************************************************************************************************
# * Metaheuristics Side Functions
# *****************************************************************************************************
# UNUSED