Beispiel #1
0
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

def evalOneMax(individual):
    return sum(individual),

toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("map", dtm.map)

def main():
    random.seed(64)

    pop = toolbox.population(n=300)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", tools.mean)
    stats.register("std", tools.std)
    stats.register("min", min)
    stats.register("max", max)
    
    algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats,
                        halloffame=hof, verbose=True)
    logging.info("Best individual is %s, %s", hof[0], hof[0].fitness.values)

if __name__ == "__main__":
    dtm.setOptions(taskGranularity=0.01)
    dtm.start(main)
Beispiel #2
0
#    the License, or (at your option) any later version.
#
#    DEAP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""
Calculation of Pi using a Monte Carlo method.
"""

from math import hypot
from random import random

from deap import dtm

def test(tries):
    return sum(hypot(random(), random()) < 1 for i in range(tries))

def calcPi(n, t):
    expr = dtm.repeat(test, n, t)
    pi_value = 4. * sum(expr) / float(n*t)
    print("pi = " + str(pi_value))
    return pi_value

dtm.setOptions(setTraceMode=True, taskGranularity=0.01)
dataPi = dtm.start(calcPi, 3000, 5000)