DroidFish: Reduced opening book memory usage.

This commit is contained in:
Peter Osterlund 2012-09-11 22:44:49 +00:00
parent 1767f8c947
commit 41e7a6922c
6 changed files with 23 additions and 23 deletions

View File

@ -62,7 +62,7 @@ class CtgBook implements IOpeningBook {
} }
@Override @Override
public List<BookEntry> getBookEntries(Position pos) { public ArrayList<BookEntry> getBookEntries(Position pos) {
RandomAccessFile ctgF = null; RandomAccessFile ctgF = null;
RandomAccessFile ctbF = null; RandomAccessFile ctbF = null;
RandomAccessFile ctoF = null; RandomAccessFile ctoF = null;
@ -75,7 +75,7 @@ class CtgBook implements IOpeningBook {
CtoFile cto = new CtoFile(ctoF); CtoFile cto = new CtoFile(ctoF);
CtgFile ctg = new CtgFile(ctgF, ctb, cto); CtgFile ctg = new CtgFile(ctgF, ctb, cto);
List<BookEntry> ret = null; ArrayList<BookEntry> ret = null;
PositionData pd = ctg.getPositionData(pos); PositionData pd = ctg.getPositionData(pos);
if (pd != null) { if (pd != null) {
boolean mirrorColor = pd.mirrorColor; boolean mirrorColor = pd.mirrorColor;
@ -86,7 +86,7 @@ class CtgBook implements IOpeningBook {
pd.pos.makeMove(be.move, ui); pd.pos.makeMove(be.move, ui);
PositionData movePd = ctg.getPositionData(pd.pos); PositionData movePd = ctg.getPositionData(pd.pos);
pd.pos.unMakeMove(be.move, ui); pd.pos.unMakeMove(be.move, ui);
double weight = be.weight; float weight = be.weight;
if (movePd == null) { if (movePd == null) {
// System.out.printf("%s : no pos\n", TextIO.moveToUCIString(be.move)); // System.out.printf("%s : no pos\n", TextIO.moveToUCIString(be.move));
weight = 0; weight = 0;
@ -417,8 +417,8 @@ class CtgBook implements IOpeningBook {
case 0x02: ent.weight = 0; break; // ? case 0x02: ent.weight = 0; break; // ?
case 0x03: ent.weight = 32; break; // !! case 0x03: ent.weight = 32; break; // !!
case 0x04: ent.weight = 0; break; // ?? case 0x04: ent.weight = 0; break; // ??
case 0x05: ent.weight = 0.5; break; // !? case 0x05: ent.weight = 0.5f; break; // !?
case 0x06: ent.weight = 0.125; break; // ?! case 0x06: ent.weight = 0.125f; break; // ?!
case 0x08: ent.weight = 1000000; break; // Only move case 0x08: ent.weight = 1000000; break; // Only move
} }
entries.add(ent); entries.add(ent);

View File

@ -37,9 +37,9 @@ import org.petero.droidfish.gamelogic.Pair;
* @author petero * @author petero
*/ */
public final class DroidBook { public final class DroidBook {
static class BookEntry { static final class BookEntry {
Move move; Move move;
double weight; float weight;
BookEntry(Move move) { BookEntry(Move move) {
this.move = move; this.move = move;
weight = 1; weight = 1;
@ -117,7 +117,7 @@ public final class DroidBook {
boolean localized) { boolean localized) {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
ArrayList<Move> bookMoveList = new ArrayList<Move>(); ArrayList<Move> bookMoveList = new ArrayList<Move>();
List<BookEntry> bookMoves = getBook().getBookEntries(pos); ArrayList<BookEntry> bookMoves = getBook().getBookEntries(pos);
// Check legality // Check legality
if (bookMoves != null) { if (bookMoves != null) {

View File

@ -18,7 +18,7 @@
package org.petero.droidfish.book; package org.petero.droidfish.book;
import java.util.List; import java.util.ArrayList;
import org.petero.droidfish.book.DroidBook.BookEntry; import org.petero.droidfish.book.DroidBook.BookEntry;
import org.petero.droidfish.gamelogic.Position; import org.petero.droidfish.gamelogic.Position;
@ -31,5 +31,5 @@ interface IOpeningBook {
void setOptions(BookOptions options); void setOptions(BookOptions options);
/** Get all book entries for a position. */ /** Get all book entries for a position. */
List<BookEntry> getBookEntries(Position pos); ArrayList<BookEntry> getBookEntries(Position pos);
} }

View File

@ -23,7 +23,6 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.petero.droidfish.book.DroidBook.BookEntry; import org.petero.droidfish.book.DroidBook.BookEntry;
import org.petero.droidfish.gamelogic.ChessParseError; import org.petero.droidfish.gamelogic.ChessParseError;
@ -33,8 +32,10 @@ import org.petero.droidfish.gamelogic.Position;
import org.petero.droidfish.gamelogic.TextIO; import org.petero.droidfish.gamelogic.TextIO;
import org.petero.droidfish.gamelogic.UndoInfo; import org.petero.droidfish.gamelogic.UndoInfo;
import android.util.FloatMath;
final class InternalBook implements IOpeningBook { final class InternalBook implements IOpeningBook {
private static Map<Long, List<BookEntry>> bookMap; private static HashMap<Long, ArrayList<BookEntry>> bookMap;
private static int numBookMoves = -1; private static int numBookMoves = -1;
InternalBook() { InternalBook() {
@ -58,15 +59,15 @@ final class InternalBook implements IOpeningBook {
} }
@Override @Override
public List<BookEntry> getBookEntries(Position pos) { public ArrayList<BookEntry> getBookEntries(Position pos) {
initInternalBook(); initInternalBook();
List<BookEntry> ents = bookMap.get(pos.zobristHash()); ArrayList<BookEntry> ents = bookMap.get(pos.zobristHash());
if (ents == null) if (ents == null)
return null; return null;
List<BookEntry> ret = new ArrayList<BookEntry>(); ArrayList<BookEntry> ret = new ArrayList<BookEntry>();
for (BookEntry be : ents) { for (BookEntry be : ents) {
BookEntry be2 = new BookEntry(be.move); BookEntry be2 = new BookEntry(be.move);
be2.weight = Math.sqrt(be.weight) * 100 + 1; be2.weight = FloatMath.sqrt(be.weight) * 100 + 1;
ret.add(be2); ret.add(be2);
} }
return ret; return ret;
@ -80,7 +81,7 @@ final class InternalBook implements IOpeningBook {
if (numBookMoves >= 0) if (numBookMoves >= 0)
return; return;
// long t0 = System.currentTimeMillis(); // long t0 = System.currentTimeMillis();
bookMap = new HashMap<Long, List<BookEntry>>(); bookMap = new HashMap<Long, ArrayList<BookEntry>>();
numBookMoves = 0; numBookMoves = 0;
try { try {
InputStream inStream = getClass().getResourceAsStream("/book.bin"); InputStream inStream = getClass().getResourceAsStream("/book.bin");
@ -131,7 +132,7 @@ final class InternalBook implements IOpeningBook {
/** Add a move to a position in the opening book. */ /** Add a move to a position in the opening book. */
private final void addToBook(Position pos, Move moveToAdd) { private final void addToBook(Position pos, Move moveToAdd) {
List<BookEntry> ent = bookMap.get(pos.zobristHash()); ArrayList<BookEntry> ent = bookMap.get(pos.zobristHash());
if (ent == null) { if (ent == null) {
ent = new ArrayList<BookEntry>(); ent = new ArrayList<BookEntry>();
bookMap.put(pos.zobristHash(), ent); bookMap.put(pos.zobristHash(), ent);

View File

@ -18,7 +18,7 @@
package org.petero.droidfish.book; package org.petero.droidfish.book;
import java.util.List; import java.util.ArrayList;
import org.petero.droidfish.book.DroidBook.BookEntry; import org.petero.droidfish.book.DroidBook.BookEntry;
import org.petero.droidfish.gamelogic.Position; import org.petero.droidfish.gamelogic.Position;
@ -31,7 +31,7 @@ class NullBook implements IOpeningBook {
} }
@Override @Override
public List<BookEntry> getBookEntries(Position pos) { public ArrayList<BookEntry> getBookEntries(Position pos) {
return null; return null;
} }

View File

@ -23,7 +23,6 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.petero.droidfish.book.DroidBook.BookEntry; import org.petero.droidfish.book.DroidBook.BookEntry;
import org.petero.droidfish.gamelogic.Move; import org.petero.droidfish.gamelogic.Move;
@ -373,7 +372,7 @@ class PolyglotBook implements IOpeningBook {
} }
@Override @Override
public final List<BookEntry> getBookEntries(Position pos) { public final ArrayList<BookEntry> getBookEntries(Position pos) {
try { try {
RandomAccessFile f = new RandomAccessFile(bookFile, "r"); RandomAccessFile f = new RandomAccessFile(bookFile, "r");
long numEntries = f.length() / 16; long numEntries = f.length() / 16;
@ -396,7 +395,7 @@ class PolyglotBook implements IOpeningBook {
} }
// Read all entries with matching hash key // Read all entries with matching hash key
List<BookEntry> ret = new ArrayList<BookEntry>(); ArrayList<BookEntry> ret = new ArrayList<BookEntry>();
long entNo = hi; long entNo = hi;
while (entNo < numEntries) { while (entNo < numEntries) {
readEntry(f, entNo, ent); readEntry(f, entNo, ent);