QDpy
Loading...
Searching...
No Matches
mix_me.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Search for cooperative three-role teams across two effort descriptors."""
6
7from argparse import ArgumentParser
8import json
9from time import perf_counter
10
11from qdpy.algorithms import MixME
12from qdpy.containers import Grid
13from qdpy.simple import run
14
15ROLES = {"guide": 2, "carrier": 3, "receiver": 1}
16
17
18def evaluate(team):
19 """Score alignment between three specialized roles and their neighbors."""
20 steer, guide_power = team["guide"]
21 load, balance, carrier_power = team["carrier"]
22 (gate,) = team["receiver"]
23 quality = (5 - (guide_power - 0.8) ** 2 - (carrier_power - 0.65) ** 2
24 - (balance - (steer + load) / 2) ** 2 - (gate - load) ** 2)
25 return (quality,), (steer, load)
26
27
28def comparison(budget, seed):
29 """Return the two archives' coverage, quality, and evaluation counts."""
30 for mixing in (True, False):
31 archive = Grid(shape=(8, 8), max_items_per_bin=1, fitness_domain=((0., 5.),),
32 features_domain=((0., 1.), (0., 1.)))
33 search = MixME(archive, budget, ROLES, team_crossover=mixing, optimisation_task="maximisation")
34 start = perf_counter()
35 result = run(search, lambda genome: evaluate(search.decode_team(genome)), seed=seed)
36 yield dict(method="mix-me" if mixing else "mutation-only", seed=seed, attempts=result.evaluations,
37 coverage=archive.filled_bins / 64, qd_score=archive.qd_score(normalized=False),
38 best_quality=archive.best_fitness.values[0], seconds=perf_counter() - start)
39
40
41if __name__ == "__main__":
42 parser = ArgumentParser(description=__doc__)
43 parser.add_argument("--budget", type=int, default=512)
44 parser.add_argument("--seeds", type=int, nargs="+", default=[11, 29, 47])
45 args = parser.parse_args()
46 for seed in args.seeds:
47 for row in comparison(args.budget, seed):
48 print(json.dumps(row, sort_keys=True, allow_nan=False))