Skip to content

Commit 9df1615

Browse files
committed
Moves getBestMoves & getLow from SearchResult static to SearchParameters
1 parent 35bad69 commit 9df1615

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

src/main/java/com/fathzer/games/ai/SearchParameters.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.fathzer.games.ai;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.fathzer.games.ai.evaluation.EvaluatedMove;
7+
38
/** The parameters of an AI search.
49
* @see SearchResult
510
*/
@@ -73,4 +78,21 @@ public void setAccuracy(int accuracy) {
7378
}
7479
this.accuracy = accuracy;
7580
}
81+
82+
public <M> int getLow(List<EvaluatedMove<M>> moves) {
83+
return moves.size()>=size ? moves.get(size-1).getScore() - accuracy -1 : Integer.MIN_VALUE;
84+
}
85+
86+
public <M> List<EvaluatedMove<M>> getBestMoves(List<EvaluatedMove<M>> moves) {
87+
final List<EvaluatedMove<M>> cut = new ArrayList<>(moves.size());
88+
final int low = getLow(moves);
89+
int currentCount = 0;
90+
for (EvaluatedMove<M> ev : moves) {
91+
if (ev.getScore()>low || currentCount<size) {
92+
cut.add(ev);
93+
currentCount++;
94+
}
95+
}
96+
return cut;
97+
}
7698
}

src/main/java/com/fathzer/games/ai/SearchResult.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fathzer.games.ai;
22

3-
import java.util.ArrayList;
43
import java.util.LinkedList;
54
import java.util.List;
65

@@ -23,8 +22,12 @@ public SearchResult(SearchParameters params) {
2322
this.result = new LinkedList<>();
2423
}
2524

25+
public SearchParameters getSearchParameters() {
26+
return params;
27+
}
28+
2629
synchronized int getLow() {
27-
return getLow(result, params);
30+
return params.getLow(result);
2831
}
2932

3033
public synchronized void add(M move, Evaluation value) {
@@ -57,26 +60,7 @@ synchronized int getIndex(M move) {
5760
* It can also have less than size elements if there's less than size legal moves or search was interrupted before it finished.
5861
*/
5962
public synchronized List<EvaluatedMove<M>> getCut() {
60-
return getBestMoves(result, params);
61-
}
62-
63-
public static <M> int getLow(List<EvaluatedMove<M>> moves, SearchParameters params) {
64-
return moves.size()>=params.getSize() ? moves.get(params.getSize()-1).getScore() - params.getAccuracy() -1 : Integer.MIN_VALUE;
65-
}
66-
67-
//TODO Move this method in SearchParameters?
68-
public static <M> List<EvaluatedMove<M>> getBestMoves(List<EvaluatedMove<M>> moves, SearchParameters params) {
69-
final List<EvaluatedMove<M>> cut = new ArrayList<>(moves.size());
70-
final int low = getLow(moves, params);
71-
int currentCount = 0;
72-
for (EvaluatedMove<M> ev : moves) {
73-
if (ev.getScore()>low || currentCount<params.getSize()) {
74-
cut.add(ev);
75-
currentCount++;
76-
}
77-
}
78-
return cut;
79-
63+
return params.getBestMoves(result);
8064
}
8165

8266
/** Gets the list of moves evaluation.

src/main/java/com/fathzer/games/ai/iterativedeepening/DeepeningPolicy.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public <M> List<M> getMovesToDeepen(DepthFirstSearchParameters currentParams, Se
139139
* @param history The search history, including the result at {@code depth}
140140
* @param evaluatedMoves The evaluations obtained at {@code currentParams}'s depth
141141
* @return The list of evaluations without the win/loose moves that we do not need to deepen.
142+
* <br>If some winning moves has been removed, <code>currentParams</code> is also decremented accordingly
143+
* (for instance, if size was 3 and 2 moves are winning, <code>currentParams</code>'s size is set to 1,
144+
* allowing deeper search to return only 1 move with an exact evaluation).
142145
*/
143146
protected <M> List<EvaluatedMove<M>> filterWinLooseMoves(DepthFirstSearchParameters currentParams, SearchHistory<M> history, List<EvaluatedMove<M>> evaluatedMoves) {
144147
final AtomicInteger size = new AtomicInteger(currentParams.getSize());
@@ -188,7 +191,7 @@ public <M> Optional<SearchResult<M>> mergeInterrupted(SearchHistory<M> history,
188191
return Optional.empty();
189192
}
190193
final List<EvaluatedMove<M>> historyMoves = history.getLastList();
191-
final int previousLow = SearchResult.getLow(historyMoves, this);
194+
final int previousLow = this.getLow(historyMoves);
192195
final boolean trap = partialList.get(partialList.size()-1).getScore()<=previousLow;
193196
final SearchResult<M> mergedResult = new SearchResult<>(this);
194197
if (trap) {

src/main/java/com/fathzer/games/ai/iterativedeepening/FirstBestMoveSelector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import com.fathzer.games.ai.SearchResult;
65
import com.fathzer.games.ai.evaluation.EvaluatedMove;
76
import com.fathzer.games.ai.moveselector.MoveSelector;
87

@@ -21,7 +20,7 @@ public List<EvaluatedMove<M>> select(SearchHistory<M> history, List<EvaluatedMov
2120

2221
protected List<EvaluatedMove<M>> filter(SearchHistory<M> history, List<EvaluatedMove<M>> bestMoves) {
2322
for (int i=history.length()-1;i>=0;i--) {
24-
final List<EvaluatedMove<M>> best = SearchResult.getBestMoves(history.getList(i), history.getSearchParameters());
23+
final List<EvaluatedMove<M>> best = history.getSearchParameters().getBestMoves(history.getList(i));
2524
final List<M> cut = best.stream().map(EvaluatedMove::getMove).toList();
2625
bestMoves = getCandidates(bestMoves, cut);
2726
log(i, cut, bestMoves);

src/main/java/com/fathzer/games/ai/iterativedeepening/SearchHistory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.List;
55

66
import com.fathzer.games.ai.SearchParameters;
7-
import com.fathzer.games.ai.SearchResult;
87
import com.fathzer.games.ai.evaluation.EvaluatedMove;
98
import com.fathzer.games.ai.moveselector.MoveSelector;
109

@@ -79,12 +78,12 @@ public List<EvaluatedMove<M>> getList(int index) {
7978
}
8079

8180
/** Returns the best moves of a result added with {@link #add(List, int)} method.
82-
* <br>This move is computed by {@link SearchResult#getBestMoves(List, SearchParameters)}
81+
* <br>This move is computed by {@link SearchParameters#getBestMoves(List)}
8382
* @param index The index of the result
8483
* @return a list of moves restricted to the size and accuracy of this history
8584
*/
8685
public List<EvaluatedMove<M>> getBestMoves(int index) {
87-
return SearchResult.getBestMoves(results.get(index), params);
86+
return params.getBestMoves(results.get(index));
8887
}
8988

9089
/** Gets the depth of the last list of moves added to this history
@@ -103,11 +102,11 @@ public List<EvaluatedMove<M>> getLastList() {
103102

104103
/** Gets the last list of best moves added to this history
105104
* @return a list of moves (restricted to the search parameters of this history), or null if this history is empty
106-
* @see SearchResult#getBestMoves(List, SearchParameters)
105+
* @see SearchParameters#getBestMoves(List)
107106
*/
108107
public List<EvaluatedMove<M>> getBestMoves() {
109108
final List<EvaluatedMove<M>> last = getLastList();
110-
return last==null ? null : SearchResult.getBestMoves(last, params);
109+
return last==null ? null : params.getBestMoves(last);
111110
}
112111

113112
/** Gets the best move according to the given selector

0 commit comments

Comments
 (0)