Ejemplo n.º 1
0
def create_multistatistics(fitness_indices):
    """ create multi statistics for evolution

    :param fitness_indices: list
        list of strings representing fit indices. These should be in same order as used during evolution
    :return: an instance of deap.tools.MultiStatistics
    """
    statistics = {}
    for i, fit_index in enumerate(fitness_indices):
        stats = create_statistics(fit_index, i)
        statistics[fit_index] = stats
    multi = MultiStatistics(statistics)
    return multi
Ejemplo n.º 2
0
Archivo: gp.py Proyecto: boliqq07/BGP
def Statis_func(stats=None):
    if stats is None:
        stats = {"fitness_dim_max": ("max",), "dim_is_target": ("sum",)}

    func = {"max": np.max, "mean": np.mean, "min": np.min, "std": np.std, "sum": np.sum}
    att = {

        "fitness": lambda ind: ind.fitness.values[0],
        "fitness_dim_max": lambda ind: ind.fitness.values[0] if ind.dim_score else -np.inf,
        "fitness_dim_min": lambda ind: ind.fitness.values[0] if ind.dim_score else np.inf,
        "dim_is_target": lambda ind: 1 if ind.dim_score else 0,
        # special
        "coef": lambda ind: score_dim(ind.y_dim, "coef", fuzzy=False),
        "integer": lambda ind: score_dim(ind.y_dim, "integer", fuzzy=False),

        "length": lambda ind: len(ind),
        "height": lambda ind: ind.height,
        "h_bgp": lambda ind: ind.h_bgp,

        # mutil-target
        "weight_fitness": lambda ind: ind.fitness.wvalues,
        "weight_fitness_dim": lambda ind: ind.fitness.wvalues if ind.dim_score else -np.inf,
        # weight have mul the "-"
    }

    sa_all = {}

    for a, f in stats.items():
        if a in att:
            a_s = att[a]
        elif isinstance(callable, a):
            a_s = a
            a = str(a).split(" ")[1]
        else:
            raise TypeError("the key must be in definition or a function")
        sa = Statistics(a_s)
        if isinstance(f, str):
            f = [f, ]
        for i, fi in enumerate(f):
            assert fi in func
            ff = func[fi]

            sa.register(fi, ff)

        sa_all["Cal_%s" % a] = sa
    stats = MultiStatistics(sa_all)

    return stats
Ejemplo n.º 3
0
Archivo: srwc.py Proyecto: boliqq07/BGP
def mainPart(x_,
             y_,
             pset,
             max_=5,
             pop_n=100,
             random_seed=2,
             cxpb=0.8,
             mutpb=0.1,
             ngen=5,
             tournsize=3,
             max_value=10,
             double=False,
             score=None,
             cal_dim=True,
             target_dim=None,
             inter_add=True,
             iner_add=True,
             random_add=False,
             store=True):
    """

    Parameters
    ----------
    target_dim
    max_
    inter_add
    iner_add
    random_add
    cal_dim
    score
    double
    x_
    y_
    pset
    pop_n
    random_seed
    cxpb
    mutpb
    ngen
    tournsize
    max_value

    Returns
    -------

    """
    if score is None:
        score = [r2_score, explained_variance_score]

    if cal_dim:
        assert all([isinstance(i, Dim) for i in pset.dim_list
                    ]), "all import dim of pset should be Dim object"

    random.seed(random_seed)
    toolbox = Toolbox()

    if isinstance(pset, ExpressionSet):
        PTrees = ExpressionTree
        Generate = genHalfAndHalf
        mutate = mutNodeReplacement
        mate = cxOnePoint
    elif isinstance(pset, FixedSet):
        PTrees = FixedTree
        Generate = generate_index
        mutate = mutUniForm_index
        mate = partial(cxOnePoint_index, pset=pset)

    else:
        raise NotImplementedError("get wrong pset")
    if double:
        Fitness_ = creator.create("Fitness_", Fitness, weights=(1.0, 1.0))
    else:
        Fitness_ = creator.create("Fitness_", Fitness, weights=(1.0, ))

    PTrees_ = creator.create("PTrees_",
                             PTrees,
                             fitness=Fitness_,
                             dim=dnan,
                             withdim=0)
    toolbox.register("generate", Generate, pset=pset, min_=1, max_=max_)
    toolbox.register("individual",
                     initIterate,
                     container=PTrees_,
                     generator=toolbox.generate)
    toolbox.register('population',
                     initRepeat,
                     container=list,
                     func=toolbox.individual)
    # def selection
    toolbox.register("select_gs", selTournament, tournsize=tournsize)
    toolbox.register("select_kbest_target_dim",
                     selKbestDim,
                     dim_type=target_dim,
                     fuzzy=True)
    toolbox.register("select_kbest_dimless", selKbestDim, dim_type="integer")
    toolbox.register("select_kbest", selKbestDim, dim_type='ignore')
    # def mate
    toolbox.register("mate", mate)
    # def mutate
    toolbox.register("mutate", mutate, pset=pset)
    if isinstance(pset, ExpressionSet):
        toolbox.decorate(
            "mate",
            staticLimit(key=operator.attrgetter("height"),
                        max_value=max_value))
        toolbox.decorate(
            "mutate",
            staticLimit(key=operator.attrgetter("height"),
                        max_value=max_value))
    # def elaluate
    toolbox.register("evaluate",
                     calculatePrecision,
                     pset=pset,
                     x=x_,
                     y=y_,
                     scoring=score[0],
                     cal_dim=cal_dim,
                     inter_add=inter_add,
                     iner_add=iner_add,
                     random_add=random_add)
    toolbox.register("evaluate2",
                     calculatePrecision,
                     pset=pset,
                     x=x_,
                     y=y_,
                     scoring=score[1],
                     cal_dim=cal_dim,
                     inter_add=inter_add,
                     iner_add=iner_add,
                     random_add=random_add)
    toolbox.register("parallel",
                     parallelize,
                     n_jobs=1,
                     func=toolbox.evaluate,
                     respective=False)
    toolbox.register("parallel2",
                     parallelize,
                     n_jobs=1,
                     func=toolbox.evaluate2,
                     respective=False)

    pop = toolbox.population(n=pop_n)

    haln = 10
    hof = HallOfFame(haln)

    stats1 = Statistics(lambda ind: ind.fitness.values[0]
                        if ind and ind.y_dim in target_dim else 0)
    stats1.register("max", np.max)

    stats2 = Statistics(lambda ind: ind.y_dim in target_dim if ind else 0)
    stats2.register("countable_number", np.sum)
    stats = MultiStatistics(score1=stats1, score2=stats2)

    population, logbook = eaSimple(pop,
                                   toolbox,
                                   cxpb=cxpb,
                                   mutpb=mutpb,
                                   ngen=ngen,
                                   stats=stats,
                                   halloffame=hof,
                                   pset=pset,
                                   store=store)

    return population, hof
Ejemplo n.º 4
0
def mainPart(x_, y_, pset, pop_n=100, random_seed=1, cxpb=0.8, mutpb=0.1, ngen=5, alpha=1,
             tournsize=3, max_value=10, double=False, score=None, **kargs):
    """

    Parameters
    ----------
    score
    double
    x_
    y_
    pset
    pop_n
    random_seed
    cxpb
    mutpb
    ngen
    alpha
    tournsize
    max_value
    kargs

    Returns
    -------

    """
    max_ = pset.max_
    if score is None:
        score = [r2_score, explained_variance_score]
    random.seed(random_seed)
    toolbox = Toolbox()
    if isinstance(pset, PrimitiveSet):
        PTrees = ExpressionTree
        Generate = genHalfAndHalf
        mutate = mutNodeReplacement
        mate = cxOnePoint
    elif isinstance(pset, FixedPrimitiveSet):
        PTrees = FixedExpressionTree
        Generate = generate_
        mate = partial(cxOnePoint_index, pset=pset)
        mutate = mutUniForm_index
    else:
        raise NotImplementedError("get wrong pset")
    if double:
        creator.create("Fitness_", Fitness, weights=(1.0, 1.0))
    else:
        creator.create("Fitness_", Fitness, weights=(1.0,))
    creator.create("PTrees_", PTrees, fitness=creator.Fitness_)
    toolbox.register("generate_", Generate, pset=pset, min_=None, max_=max_)
    toolbox.register("individual", initIterate, container=creator.PTrees_, generator=toolbox.generate_)
    toolbox.register('population', initRepeat, container=list, func=toolbox.individual)
    # def selection
    toolbox.register("select_gs", selTournament, tournsize=tournsize)
    # def mate
    toolbox.register("mate", mate)
    # def mutate
    toolbox.register("mutate", mutate, pset=pset)
    if isinstance(pset, PrimitiveSet):
        toolbox.decorate("mate", staticLimit(key=operator.attrgetter("height"), max_value=max_value))
        toolbox.decorate("mutate", staticLimit(key=operator.attrgetter("height"), max_value=max_value))
    # def elaluate
    toolbox.register("evaluate", calculate, pset=pset, x=x_, y=y_, score_method=score[0], **kargs)
    toolbox.register("evaluate2", calculate, pset=pset, x=x_, y=y_, score_method=score[1], **kargs)

    stats1 = Statistics(lambda ind: ind.fitness.values[0])
    stats = MultiStatistics(score1=stats1, )
    stats.register("avg", np.mean)
    stats.register("max", np.max)

    pop = toolbox.population(n=pop_n)

    haln = 5
    hof = HallOfFame(haln)

    if double:
        population, logbook = multiEaSimple(pop, toolbox, cxpb=cxpb, mutpb=mutpb, ngen=ngen, stats=stats, alpha=alpha,
                                            halloffame=hof, pset=pset)
    else:
        population, logbook = eaSimple(pop, toolbox, cxpb=cxpb, mutpb=mutpb, ngen=ngen, stats=stats,
                                       halloffame=hof, pset=pset)

    return population, logbook, hof