Skip to content

Commit

Permalink
Merge branch 'features/CHT-298'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoria committed Sep 25, 2024
2 parents fc588bd + 099516a commit 4fac476
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.jenetics.engine.EvolutionResult;
import io.jenetics.engine.EvolutionStart;
import net.chesstango.tools.tuning.factories.GameEvaluatorFactory;
import net.chesstango.tools.tuning.fitnessfunctions.FitnessByEpdBottomMoveCounter;
import net.chesstango.tools.tuning.fitnessfunctions.FitnessByEpdBestMove;
import net.chesstango.tools.tuning.fitnessfunctions.FitnessFunction;
import net.chesstango.tools.tuning.geneticproviders.GPEvaluatorByMaterial;
import net.chesstango.tools.tuning.geneticproviders.GeneticProvider;
Expand All @@ -20,17 +20,15 @@
public class JeneticsMain extends EvalTuningAbstract {
private static final Logger logger = LoggerFactory.getLogger(JeneticsMain.class);
private static final int POPULATION_SIZE = 20;
private static final int GENERATION_LIMIT = 100;
private static final int GENERATION_LIMIT = 200;
private volatile boolean stopped;

public static void main(String[] args) {
//GeneticProvider geneticProvider = new GeneticProvider2FactorsGenes();
GeneticProvider geneticProvider = new GPEvaluatorByMaterial();
//GeneticProvider geneticProvider = new GeneticProviderNIntChromosomes(10);

//FitnessFunction fitnessFunction = new FitnessByMatch();
FitnessFunction fitnessFunction = new FitnessByEpdBottomMoveCounter();
//FitnessFunction fitnessFunction = new FitnessByLeastSquare();
FitnessFunction fitnessFunction = new FitnessByEpdBestMove();

JeneticsMain main = new JeneticsMain(fitnessFunction, geneticProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public EvaluatorByMaterialFactory(int[] pieceValues) {

int computedKey = Arrays.hashCode(pieceValues);

//this.key = String.format("%s", Long.toUnsignedString(computedKey, 16));
this.key = String.format("%08x", computedKey);
this.key = Integer.toHexString(computedKey);
}

@Override
public Evaluator createGameEvaluator() {
return new EvaluatorByMaterial(pieceValues[0], pieceValues[1], pieceValues[2], pieceValues[3], pieceValues[4]);
EvaluatorByMaterial.EvaluatorByMaterialTable table = new EvaluatorByMaterial.EvaluatorByMaterialTable(key,
pieceValues[0], pieceValues[1], pieceValues[2], pieceValues[3], pieceValues[4]);
return new EvaluatorByMaterial(table);
}

@Override
Expand All @@ -43,7 +44,7 @@ public String getKey() {

@Override
public String getRepresentation() {
EvaluatorByMaterial.Tables obj = new EvaluatorByMaterial.Tables(key,
EvaluatorByMaterial.EvaluatorByMaterialTable obj = new EvaluatorByMaterial.EvaluatorByMaterialTable(key,
pieceValues[0], pieceValues[1], pieceValues[2], pieceValues[3], pieceValues[4]);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/**
* @author Mauricio Coria
*/
@Disabled
public class FitnessByEpdBestMoveTest {
private FitnessByEpdBestMove fitnessByEpdBestMove;

Expand Down Expand Up @@ -40,9 +41,20 @@ public void test01() {
@Disabled
public void test02() {
long fitness = fitnessByEpdBestMove.fitness(() ->
new EvaluatorByMaterial(
EvaluatorByMaterial.readValues("{\"id\":\"01fd3aaa\",\"pawn\":5,\"knight\":4,\"bishop\":7,\"rook\":6,\"queen\":9}"))
new EvaluatorByMaterial("{\"id\":\"01fd3aaa\",\"pawn\":5,\"knight\":4,\"bishop\":7,\"rook\":6,\"queen\":9}")
);
assertEquals(222, fitness);
}

@Test
@Disabled
public void test03() {
long fitness = fitnessByEpdBestMove.fitness(() ->
new EvaluatorByMaterial("{\"id\":\"1c4d904\",\"pawn\":1,\"knight\":4,\"bishop\":6,\"rook\":7,\"queen\":9}")
);
assertEquals(252, fitness);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Mauricio Coria
*/
@Disabled
public class FitnessByEpdBottomMoveCounterTest {
private FitnessByEpdBottomMoveCounter fitnessByEpdBottomMoveCounter;

Expand All @@ -35,8 +36,7 @@ public void tearDown() {
@Disabled
public void test01() {
long fitness = fitnessByEpdBottomMoveCounter.fitness(() ->
new EvaluatorByMaterial(
EvaluatorByMaterial.readValues("{\"id\":\"01fd3aaa\",\"pawn\":5,\"knight\":4,\"bishop\":7,\"rook\":6,\"queen\":9}"))
new EvaluatorByMaterial("{\"id\":\"01fd3aaa\",\"pawn\":5,\"knight\":4,\"bishop\":7,\"rook\":6,\"queen\":9}")
);
assertEquals(56037, fitness);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ public void test06() {
FitnessByEpdBestMove fitnessByEpdBestMove = new FitnessByEpdBestMove(files, 2);

fitnessByEpdBestMove.start();
long points = fitnessByEpdBestMove.fitness(() -> new EvaluatorImp06(EvaluatorImp06.readValues("{\"id\":\"ee0cb738\",\"weighs\":[540,436,25],\"mgPawnTbl\":[-705,779,226,-315,-683,-649,-433,526,-405,-587,-112,502,-953,-173,789,399,454,479,-813,-251,56,-161,-834,658,163,872,-954,124,190,-685,17,268,872,109,695,11,552,-547,263,-260,-741,-246,232,-86,-968,760,-682,862,606,230,360,808,-726,774,-7,-251,114,-804,-222,194,-770,-911,-799,-256],\"mgKnightTbl\":[821,-921,-396,-411,-410,-90,-458,-462,784,-4,-555,-298,-726,346,409,-319,-438,-474,-654,128,-68,-714,-59,788,907,611,624,-949,-503,882,-311,-602,718,998,-539,-534,-891,-2,-859,342,314,-608,-17,691,799,624,585,24,298,878,-256,982,525,-330,632,750,52,34,798,235,38,798,155,-578],\"mgBishopTbl\":[-803,455,202,-429,-875,25,-556,-926,-457,-398,-399,178,-161,-821,-605,102,53,-729,331,360,752,207,-312,-328,-48,151,-349,558,59,-64,-881,164,-834,-913,-76,693,487,-193,934,462,638,40,624,202,244,-33,-953,41,690,-45,471,-822,415,-267,225,470,-771,-715,263,-834,-897,317,-508,449],\"mgRookTbl\":[862,900,435,-794,-377,833,-350,-49,-847,-428,-241,334,-374,-685,71,-373,-885,-835,-205,-555,515,-981,-755,967,-924,287,483,448,-620,735,-800,33,-167,845,299,-618,-135,-620,662,-948,-388,-158,671,406,670,818,833,199,247,405,598,108,832,-225,483,274,-647,-286,-762,147,869,-301,-874,404],\"mgQueenTbl\":[252,817,128,-637,242,-870,-969,606,-339,-162,257,-18,-617,-848,944,-464,-345,785,-8,-386,760,-718,297,-434,-918,631,-792,102,766,770,-831,400,-415,253,250,468,-329,282,-172,76,-681,-996,394,232,682,-601,847,181,603,418,-837,-820,-510,-880,-221,-390,-124,-152,966,312,806,141,-299,994],\"mgKingTbl\":[-973,878,739,458,-698,140,155,-976,-831,875,-375,-401,274,692,80,869,-103,-256,260,-414,959,839,-713,-277,-605,398,461,-203,719,918,769,-164,344,788,-765,-876,124,693,876,34,-584,-99,-985,-239,274,475,-213,-668,-500,-660,70,-93,-184,488,-335,-887,-560,839,530,-439,666,-606,-742,-306],\"egPawnTbl\":[606,-140,-381,-156,-938,-973,-393,-630,747,243,-413,-405,820,-625,-794,-310,-193,-357,854,-816,437,-258,184,-463,129,-495,909,678,273,-689,90,685,509,181,-315,-317,-502,-839,425,407,-366,-97,-223,938,-369,-315,-139,88,-129,-101,831,127,-209,21,-863,-714,-721,-427,630,280,984,721,-137,940],\"egKnightTbl\":[-266,-427,-32,-92,-824,544,-409,-757,126,144,702,887,586,604,708,-584,-47,129,353,-760,688,-948,-843,-881,739,-411,-154,30,-990,-269,720,-853,258,-237,-664,159,44,382,-780,694,516,-74,-759,-233,-123,500,385,-265,103,-146,-187,-275,720,340,336,-89,-343,31,-19,-439,999,531,173,-711],\"egBishopTbl\":[-891,-694,-7,-767,522,191,250,180,-865,546,-263,-717,-552,146,-349,125,-136,-484,-532,-199,-401,-498,442,-971,569,-134,-849,-387,-298,495,732,389,-498,166,699,738,-383,-808,619,-665,555,-782,97,-200,541,261,55,355,-716,-210,-618,331,836,-83,382,-673,-847,430,-371,-513,448,968,492,58],\"egRookTbl\":[990,-309,-770,146,246,-732,300,868,201,994,-606,238,432,891,-219,290,-519,-955,982,-822,-703,-822,282,-846,621,-869,70,18,705,-589,923,-552,777,-618,-344,-325,855,-886,176,851,243,-677,968,619,-315,-640,-907,986,-579,-741,-581,-312,162,808,251,983,27,566,186,58,859,-623,12,260],\"egQueenTbl\":[673,270,775,-894,655,276,-87,-798,-233,819,-142,-653,528,-185,616,-826,188,219,-367,-505,-690,-237,-284,-676,-524,880,125,54,899,703,-730,-804,-838,-339,380,-939,-211,-146,-459,-359,-196,-960,7,433,550,430,-337,19,216,112,-53,510,37,-569,469,662,555,165,-845,388,762,904,-838,901],\"egKingTbl\":[-121,-974,508,906,548,-785,560,184,-489,79,293,-511,407,630,-558,266,617,349,708,-795,876,-333,435,-672,584,214,-471,482,-318,-341,-8,408,811,-41,-858,-62,551,149,783,-977,-365,-125,-1,34,-344,72,-737,45,49,770,-437,-581,-498,-840,461,485,631,-32,-985,-912,-318,-423,-729,661]}")));
long points = fitnessByEpdBestMove.fitness(() -> new EvaluatorImp06("{\"id\":\"ee0cb738\",\"weighs\":[540,436,25],\"mgPawnTbl\":[-705,779,226,-315,-683,-649,-433,526,-405,-587,-112,502,-953,-173,789,399,454,479,-813,-251,56,-161,-834,658,163,872,-954,124,190,-685,17,268,872,109,695,11,552,-547,263,-260,-741,-246,232,-86,-968,760,-682,862,606,230,360,808,-726,774,-7,-251,114,-804,-222,194,-770,-911,-799,-256],\"mgKnightTbl\":[821,-921,-396,-411,-410,-90,-458,-462,784,-4,-555,-298,-726,346,409,-319,-438,-474,-654,128,-68,-714,-59,788,907,611,624,-949,-503,882,-311,-602,718,998,-539,-534,-891,-2,-859,342,314,-608,-17,691,799,624,585,24,298,878,-256,982,525,-330,632,750,52,34,798,235,38,798,155,-578],\"mgBishopTbl\":[-803,455,202,-429,-875,25,-556,-926,-457,-398,-399,178,-161,-821,-605,102,53,-729,331,360,752,207,-312,-328,-48,151,-349,558,59,-64,-881,164,-834,-913,-76,693,487,-193,934,462,638,40,624,202,244,-33,-953,41,690,-45,471,-822,415,-267,225,470,-771,-715,263,-834,-897,317,-508,449],\"mgRookTbl\":[862,900,435,-794,-377,833,-350,-49,-847,-428,-241,334,-374,-685,71,-373,-885,-835,-205,-555,515,-981,-755,967,-924,287,483,448,-620,735,-800,33,-167,845,299,-618,-135,-620,662,-948,-388,-158,671,406,670,818,833,199,247,405,598,108,832,-225,483,274,-647,-286,-762,147,869,-301,-874,404],\"mgQueenTbl\":[252,817,128,-637,242,-870,-969,606,-339,-162,257,-18,-617,-848,944,-464,-345,785,-8,-386,760,-718,297,-434,-918,631,-792,102,766,770,-831,400,-415,253,250,468,-329,282,-172,76,-681,-996,394,232,682,-601,847,181,603,418,-837,-820,-510,-880,-221,-390,-124,-152,966,312,806,141,-299,994],\"mgKingTbl\":[-973,878,739,458,-698,140,155,-976,-831,875,-375,-401,274,692,80,869,-103,-256,260,-414,959,839,-713,-277,-605,398,461,-203,719,918,769,-164,344,788,-765,-876,124,693,876,34,-584,-99,-985,-239,274,475,-213,-668,-500,-660,70,-93,-184,488,-335,-887,-560,839,530,-439,666,-606,-742,-306],\"egPawnTbl\":[606,-140,-381,-156,-938,-973,-393,-630,747,243,-413,-405,820,-625,-794,-310,-193,-357,854,-816,437,-258,184,-463,129,-495,909,678,273,-689,90,685,509,181,-315,-317,-502,-839,425,407,-366,-97,-223,938,-369,-315,-139,88,-129,-101,831,127,-209,21,-863,-714,-721,-427,630,280,984,721,-137,940],\"egKnightTbl\":[-266,-427,-32,-92,-824,544,-409,-757,126,144,702,887,586,604,708,-584,-47,129,353,-760,688,-948,-843,-881,739,-411,-154,30,-990,-269,720,-853,258,-237,-664,159,44,382,-780,694,516,-74,-759,-233,-123,500,385,-265,103,-146,-187,-275,720,340,336,-89,-343,31,-19,-439,999,531,173,-711],\"egBishopTbl\":[-891,-694,-7,-767,522,191,250,180,-865,546,-263,-717,-552,146,-349,125,-136,-484,-532,-199,-401,-498,442,-971,569,-134,-849,-387,-298,495,732,389,-498,166,699,738,-383,-808,619,-665,555,-782,97,-200,541,261,55,355,-716,-210,-618,331,836,-83,382,-673,-847,430,-371,-513,448,968,492,58],\"egRookTbl\":[990,-309,-770,146,246,-732,300,868,201,994,-606,238,432,891,-219,290,-519,-955,982,-822,-703,-822,282,-846,621,-869,70,18,705,-589,923,-552,777,-618,-344,-325,855,-886,176,851,243,-677,968,619,-315,-640,-907,986,-579,-741,-581,-312,162,808,251,983,27,566,186,58,859,-623,12,260],\"egQueenTbl\":[673,270,775,-894,655,276,-87,-798,-233,819,-142,-653,528,-185,616,-826,188,219,-367,-505,-690,-237,-284,-676,-524,880,125,54,899,703,-730,-804,-838,-339,380,-939,-211,-146,-459,-359,-196,-960,7,433,550,430,-337,19,216,112,-53,510,37,-569,469,662,555,165,-845,388,762,904,-838,901],\"egKingTbl\":[-121,-974,508,906,548,-785,560,184,-489,79,293,-511,407,630,-558,266,617,349,708,-795,876,-333,435,-672,584,214,-471,482,-318,-341,-8,408,811,-41,-858,-62,551,149,783,-977,-365,-125,-1,34,-344,72,-737,45,49,770,-437,-581,-498,-840,461,485,631,-32,-985,-912,-318,-423,-729,661]}"));
fitnessByEpdBestMove.stop();

assertEquals(29429L, points);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ public EvaluatorByMaterial() {
this(readDefaultValues());
}

public EvaluatorByMaterial(Tables tables) {
this(tables.pawn, tables.knight, tables.bishop, tables.rook, tables.queen);
public EvaluatorByMaterial(String json) {
this(readValues(json));
}

public EvaluatorByMaterial(int pawn,
int knight,
int bishop,
int rook,
int queen) {

this.PAWN_VALUE = pawn;
this.KNIGHT_VALUE = knight;
this.BISHOP_VALUE = bishop;
this.ROOK_VALUE = rook;
this.QUEEN_VALUE = queen;
}

public EvaluatorByMaterial(EvaluatorByMaterialTable evaluatorByMaterialTable) {
this(evaluatorByMaterialTable.pawn, evaluatorByMaterialTable.knight, evaluatorByMaterialTable.bishop, evaluatorByMaterialTable.rook, evaluatorByMaterialTable.queen);
}

@Override
public int evaluateNonFinalStatus() {
return evaluateByMaterial();
Expand Down Expand Up @@ -90,36 +93,40 @@ protected int getPieceValue(Piece piece) {
};
}

public record Tables(String id,
int pawn,
int knight,
int bishop,
int rook,
int queen) {
public record EvaluatorByMaterialTable(String id,
int pawn,
int knight,
int bishop,
int rook,
int queen) {
}

private static Tables readDefaultValues() {
try (InputStream inputStream = Tables.class.getClassLoader()
.getResourceAsStream("evaluatorByMaterial.json");) {
private static EvaluatorByMaterialTable readDefaultValues() {
String fileName = "EvaluatorByMaterial.json";
try (InputStream inputStream = EvaluatorByMaterialTable.class.getClassLoader()
.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new RuntimeException(String.format("File doesn't exist: %s", fileName));
}
return readValues(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static Tables readValues(InputStream inputStream) {
private static EvaluatorByMaterialTable readValues(InputStream inputStream) {
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream)) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(inputStreamReader, Tables.class);
return objectMapper.readValue(inputStreamReader, EvaluatorByMaterialTable.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static Tables readValues(String dump) {
private static EvaluatorByMaterialTable readValues(String dump) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(dump, Tables.class);
return objectMapper.readValue(dump, EvaluatorByMaterialTable.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public EvaluatorImp06() {
this(readDefaultValues());
}

public EvaluatorImp06(String json) {
this(readValues(json));
}

public EvaluatorImp06(Tables tables) {
this(tables.weighs,
tables.mgPawnTbl,
Expand Down Expand Up @@ -256,9 +260,13 @@ public record Tables(String id,
int[] egKingTbl) {
}

private static Tables readDefaultValues() {
try (InputStream inputStream = Tables.class.getClassLoader()
.getResourceAsStream("evaluatorImp06-tables.json");) {
public static Tables readDefaultValues() {
String fileName = "EvaluatorImp06.json";
try (InputStream inputStream = EvaluatorByMaterial.EvaluatorByMaterialTable.class.getClassLoader()
.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new RuntimeException(String.format("File doesn't exist: %s", fileName));
}
return readValues(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 4fac476

Please sign in to comment.