Exemplo n.º 1
0
def train():
    # Create CGP graph.
    atari_cgp = Cartesian("atari_cgp", **config.cartesian_params)
    # Optimise it!
    res = oneplus(optimisation_fce, cls=atari_cgp, **config.oneplus_params)
    env.close()
    return res
Exemplo n.º 2
0
import numpy as np
from sklearn.utils.validation import check_random_state

from cartesian.algorithm import oneplus
from cartesian.cgp import *

rng = check_random_state(1337)
primitives = [
    Primitive("add", np.add, 2),
    Primitive("mul", np.multiply, 2),
    Symbol("x_0"),
    Symbol("x_1"),
    Ephemeral("erc", rng.normal),
]
pset = PrimitiveSet.create(primitives)
x = rng.normal(size=(100, 2))
y = x[:, 1] * x[:, 0] + 0.3
y += 0.05 * rng.normal(size=y.shape)


def func(individual):
    f = compile(individual)
    yhat = f(*x.T)
    return np.sqrt(np.mean((y - yhat) ** 2)) / (y.max() - y.min())


MyCartesian = Cartesian("MyCartesian", pset, n_rows=2, n_columns=3, n_out=1, n_back=1)
res = oneplus(func, cls=MyCartesian, f_tol=0.01, random_state=rng, maxfev=50000, n_jobs=1)
print(res)
Exemplo n.º 3
0
    Primitive("mul", np.multiply, 2),
    Symbol("x_0"),
    Symbol("x_1")
]
pset = PrimitiveSet.create(primitives)
rng = check_random_state(42)
x = rng.normal(size=(100, 2))
y = x[:, 1] * x[:, 1] + x[:, 0]
y += 0.05 * rng.normal(size=y.shape)


def func(individual):
    yhat = individual.fit_transform(x)
    return np.sqrt(np.mean((y - yhat)**2))


MyCartesian = Cartesian("MyCartesian",
                        pset,
                        n_rows=2,
                        n_columns=3,
                        n_out=1,
                        n_back=1)
res = oneplus(func,
              f_tol=0.1,
              cls=MyCartesian,
              random_state=rng,
              maxiter=10000,
              n_jobs=1)
print(res)
print(to_polish(res.ind, return_args=False))
Exemplo n.º 4
0
            stack.append(node.function(*args))
        elif node.arity == 0:
            stack.append(node.name)
        else:
            raise ValueError("Incorrect program.")
    return re.compile(stack[-1])


data = {
    k: -1 if "aba" in k else 1
    for k in map(
        lambda x: "".join(random.sample("aaaaabbbbb", k=random.randint(1, 7))),
        range(20))
}

print(data, sum(v for v in data.values() if v == -1))


def evaluate(program):
    regex = compile(program)
    return sum(v for k, v in data.items() if re.match(regex, k))


res = oneplus(evaluate,
              cls=MyCartesian,
              maxiter=10000,
              n_jobs=1,
              f_tol=-1000000)
print(res)
print(compile(res.ind))