Skip to content

Instantly share code, notes, and snippets.

@RileyLazarou
Last active November 17, 2019 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RileyLazarou/418f177a988459576d9144e0e4909e9e to your computer and use it in GitHub Desktop.
Save RileyLazarou/418f177a988459576d9144e0e4909e9e to your computer and use it in GitHub Desktop.
# The function to create the initial population
organism_creator = lambda : Organism([1, 16, 16, 16, 1], output='linear')
# The function we are trying to learn. numpy doesn't have tau...
true_function = lambda x : np.sin(2 * np.pi * x) #
# The loss function, mean squared error, will serve as the negative fitness
loss_function = lambda y_true, y_estimate : np.mean((y_true - y_estimate)**2)
def simulate_and_evaluate(organism, replicates=1):
"""
Randomly generate `replicates` samples in [0,1],
use the organism to predict their corresponding value,
and return the fitness score of the organism
"""
X = np.random.random((replicates, 1))
predictions = organism.predict(X)
loss = loss_function(true_function(X), predictions)
return -loss
# Ecosystem requires a function that maps an organism to a real number fitness
scoring_function = lambda organism : simulate_and_evaluate(organism, replicates=100)
# Create the ecosystem
ecosystem = Ecosystem(organism_creator, scoring_function,
population_size=100, holdout=0.1, mating=True)
# Save the fitness score of the best organism in each generation
best_organism_scores = [ecosystem.get_best_organism(include_reward=True)[1]]
generations = 201
for i in range(generations):
ecosystem.generation()
this_generation_best = ecosystem.get_best_organism(include_reward=True)
best_organism_scores.append(this_generation_best[1])
# [Visualization code omitted...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment