Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def mutate(self, x, a, b):
differential = [self.MIXING_RATE*(ai-bi)
for ai, bi
in
zip(a.params, b.params)
]
mutantParams = [xi + diffi
for xi, diffi
in
zip(x.params, differential)]
if (self.NON_NEGATIVE):
npParams = np.asarray(mutantParams)
negativeIndicies = npParams < 0
npParams[negativeIndicies] = 0
mutantParams = npParams.tolist()
return Member(mutantParams)
np.random.randint(0, self.POPULATION)]
# Create mutant
mutant = self.mutate(member, a, b)
# Crossover to create trial member
trialP = []
pInd = np.random.randint(
0, numParams) # random index of parameter to cross over
for j, (
mutantP, originalP
) in enumerate(zip(mutant.params, member.params)):
r = np.random.uniform(0, 1)
if r < self.CROSSOVER_RATE or j == pInd:
trialP.append(mutantP)
else:
trialP.append(originalP)
trialMember = Member(trialP)
# In the async case, create list of trial members and
# async sim jobs to feed into fitness function
sim = self.simFcn(self.rr, trialMember.params)
asyncTrialMembers.append(trialMember)
asyncSims.append(sim)
# Async operation so still need to make members
for i, (simResult,
trialMember
) in enumerate(zip(asyncSims, asyncTrialMembers)):
try:
sim = np.copy(simResult.get()[-1])
trialFitness = self.fitnessFcn(sim)
except Exception as ex:
if ex.message.find('CVODE') > -1:
def createRandomMember(self):
newParams = [np.random.uniform(self.paramRange[i][0],
self.paramRange[i][1])
for i, p
in
enumerate(self.rr.model.getGlobalParameterValues())]
newMember = Member(newParams)
return newMember
np.random.randint(0, self.POPULATION)]
# Create mutant
mutant = self.mutate(member, a, b)
# Crossover to create trial member
trialP = []
pInd = np.random.randint(
0, numParams) # random index of parameter to cross over
for j, (
mutantP, originalP
) in enumerate(zip(mutant.params, member.params)):
r = np.random.uniform(0, 1)
if r < self.CROSSOVER_RATE or j == pInd:
trialP.append(mutantP)
else:
trialP.append(originalP)
trialMember = Member(trialP)
# Fitness function should know how to handle trialmember
# and run simulation
try:
sim = np.copy(self.simFcn(self.rr, trialMember.params))
trialFitness = self.fitnessFcn(sim)
except RuntimeError:
sim = None
trialFitness = float('inf')
memberFitness = self.generations[-1]['fitness'][i]
memberSim = self.generations[-1]['sim'][i]
# Replace member with trial member if better fitness
if (trialFitness < memberFitness):
newMembers.append(trialMember)
newFitnesses.append(trialFitness)
self.updateBestFitness(trialFitness)