Last active
December 14, 2015 07:09
-
-
Save cmd-ntrf/5048386 to your computer and use it in GitHub Desktop.
Example of a evaluation function for a trivial symbolic regression problems with 12 inputs using DEAP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
import numpy | |
from math import sqrt | |
from deap import gp | |
# Our dataset is 100 random arrays of 0s and 1s | |
# and the function we are trying to find is simply | |
# the sum of every arguments ARG0+ARG1+...+ARG11 | |
dataset = numpy.random.random_integers(0, 1, (100, 12)) | |
targets = numpy.sum(dataset, axis=1) | |
# Initialization of a trivial primitive set | |
pset = gp.PrimitiveSet("MAIN", 12) | |
pset.addPrimitive(operator.add, 2) | |
pset.addTerminal(1.0) | |
def evaluate(individual): | |
func = gp.lambdify(individual, pset) | |
sum_ = 0 | |
# Compute the output for each input then | |
# sum to the rest the squared difference | |
# between the target and the actual output. | |
for data, target in zip(dataset, targets): | |
# The star (*) unpack the list of 12 elements | |
# to provide the 12 argument to the function. | |
output = func(*data) | |
sum_ += (target - output)**2 | |
rmse = sqrt(sum_ / len(outputs)) | |
# DEAP requires that evaluation function to always | |
# return a tuple | |
return rmse, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment