Skip to content

Commit

Permalink
Added more comments and did some restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Aug 31, 2014
1 parent f41117c commit 7e3438d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
Expand Up @@ -40,6 +40,7 @@ public FieldProxy<T> getProxyFor(T field) {
double totalCombinations = analyze.getTotal();
Map<GroupValues<T>, FieldProxy<T>> bufferedValues = new HashMap<GroupValues<T>, FieldProxy<T>>();
for (FieldProxy<T> proxy : proxies.values()) {
// Check if it is possible to re-use a previous value
FieldProxy<T> bufferedValue = bufferedValues.get(proxy.getNeighbors());
if (bufferedValue != null && bufferedValue.getFieldGroup() == proxy.getFieldGroup()) {
proxy.copyFromOther(bufferedValue, totalCombinations);
Expand Down
69 changes: 45 additions & 24 deletions src/main/java/net/zomis/minesweeper/analyze/detail/FieldProxy.java
Expand Up @@ -32,27 +32,44 @@ public FieldProxy(FieldGroup<T> group, T field) {
}

void addSolution(Solution<T> solution) {
solution = solution.copyWithoutNCRData();

recursiveRemove(solution, 1, 0);
recursiveRemove(solution.copyWithoutNCRData(), 1, 0);
}

/**
* This field has the same values as another field, copy the values.
*
* @param copyFrom {@link FieldProxy} to copy from
* @param analyzeTotal Total number of combinations
*/
void copyFromOther(FieldProxy<T> copyFrom, double analyzeTotal) {
for (int i = 0; i < this.detailedCombinations.length - this.found; i++) {
if (copyFrom.detailedCombinations.length <= i + copyFrom.found) break;
if (copyFrom.detailedCombinations.length <= i + copyFrom.found) {
break;
}
this.detailedCombinations[i + this.found] = copyFrom.detailedCombinations[i + copyFrom.found];
}

this.finalCalculation(analyzeTotal);
}

/**
* Calculate final probabilities from the combinations information
*
* @param analyzeTotal Total number of combinations
*/
void finalCalculation(double analyzeTotal) {
this.detailedProbabilities = new double[this.detailedCombinations.length];
for (int i = 0; i < this.detailedProbabilities.length; i++) {
this.detailedProbabilities[i] = this.detailedCombinations[i] / analyzeTotal;
}
}

/**
* Setup the neighbors for this field
*
* @param neighborStrategy {@link NeighborFind} strategy
* @param proxyProvider Interface to get the related proxies
*/
void fixNeighbors(NeighborFind<T> neighborStrategy, ProxyProvider<T> proxyProvider) {
Collection<T> realNeighbors = neighborStrategy.getNeighborsFor(field);
this.detailedCombinations = new double[realNeighbors.size() + 1];
Expand All @@ -74,15 +91,14 @@ void fixNeighbors(NeighborFind<T> neighborStrategy, ProxyProvider<T> proxyProvid
continue;
}

Integer getValue = neighbors.get(neighborGroup);

if (getValue == null) {
// Increase the number of neighbors
Integer currentNeighborAmount = neighbors.get(neighborGroup);
if (currentNeighborAmount == null) {
neighbors.put(neighborGroup, 1);
}
else neighbors.put(neighborGroup, getValue + 1);
else neighbors.put(neighborGroup, currentNeighborAmount + 1);
}
}

}

@Override
Expand Down Expand Up @@ -120,34 +136,39 @@ private void recursiveRemove(Solution<T> solution, double combinations, int mine
throw new RuntimeTimeoutException();
}

// Check if there are more field groups with values
GroupValues<T> remaining = solution.getSetGroupValues();
if (remaining.isEmpty()) { // or if combinations equals zero ?
if (remaining.isEmpty()) {
// TODO: or if combinations equals zero ?
this.detailedCombinations[mines + this.found] += combinations;
return;
}

Entry<FieldGroup<T>, Integer> ee = remaining.entrySet().iterator().next();
FieldGroup<T> group = ee.getKey();


int N = ee.getKey().size();
int n = ee.getValue();
Integer K = this.neighbors.get(group);
// Get the first assignment
Entry<FieldGroup<T>, Integer> fieldGroupAssignment = remaining.entrySet().iterator().next();
FieldGroup<T> group = fieldGroupAssignment.getKey();
remaining.remove(group);
solution = Solution.createSolution(remaining);

if (this.group == group) N--;
// Setup values for the hypergeometric distribution calculation. See http://en.wikipedia.org/wiki/Hypergeometric_distribution
int N = group.size();
int n = fieldGroupAssignment.getValue();
Integer K = this.neighbors.get(group);
if (this.group == group) {
N--; // Always exclude self becuase you can't be neighbor to yourself
}

if (K == null) {
// This field does not have any neighbors to that group.
recursiveRemove(solution, combinations * Combinatorics.nCr(N, n), mines);
return;
}
else {
int maxLoop = Math.min(K, n);
for (int k = minK(N, K, n); k <= maxLoop; k++) {
double thisCombinations = Combinatorics.NNKK(N, n, K, k);
recursiveRemove(solution, combinations * thisCombinations, mines + k);
}

// Calculate the values and then calculate for the next group
int maxLoop = Math.min(K, n);
for (int k = minK(N, K, n); k <= maxLoop; k++) {
double thisCombinations = Combinatorics.NNKK(N, n, K, k);
recursiveRemove(solution, combinations * thisCombinations, mines + k);
}
}

Expand Down

0 comments on commit 7e3438d

Please sign in to comment.