Skip to content

Commit

Permalink
Merge 08c2643 into 3b69822
Browse files Browse the repository at this point in the history
  • Loading branch information
firefly-cpp committed Apr 13, 2019
2 parents 3b69822 + 08c2643 commit 002de57
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
11 changes: 6 additions & 5 deletions NiaPy/algorithms/basic/ba.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ def initPopulation(self, task):
d.update({'S': S, 'Q': Q, 'v': v})
return Sol, Fitness, d

def runIteration(self, task, Sol, Fitness, xb, fxb, S, Q, v, **dparams):
def runIteration(self, task, Sol, Fitness, best, f_min, S, Q, v, **dparams):
r"""Core function of Bat Algorithm.
Parameters:
task (Task): Optimization task.
Sol (numpy.ndarray): Current population
Fitness (numpy.ndarray[float]): Current population fitness/funciton values
xb (numpy.ndarray): Current best individual
fxb (float): Current best individual function/fitness value
best (numpy.ndarray): Current best individual
f_min (float): Current best individual function/fitness value
S (numpy.ndarray): TODO
Q (numpy.ndarray[float]): TODO
v (numpy.ndarray[float]): TODO
Expand All @@ -126,10 +126,11 @@ def runIteration(self, task, Sol, Fitness, xb, fxb, S, Q, v, **dparams):
* v (numpy.ndarray[float]): TODO
"""
for i in range(self.NP):
Q[i], v[i], S[i] = self.Qmin + (self.Qmax - self.Qmin) * self.uniform(0, 1), v[i] + (Sol[i] - xb) * Q[i], task.repair(Sol[i] + v[i], rnd=self.Rand)
if self.rand() > self.r: S[i] = task.repair(xb + 0.001 * self.normal(0, 1, task.D), rnd=self.Rand)
Q[i], v[i], S[i] = self.Qmin + (self.Qmax - self.Qmin) * self.uniform(0, 1), v[i] + (Sol[i] - best) * Q[i], task.repair(Sol[i] + v[i])
if self.rand() > self.r: S[i] = task.repair(best + 0.001 * self.normal(0, 1, task.D))
Fnew = task.eval(S[i])
if (Fnew <= Fitness[i]) and (self.rand() < self.A): Sol[i], Fitness[i] = S[i], Fnew
if Fnew <= f_min: best, f_min = S[i], Fnew
return Sol, Fitness, {'S': S, 'Q': Q, 'v': v}

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
9 changes: 5 additions & 4 deletions NiaPy/algorithms/modified/hba.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def typeParameters():
d['CR'] = lambda x: isinstance(x, float) and 0 <= x <= 1
return d

def setParameters(self, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
def setParameters(self, NP=40, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
r"""Set core parameters of HybridBatAlgorithm algorithm.
Arguments:
Expand All @@ -66,7 +66,7 @@ def setParameters(self, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
"""
BatAlgorithm.setParameters(self, **ukwargs)
self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
self.A, self.r, self.Qmin, self.Qmax, self.F, self.CR, self.CrossMutt = A, r, Qmin, Qmax, F, CR, CrossMutt
if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))

def initPopulation(self, task):
Expand All @@ -87,7 +87,7 @@ def initPopulation(self, task):
return Sol, Fitness, {'v': v}

def runIteration(self, task, Sol, Fitness, best, f_min, v, **dparams):
r"""Core funciton of HybridBatAlgorithm algorithm.
r"""Core function of HybridBatAlgorithm algorithm.
Args:
task (Task): Optimization task.
Expand All @@ -112,6 +112,7 @@ def runIteration(self, task, Sol, Fitness, best, f_min, v, **dparams):
if self.rand() > self.r: S = task.repair(self.CrossMutt(Sol, i, best, self.F, self.CR, rnd=self.Rand), rnd=self.Rand)
f_new = task.eval(S)
if f_new <= Fitness[i] and self.rand() < self.A: Sol[i], Fitness[i] = S, f_new
return Sol, Fitness, {'v': v}
if f_new <= f_min: best, f_min = S, f_new
return Sol, Fitness, {'S': S, 'Q': Q, 'v': v}

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
3 changes: 1 addition & 2 deletions examples/run_hba.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#we will run Hybrid Bat Algorithm for 5 independent runs
for i in range(5):
task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = HybridBatAlgorithm(NP=40, A=0.9, r=0.1, F=0.001, CR=0.9, Qmin=0.0, Qmax=2.0,)
algo = HybridBatAlgorithm(NP=40, A=0.5, r=0.5, F=0.5, CR=0.9, Qmin=0.0, Qmax=2.0)
best = algo.run(task=task)
print(best)

4 changes: 2 additions & 2 deletions examples/run_pso.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#we will run ParticleSwarmAlgorithm for 5 independent runs
for i in range(5):
task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = ParticleSwarmAlgorithm(NP=40, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4)
best = algo.run(task=task)
print best
print(best)
27 changes: 12 additions & 15 deletions examples/stopping_criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,31 @@

import random
import logging
from NiaPy import Runner
from NiaPy.util import Task, TaskConvPrint, TaskConvPlot, OptimizationType, getDictArgs
from NiaPy.algorithms.modified import SelfAdaptiveDifferentialEvolution
from NiaPy.algorithms.basic import DifferentialEvolution, MonkeyKingEvolutionV3
from NiaPy.util import StoppingTask, OptimizationType
from NiaPy.algorithms.basic import DifferentialEvolution
from NiaPy.benchmarks import Griewank, Sphere

#1 Number of function evaluations (nFES) as a stopping criteria
for i in range(10):
task = Task(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(task=task, NP=40, CR=0.9, F=0.5)
best = algo.run()
task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5)
best = algo.run(task)
print (best)

print ('---------------------------------------')

#1 Number of generations (iterations) as a stopping criteria
#2 Number of generations (iterations) as a stopping criteria
for i in range(10):
task = Task(D=10, nGEN=100, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(task=task, NP=40, CR=0.5)
best = algo.run()
task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5)
best = algo.run(task)
print (best)

print ('---------------------------------------')

#3 Reference value as a stopping criteria
for i in range(10):
task = Task(D=10, refValue=50.0, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(task=task, NP=40, CR=0.5)
best = algo.run()
task = StoppingTask(D=10, refValue=50.0, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5)
best = algo.run(task)
print (best)

0 comments on commit 002de57

Please sign in to comment.