Fixed metrics in AIUtil

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@361 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-31 21:29:27 +02:00
parent 2e819f352b
commit 56dc0f99b1

View file

@ -27,12 +27,13 @@ public class AIUtil {
* settings * settings
* *
* @param settings * @param settings
* the underlying game settings * the underlying game settings
*/ */
public AIUtil(GameSettings settings) { public AIUtil(GameSettings settings) {
this.settings = settings; this.settings = settings;
stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values())); stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor
.values()));
stoneColors.retainAll(settings.getStoneColors()); stoneColors.retainAll(settings.getStoneColors());
} }
@ -42,85 +43,112 @@ public class AIUtil {
int jokerCount; int jokerCount;
SearchState(int pointsMissing, SearchState(int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) { TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
int jokerCount) {
this.pointsMissing = pointsMissing; this.pointsMissing = pointsMissing;
this.stoneCounts = stoneCounts; this.stoneCounts = stoneCounts;
this.jokerCount = jokerCount; this.jokerCount = jokerCount;
} }
} }
@SuppressWarnings("serial")
private static class EnoughPoints extends Throwable {
private Pair<List<StoneSet>, Integer> result;
public EnoughPoints(Pair<List<StoneSet>, Integer> result) {
this.result = result;
}
public Pair<List<StoneSet>, Integer> getResult() {
return result;
}
}
/** /**
* Tries to find sets with a certain total of points. If only lower totals are * Tries to find sets with a certain total of points. If only lower totals
* found, returns the sets with the highest cumulated point total. * are found, returns the sets with the highest cumulated point total.
* *
* @param pointsMissing * @param pointsMissing
* the desired number of points * the desired number of points
* @param stoneCounts * @param stoneCounts
* the number of each stone * the number of each stone
* @param jokerCount * @param jokerCount
* the total number of jokers in the game * the total number of jokers in the game
* @return the sets that have the desired point total or the highest found * @return the sets that have the desired point total or the highest found
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints( public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
int pointsMissing, int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) { TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
int jokerCount) {
if (pointsMissing <= 0) { Pair<List<StoneSet>, Integer> emptyResult = new Pair<List<StoneSet>, Integer>(
return new Pair<List<StoneSet>, Integer>( Collections.<StoneSet> emptyList(), 0);
Collections.<StoneSet> emptyList(), 0); if (pointsMissing <= 0)
} return emptyResult;
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
.clone(); .clone();
Pair<List<StoneSet>, Integer> result, bestResult; SearchHelper searchHelper = new SearchHelper(pointsMissing,
bestResult = new Pair<List<StoneSet>, Integer>( new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0); Collections.<StoneSet> emptyList(), 0));
try {
for (int value = settings.getHighestValue(); value >= 1; value--) {
for (StoneColor color : stoneColors) {
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(
value, color);
for (int value = settings.getHighestValue(); value >= 1; value--) { if (stoneCounts.containsKey(stone)) {
for (StoneColor color : stoneColors) { SearchState searchState = new SearchState(pointsMissing
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(value, - value, stoneCounts, jokerCount);
color); decrementStoneCount(stoneCounts, stone);
searchHelper.checkResult(findRunsWithTotalPoints(
searchState, stone, 1));
if (stoneCounts.containsKey(stone)) { searchHelper.checkResult(findGroupsWithTotalPoints(
decrementStoneCount(stoneCounts, stone); searchState, value,
result = findRunsWithTotalPoints(new SearchState(pointsMissing Collections.singletonList(color), color));
- value, stoneCounts, jokerCount), stone, 1); }
if (result.getSecond() >= pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
result = findGroupsWithTotalPoints(new SearchState(pointsMissing if (jokerCount > 0) {
- value, stoneCounts, jokerCount), value, SearchState searchState = new SearchState(pointsMissing
Collections.singletonList(color), color); - value, stoneCounts, jokerCount - 1);
if (result.getSecond() >= pointsMissing) searchHelper.checkResult(findRunsWithTotalPoints(
return result; searchState, stone, 1));
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
}
if (jokerCount > 0) { searchHelper.checkResult(findGroupsWithTotalPoints(
result = findRunsWithTotalPoints(new SearchState(pointsMissing searchState, value,
- value, stoneCounts, jokerCount - 1), stone, 1); Collections.singletonList(color), color));
if (result.getSecond() >= pointsMissing) }
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
result = findGroupsWithTotalPoints(new SearchState(pointsMissing
- value, stoneCounts, jokerCount - 1), value,
Collections.singletonList(color), color);
if (result.getSecond() >= pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
} }
} catch (EnoughPoints enoughPoints) {
return enoughPoints.getResult();
}
return searchHelper.getBestResult();
}
private static class SearchHelper {
Pair<List<StoneSet>, Integer> bestResult;
int pointsMissing;
public SearchHelper(int pointsMissing,
Pair<List<StoneSet>, Integer> bestResult) {
this.pointsMissing = pointsMissing;
this.bestResult = bestResult;
} }
return bestResult; public void checkResult(Pair<List<StoneSet>, Integer> result)
throws EnoughPoints {
if (result.getSecond() >= pointsMissing)
throw new EnoughPoints(result);
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
}
public Pair<List<StoneSet>, Integer> getBestResult() {
return bestResult;
}
} }
private Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints( private Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
@ -128,129 +156,125 @@ public class AIUtil {
StoneColor testedColor) { StoneColor testedColor) {
StoneColor nextColor = getNextColor(testedColor); StoneColor nextColor = getNextColor(testedColor);
Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(value, Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(
nextColor); value, nextColor);
Pair<List<StoneSet>, Integer> result, bestResult; SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
bestResult = new Pair<List<StoneSet>, Integer>( new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0); Collections.<StoneSet> emptyList(), 0));
try {
if (nextColor != null) { if (nextColor != null) {
if (searchState.stoneCounts.containsKey(nextStone)) { List<StoneColor> newColors = new ArrayList<StoneColor>(
decrementStoneCount(searchState.stoneCounts, nextStone); chosenColors);
List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
newColors.add(nextColor); newColors.add(nextColor);
result = findGroupsWithTotalPoints(new SearchState( if (searchState.stoneCounts.containsKey(nextStone)) {
searchState.pointsMissing, searchState.stoneCounts, decrementStoneCount(searchState.stoneCounts, nextStone);
searchState.jokerCount), value, newColors, nextColor); searchHelper.checkResult(findGroupsWithTotalPoints(
if (result.getSecond() >= searchState.pointsMissing) searchState, value, newColors, nextColor));
return result; incrementStoneCount(searchState.stoneCounts, nextStone);
if (result.getSecond() > bestResult.getSecond()) }
bestResult = result; if (searchState.jokerCount > 0) {
incrementStoneCount(searchState.stoneCounts, nextStone); searchHelper.checkResult(findGroupsWithTotalPoints(
new SearchState(searchState.pointsMissing,
searchState.stoneCounts,
searchState.jokerCount - 1), value,
newColors, nextColor));
}
searchHelper.checkResult(findGroupsWithTotalPoints(searchState,
value, chosenColors, nextColor));
} }
if (searchState.jokerCount > 0) {
List<StoneColor> newColors = new ArrayList<StoneColor>(chosenColors);
newColors.add(nextColor);
result = findGroupsWithTotalPoints(new SearchState(
searchState.pointsMissing, searchState.stoneCounts,
searchState.jokerCount - 1), value, newColors, nextColor);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
}
result = findGroupsWithTotalPoints(new SearchState(
searchState.pointsMissing, searchState.stoneCounts,
searchState.jokerCount), value, chosenColors, nextColor);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
}
if (chosenColors.size() >= 3) { if (chosenColors.size() >= 3) {
int groupPoints = chosenColors.size() * value; searchHelper.checkResult(handleFoundGroup(searchState, value,
result = findSetsWithTotalPoints(searchState.pointsMissing - groupPoints, chosenColors));
searchState.stoneCounts, searchState.jokerCount);
List<Stone> newStones = new ArrayList<Stone>();
for (StoneColor color : chosenColors) {
newStones.add(new Stone(value, color));
} }
List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst()); } catch (EnoughPoints enoughPoints) {
newResultList.add(new StoneSet(newStones)); return enoughPoints.getResult();
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + groupPoints);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
return bestResult; return searchHelper.getBestResult();
}
private Pair<List<StoneSet>, Integer> handleFoundGroup(
SearchState searchState, int value, List<StoneColor> chosenColors) {
int groupPoints = chosenColors.size() * value;
Pair<List<StoneSet>, Integer> result = findSetsWithTotalPoints(
searchState.pointsMissing - groupPoints,
searchState.stoneCounts, searchState.jokerCount);
List<Stone> newStones = new ArrayList<Stone>();
for (StoneColor color : chosenColors) {
newStones.add(new Stone(value, color));
}
List<StoneSet> newResultList = new ArrayList<StoneSet>(
result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + groupPoints);
return result;
} }
private Pair<List<StoneSet>, Integer> findRunsWithTotalPoints( private Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
SearchState searchState, Pair<Integer, StoneColor> testedStone, SearchState searchState, Pair<Integer, StoneColor> testedStone,
int runLength) { int runLength) {
Pair<List<StoneSet>, Integer> result, bestResult; SearchHelper searchHelper = new SearchHelper(searchState.pointsMissing,
bestResult = new Pair<List<StoneSet>, Integer>( new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0); Collections.<StoneSet> emptyList(), 0));
try {
Pair<Integer, StoneColor> nextStone = null;
if (testedStone.getFirst() > 1) {
int nextValue = testedStone.getFirst() - 1;
nextStone = new Pair<Integer, StoneColor>(nextValue,
testedStone.getSecond());
Pair<Integer, StoneColor> nextStone = null; if (searchState.stoneCounts.containsKey(nextStone)) {
if (testedStone.getFirst() > 1) { decrementStoneCount(searchState.stoneCounts, nextStone);
int nextValue = testedStone.getFirst() - 1; searchHelper.checkResult(findRunsWithTotalPoints(
nextStone = new Pair<Integer, StoneColor>(nextValue, searchState, nextStone, runLength + 1));
testedStone.getSecond()); incrementStoneCount(searchState.stoneCounts, nextStone);
if (searchState.stoneCounts.containsKey(nextStone)) { }
decrementStoneCount(searchState.stoneCounts, nextStone); if (searchState.jokerCount > 0) {
result = findRunsWithTotalPoints(new SearchState( searchHelper.checkResult(findRunsWithTotalPoints(
searchState.pointsMissing, searchState.stoneCounts, new SearchState(searchState.pointsMissing,
searchState.jokerCount), nextStone, runLength + 1); searchState.stoneCounts,
if (result.getSecond() >= searchState.pointsMissing) searchState.jokerCount - 1), nextStone,
return result; runLength + 1));
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
incrementStoneCount(searchState.stoneCounts, nextStone);
}
} }
if (searchState.jokerCount > 0) {
result = findRunsWithTotalPoints(new SearchState( if (runLength >= 3) {
searchState.pointsMissing, searchState.stoneCounts, searchHelper.checkResult(handleFoundRun(searchState,
searchState.jokerCount - 1), nextStone, runLength + 1); testedStone, runLength));
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
} catch (EnoughPoints enoughPoints) {
return enoughPoints.getResult();
} }
return searchHelper.getBestResult();
}
if (runLength >= 3) { private Pair<List<StoneSet>, Integer> handleFoundRun(
SearchState searchState, Pair<Integer, StoneColor> testedStone,
int runLength) {
int below = testedStone.getFirst() - 1;
int high = below + runLength;
int runPoints = (high * (high + 1)) / 2 - (below * (below + 1)) / 2;
int below = testedStone.getFirst() - 1; Pair<List<StoneSet>, Integer> result = findSetsWithTotalPoints(
int high = below + runLength; searchState.pointsMissing - runPoints, searchState.stoneCounts,
int runPoints = (high * (high + 1)) / 2 - (below * (below + 1)) / 2; searchState.jokerCount);
result = findSetsWithTotalPoints(searchState.pointsMissing - runPoints, List<Stone> newStones = new ArrayList<Stone>();
searchState.stoneCounts, searchState.jokerCount); for (int i = 0; i < runLength; i++) {
newStones.add(new Stone(i + testedStone.getFirst(), testedStone
List<Stone> newStones = new ArrayList<Stone>(); .getSecond()));
for (int i = 0; i < runLength; i++) {
newStones.add(new Stone(i + testedStone.getFirst(), testedStone
.getSecond()));
}
List<StoneSet> newResultList = new ArrayList<StoneSet>(result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + runPoints);
if (result.getSecond() >= searchState.pointsMissing)
return result;
if (result.getSecond() > bestResult.getSecond())
bestResult = result;
} }
return bestResult; List<StoneSet> newResultList = new ArrayList<StoneSet>(
result.getFirst());
newResultList.add(new StoneSet(newStones));
result = new Pair<List<StoneSet>, Integer>(newResultList,
result.getSecond() + runPoints);
return result;
} }
static void incrementStoneCount( static void incrementStoneCount(
@ -302,7 +326,7 @@ public class AIUtil {
* Counts the numbers of stones * Counts the numbers of stones
* *
* @param stones * @param stones
* the stones to count * the stones to count
* @return the numbers for all stones * @return the numbers for all stones
*/ */
public static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones( public static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones(