Skip to content

Commit

Permalink
Moved hv.py to pyhv and made the import transparent in indicator.
Browse files Browse the repository at this point in the history
Added debug info in cma_mo

--HG--
branch : dev
rename : deap/tools/hv.py => deap/tools/pyhv.py
  • Loading branch information
fmder committed Apr 29, 2014
1 parent 538ef2c commit 5d93e79
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 69 deletions.
9 changes: 7 additions & 2 deletions deap/tools/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

import numpy

import deap.tools.hv
try:
# try importing the C version
from . import hv as hv
except ImportError:
# fallback on python version
from . import pyhv as hv

def hypervolume(front, ref=None):
"""Returns the index of the individual with the least the hypervolume
Expand All @@ -29,7 +34,7 @@ def hypervolume(front, ref=None):
ref = numpy.max(wobj, axis=0) + 1

def contribution(i):
return deap.tools.hv.hypervolume(numpy.concatenate((wobj[:i], wobj[i+1:])), ref)
return hv.hypervolume(numpy.concatenate((wobj[:i], wobj[i+1:])), ref)

# TODO: Parallelize this?
contrib_value = map(contribution, range(len(front)))
Expand Down
56 changes: 5 additions & 51 deletions deap/tools/hv.py → deap/tools/pyhv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,13 @@
__author__ = "Simon Wessing"
# Wrapped by Francois-Michel De Rainville

def hypervolume_kmax(front, k, ref=None):
"""Find the *k* individuals index contributing the most to
the hypervolume among *front*.
"""
if k >= len(front):
return range(len(front))

# Must use wvalues * -1 since _HyperVolume use implicit minimization
wobj = numpy.array([ind.fitness.wvalues for ind in front]) * -1
if ref is None:
ref = numpy.max(wobj, axis=0) + 1

# print "ref", ref

hv = _HyperVolume(ref)
indices = numpy.arange(0, len(front))
contrib = numpy.zeros(len(front))

# import matplotlib.pyplot as plt
# plt.scatter(wobj[:, 0], wobj[:, 1])
# plt.show()

for i in range(len(front) - k):
for j in indices:
indices_j = indices[numpy.where(indices != j)]
s_a_j = hv.compute(wobj[indices_j])
contrib[j] = s_a_j

# Select randomly from equaly contributing
## Retreive the indices
least_contributers = numpy.flatnonzero(numpy.isclose(contrib, contrib.max()))
idx = numpy.random.choice(least_contributers)
indices = indices[numpy.where(indices != idx)]
contrib[idx] = 0
# print "====="
# print s_a
# print contrib
# print indices, idx, contrib.min()

# plt.scatter(wobj[:, 0], wobj[:, 1], c="r")
# plt.scatter(wobj[indices, 0], wobj[indices, 1], c="b")
# plt.show()
return indices

def hypervolume(population, ref=None):
"""Compute the absolute hypervolume of a *population*."""
# Must use (wvalues * -1) since _HyperVolume use implicit minimization
wobj = numpy.array([-ind.fitness.wvalues for ind in population]) * -1
if ref is None:
ref = numpy.max(wobj, axis=0) + 1

def hypervolume(pointset, ref):
"""Compute the absolute hypervolume of a *pointset* according to the
reference point *ref*.
"""
hv = _HyperVolume(ref)
return hv.compute(wobj)
return hv.compute(pointset)


class _HyperVolume:
Expand Down
40 changes: 24 additions & 16 deletions examples/es/cma_mo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from deap import creator
from deap import tools

import cma_mo_debug

# Problem size
N = 30

Expand Down Expand Up @@ -65,14 +67,16 @@ def main():
distances = list()

MU, LAMBDA = 100, 100
NGEN = 500
NGEN = 1
verbose = False

# The MO-CMA-ES algorithm takes a full population as argument
population = [creator.Individual(x) for x in (numpy.random.uniform(0, 1, (MU + LAMBDA, N)))]
# population = [creator.Individual(x) for x in (numpy.random.uniform(0, 1, (MU + LAMBDA, N)))]
# init = numpy.zeros((MU, N))
# init[:, 0] = numpy.linspace(0, 1, 100)
# population = [creator.Individual(x) for x in init]
population = [creator.Individual(z) for z in cma_mo_debug.parents]

for ind in population:
ind.fitness.values = toolbox.evaluate(ind)

Expand All @@ -95,7 +99,11 @@ def main():

for gen in range(NGEN):
# Generate a new population
population = toolbox.generate()
# population = toolbox.generate()
population = [creator.Individual(z) for z in cma_mo_debug.offspring]
for i, ind in enumerate(population):
ind._ps = "o", i

# Evaluate the individuals
fitnesses = toolbox.map(toolbox.evaluate, population)
for ind, fit in zip(population, fitnesses):
Expand Down Expand Up @@ -127,24 +135,24 @@ def main():

print("success", strategy.success_count)

import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D
# import matplotlib.pyplot as plt
# # from mpl_toolkits.mplot3d import Axes3D

valid_front = numpy.array([ind.fitness.values for ind in population if valid(ind)])
invalid_front = numpy.array([ind.fitness.values for ind in population if not valid(ind)])
# valid_front = numpy.array([ind.fitness.values for ind in population if valid(ind)])
# invalid_front = numpy.array([ind.fitness.values for ind in population if not valid(ind)])

fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# ax.scatter(front[:,0], front[:,1], front[:,2], c="b")
# fig = plt.figure()
# # ax = fig.add_subplot(111, projection='3d')
# # ax.scatter(front[:,0], front[:,1], front[:,2], c="b")

plt.scatter(valid_front[:,0], valid_front[:,1], c="g")
plt.scatter(invalid_front[:,0], invalid_front[:,1], c="r")
# plt.scatter(valid_front[:,0], valid_front[:,1], c="g")
# plt.scatter(invalid_front[:,0], invalid_front[:,1], c="r")

plt.figure()
sigmas = numpy.mean(sigmas, axis=0)
plt.plot(sigmas)
# plt.figure()
# sigmas = numpy.mean(sigmas, axis=0)
# plt.plot(sigmas)

plt.show()
# plt.show()

return halloffame

Expand Down

0 comments on commit 5d93e79

Please sign in to comment.