Code for new termination function

2 views
Skip to first unread message

zunzun

unread,
Sep 12, 2011, 1:31:33 PM9/12/11
to Evolutionary Computations in Python
I am using a genetic algorithm to make an initial estimation of
function parameters for non-linear regressions, and would like one of
the terminations conditions to be, "Has any function evaluation
reached a limit of X", where X would be what I consider to be a
sufficiently good enough guess at intial parameters.

Here is my termination function code, please consider it for inclusion
into ecspy:


def best_fitness_limit_termination(population, num_generations,
num_evaluations, args):
if args.get('_ec').maximize:
best_fit = max([x.fitness for x in population])
if best_fit >= args.get('best_fitness_limit'):
return True
else:
best_fit = min([x.fitness for x in population])
if best_fit <= args.get('best_fitness_limit'):
return True
return False


James Phillips
2548 Vera Cruz Drive
Birmingham, AL USA 35235

http://zunzun.com
zunzun at zunzun.com

zunzun

unread,
Sep 13, 2011, 9:52:35 AM9/13/11
to Evolutionary Computations in Python
Using the best fitness limit terminator, here is a brute force method
for finding optimal parameters for the Rosenbrock problem:

def main():

prng = Random() # random number generator

numberOfLoopsToAverage = 10

# limits
additional_arguments = {'best_fitness_limit':0.005}
maximum_number_of_function_evaluations = 10000
maximum_number_of_generations = 100

#initial values and iteration lists
initial_random_generator_seed = 3
initial_population_seeds = [] # lists of initial population
candidates if any have been estimated
population_sizes_for_iteration = [50, 75, 100, 200, 300]
number_of_selected_for_iteration = [1, 2, 5, 7]
number_of_offspring_for_iteration = [1, 2, 5, 7]
mutation_rates_for_iteration = [0.0001, 0.001, 0.01, 0.1, 0.2,
0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
use_one_fifth_rule_flags_for_iteration = [True, False]
number_of_elites_for_iteration = [0, 1, 2, 5, 7, 10]
number_of_tourney_sizes_for_iteration = [1, 2, 5, 7, 10]
neighborhood_sizes_for_iteration = [1, 2, 5, 7, 10]

# genetic algorithm
problem = benchmarks.Rosenbrock(2)
for populationSize in population_sizes_for_iteration:
for numberOfElites in number_of_elites_for_iteration:
if numberOfElites >= populationSize:
continue
for useOneFifthRuleFlag in
use_one_fifth_rule_flags_for_iteration:
for mutationRate in mutation_rates_for_iteration:
averageFitness = 0.0
averageNumberOfEvaluations = 0
maxNumberOfEvaluationsFlag = False
maxNumberOfGenerationsFlag = False
for loopCount in range(numberOfLoopsToAverage):
prng.seed(initial_random_generator_seed +
loopCount)
ea = ec.GA(prng)
ea.terminator =
[best_fitness_limit_termination, terminators.evaluation_termination,
terminators.generation_termination]
ea.variator = [variators.uniform_crossover,
variators.gaussian_mutation]
final_pop = ea.evolve(generator =
problem.generator,
evaluator =
problem.evaluator,
pop_size =
populationSize,
maximize =
problem.maximize,
bounder =
problem.bounder,
max_evaluations =
maximum_number_of_function_evaluations,
max_generations =
maximum_number_of_generations,
seeds =
initial_population_seeds,
num_elites =
numberOfElites,
mutation_rate =
mutationRate,
use_one_fifth_rule =
useOneFifthRuleFlag,
**additional_arguments)
if ea.num_evaluations >=
maximum_number_of_function_evaluations:
maxNumberOfEvaluationsFlag = True
break
if ea.num_generations >=
maximum_number_of_generations:
maxNumberOfGenerationsFlag = True
break
final_pop.sort(reverse=True)
averageFitness += final_pop[0].fitness
averageNumberOfEvaluations +=
ea.num_evaluations

if maxNumberOfEvaluationsFlag or
maxNumberOfGenerationsFlag:
continue
averageFitness /= numberOfLoopsToAverage
averageNumberOfEvaluations /=
numberOfLoopsToAverage
print averageFitness, \
averageNumberOfEvaluations, \
populationSize, \
numberOfElites, \
useOneFifthRuleFlag, \
mutationRate, \
ea.__class__.__name__, \
problem.__class__.__name__

James

Aaron Garrett

unread,
Sep 13, 2011, 11:51:53 PM9/13/11
to ec...@googlegroups.com
I think you can do the same thing for your function like this:

def best_fitness_limit_termination(population, num_generations, num_evaluations, args): 
    best_fit = max([x for x in population])
    dummy = ec.Individual(None, best_fit.maximize)
    dummy.fitness = args.get('best_fitness_limit')
    return best_fit >= dummy

I haven't tested that, but I think it will work just as well as what you have.

I'd like to talk with you more about your function contribution and how you generally use ecspy. Can you email me at aaron.le...@gmail.com?


Aaron Garrett, Ph.D.
Assistant Professor
Computer Science
Jacksonville State University
Jacksonville, AL  36265
agar...@jsu.edu
256-782-5364
http://mcis.jsu.edu/faculty/agarrett


--
You received this message because you are subscribed to the Google Groups "Evolutionary Computations in Python" group.
To post to this group, send email to ec...@googlegroups.com.
To unsubscribe from this group, send email to ecspy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ecspy?hl=en.


zunzun

unread,
Sep 15, 2011, 9:48:16 AM9/15/11
to Evolutionary Computations in Python
Will do. Note how Google Groups chopped up your e-mail address,
although it is on the JSU page that you linked to.

James

On Sep 13, 10:51 pm, Aaron Garrett <aaron.lee.garr...@gmail.com>
wrote:
> Can you email me at aaron.lee.garr...@gmail.com?
Reply all
Reply to author
Forward
0 new messages