DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.88k stars 1.13k forks source link

Couldn't run Constraint handling along with SCOOP. Facing pickling error and slowness despite using SCOOP #496

Open anjanikeshari opened 4 years ago

anjanikeshari commented 4 years ago

Hi Team,

I was trying SCOOP for multi processing along with Constraint Handling.

I simply combined examples used in DEAP documentation for Constraint Handling and SCOOP

I faced two issues, which is mentioned below -

  1. When using toolbox.decorate to apply DeltaPenalty along with 'evalFct function, i get SCOOP error as WARNING Pickling Error: Can't pickle <function evalFct at 0x000001E19A71B048>: it's not the same object as main.evalFct

  2. I commented below 'decorate' code and run program: toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 7.0, distance)) This time program ran but it took a lot of time than it took without using SCOOP (i.e. simply commented toolbox.register("map", futures.map)

Attaching the program for reference

------------------------------------------------

from math import sin from deap import base, creator, tools import random import numpy as np import math from deap import algorithms import matplotlib.pyplot as plt import seaborn as sns from scoop import futures

def evalFct(individual):

Evaluation function for the individual.

x = individual[0]
return (x - 5)**2 * sin(x) * (x/3),

def feasible(individual):

Feasibility function for the individual. Returns True if feasible False otherwise.

if 3 < individual[0] < 7:
    return True
return False

def distance(individual):

A distance function to the feasibility region.

return (individual[0] - 5.0)**2

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

toolbox = base.Toolbox() toolbox.register("attr_float", random.uniform, 0, 10) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, 1) toolbox.register("population", tools.initRepeat, list, toolbox.individual)

genetic operators:

toolbox.register("select", tools.selTournament, tournsize=2) toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=0, up=10, eta=20) toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=10, eta=20, indpb=0.05) toolbox.register("evaluate", evalFct) toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 7.0, distance)) toolbox.register("map", futures.map)

Genetic Algorithm flow:

def main():

# create initial population (generation 0):
population = toolbox.population(n=300)

# prepare the statistics object:
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("min", np.min)
stats.register("avg", np.mean)

# define the hall-of-fame object:
hof = tools.HallOfFame(30)

# perform the Genetic Algorithm flow with elitism:
population, logbook = algorithms.eaSimple(population, toolbox, cxpb=0.9, mutpb=0.5,
                                          ngen=50, stats=stats, halloffame=hof)
# print info for best solution found:
best = hof.items[0]
print("-- Best Individual = ", best)
print("-- Best Fitness = ", best.fitness.values[0])

# extract statistics:
minFitnessValues, meanFitnessValues = logbook.select("min", "avg")

# plot statistics:
sns.set_style("whitegrid")
plt.plot(minFitnessValues, color='red')
plt.plot(meanFitnessValues, color='green')
plt.xlabel('Generation')
plt.ylabel('Min / Average Fitness')
plt.title('Min and Average fitness over Generations')

plt.show()

if name == "main": main()

-------------------------------------------

anjanikeshari commented 4 years ago

constraint_scoop.txt

Attached is code for reference. Kindly convert it to .py and run the code using 'python -m scoop constraint_scoop.py'