Example #1
0

"""Prepatory steps"""


"""Steps 1 & 2: Specify the function and terminal sets

The function and terminal sets, together known as the primitive set, specify the
components which can be used to construct possible solutions to the problem. For
a mathematical problem, these will be mathematical functions and constants.
"""
pset = primitives.pset
v = ["x", "y"]
for item in v:
    pset[item] = 0
s = tools.primitive_handler(pset, v)


"""Step 3: Define the fitness measure

The fitness measure is the pygp.fitness function found in the pygp module; the
data this fitness function will use to evaluate evolved programs and determine
their fitnesses is imported below.
"""
filename = "data.csv"
data = tools.read_data(filename)


"""Step 4: Set run parameters

These controls determine the size of the population, maximum starting
Example #2
0
run: "))
print("Enter the rates of crossover, reproduction, and mutation. Their sum\
must be exactly 1.0.")
controls["cross_rate"] = float(input("Crossover rate: "))
controls["rep_rate"] = float(input("Reproduction rate: ")) + controls["cross_rate"]
controls["mut_rate"] = float(input("Mutation rate: ")) + controls["cross_rate"] + controls["rep_rate"]


p = primitives.pset

#make a function
vs = input("Enter the variables corresponding to the columns in the data \
file, in order from left to right, separated only by commas: ")
print("The specified variables are:", vs)
v = vs.split(",")
for item in v:
    p[item] = 0
s = tools.primitive_handler(p, v)


print("Generating initial population")
pop = me.ramped(p, s, controls)
print("")
solutioninfo = me.evolve(pop, data, controls)
winner = deepcopy(solutioninfo["best"])
print("The winning program is:")
print(winner.display())
print("Its fitness score was", solutioninfo["score"],
      "and it appeared in generation", solutioninfo["gen"])
print()