AI: little refactor

This commit is contained in:
Oleg Agafonov 2023-11-26 21:37:47 +04:00
parent 939ff7b441
commit 58ce3296b6
2 changed files with 17 additions and 2 deletions

View file

@ -46,7 +46,20 @@ import java.util.stream.Collectors;
public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ {
private static final Logger logger = Logger.getLogger(ComputerPlayer6.class);
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
// same params as Executors.newFixedThreadPool
// no needs erorrs check in afterExecute here cause that pool used for FutureTask with result check already
private static final ExecutorService threadPoolSimulations = new ThreadPoolExecutor(
COMPUTER_MAX_THREADS_FOR_SIMULATIONS,
COMPUTER_MAX_THREADS_FOR_SIMULATIONS,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
r -> {
Thread thread = new Thread(r);
thread.setName("AI-SIM-" + thread.getId());
return thread;
});
protected int maxDepth;
protected int maxNodes;
protected int maxThink;
@ -426,7 +439,7 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ {
protected Integer addActionsTimed() {
// run new game simulation in parallel thread
FutureTask<Integer> task = new FutureTask<>(() -> addActions(root, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE));
pool.execute(task);
threadPoolSimulations.execute(task);
try {
int maxSeconds = maxThink;
if (COMPUTER_DISABLE_TIMEOUT_IN_GAME_SIMULATIONS) {

View file

@ -74,6 +74,8 @@ public class ComputerPlayer extends PlayerImpl implements Player {
// debug only: set TRUE to debug simulation's code/games (on false sim thread will be stopped after few secs by timeout)
protected boolean COMPUTER_DISABLE_TIMEOUT_IN_GAME_SIMULATIONS = false;
final static int COMPUTER_MAX_THREADS_FOR_SIMULATIONS = 1; // TODO: rework simulations logic to use multiple calcs instead one by one
private transient Map<Mana, Card> unplayable = new TreeMap<>();
private transient List<Card> playableNonInstant = new ArrayList<>();
private transient List<Card> playableInstant = new ArrayList<>();