forked from anyoptimization/pymoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
160 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import numpy as np | ||
|
||
from pymoo.algorithms.genetic_algorithm import GeneticAlgorithm | ||
from pymoo.operators.crossover.real_differental_evolution_crossover import DifferentialEvolutionCrossover | ||
from pymoo.operators.default_operators import set_default_if_none | ||
from pymoo.operators.mutation.real_differential_evoluation_mutation import DifferentialEvolutionMutation | ||
from pymoo.operators.selection.random_selection import RandomSelection | ||
from pymoo.util.display import disp_single_objective | ||
|
||
|
||
class DifferentialEvolution(GeneticAlgorithm): | ||
def __init__(self, | ||
**kwargs): | ||
set_default_if_none("real", kwargs) | ||
super().__init__(**kwargs) | ||
self.selection = RandomSelection() | ||
self.crossover = DifferentialEvolutionCrossover(weight=0.75) | ||
self.mutation = DifferentialEvolutionMutation("binomial", 0.1) | ||
self.func_display_attrs = disp_single_objective | ||
|
||
def _next(self, pop): | ||
|
||
# create offsprings and add it to the data of the algorithm | ||
P = self.selection.do(pop, self.pop_size, self.crossover.n_parents) | ||
off = self.crossover.do(self.problem, pop, P) | ||
self.data = {**self.data, "off": off} | ||
|
||
# do the mutation by using the offsprings | ||
off = self.mutation.do(self.problem, pop, D=self.data) | ||
|
||
# evaluate the results | ||
self.evaluator.eval(self.problem, off, D=self.data) | ||
|
||
# replace whenever offspring is better than population member | ||
for i in range(len(pop)): | ||
if off[i].F < pop[i].F: | ||
pop[i] = off[i] | ||
|
||
return pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 6 additions & 53 deletions
59
pymoo/operators/crossover/real_differental_evolution_crossover.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,13 @@ | ||
import numpy as np | ||
|
||
from pymoo.model.crossover import Crossover | ||
from pymoo.rand import random | ||
|
||
|
||
class DifferentialEvolutionCrossover(Crossover): | ||
|
||
def __init__(self, variant="DE/rand/1", prob=0.5, weight=0.8, method="binomial", bounce_back_in_bounds=True): | ||
super().__init__(4, 1, True) | ||
self.prob = prob | ||
def __init__(self, weight=0.8): | ||
super().__init__(3, 1, True) | ||
self.weight = weight | ||
self.type = method | ||
self.variant = variant | ||
self.bounce_back_in_bounds = bounce_back_in_bounds | ||
|
||
def _do(self, p, parents, children, **kwargs): | ||
|
||
n_var = parents.shape[2] | ||
n_offsprings = parents.shape[0] | ||
|
||
# do the crossover | ||
if self.type == "binomial": | ||
|
||
# uniformly for each individual and each entry | ||
r = random.random(size=(n_offsprings, n_var)) < self.prob | ||
|
||
elif self.type == "exponential": | ||
|
||
r = np.full((n_offsprings, n_var), False) | ||
|
||
# start point of crossover | ||
n = random.randint(0, n_var, size=n_var) | ||
# length of chromosome to do the crossover | ||
L = np.argmax((random.random(n_offsprings) > self.prob), axis=1) | ||
|
||
# create for each individual the crossover range | ||
for i in range(n_offsprings): | ||
for l in range(L[i] + 1): | ||
r[i, (n[i] + l) % n_var] = True | ||
|
||
else: | ||
raise Exception("Unknown crossover type. Either binomial or exponential.") | ||
|
||
# the so called donor vector | ||
children[:, :] = parents[:, 0] | ||
|
||
if self.variant == "DE/rand/1": | ||
trial = parents[:, 1] + self.weight * (parents[:, 2] - parents[:, 3]) | ||
else: | ||
raise Exception("DE variant %s not known." % self.variant) | ||
|
||
# set only if larger than F_CR | ||
children[r] = trial[r] | ||
|
||
# bounce back into bounds | ||
if self.bounce_back_in_bounds: | ||
smaller_than_min, larger_than_max = p.xl > children, p.xu < children | ||
children[smaller_than_min] = (p.xl + (p.xl - children))[smaller_than_min] | ||
children[larger_than_max] = (p.xu - (children - p.xu))[larger_than_max] | ||
def _do(self, problem, pop, parents, **kwargs): | ||
X = pop.get("X")[parents.T] | ||
_X = X[0] + self.weight * (X[1] - X[2]) | ||
return pop.new("X", _X) |
28 changes: 28 additions & 0 deletions
28
pymoo/operators/mutation/real_differential_evoluation_mutation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from pymoo.model.mutation import Mutation | ||
from pymoo.rand import random | ||
|
||
|
||
class DifferentialEvolutionMutation(Mutation): | ||
def __init__(self, variant, CR): | ||
super().__init__(True) | ||
self.CR = CR | ||
self.variant = variant | ||
|
||
def _do(self, problem, pop, D=None, **kwargs): | ||
|
||
X = pop.get("X") | ||
|
||
off = D['off'] | ||
_X = off.get("X") | ||
|
||
# do the crossover | ||
if self.variant == "binomial": | ||
# uniformly for each individual and each entry | ||
r = random.random(size=(len(off), problem.n_var)) < self.CR | ||
|
||
X[r] = _X[r] | ||
return pop.new("X", X) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.