コード例 #1
0
"""
structural constants
====================
"""

from functools import partial

import deap.gp
import deap.tools
import numpy as np

from glyph import gp
from glyph.utils import Memoize
from glyph.utils.numeric import nrmse, silent_numpy

pset = gp.numpy_primitive_set(
    arity=1, categories=["algebraic", "trigonometric", "exponential"])
pset = gp.individual.add_sc(pset, gp.individual.sc_mmqout)
Individual = gp.Individual(pset=pset)


@Memoize
@silent_numpy
def measure(ind):
    g = lambda x: x**2 - 1.1
    points = np.linspace(-1, 1, 100, endpoint=True)
    y = g(points)
    f = gp.individual.numpy_phenotype(ind)
    try:
        yhat = f(points)
    except TypeError:
        yhat = np.infty
コード例 #2
0
from functools import partial

import numpy as np
import deap.gp
import deap.tools

from glyph import gp
from glyph.assessment import const_opt
from glyph.utils import Memoize
from glyph.utils.numeric import silent_numpy, nrmse

pset = gp.numpy_primitive_set(
    arity=1, categories=['algebraic', 'trigonometric', 'exponential', 'symc'])
Individual = gp.Individual(pset=pset)


class ADF(deap.gp.Primitive):
    def __init__(self, name, arity, variable_names=None):
        self.name = name
        self.arity = arity
        self.args = [deap.gp.__type__] * arity
        self.ret = deap.gp.__type__
        self.variable_names = variable_names or [
            "x_{}".format(i) for i in range(arity)
        ]
        self._format()

    def _format(self):
        self.fmt = self.name
        for i, v in enumerate(self.variable_names):
            self.fmt = self.fmt.replace(v, "{{{0}}}".format(i))
コード例 #3
0
from glyph import gp
from glyph.utils import Memoize
from glyph.utils.numeric import nrmse, silent_numpy


class Terminal(deap.gp.Terminal):
    def __init__(self):
        self.name = "w.dot(x) + b"
        self.ret = deap.gp.__type__

    def format(self, *args):
        return "w.dot(x) + b"


pset = gp.numpy_primitive_set(arity=0, categories=['algebraic'])
pset.terminals[object].append(Terminal())


def phenotype(individual):
    params = ",".join(" w{i}, b{i}".format(i=i)
                      for i in range(individual.n_args))
    args = "x," + params
    code = repr(individual)
    for i in range(individual.n_args):
        code = code.replace("w.dot(x) + b", "w{i}.dot(x) + b{i}".format(i=i),
                            1)
    expr = "lambda {}: {}".format(args, code)
    func = eval(expr, pset.context)
    return func