improve ai (sort moves)
This commit is contained in:
@@ -28,9 +28,8 @@ public class AlphaBetaAI extends AI {
|
|||||||
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
||||||
super(commandExecutor, color);
|
super(commandExecutor, color);
|
||||||
this.searchDepth = searchDepth;
|
this.searchDepth = searchDepth;
|
||||||
int threadCount = Runtime.getRuntime().availableProcessors() - 1;
|
int threadCount = Runtime.getRuntime().availableProcessors() / 2;
|
||||||
this.threadPool = Executors.newFixedThreadPool(threadCount, new AlphaBetaThreadCreator(commandExecutor, color, threadCount));
|
this.threadPool = Executors.newFixedThreadPool(threadCount, new AlphaBetaThreadCreator(commandExecutor, color, threadCount));
|
||||||
System.out.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Move getBestMove() {
|
private Move getBestMove() {
|
||||||
@@ -48,7 +47,9 @@ public class AlphaBetaAI extends AI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < moves.size(); i++) {
|
for (int i = 0; i < moves.size(); i++) {
|
||||||
|
System.out.printf("Progress : %.2f %% ", (float) (i) / moves.size() * 100.0f);
|
||||||
Move move = moves.get(i);
|
Move move = moves.get(i);
|
||||||
|
System.out.print("\r");
|
||||||
float value = MIN_FLOAT;
|
float value = MIN_FLOAT;
|
||||||
try {
|
try {
|
||||||
value = moveEvaluations.get(i).get();
|
value = moveEvaluations.get(i).get();
|
||||||
@@ -61,7 +62,7 @@ public class AlphaBetaAI extends AI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Best move : " + bestMoveValue);
|
System.out.println("Best move : " + bestMoveValue + " ");
|
||||||
|
|
||||||
return bestMove;
|
return bestMove;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package chess.ai.minimax;
|
package chess.ai.minimax;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import chess.ai.PieceCost;
|
import chess.ai.PieceCost;
|
||||||
import chess.ai.PiecePosCost;
|
import chess.ai.PiecePosCost;
|
||||||
@@ -76,7 +80,23 @@ public class AlphaBetaThread extends Thread {
|
|||||||
if (moves.isEmpty())
|
if (moves.isEmpty())
|
||||||
return getEndGameEvaluation();
|
return getEndGameEvaluation();
|
||||||
|
|
||||||
|
List<Entry<Move, Float>> movesCost = new ArrayList<>(moves.size());
|
||||||
|
|
||||||
for (Move move : moves) {
|
for (Move move : moves) {
|
||||||
|
this.simulation.tryMove(move);
|
||||||
|
movesCost.add(Map.entry(move, getBoardEvaluation()));
|
||||||
|
this.simulation.undoMove();
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(movesCost, (first, second) -> {
|
||||||
|
return Float.compare(first.getValue(), second.getValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
if (depth == 1)
|
||||||
|
return -movesCost.getFirst().getValue();
|
||||||
|
|
||||||
|
for (var moveEntry : movesCost) {
|
||||||
|
Move move = moveEntry.getKey();
|
||||||
this.simulation.tryMove(move);
|
this.simulation.tryMove(move);
|
||||||
value = Float.max(value, -negaMax(depth - 1, -beta, -alpha));
|
value = Float.max(value, -negaMax(depth - 1, -beta, -alpha));
|
||||||
this.simulation.undoMove();
|
this.simulation.undoMove();
|
||||||
|
|||||||
Reference in New Issue
Block a user