當前位置: 首頁>>代碼示例>>Python>>正文


Python creator.FitnessMax方法代碼示例

本文整理匯總了Python中deap.creator.FitnessMax方法的典型用法代碼示例。如果您正苦於以下問題:Python creator.FitnessMax方法的具體用法?Python creator.FitnessMax怎麽用?Python creator.FitnessMax使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在deap.creator的用法示例。


在下文中一共展示了creator.FitnessMax方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: geneticAlgorithm

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def geneticAlgorithm(X, y, n_population, n_generation):
    """
    Deap global variables
    Initialize variables to use eaSimple
    """
    # create individual
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    # create toolbox
    toolbox = base.Toolbox()
    toolbox.register("attr_bool", random.randint, 0, 1)
    toolbox.register("individual", tools.initRepeat,
                     creator.Individual, toolbox.attr_bool, len(X.columns))
    toolbox.register("population", tools.initRepeat, list,
                     toolbox.individual)
    toolbox.register("evaluate", getFitness, X=X, y=y)
    toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
    toolbox.register("select", tools.selTournament, tournsize=3)

    # initialize parameters
    pop = toolbox.population(n=n_population)
    hof = tools.HallOfFame(n_population * n_generation)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("min", np.min)
    stats.register("max", np.max)

    # genetic algorithm
    pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2,
                                   ngen=n_generation, stats=stats, halloffame=hof,
                                   verbose=True)

    # return hall of fame
    return hof 
開發者ID:renatoosousa,項目名稱:GeneticAlgorithmForFeatureSelection,代碼行數:38,代碼來源:gaFeatureSelection.py

示例2: create_toolbox

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def create_toolbox(num_bits):
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    # Initialize the toolbox
    toolbox = base.Toolbox()

    # Generate attributes 
    toolbox.register("attr_bool", random.randint, 0, 1)

    # Initialize structures
    toolbox.register("individual", tools.initRepeat, creator.Individual, 
        toolbox.attr_bool, num_bits)

    # Define the population to be a list of individuals
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    # Register the evaluation operator 
    toolbox.register("evaluate", eval_func)

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

    # Register a mutation operator
    toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)

    # Operator for selecting individuals for breeding
    toolbox.register("select", tools.selTournament, tournsize=3)
    
    return toolbox 
開發者ID:PacktPublishing,項目名稱:Artificial-Intelligence-with-Python,代碼行數:32,代碼來源:bit_counter.py

示例3: create_toolbox

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def create_toolbox():
    global robot, pset

    pset = gp.PrimitiveSet("MAIN", 0)
    pset.addPrimitive(robot.if_target_ahead, 2)
    pset.addPrimitive(Prog().prog2, 2)
    pset.addPrimitive(Prog().prog3, 3)
    pset.addTerminal(robot.move_forward)
    pset.addTerminal(robot.turn_left)
    pset.addTerminal(robot.turn_right)

    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)

    toolbox = base.Toolbox()

    # Attribute generator
    toolbox.register("expr_init", gp.genFull, pset=pset, min_=1, max_=2)

    # Structure initializers
    toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", eval_func)
    toolbox.register("select", tools.selTournament, tournsize=7)
    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)

    return toolbox 
開發者ID:PacktPublishing,項目名稱:Artificial-Intelligence-with-Python,代碼行數:32,代碼來源:robot.py

示例4: setUp

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def setUp(self):
        creator.create("FitnessMax", base.Fitness, weights=(1.0,))
        creator.create("IndList", list, fitness=creator.FitnessMax)
        creator.create("IndArray", array.array,  typecode='f', fitness=creator.FitnessMax)
        creator.create("IndNDArray", numpy.ndarray,  typecode='f', fitness=creator.FitnessMax)
        creator.create("IndTree", gp.PrimitiveTree, fitness=creator.FitnessMax)
        self.toolbox = base.Toolbox()
        self.toolbox.register("func", func)
        self.toolbox.register("lambda_func", lambda: "True") 
開發者ID:DEAP,項目名稱:deap,代碼行數:11,代碼來源:test_pickle.py

示例5: tearDown

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def tearDown(self):
        del creator.FitnessMax
        del creator.IndList
        del creator.IndArray
        del creator.IndNDArray
        del creator.IndTree 
開發者ID:DEAP,項目名稱:deap,代碼行數:8,代碼來源:test_pickle.py

示例6: test_pickle_fitness

# 需要導入模塊: from deap import creator [as 別名]
# 或者: from deap.creator import FitnessMax [as 別名]
def test_pickle_fitness(self):
        fitness = creator.FitnessMax()
        fitness.values = (1.0,)
        fitness_s = pickle.dumps(fitness)
        fitness_l = pickle.loads(fitness_s)
        self.assertEqual(fitness, fitness_l, "Unpickled fitness != pickled fitness") 
開發者ID:DEAP,項目名稱:deap,代碼行數:8,代碼來源:test_pickle.py


注:本文中的deap.creator.FitnessMax方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。