Skip to content
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

add mutMixedAttributes mutation function #752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion deap/tools/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import random

from itertools import repeat
from webbrowser import get

from deap.base import Toolbox

try:
from collections.abc import Sequence
Expand Down Expand Up @@ -243,5 +246,34 @@ def mutESLogNormal(individual, c, indpb):
return individual,


# Custom mutation function
def mutMixedAttributes(seqFunc, individual, indpb):

'''Mutate an individual by applying a mutation function to each attribute
with probability indpb.
:param individual: Individual to be mutated.
:param seqFunc: sequence of functions to be applied to attributes.
:param indpb: Independent probability for each attribute to be mutated.
:returns: A tuple of one individual.

the individual is assumed to be created using initCycle function
'''
func, args, keywords = seqFunc.func, seqFunc.args, seqFunc.keywords

if func.__name__ != 'initCycle':
raise ValueError("Individual must have been created using initCycle function")

n = keywords['n'] # number of times to iterate through the list of functions
index = 1 # index to function sequence
num_Attr = len(args[index])

for i in range(n * num_Attr):
if random.random() < indpb:
individual[i] = args[index][i % num_Attr]()
return individual,



__all__ = ['mutGaussian', 'mutPolynomialBounded', 'mutShuffleIndexes',
'mutFlipBit', 'mutUniformInt', 'mutInversion', 'mutESLogNormal']
'mutFlipBit', 'mutUniformInt', 'mutInversion', 'mutESLogNormal',
'mutMixedAttributes']