Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plotting in nsga2 example #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions examples/ga/nsga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from deap import creator
from deap import tools

from pathlib import Path

creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)

Expand Down Expand Up @@ -121,24 +123,25 @@ def main(seed=None):
return pop, logbook

if __name__ == "__main__":
# with open("pareto_front/zdt1_front.json") as optimal_front_data:
# optimal_front = json.load(optimal_front_data)
path = Path(__file__).parent / "pareto_front/zdt1_front.json"
with path.open() as optimal_front_data:
optimal_front = json.load(optimal_front_data)
# Use 500 of the 1000 points in the json file
# optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2))
optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2))

pop, stats = main()
# pop.sort(key=lambda x: x.fitness.values)
pop.sort(key=lambda x: x.fitness.values)

# print(stats)
# print("Convergence: ", convergence(pop, optimal_front))
# print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1]))
print(stats)
print("Convergence: ", convergence(pop, optimal_front))
print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1]))

# import matplotlib.pyplot as plt
# import numpy
import matplotlib.pyplot as plt
import numpy

# front = numpy.array([ind.fitness.values for ind in pop])
# optimal_front = numpy.array(optimal_front)
# plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r")
# plt.scatter(front[:,0], front[:,1], c="b")
# plt.axis("tight")
# plt.show()
front = numpy.array([ind.fitness.values for ind in pop])
optimal_front = numpy.array(optimal_front)
plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r")
plt.scatter(front[:,0], front[:,1], c="b")
plt.axis("tight")
plt.show()