QDpy
Loading...
Searching...
No Matches
spherical_archive.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Write a spherical archive figure from a fixed set of unit directions."""
6
7import argparse
8from pathlib import Path
9
10import matplotlib.pyplot as plt
11import numpy as np
12
13from qdpy.containers import CVTGrid
14from qdpy.phenotype import Features, Fitness, Individual
15from qdpy.plots import plot_archive
16
17
18def example_archive():
19 """Use 64 Fibonacci directions as the stored CVT geometry."""
20 steps = np.arange(64)
21 z = 1 - (2 * steps + 1) / len(steps)
22 phi = np.pi * (3 - np.sqrt(5)) * steps
23 radius = np.sqrt(1 - z ** 2)
24 directions = np.column_stack((radius * np.cos(phi), radius * np.sin(phi), z))
25 archive = CVTGrid(shape=len(directions), centroids=directions,
26 features_domain=((-1, 1),) * 3, fitness_domain=((0, 1),))
27 for cell, direction in enumerate(directions):
28 if cell % 7:
29 value = (1 + direction @ np.array([0.4, 0.5, 0.7])) / 2
30 archive.add(Individual([cell], fitness=Fitness((value,)), features=Features(direction)))
31 return archive
32
33
34if __name__ == "__main__":
35 parser = argparse.ArgumentParser(description=__doc__)
36 parser.add_argument("output", type=Path)
37 args = parser.parse_args()
38 axes = plot_archive(example_archive(), view="sphere")
39 args.output.parent.mkdir(parents=True, exist_ok=True)
40 axes.figure.savefig(args.output, dpi=140)
41 plt.close(axes.figure)