Skip to content

Commit

Permalink
Merge pull request #30 from ELC/MinorChanges
Browse files Browse the repository at this point in the history
Minor changes - First 3 commits of #29 (@ELC contribution)
  • Loading branch information
pablormier committed Mar 14, 2018
2 parents 8d6d581 + 117b21b commit 97fa2c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
16 changes: 7 additions & 9 deletions yabox/algorithms/base.py
Expand Up @@ -10,23 +10,21 @@ def binomial_crossover(target, mutant, cr):
return np.where(p, mutant, target)


def random_sample(population, exclude, size):
def random_sample(population, exclude, size=3):
# Optimized version using numpy
idxs = [i for i in range(population.shape[0]) if i != exclude]
np.random.shuffle(idxs)
# Select the number of elements specified by size
sample = idxs[:size]
idxs = list(range(population.shape[0]))
idxs.remove(exclude)
sample = np.random.choice(idxs, size=size, replace=False)
return population[sample]


def rand1(target_idx, population, f):
sample = random_sample(population, target_idx, 3)
a, b, c = sample
a, b, c = random_sample(population, target_idx)
return a + f * (b - c)


def denormalize(min, diff, matrix):
return min + matrix * diff
def denormalize(min_, diff, matrix):
return min_ + matrix * diff


def random_repair(x):
Expand Down
22 changes: 11 additions & 11 deletions yabox/algorithms/de.py
Expand Up @@ -96,23 +96,23 @@ def __init__(self, fobj, bounds, mutation=(0.5, 1.0), crossover=0.7, maxiters=10
# Convert crossover param to an interval, as in mutation. If min/max values in the interval are
# different, a dither mechanism is used for crossover (although this is not recommended, but still supported)
# TODO: Clean duplicate code
if hasattr(crossover, '__len__'):
self.crossover_bounds = crossover
else:
self.crossover_bounds = (crossover, crossover)
if hasattr(mutation, '__len__'):
self.mutation_bounds = mutation
else:
self.mutation_bounds = (mutation, mutation)

self.crossover_bounds = crossover
self.mutation_bounds = mutation

if getattr(crossover, '__len__', None) is None:
self.crossover_bounds = [crossover, crossover]

if getattr(mutation, '__len__', None) is None:
self.mutation_bounds = [mutation, mutation]

# If self-adaptive, include mutation and crossover as two new variables
bnd = list(bounds)
if self_adaptive:
bnd.append(self.mutation_bounds)
bnd.append(self.crossover_bounds)
self.extra_params = 2
B = np.asarray(bnd, dtype='f8').T
self._MIN = B[0]
self._MAX = B[1]
self._MIN, self._MAX = np.asarray(bnd, dtype='f8').T
self._DIFF = np.fabs(self._MAX - self._MIN)
self.dims = len(bnd)
self.fobj = fobj
Expand Down

0 comments on commit 97fa2c1

Please sign in to comment.