Created
September 14, 2010 22:45
-
-
Save jbn/579915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Classifier{ | |
public static boolean[] stringToBoolean(String s){ | |
boolean[] b = new boolean[s.length()]; | |
for(int i=0; i<s.length(); ++i){ | |
b[i] = (s.charAt(i) == '1'); | |
} | |
return b; | |
} | |
public static void main(String[] args){ | |
boolean[] testA = stringToBoolean("10011001"); | |
boolean[] testB = stringToBoolean(""); | |
Classifier classifier = new Classifier( | |
stringToBoolean("10011001"), | |
stringToBoolean("01111000") | |
); | |
System.out.println(classifier.probabilityOfRetaliation); | |
System.out.println(classifier.probabilityOfForgiveness); | |
classifier = new Classifier( | |
stringToBoolean("00000000"), | |
stringToBoolean("11111111") | |
); | |
System.out.println(classifier.probabilityOfRetaliation); | |
System.out.println(classifier.probabilityOfForgiveness); | |
} | |
public final double probabilityOfRetaliation, | |
probabilityOfForgiveness; | |
/** | |
Creates a classification of a after a game of b | |
*/ | |
public Classifier(boolean[] a, boolean[] b){ | |
double slights=0, retaliations=0, forgivens=0; | |
for(int i=2; i<a.length; ++i){ | |
// a cooperated then b defected. | |
if(a[i-2] && b[i]){ | |
slights++; | |
if(a[i]){ | |
forgivens++; | |
}else{ | |
retaliations++; | |
} | |
} | |
} | |
probabilityOfForgiveness = forgivens/slights; | |
probabilityOfRetaliation = retaliations/slights; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment