-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Store principal continuations in static arrays rather than li…
…sts, to reduce memory overhead." This reverts commit 45a0e23.
- Loading branch information
Showing
5 changed files
with
91 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 65 additions & 42 deletions
107
EubosChess/src/main/java/eubos/search/PrincipalContinuation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,128 @@ | ||
package eubos.search; | ||
|
||
import eubos.main.EubosEngineMain; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import eubos.position.Move; | ||
import it.unimi.dsi.fastutil.ints.IntArrays; | ||
|
||
public class PrincipalContinuation { | ||
|
||
private int [][] pc; | ||
private int [] length; | ||
private List<List<Integer>> pc; | ||
private SearchDebugAgent sda; | ||
|
||
public PrincipalContinuation(int searchDepth, SearchDebugAgent sda) { | ||
// Create the pc list at each ply | ||
pc = new int[EubosEngineMain.SEARCH_DEPTH_IN_PLY][]; | ||
for (int i = 0; i < EubosEngineMain.SEARCH_DEPTH_IN_PLY; i++) { | ||
pc[i] = new int[EubosEngineMain.SEARCH_DEPTH_IN_PLY]; | ||
pc = new ArrayList<List<Integer>>(searchDepth); | ||
for (int i=0; i<searchDepth; i++) { | ||
pc.add(new ArrayList<Integer>(searchDepth)); | ||
} | ||
length = new int[EubosEngineMain.SEARCH_DEPTH_IN_PLY]; | ||
this.sda = sda; | ||
} | ||
|
||
public int getBestMove(byte currPly) { | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
if (length[currPly] != 0) | ||
return pc[currPly][0]; | ||
if (currPly < pc.size()) { | ||
List<Integer> plyList = pc.get(currPly); | ||
if (!plyList.isEmpty()) | ||
return plyList.get(0); | ||
} | ||
return Move.NULL_MOVE; | ||
} | ||
|
||
public String toString() { | ||
StringBuilder output = new StringBuilder(); | ||
for (int currPly = 0; currPly < length[0]; currPly++) { | ||
for (int currPly = 0; currPly < pc.size(); currPly++) { | ||
output.append(String.format("Ply %d (%s) ,", currPly, toStringAt(currPly))); | ||
} | ||
return output.toString(); | ||
} | ||
|
||
public String toStringAt(int currPly) { | ||
StringBuilder output = new StringBuilder(); | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
if (length[currPly] != 0) { | ||
for (int i=0; i<length[currPly]; i++) { | ||
int currMove = pc[currPly][i]; | ||
if (EubosEngineMain.ENABLE_ASSERTS) { | ||
assert currMove != Move.NULL_MOVE; | ||
} | ||
output.append(Move.toString(currMove)); | ||
if (currPly < pc.size()) { | ||
List<Integer> plyList = pc.get(currPly); | ||
if (!plyList.isEmpty()) { | ||
for (int currMove : plyList) { | ||
//assert currMove != Move.NULL_MOVE; | ||
output.append((Move.toString(currMove))); | ||
output.append(' '); | ||
} | ||
} | ||
} | ||
return output.toString(); | ||
} | ||
|
||
public int [] toPvList(int currPly) { | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
int [] pv = IntArrays.trim(pc[currPly], length[currPly]); | ||
return pv; | ||
public static String pvToString(List<Integer> pv) { | ||
StringBuilder output = new StringBuilder(); | ||
if (pv != null && !pv.isEmpty()) { | ||
for (int currMove : pv) { | ||
//assert currMove != Move.NULL_MOVE; | ||
output.append((Move.toString(currMove))); | ||
output.append(' '); | ||
} | ||
} | ||
return output.toString(); | ||
} | ||
|
||
public List<Integer> toPvList(int currPly) { | ||
List<Integer> mv = new ArrayList<Integer>(pc.size()); | ||
if (currPly < pc.size()) { | ||
for (int currMove : pc.get(currPly)) { | ||
mv.add(currMove); | ||
} | ||
} | ||
return null; | ||
return mv; | ||
} | ||
|
||
void initialise(int currPly, int currMove) { | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
length[currPly] = 1; | ||
pc[currPly][0] = currMove; | ||
if (currPly < pc.size()) { | ||
List<Integer> plyToUpdatePc = pc.get(currPly); | ||
plyToUpdatePc.clear(); | ||
plyToUpdatePc.add(currMove); | ||
} | ||
} | ||
|
||
// Bring down a pv from node further down the tree, with curr move added at the head | ||
void update(int currPly, int currMove) { | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
length[currPly] = 1; | ||
pc[currPly][0] = currMove; | ||
if (currPly < pc.size()) { | ||
List<Integer> plyToUpdatePc = pc.get(currPly); | ||
plyToUpdatePc.clear(); | ||
plyToUpdatePc.add(currMove); | ||
int nextPly = currPly+1; | ||
if (nextPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
if (nextPly < pc.size()) { | ||
// Bring down, if possible | ||
for (int i=0; i<length[nextPly]; i++) { | ||
pc[currPly][i+1] = pc[nextPly][i]; | ||
length[currPly] += 1; | ||
} | ||
plyToUpdatePc.addAll(pc.get(nextPly)); | ||
} | ||
if (SearchDebugAgent.DEBUG_ENABLED) sda.printPrincipalContinuation(this); | ||
} | ||
} | ||
|
||
// Update a principal continuation from a Transposition hit where we don't have onwards pv | ||
void set(int currPly, int currMove) { | ||
if (currPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
length[currPly] = 1; | ||
pc[currPly][0] = currMove; | ||
if (currPly < pc.size()) { | ||
List<Integer> plyToUpdatePc = pc.get(currPly); | ||
plyToUpdatePc.clear(); | ||
plyToUpdatePc.add(currMove); | ||
clearContinuationBeyondPly(currPly); | ||
if (SearchDebugAgent.DEBUG_ENABLED) sda.printPrincipalContinuation(this); | ||
} | ||
} | ||
|
||
// Update a principal continuation from a Transposition hit where we do have an onwards pv | ||
public void update(int currPly, List<Integer> onwards_pv) { | ||
if (currPly < pc.size()) { | ||
List<Integer> plyToUpdatePc = pc.get(currPly); | ||
plyToUpdatePc.clear(); | ||
if (onwards_pv != null) { | ||
plyToUpdatePc.addAll(onwards_pv); | ||
} | ||
clearContinuationBeyondPly(currPly); | ||
} | ||
} | ||
|
||
// Clear all downstream pv's, from the current ply | ||
void clearContinuationBeyondPly(int currPly) { | ||
int nextPly = currPly+1; | ||
if (nextPly < EubosEngineMain.SEARCH_DEPTH_IN_PLY) { | ||
length[nextPly] = 0; | ||
if (nextPly < pc.size()) { | ||
pc.get(nextPly).clear(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters