mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 10:18:01 +00:00
Refactored GA runners into GA classes, configured tournaments for GA, working Ixalan plane
(cherry picked from commit d7d3ea3)
This commit is contained in:
@@ -1,53 +0,0 @@
|
|||||||
package forge.planarconquestgenerate;
|
|
||||||
|
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
|
||||||
import forge.GuiBase;
|
|
||||||
import forge.GuiDesktop;
|
|
||||||
import forge.deck.Deck;
|
|
||||||
import forge.deck.DeckFormat;
|
|
||||||
import forge.deck.io.DeckStorage;
|
|
||||||
import forge.game.GameFormat;
|
|
||||||
import forge.game.GameRules;
|
|
||||||
import forge.game.GameType;
|
|
||||||
import forge.model.FModel;
|
|
||||||
import forge.properties.ForgeConstants;
|
|
||||||
import forge.properties.ForgePreferences;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public class PlanarConquestGeneratorGAStandardTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test(){
|
|
||||||
|
|
||||||
GuiBase.setInterface(new GuiDesktop());
|
|
||||||
FModel.initialize(null, new Function<ForgePreferences, Void>() {
|
|
||||||
@Override
|
|
||||||
public Void apply(ForgePreferences preferences) {
|
|
||||||
preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
PlanarConquestGeneraterGA ga = new PlanarConquestGeneraterGAStandard(new GameRules(GameType.Constructed),
|
|
||||||
FModel.getFormats().getStandard(),
|
|
||||||
DeckFormat.Constructed,
|
|
||||||
40,
|
|
||||||
4,
|
|
||||||
10);
|
|
||||||
ga.run();
|
|
||||||
List<Deck> winners = ga.listFinalPopulation();
|
|
||||||
|
|
||||||
DeckStorage storage = new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR);
|
|
||||||
|
|
||||||
for(Deck deck:winners){
|
|
||||||
storage.save(deck);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package forge.planarconquestgenerate;
|
|
||||||
|
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import forge.GuiBase;
|
|
||||||
import forge.GuiDesktop;
|
|
||||||
import forge.StaticData;
|
|
||||||
import forge.card.CardRulesPredicates;
|
|
||||||
import forge.deck.CardRelationMatrixGenerator;
|
|
||||||
import forge.deck.Deck;
|
|
||||||
import forge.deck.DeckFormat;
|
|
||||||
import forge.deck.io.DeckStorage;
|
|
||||||
import forge.game.GameFormat;
|
|
||||||
import forge.game.GameRules;
|
|
||||||
import forge.game.GameType;
|
|
||||||
import forge.item.PaperCard;
|
|
||||||
import forge.limited.CardRanker;
|
|
||||||
import forge.model.FModel;
|
|
||||||
import forge.properties.ForgeConstants;
|
|
||||||
import forge.properties.ForgePreferences;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public class PlanarConquestGeneratorGATest {
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test(){
|
|
||||||
|
|
||||||
GuiBase.setInterface(new GuiDesktop());
|
|
||||||
FModel.initialize(null, new Function<ForgePreferences, Void>() {
|
|
||||||
@Override
|
|
||||||
public Void apply(ForgePreferences preferences) {
|
|
||||||
preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
List<String> sets = new ArrayList<>();
|
|
||||||
sets.add("XLN");
|
|
||||||
sets.add("RIX");
|
|
||||||
|
|
||||||
PlanarConquestGeneraterGA ga = new PlanarConquestGeneraterGA(new GameRules(GameType.Constructed),
|
|
||||||
new GameFormat("conquest",sets,null),
|
|
||||||
DeckFormat.PlanarConquest,
|
|
||||||
10,
|
|
||||||
2,
|
|
||||||
5);
|
|
||||||
ga.run();
|
|
||||||
List<Deck> winners = ga.listFinalPopulation();
|
|
||||||
|
|
||||||
DeckStorage storage = new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR);
|
|
||||||
|
|
||||||
for(Deck deck:winners){
|
|
||||||
storage.save(deck);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,365 +0,0 @@
|
|||||||
package forge.planarconquestgenerate;
|
|
||||||
import com.google.common.base.Function;
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import forge.GuiBase;
|
|
||||||
import forge.GuiDesktop;
|
|
||||||
import forge.LobbyPlayer;
|
|
||||||
import forge.StaticData;
|
|
||||||
import forge.card.CardRulesPredicates;
|
|
||||||
import forge.deck.*;
|
|
||||||
import forge.deck.io.DeckStorage;
|
|
||||||
import forge.game.GameFormat;
|
|
||||||
import forge.game.GameRules;
|
|
||||||
import forge.game.GameType;
|
|
||||||
import forge.game.Match;
|
|
||||||
import forge.game.player.RegisteredPlayer;
|
|
||||||
import forge.item.PaperCard;
|
|
||||||
import forge.limited.CardRanker;
|
|
||||||
import forge.model.FModel;
|
|
||||||
import forge.player.GamePlayerUtil;
|
|
||||||
import forge.properties.ForgeConstants;
|
|
||||||
import forge.properties.ForgePreferences;
|
|
||||||
|
|
||||||
import forge.tournament.system.AbstractTournament;
|
|
||||||
import forge.tournament.system.TournamentPairing;
|
|
||||||
import forge.tournament.system.TournamentPlayer;
|
|
||||||
import forge.tournament.system.TournamentSwiss;
|
|
||||||
import forge.util.TextUtil;
|
|
||||||
import forge.view.SimulateMatch;
|
|
||||||
import org.apache.commons.lang3.text.WordUtils;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public class PlanarConquestGeneratorTest {
|
|
||||||
|
|
||||||
Map<String,List<Map.Entry<PaperCard,Integer>>> standardMap;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void generatePlanarConquestDecks() {
|
|
||||||
|
|
||||||
GuiBase.setInterface(new GuiDesktop());
|
|
||||||
FModel.initialize(null, new Function<ForgePreferences, Void>() {
|
|
||||||
@Override
|
|
||||||
public Void apply(ForgePreferences preferences) {
|
|
||||||
preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
GameFormat format = FModel.getFormats().getStandard();
|
|
||||||
GameRules rules = new GameRules(GameType.Constructed);
|
|
||||||
standardMap = CardRelationMatrixGenerator.cardPools.get(format.getName());
|
|
||||||
List<String> cardNames = new ArrayList<>(standardMap.keySet());
|
|
||||||
List<PaperCard> cards = new ArrayList<>();
|
|
||||||
for(String cardName:cardNames){
|
|
||||||
cards.add(StaticData.instance().getCommonCards().getUniqueByName(cardName));
|
|
||||||
}
|
|
||||||
List<PaperCard> rankedList = CardRanker.rankCardsInDeck(cards);
|
|
||||||
List<String> sets = new ArrayList<>();
|
|
||||||
sets.add("XLN");
|
|
||||||
sets.add("RIX");
|
|
||||||
DeckStorage storage = new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR);
|
|
||||||
GameFormat planarConquestFormat = new GameFormat("conquest",sets,null);
|
|
||||||
DeckFormat deckFormat = DeckFormat.PlanarConquest;
|
|
||||||
|
|
||||||
Iterable<PaperCard> filtered= Iterables.filter(rankedList, Predicates.and(
|
|
||||||
Predicates.compose(CardRulesPredicates.IS_KEPT_IN_AI_DECKS, PaperCard.FN_GET_RULES),
|
|
||||||
Predicates.compose(CardRulesPredicates.Presets.IS_NON_LAND, PaperCard.FN_GET_RULES),
|
|
||||||
planarConquestFormat.getFilterPrinted()));
|
|
||||||
|
|
||||||
List<PaperCard> filteredList = Lists.newArrayList(filtered);
|
|
||||||
|
|
||||||
for(PaperCard card: filteredList.subList(0,50)){
|
|
||||||
if(planarConquestFormat.getFilterPrinted().apply(card)) {
|
|
||||||
System.out.println(card.getName());
|
|
||||||
DeckGroup deckGroup = new DeckGroup("SimulatedTournament");
|
|
||||||
List<TournamentPlayer> players = new ArrayList<>();
|
|
||||||
int numPlayers=0;
|
|
||||||
for( int i=0; i<32;i++){
|
|
||||||
Deck genDeck = DeckgenUtil.buildPlanarConquestDeck(card, planarConquestFormat, deckFormat);
|
|
||||||
Deck d = new Deck(genDeck,genDeck.getName()+"_"+i);
|
|
||||||
deckGroup.addAiDeck(d);
|
|
||||||
players.add(new TournamentPlayer(GamePlayerUtil.createAiPlayer(d.getName(), 0), numPlayers));
|
|
||||||
numPlayers++;
|
|
||||||
}
|
|
||||||
TournamentSwiss tourney = new TournamentSwiss(players, 2);
|
|
||||||
|
|
||||||
tourney = runTournament(tourney, rules, numPlayers, deckGroup);
|
|
||||||
String deckName = tourney.getAllPlayers().get(0).getPlayer().getName();
|
|
||||||
Deck winningDeck;
|
|
||||||
for(Deck deck:deckGroup.getAiDecks()){
|
|
||||||
if(deck.getName().equals(deckName)){
|
|
||||||
winningDeck=deck;
|
|
||||||
storage.save(winningDeck);
|
|
||||||
System.out.println(card.toString());
|
|
||||||
System.out.println(winningDeck.getName());
|
|
||||||
System.out.println(winningDeck.getAllCardsInASinglePool().toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void generateBestDecksGA() {
|
|
||||||
|
|
||||||
GuiBase.setInterface(new GuiDesktop());
|
|
||||||
FModel.initialize(null, new Function<ForgePreferences, Void>() {
|
|
||||||
@Override
|
|
||||||
public Void apply(ForgePreferences preferences) {
|
|
||||||
preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
int cardsToUse=40;
|
|
||||||
GameFormat format = FModel.getFormats().getStandard();
|
|
||||||
GameRules rules = new GameRules(GameType.Constructed);
|
|
||||||
standardMap = CardRelationMatrixGenerator.cardPools.get(format.getName());
|
|
||||||
List<String> cardNames = new ArrayList<>(standardMap.keySet());
|
|
||||||
List<PaperCard> cards = new ArrayList<>();
|
|
||||||
for(String cardName:cardNames){
|
|
||||||
cards.add(StaticData.instance().getCommonCards().getUniqueByName(cardName));
|
|
||||||
}
|
|
||||||
List<PaperCard> rankedList = CardRanker.rankCardsInDeck(cards);
|
|
||||||
List<String> sets = new ArrayList<>();
|
|
||||||
sets.add("XLN");
|
|
||||||
sets.add("RIX");
|
|
||||||
DeckStorage storage = new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR);
|
|
||||||
GameFormat planarConquestFormat = new GameFormat("conquest",sets,null);
|
|
||||||
DeckFormat deckFormat = DeckFormat.PlanarConquest;
|
|
||||||
|
|
||||||
Iterable<PaperCard> filtered= Iterables.filter(rankedList, Predicates.and(
|
|
||||||
Predicates.compose(CardRulesPredicates.IS_KEPT_IN_AI_DECKS, PaperCard.FN_GET_RULES),
|
|
||||||
Predicates.compose(CardRulesPredicates.Presets.IS_NON_LAND, PaperCard.FN_GET_RULES),
|
|
||||||
planarConquestFormat.getFilterPrinted()));
|
|
||||||
|
|
||||||
List<PaperCard> filteredList = Lists.newArrayList(filtered);
|
|
||||||
|
|
||||||
DeckGroup deckGroup = new DeckGroup("SimulatedTournament");
|
|
||||||
List<TournamentPlayer> players = new ArrayList<>();
|
|
||||||
int numPlayers=0;
|
|
||||||
for(PaperCard card: filteredList.subList(0,cardsToUse)){
|
|
||||||
System.out.println(card.getName());
|
|
||||||
|
|
||||||
|
|
||||||
for( int i=0; i<4;i++){
|
|
||||||
Deck genDeck = DeckgenUtil.buildPlanarConquestDeck(card, planarConquestFormat, deckFormat);
|
|
||||||
Deck d = new Deck(genDeck,genDeck.getName()+"_"+i+"_"+0);
|
|
||||||
deckGroup.addAiDeck(d);
|
|
||||||
players.add(new TournamentPlayer(GamePlayerUtil.createAiPlayer(d.getName(), 0), numPlayers));
|
|
||||||
numPlayers++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TournamentSwiss tourney = null;
|
|
||||||
for (int m=1;m<10;++m) {
|
|
||||||
|
|
||||||
//get best decks in population
|
|
||||||
tourney = new TournamentSwiss(players, 2);
|
|
||||||
tourney = runTournament(tourney, rules, numPlayers, deckGroup);
|
|
||||||
players = new ArrayList<>();
|
|
||||||
DeckGroup newDeckGroup = new DeckGroup("SimulatedTournament"+m);
|
|
||||||
numPlayers=0;
|
|
||||||
int winnerCount = new Float(tourney.getAllPlayers().size()* .5f).intValue();
|
|
||||||
for (int k = 0; k < winnerCount; k++) {
|
|
||||||
String deckName = tourney.getAllPlayers().get(k).getPlayer().getName();
|
|
||||||
for (Deck winningDeck : deckGroup.getAiDecks()) {
|
|
||||||
if (winningDeck.getName().equals(deckName)) {
|
|
||||||
newDeckGroup.addAiDeck(winningDeck);
|
|
||||||
players.add(new TournamentPlayer(GamePlayerUtil.createAiPlayer(winningDeck.getName(), 0), numPlayers));
|
|
||||||
numPlayers++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
deckGroup = newDeckGroup;
|
|
||||||
|
|
||||||
//add random decks
|
|
||||||
for(PaperCard card: filteredList.subList(0,cardsToUse)){
|
|
||||||
System.out.println(card.getName());
|
|
||||||
for( int i=0; i<2;i++){
|
|
||||||
Deck genDeck = DeckgenUtil.buildPlanarConquestDeck(card, planarConquestFormat, deckFormat);
|
|
||||||
Deck d = new Deck(genDeck,genDeck.getName()+"_"+i+"_"+m);
|
|
||||||
deckGroup.addAiDeck(d);
|
|
||||||
players.add(new TournamentPlayer(GamePlayerUtil.createAiPlayer(d.getName(), 0), numPlayers));
|
|
||||||
numPlayers++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tourney = new TournamentSwiss(players, 2);
|
|
||||||
tourney = runTournament(tourney, rules, numPlayers, deckGroup);
|
|
||||||
|
|
||||||
int winnerCount = new Float(tourney.getAllPlayers().size()* 0.25f).intValue();
|
|
||||||
for (int k = 0; k < winnerCount; k++) {
|
|
||||||
String deckName = tourney.getAllPlayers().get(k).getPlayer().getName();
|
|
||||||
Deck winningDeck;
|
|
||||||
for(Deck deck:deckGroup.getAiDecks()){
|
|
||||||
if(deck.getName().equals(deckName)){
|
|
||||||
winningDeck=deck;
|
|
||||||
storage.save(winningDeck);
|
|
||||||
System.out.println(winningDeck.getName());
|
|
||||||
System.out.println(winningDeck.getAllCardsInASinglePool().toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void generatePlanarConquestCommanderDecks() {
|
|
||||||
|
|
||||||
GuiBase.setInterface(new GuiDesktop());
|
|
||||||
FModel.initialize(null, new Function<ForgePreferences, Void>() {
|
|
||||||
@Override
|
|
||||||
public Void apply(ForgePreferences preferences) {
|
|
||||||
preferences.setPref(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
GameFormat format = FModel.getFormats().getStandard();
|
|
||||||
GameRules rules = new GameRules(GameType.Constructed);
|
|
||||||
standardMap = CardRelationMatrixGenerator.cardPools.get(format.getName());
|
|
||||||
List<String> cardNames = new ArrayList<>(standardMap.keySet());
|
|
||||||
List<PaperCard> cards = new ArrayList<>();
|
|
||||||
for(String cardName:cardNames){
|
|
||||||
cards.add(StaticData.instance().getCommonCards().getUniqueByName(cardName));
|
|
||||||
}
|
|
||||||
List<PaperCard> rankedList = CardRanker.rankCardsInDeck(cards);
|
|
||||||
List<String> sets = new ArrayList<>();
|
|
||||||
sets.add("XLN");
|
|
||||||
sets.add("RIX");
|
|
||||||
DeckStorage storage = new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR);
|
|
||||||
GameFormat planarConquestFormat = new GameFormat("conquest",sets,null);
|
|
||||||
DeckFormat deckFormat = DeckFormat.PlanarConquest;
|
|
||||||
|
|
||||||
Iterable<PaperCard> filtered= Iterables.filter(rankedList, Predicates.and(
|
|
||||||
Predicates.compose(CardRulesPredicates.IS_KEPT_IN_AI_DECKS, PaperCard.FN_GET_RULES),
|
|
||||||
Predicates.compose(CardRulesPredicates.Presets.IS_CREATURE, PaperCard.FN_GET_RULES),
|
|
||||||
Predicates.compose(CardRulesPredicates.Presets.IS_LEGENDARY, PaperCard.FN_GET_RULES),
|
|
||||||
planarConquestFormat.getFilterPrinted()));
|
|
||||||
|
|
||||||
List<PaperCard> filteredList = Lists.newArrayList(filtered);
|
|
||||||
|
|
||||||
int maxListSize = 50;
|
|
||||||
if (maxListSize > filteredList.size()){
|
|
||||||
maxListSize = filteredList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
for(PaperCard card: filteredList.subList(0,maxListSize)){
|
|
||||||
if(planarConquestFormat.getFilterPrinted().apply(card)) {
|
|
||||||
System.out.println(card.getName());
|
|
||||||
DeckGroup deckGroup = new DeckGroup("SimulatedTournament");
|
|
||||||
List<TournamentPlayer> players = new ArrayList<>();
|
|
||||||
int numPlayers=0;
|
|
||||||
for( int i=0; i<16;i++){
|
|
||||||
Deck genDeck = DeckgenUtil.buildPlanarConquestCommanderDeck(card, planarConquestFormat, deckFormat);
|
|
||||||
Deck d = new Deck(genDeck,genDeck.getName()+"_"+i);
|
|
||||||
deckGroup.addAiDeck(d);
|
|
||||||
players.add(new TournamentPlayer(GamePlayerUtil.createAiPlayer(d.getName(), 0), numPlayers));
|
|
||||||
numPlayers++;
|
|
||||||
}
|
|
||||||
TournamentSwiss tourney = new TournamentSwiss(players, 2);
|
|
||||||
|
|
||||||
tourney = runTournament(tourney, rules, numPlayers, deckGroup);
|
|
||||||
String deckName = tourney.getAllPlayers().get(0).getPlayer().getName();
|
|
||||||
Deck winningDeck;
|
|
||||||
for(Deck deck:deckGroup.getAiDecks()){
|
|
||||||
if(deck.getName().equals(deckName)){
|
|
||||||
winningDeck=deck;
|
|
||||||
storage.save(winningDeck);
|
|
||||||
System.out.println(card.toString());
|
|
||||||
System.out.println(winningDeck.getName());
|
|
||||||
System.out.println(winningDeck.getAllCardsInASinglePool().toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TournamentSwiss runTournament(TournamentSwiss tourney, GameRules rules, int numPlayers, DeckGroup deckGroup){
|
|
||||||
tourney.initializeTournament();
|
|
||||||
|
|
||||||
String lastWinner = "";
|
|
||||||
int curRound = 0;
|
|
||||||
System.out.println(TextUtil.concatNoSpace("Starting a tournament with ",
|
|
||||||
String.valueOf(numPlayers), " players over ",
|
|
||||||
String.valueOf(tourney.getTotalRounds()), " rounds"));
|
|
||||||
while(!tourney.isTournamentOver()) {
|
|
||||||
if (tourney.getActiveRound() != curRound) {
|
|
||||||
if (curRound != 0) {
|
|
||||||
System.out.println(TextUtil.concatNoSpace("End Round - ", String.valueOf(curRound)));
|
|
||||||
}
|
|
||||||
curRound = tourney.getActiveRound();
|
|
||||||
System.out.println("");
|
|
||||||
System.out.println(TextUtil.concatNoSpace("Round ", String.valueOf(curRound) ," Pairings:"));
|
|
||||||
|
|
||||||
for(TournamentPairing pairing : tourney.getActivePairings()) {
|
|
||||||
System.out.println(pairing.outputHeader());
|
|
||||||
}
|
|
||||||
System.out.println("");
|
|
||||||
}
|
|
||||||
|
|
||||||
TournamentPairing pairing = tourney.getNextPairing();
|
|
||||||
List<RegisteredPlayer> regPlayers = AbstractTournament.registerTournamentPlayers(pairing, deckGroup);
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("Round ").append(tourney.getActiveRound()).append(" - ");
|
|
||||||
sb.append(pairing.outputHeader());
|
|
||||||
System.out.println(sb.toString());
|
|
||||||
|
|
||||||
if (!pairing.isBye()) {
|
|
||||||
Match mc = new Match(rules, regPlayers, "TourneyMatch");
|
|
||||||
|
|
||||||
int exceptions = 0;
|
|
||||||
int iGame = 0;
|
|
||||||
while (!mc.isMatchOver()) {
|
|
||||||
// play games until the match ends
|
|
||||||
try{
|
|
||||||
SimulateMatch.simulateSingleMatch(mc, iGame, false);
|
|
||||||
iGame++;
|
|
||||||
} catch(Exception e) {
|
|
||||||
exceptions++;
|
|
||||||
System.out.println(e.toString());
|
|
||||||
if (exceptions > 5) {
|
|
||||||
System.out.println("Exceeded number of exceptions thrown. Abandoning match...");
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
System.out.println("Game threw exception. Abandoning game and continuing...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
LobbyPlayer winner = mc.getWinner().getPlayer();
|
|
||||||
for (TournamentPlayer tp : pairing.getPairedPlayers()) {
|
|
||||||
if (winner.equals(tp.getPlayer())) {
|
|
||||||
pairing.setWinner(tp);
|
|
||||||
lastWinner = winner.getName();
|
|
||||||
System.out.println(TextUtil.concatNoSpace("Match Winner - ", lastWinner, "!"));
|
|
||||||
System.out.println("");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tourney.reportMatchCompletion(pairing);
|
|
||||||
}
|
|
||||||
tourney.outputTournamentResults();
|
|
||||||
return tourney;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,7 @@ public class TournamentSwiss extends AbstractTournament {
|
|||||||
|
|
||||||
public TournamentSwiss(List<TournamentPlayer> allPlayers, int pairingAmount) {
|
public TournamentSwiss(List<TournamentPlayer> allPlayers, int pairingAmount) {
|
||||||
// Technically not 100% correct for huge tournaments, but yknow.. close enough.
|
// Technically not 100% correct for huge tournaments, but yknow.. close enough.
|
||||||
super((int)Math.ceil(Math.log(allPlayers.size())/Math.log(2)), allPlayers);
|
super((int)Math.ceil(Math.log(allPlayers.size())/Math.log(2))*2, allPlayers);
|
||||||
this.playersInPairing = pairingAmount;
|
this.playersInPairing = pairingAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ public abstract class AbstractGeneticAlgorithm<T> {
|
|||||||
|
|
||||||
protected abstract void evaluateFitness();
|
protected abstract void evaluateFitness();
|
||||||
|
|
||||||
|
protected abstract T expandPool();
|
||||||
|
|
||||||
public void pruneWeakest(){
|
public void pruneWeakest(){
|
||||||
population = population.subList(0, new Float(population.size()*pruneRatio).intValue());
|
population = population.subList(0, new Float(population.size()*pruneRatio).intValue());
|
||||||
}
|
}
|
||||||
@@ -23,15 +25,24 @@ public abstract class AbstractGeneticAlgorithm<T> {
|
|||||||
int prunedSize = population.size();
|
int prunedSize = population.size();
|
||||||
while(population.size()<targetPopulationSize){
|
while(population.size()<targetPopulationSize){
|
||||||
int randomIndex = new Double(prunedSize*Math.pow(MyRandom.getRandom().nextDouble(), 0.25)/2d).intValue();
|
int randomIndex = new Double(prunedSize*Math.pow(MyRandom.getRandom().nextDouble(), 0.25)/2d).intValue();
|
||||||
if(MyRandom.getRandom().nextBoolean()){
|
float rand = MyRandom.getRandom().nextFloat();
|
||||||
population.add(mutateObject(population.get(randomIndex)));
|
if(rand>0.85f){
|
||||||
}else{
|
T child = mutateObject(population.get(randomIndex));
|
||||||
|
if(child != null) {
|
||||||
|
population.add(child);
|
||||||
|
}
|
||||||
|
}else if(rand>0.70f){
|
||||||
int secondIndex = randomIndex;
|
int secondIndex = randomIndex;
|
||||||
while(secondIndex != randomIndex){
|
while(secondIndex != randomIndex){
|
||||||
secondIndex = new Double(prunedSize*Math.pow(MyRandom.getRandom().nextDouble(), 0.25)/2d).intValue();
|
secondIndex = new Double(prunedSize*Math.pow(MyRandom.getRandom().nextDouble(), 0.25)/2d).intValue();
|
||||||
}
|
}
|
||||||
population.add(createChild(population.get(randomIndex)
|
T child = createChild(population.get(randomIndex)
|
||||||
, population.get(secondIndex)));
|
, population.get(secondIndex));
|
||||||
|
if(child != null) {
|
||||||
|
population.add(child);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
population.add(expandPool());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user