mtg-genetic-deckbuilding

Generating and improving Magic: The Gathering decks using a genetic algorithm
git clone https://kevincorvisier.fr/git/mtg-genetic-deckbuilding.git
Log | Files | Refs | LICENSE

commit a112a9cec219ecddb31bad9a581001543369294b
parent aff54ec2c38cb7c73d3ba550a87eb9211cdb96d0
Author: Kevin Corvisier <git@kevincorvisier.fr>
Date:   Thu,  9 Jan 2025 18:44:40 +0900

Do not validate elite individuals for whether they were already
generated, fix new generation never containing elite individuals
Diffstat:
Msrc/main/java/fr/kevincorvisier/mtg/gdb/population/NextGenerationService.java | 18++++++++++++------
Msrc/main/java/fr/kevincorvisier/mtg/gdb/population/PopulationFactory.java | 4++--
2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/main/java/fr/kevincorvisier/mtg/gdb/population/NextGenerationService.java b/src/main/java/fr/kevincorvisier/mtg/gdb/population/NextGenerationService.java @@ -72,10 +72,16 @@ public class NextGenerationService for (int i = 0; i != eliteCount; i++) { - final Individual child = population.get(i); + final Individual elite = population.get(i); - if (validate(child, next)) - next.addIndividual(child); + final String conformanceProblem = validationService.validate(elite); + if (conformanceProblem != null) + { + log.warn("Ignoring elite {}: {}", elite.getName(), conformanceProblem); + continue; + } + + next.addIndividual(elite); } } @@ -89,7 +95,7 @@ public class NextGenerationService final Deck deck = deckGenerator.getRandomDeck(); final Individual child = new Individual(individualNameGenerator.createName() + "_new", deck); - if (validate(child, next)) + if (validateNewChild(child, next)) next.addIndividual(child); } } @@ -127,13 +133,13 @@ public class NextGenerationService if (result.size() >= maximumSize) continue; // Ensure second child will not breach the population limit - if (validate(individual, next)) + if (validateNewChild(individual, next)) next.addIndividual(individual); } } } - /* package */ boolean validate(final Individual child, final Population population) + /* package */ boolean validateNewChild(final Individual child, final Population population) { if (consecutiveFailures == maximumSize * 100) { diff --git a/src/main/java/fr/kevincorvisier/mtg/gdb/population/PopulationFactory.java b/src/main/java/fr/kevincorvisier/mtg/gdb/population/PopulationFactory.java @@ -61,7 +61,7 @@ public class PopulationFactory for (final Deck deck : forgeUtils.loadDecks(dir)) { final Individual individual = new Individual(deck.getName(), deck); - if (nextGenerationService.validate(individual, population)) + if (nextGenerationService.validateNewChild(individual, population)) population.addIndividual(individual); else log.warn("Deck from initial population failed validation: ", deck.getName(), deck); @@ -78,7 +78,7 @@ public class PopulationFactory while (population.getSize() < maximumSize) { final Individual individual = new Individual("g0_" + population.getSize(), deckGenerator.getRandomDeck()); - if (nextGenerationService.validate(individual, population)) + if (nextGenerationService.validateNewChild(individual, population)) population.addIndividual(individual); }