我很蠻力一場比賽,我需要存儲所有位置和結果的數據。數據可能會有數百GB的大小。我考慮過SQL,但是恐怕在緊密的循環中查找可能會導致性能下降。程序將迭代可能的位置,並在已知的情況下返回獲勝移動,如果已知所有移動都丟失並且檢查未知移動的結果,則返回最長失序。如何高效地存儲大型Java地圖?
什麼是最好的方式來存儲一個大的Map<Long,Long[]> positionIdToBestMoves
?我正在考慮SQL或數據序列化。
我想通過在Java中強制執行所有可行的動作來解決微小的跳棋。職位的上限是大約100億美元。他們中的大多數不合理(即比比賽開始時更多的棋子)。大約10億美元是合理的估計。每個Map<Long, Long[]> position
將Long positionID
映射到Long whiteToMove
和Long blackToMove
。正值表示該位置正在贏,並且應該選擇導致位置存儲在值中的一個移動。負值-n
意味着頭寸最多失去n
移動。
搜索本身就具有這樣的遞歸:
//this is a stub
private Map<Long, Long[]> boardBook =...
//assuming that all winning positions are known
public Long nextMove(Long currentPos, int whiteOrBlack){
Set<Long> validMoves = calculateValidMoves(currentPos, whiteOrBlack);
boolean hasWinner = checkIfValidMoveIsKnownToWin(validMoves, whiteOrBlack);
if(hasWinner){ //there is a winning move - play it
Long winningMove = getWinningMove(validMoves, whiteOrBlack);
boardBook.get(currentPos)[whiteOrBlack] = winningMove ;
return winningMove ;
}
boolean areAllPositionsKnown = checkIfAllPositionsKnown(validMoves, whiteOrBlack);
if(areAllPositionsKnown){ //all moves are losing.. choose longest struggle
Long longestSequenceToDefeat = findPositionToLongestSequenceToDefeat(validMoves, whiteOrBlack);
int numberOfStepsTodefeat = boardBook.get(longestSequenceToDefeat)[whiteOrBlack];
boardBook.get(currentPos)[whiteOrBlack] = longestSequenceToDefeat ;
return longestSequenceToDefeat;
}
Set<Long> movesToCheck = getUntestedMoves(validMoves, whiteOrBlack);
Long longeststruggle;
int maxNumberOfMovesToDefeat =-1;
for(Long moveTocheck : movesToCheck){
Long result = nextMove(moveToCheck, whiteOrBlack);
if(result>0){ //just discovered a winning move
boardBook.get(currentPos)[whiteOrBlack] = winningMove ;
return winningMove ;
}else {
int numOfMovesToDefeat = -1*boardBook.get(moveTocheck)[whiteOrBlack];
if(numOfMovesToDefeat >maxNumberOfMovesToDefeat){
maxNumberOfMovesToDefeat =numOfMovesToDefeat ;
longeststruggle = moveTocheck;
}
}
}
boardBook.get(currentPos)[whiteOrBlack] = -1*maxNumberOfMovesToDefeat;
return longeststruggle;
}
有趣的問題。它超出了我的技能來回答,但是您所描述的聽起來更像是大數據/ NoSQL解決方案的領域,而不是傳統的SQL。 – Gimby