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

modified by adaptive mutation&crossove(ILM/DHC method, DHM/ILC method… #2284

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions ravenframework/Optimizers/crossOverOperators/crossovers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def onePointCrossover(parents,**kwargs):
dims=['chromosome','Gene'],
coords={'chromosome': np.arange(int(2*comb(nParents,2))),
'Gene':kwargs['variables']})


# defaults
if (kwargs['crossoverProb'] == None) or ('crossoverProb' not in kwargs.keys()):
crossoverProb = randomUtils.random(dim=1, samples=1)
Expand All @@ -54,11 +52,11 @@ def onePointCrossover(parents,**kwargs):

# create children
parentsPairs = list(combinations(parents,2))

for ind,parent in enumerate(parentsPairs):
parent = np.array(parent).reshape(2,-1) # two parents at a time

if randomUtils.random(dim=1,samples=1) <= crossoverProb:
new_crossoverProb=1-((2*ind)/len(children)) #ILM/DHC method
new_crossoverProb2=((2*ind)/len(children)) #DHM/ILC method
if randomUtils.random(dim=1,samples=1) <= new_crossoverProb2:
if (kwargs['points'] == None) or ('points' not in kwargs.keys()):
point = list([randomUtils.randomIntegers(1,nGenes-1,None)])
elif (any(i>=nGenes-1 for i in kwargs['points'])):
Expand Down
23 changes: 21 additions & 2 deletions ravenframework/Optimizers/mutators/mutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def swapMutator(offSprings, distDict, **kwargs):
for i in range(np.shape(offSprings)[0]):
children[i] = offSprings[i]
## TODO What happens if loc1 or 2 is out of range?! should we raise an error?
if randomUtils.random(dim=1,samples=1)<=kwargs['mutationProb']:
new_mutationProb=(i/len(children)) #ILM/DHC method
new_mutationProb2=1-(i/len(children)) #DHM/ILC method
if randomUtils.random(dim=1,samples=1)<=new_mutationProb2:
#kwargs['mutationProb']
# convert loc1 and loc2 in terms on cdf values
cdf1 = distDict[offSprings.coords['Gene'].values[loc1]].cdf(float(offSprings[i,loc1].values))
cdf2 = distDict[offSprings.coords['Gene'].values[loc2]].cdf(float(offSprings[i,loc2].values))
Expand Down Expand Up @@ -136,9 +139,13 @@ def randomMutator(offSprings, distDict, **kwargs):
"""
if kwargs['locs'] is not None and 'locs' in kwargs.keys():
raise ValueError('Locs arguments are not being used by randomMutator')
k=0
for child in offSprings:
# the mutation is performed for each child independently
if randomUtils.random(dim=1,samples=1)<kwargs['mutationProb']:
new_mutationProb=(k/len(offSprings))
k=k+1
if randomUtils.random(dim=1,samples=1)<new_mutationProb:
# kwargs['mutationProb']
# sample gene location to be flipped: i.e., determine loc
chromosomeSize = child.values.shape[0]
loc = randomUtils.randomIntegers(0, chromosomeSize, caller=None, engine=None)
Expand Down Expand Up @@ -217,3 +224,15 @@ def returnInstance(cls, name):
if name not in __mutators:
cls.raiseAnError (IOError, "{} MECHANISM NOT IMPLEMENTED!!!!!".format(name))
return __mutators[name]

# def adaptivemutator(**kwargs):
# """
# Method designed to adaptive mutation rate:
# @ In, kwargs['mutationProb']
# @ Out, kwargs['new_mutationProb']
# """
# for i in range():
# new_mutationProb=1-(i/len(children))
# kwargs['mutationProb']=new_mutationProb
# print(new_mutationProb)
# return new_mutationProb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

<GAparams>
<populationSize>17</populationSize>
<!-- 17 -->
<reproduction>
<crossover type="onePointCrossover">
<crossoverProb>0.9</crossoverProb>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<crossover type="onePointCrossover">
<crossoverProb>0.8</crossoverProb>
</crossover>
<mutation type="swapMutator">
<mutation type="randomMutator">
<mutationProb>0.9</mutationProb>
</mutation>
</reproduction>
Expand Down