Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Genetic Algorithm: Threshold #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

class Problem(object):

"""The abstract class for a formal problem. You should subclass
"""The abstract class for a formal problem. You should subclass
this and implement the methods actions and result, and possibly
__init__, goal_test, and path_cost. Then you will create instances
of your subclass and solve them with the various search functions."""

def __init__(self, initial, goal=None):
"""The constructor specifies the initial state, and possibly a goal
state, if there is a unique goal. Your subclass's constructor can add
state, if there is a unique goal. Your subclass's constructor can add
other arguments."""
self.initial = initial
self.goal = goal
Expand Down Expand Up @@ -708,14 +708,26 @@ def genetic_algorithm(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ng
population = [mutate(recombine(*select(2, population, fitness_fn)), gene_pool, pmut)
for i in range(len(population))]

if f_thres:
fittest_individual = argmax(population, key=fitness_fn)
if fitness_fn(fittest_individual) >= f_thres:
return fittest_individual
fittest_individual = fitness_threshold(fitness_fn, f_thres, population)
if fittest_individual:
return fittest_individual


return argmax(population, key=fitness_fn)


def fitness_threshold(fitness_fn, f_thres, population):
if not f_thres:
return None

fittest_individual = argmax(population, key=fitness_fn)
if fitness_fn(fittest_individual) >= f_thres:
return fittest_individual

return None



def init_population(pop_number, gene_pool, state_length):
"""Initializes population for genetic algorithm
pop_number : Number of individuals in population
Expand Down