[Polycasso-commit] SF.net SVN: polycasso:[58] trunk/polycasso/src/com/mebigfatguy/polycasso/ Improv
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-11-26 06:30:43
|
Revision: 58
http://polycasso.svn.sourceforge.net/polycasso/?rev=58&view=rev
Author: dbrosius
Date: 2009-11-26 06:30:31 +0000 (Thu, 26 Nov 2009)
Log Message:
-----------
a class to maintain success/failure statistics per improvement type, so that 'random' selection of new improvement types can increase the probability of success, by favoring types that are higher successes
Added Paths:
-----------
trunk/polycasso/src/com/mebigfatguy/polycasso/ImprovementTypeStats.java
Added: trunk/polycasso/src/com/mebigfatguy/polycasso/ImprovementTypeStats.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/ImprovementTypeStats.java (rev 0)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/ImprovementTypeStats.java 2009-11-26 06:30:31 UTC (rev 58)
@@ -0,0 +1,102 @@
+/*
+ * polycasso - Cubism Artwork generator
+ * Copyright 2009 MeBigFatGuy.com
+ * Copyright 2009 Dave Brosius
+ * Inspired by work by Roger Alsing
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package com.mebigfatguy.polycasso;
+
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * a class to keep track of success statistics by improvement type, in order to further tune what
+ * improvement types to try in the future. It modifies a purely random selection criteria to one that
+ * is tuned by performance.
+ */
+public class ImprovementTypeStats {
+
+ private static class Stats {
+ public int successes = 1;
+ public int totals = 1;
+ public double pct = 0.0;
+
+ @Override
+ public String toString() {
+ return successes + "/" + totals + " [" + pct + "]";
+ }
+ }
+
+ private Map<ImprovementType, Stats> typeStats = new EnumMap<ImprovementType, Stats>(ImprovementType.class);
+ private Random r = new Random();
+
+ /**
+ * creates an initial state of statistics
+ */
+ public ImprovementTypeStats() {
+ initStats();
+ }
+
+ /**
+ * increment a type's statistics whether success or fail
+ *
+ * @param type the improvement type that is to be updated
+ * @param successful if the improvement was successful
+ */
+ public void typeWasSuccessful(ImprovementType type, boolean successful) {
+ Stats stats = typeStats.get(type);
+ if (successful)
+ stats.successes++;
+ stats.totals++;
+ }
+
+ /**
+ * returns a random improvement type that is influenced by how successful the types
+ * have been in the past.
+ *
+ * @returns the improvement type to try
+ */
+ public ImprovementType getRandomImprovementType() {
+ double pct = r.nextDouble();
+
+ double totalPct = 0.0;
+ for (Stats stat : typeStats.values()) {
+ stat.pct = ((double) stat.successes) / ((double) stat.totals);
+ totalPct += stat.pct;
+ }
+
+ for (Map.Entry<ImprovementType, Stats> entry : typeStats.entrySet()) {
+ Stats stat = entry.getValue();
+ double typePct = stat.pct / totalPct;
+ if (pct <= typePct)
+ return entry.getKey();
+
+ pct -= typePct;
+ }
+
+ return ImprovementType.CompleteChange;
+ }
+
+ /**
+ * sets the stats to an initial state
+ */
+ private void initStats() {
+ ImprovementType[] values = ImprovementType.values();
+ for (ImprovementType type : values) {
+ typeStats.put(type, new Stats());
+ }
+ }
+}
Property changes on: trunk/polycasso/src/com/mebigfatguy/polycasso/ImprovementTypeStats.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|