Esempio n. 1
0
def toolsInit(options, graph):
    """
    Set up the EAP toolbox based on provided options
    """
    creator.create("Fitness", base.Fitness, weights=(1.0, 1.0, 1.0, 1.0))
    creator.create("Individual", nx.Graph, fitness=creator.Fitness)
    creator.create("Population", list)
    left, right = splitGraph(graph)

    tools = toolbox.Toolbox()

    # Structure initializers
    tools.register("attr_bipartite", randomBipartite, graph, 2)
    tools.register("individual", graphInd,
        tools.attr_bipartite, creator.Fitness) 
    tools.register("population", creator.Population,
        content_init=tools.individual, size_init=options.popSize)
    tools.register("info", printPartnerships)
    
    # Operator registering

    tools.register("select", getattr(toolbox, options.select))
    tools.register("mate", toolbox.cxTwoPoints) # Irrelevant for now, I think
    tools.register("mutate", mutDancePartners, graph, 1.0/len(left))

    tools.register("evaluate", evalPartnering)

    return tools
Esempio n. 2
0
# logic operators
# Define a new if-then-else function
def if_then_else(input, output1, output2):
    if input: return output1
    else: return output2

pset.addPrimitive(operator.lt, ["float", "float"], "bool")
pset.addPrimitive(operator.eq, ["float", "float"], "bool")
pset.addPrimitive(if_then_else, ["bool", "float", "float"], "float")

# terminals
pset.addEphemeralConstant(lambda: random.random() * 100, "float")
pset.addTerminal(0, "bool")
pset.addTerminal(1, "bool")

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

tools = toolbox.Toolbox()
tools.register("expr", gp.generateRamped, pset=pset, type_=pset.ret, min_=1, max_=2)
tools.register("individual", creator.Individual, content_init=tools.expr)
tools.register("population", list, content_init=tools.individual, size_init=100)
tools.register("lambdify", gp.lambdify, pset=pset)

def evalSpambase(individual):
    # Transform the tree expression in a callable function
    func = tools.lambdify(expr=individual)
    # Randomly sample 400 mails in the spam database
    spam_samp = random.sample(spam, 400)
    # Evaluate the sum of correctly identified mail as spam
    result = sum(bool(func(*mail[:57])) is bool(mail[57]) for mail in spam_samp)
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with EAP. If not, see <http://www.gnu.org/licenses/>.

import random

from eap import base
from eap import creator
from eap import toolbox
from eap import gp

# gp_symbreg already defines some usefull structures
import gp_symbreg

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


def refFunc(x):
    return x**4 + x**3 + x**2 + x


def evalSymbReg(expr, data):
    # Transform the tree expression in a callable function
    func = tools_gp.lambdify(expr=expr)
    # Evaluate the sum of squared difference between the expression
    # and the real function : x**4 + x**3 + x**2 + x
    values = data
    diff_func = lambda x: (func(x) - refFunc(x))**2
    diff = sum(map(diff_func, values))
import array
import inspect
import logging
import math
import sys
import random

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

from eap import algorithms
from eap import base
from eap import creator
from eap import halloffame
from eap import toolbox

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

tools = toolbox.Toolbox()

# Attribute generator
tools.register("attr_float", random.uniform, -5, 5)

# Structure initializers
tools.register("individual", creator.Individual, "f", content_init=tools.attr_float, size_init=3)
tools.register("population", creator.Population, content_init=tools.individual, size_init=50)

def evalKursawe(ind):
    f1 = sum(-10 * math.exp(-0.2 * math.sqrt(x * x + y * y)) for x, y in zip(ind[:-1], ind[1:]))
    f2 = sum(abs(x)**0.8 + 5 * math.sin(x * x * x) for x in ind)
Esempio n. 5
0
import random
import logging

from eap import algorithms
from eap import base
from eap import creator
from eap import halloffame
from eap import toolbox

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

IND_SIZE = 30

tools = toolbox.Toolbox()

creator.create("Strategy", array.array)

tools.register("strategy", creator.Strategy, "d", content_init=lambda: 1., size_init=IND_SIZE) 

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

# Attribute generator
tools.register("attr_float", random.uniform, -3, 3)

# Structure initializers
tools.register("individual", creator.Individual, "d", content_init=tools.attr_float, size_init=IND_SIZE)
tools.register("population", list, content_init=tools.individual, size_init=50)

def evalSphere(individual):
    return sum(map(lambda x: x * x, individual)),
Esempio n. 6
0
#
#    EAP 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 EAP. If not, see <http://www.gnu.org/licenses/>.

import random

from eap import base
from eap import creator
from eap import toolbox

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

tools = toolbox.Toolbox()
# Attribute generator
tools.register("attr_bool", random.randint, 0, 1)
# Structure initializers
tools.register("individual",
               creator.Individual,
               content_init=tools.attr_bool,
               size_init=100)
tools.register("population",
               creator.Population,
               content_init=tools.individual,
               size_init=300)
Esempio n. 7
0
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

from eap import base
from eap import creator
from eap import toolbox
from eap import halloffame
from eap import algorithms

# gr*.yml contains the distance map in list of list style in YAML/JSON format
# Optimal solutions are : gr17 = 2085, gr24 = 1272, gr120 = 6942
tsp = yaml.load(open("gr17.yml", "r"))
distance_map = tsp["DistanceMatrix"]
IND_SIZE = tsp["TourSize"]

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

tools = toolbox.Toolbox()

# Attribute generator
tools.register("indices", random.sample, xrange(IND_SIZE), IND_SIZE)

# Structure initializers
tools.register("individual", creator.Individual, "i", content_init=tools.indices)
tools.register("population", list, content_init=tools.individual, size_init=300)

def evalTSP(individual):
    distance = distance_map[individual[-1]][individual[0]]
    for gene1, gene2 in zip(individual[0:-1], individual[1:]):
        distance += distance_map[gene1][gene2]
Esempio n. 8
0
    for index, elem in enumerate(individual):
        if random.random() < indpb:
            individual[index] = genWire(dimension)


def mutAddWire(individual, dimension):
    index = random.randint(0, len(individual))
    individual.insert(index, genWire(dimension))


def mutDelWire(individual):
    index = random.randrange(len(individual))
    del individual[index]


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

tools = toolbox.Toolbox()

# Gene initializer
tools.register("network",
               genNetwork,
               dimension=INPUTS,
               min_size=9,
               max_size=12)

# Structure initializers
tools.register("individual", creator.Individual, content_init=tools.network)
tools.register("population",
               list,
Esempio n. 9
0
from multiprocessing import Process, Queue, cpu_count, Value
#from pokerbots.engine.game import Card, Board, Check
from pokerbots.engine.game import Table
from pokerbots.player.pokerbot import Pokerbot
from pokerbots.player.BostonDerby_old import TheBostonDerby8
from pokerbots.player.LukeBotAgg import LukeBotAgg
from eap import base
from eap import creator
from eap import toolbox
from eap import algorithms

bot = Pokerbot(LukeBotAgg())
test_bots = [Pokerbot(TheBostonDerby8())]
num_cpus = cpu_count()

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
creator.create("Population", list)
def gen():
    return sorted([random.randint(1,20)*5 for y in range(4)])
    #return random.randint(1,20)*5
tools = toolbox.Toolbox()
# Attribute generator
tools.register("attr_bool", gen)
# Structure initializers
tools.register("individual", creator.Individual,
    content_init=tools.attr_bool, size_init=4)
tools.register("population", creator.Population,
    content_init=tools.individual, size_init=50)

def playgame(p1, p2):
import sys
import random
import logging

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

from eap import algorithms
from eap import base
from eap import creator
from eap import halloffame
from eap import toolbox

MAX_ITEM = 50
MAX_WEIGHT = 50

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

# Create the item dictionary, items' name is an integer, and value is a (weight, value) 2-uple
items = dict([(i, (random.randint(1, 10), random.uniform(0, 100))) for i in xrange(100)])

tools = toolbox.Toolbox()

# Attribute generator
tools.register("attr_item", random.choice, items.keys())

# Structure initializers
tools.register("individual", creator.Individual, content_init=tools.attr_item, size_init=30)
tools.register("population", creator.Population, content_init=tools.individual, size_init=100)
Esempio n. 11
0
pset = gp.PrimitiveSet("MAIN", 1)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(safeDiv, 2)
pset.addPrimitive(operator.neg, 1)
pset.addPrimitive(math.cos, 1)
pset.addPrimitive(math.sin, 1)
pset.addEphemeralConstant(lambda: random.randint(-1, 1))
pset.addADF(adfset0)
pset.addADF(adfset1)
pset.addADF(adfset2)
pset.renameArguments({"ARG0": "x"})

creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
creator.create("ADF0", gp.PrimitiveTree, pset=adfset0)
creator.create("ADF1", gp.PrimitiveTree, pset=adfset1)
creator.create("ADF2", gp.PrimitiveTree, pset=adfset2)
creator.create("MAIN", gp.PrimitiveTree, pset=pset)

creator.create("Individual", list, fitness=creator.FitnessMin)

tools = toolbox.Toolbox()
tools.register('adf_expr0', gp.generateFull, pset=adfset0, min_=1, max_=2)
tools.register('adf_expr1', gp.generateFull, pset=adfset1, min_=1, max_=2)
tools.register('adf_expr2', gp.generateFull, pset=adfset2, min_=1, max_=2)
tools.register('main_expr', gp.generateRamped, pset=pset, min_=1, max_=2)

tools.register('ADF0', creator.ADF0, content_init=tools.adf_expr0)
tools.register('ADF1', creator.ADF1, content_init=tools.adf_expr1)