The solution to any problem, the answer to any question, the meaning of life, universe and everything depends solely on our perception.
1+1=?
11 to a writer
2 to a mathematician
10 to a programmer
0 to a scientist
1 to a lover
pointless to a philosopher
profit to a businessman
symbols to a baby
3-1 to a teacher
the root of the whole problem to a politician
Frisbee time to a dog
waste of time, electricity, graphite, paper and human innovation to an environmentalist
whatever you say it means to the ignorant
2-14-2009
Showing posts with label Pattern Analysis. Show all posts
Showing posts with label Pattern Analysis. Show all posts
Saturday, April 11, 2009
Perception
Labels:
1,
AI,
Answer,
Everything,
Life,
Meaning,
Pattern Analysis,
Perception,
Poetry,
Problem,
Question,
Solution,
Universe,
Writing
Friday, January 2, 2009
Controlling Chaos
The Butterfly effect, one aspect of Chaos Theory, says "a butterfly's wings might create tiny changes in the atmosphere that may ultimately alter the path of a tornado or delay, accelerate or even prevent the occurrence of a tornado in a certain location." (http://en.wikipedia.org/wiki/Butterfly_effect)
The effect of small details on the resulting answer causes a huge problem of uncertanity and a huge margin of error that limit the prediction capabilities of anys simulation to just a few iterations. Take for example, the equation n = n^2 with a starting n value of 2
2, 4,16,256,65536,4294967296...
If the starting value was to n = 1.99, the sequence would be like...
1.99, 3.9601, 15.68239201, 245.9374192, 60485.21414, 3658461130....
See how that small change resulted in a very large difference??
Suppose that this calculation was conducted as an experiment. We knew the initial value to be something close to 2. After a few "turns" the value was measured to be 60485. I picked a number a bit far from the initial turn so that the change was measurable. Using these 3 pieces of information, 60485, n = n^2, turn = 5, we can find the initial value by simply changing the equation to n = squareroot(n). Using this, we get the sequence
60485, 245.9369838, 15.68237813, 3.960098247, 1.98999956
Ofcourse, there is the problem of loosing data while backtrackign (more chaos....). I'd suggest finding the midpoint between the measured initial value and the calculated initial value to find a better estimation of the initial value.
(2+1.98999956)/2 = 1.99499978.
If we were to use this newly computed value as the starting point, the sequence would look like:
1.99499978, 3.980024122, 15.84059201, 250.9243552, 62963.03202, 3964343401....
Looking back at all three of the sequences and the benefit of this method:
Actual Values: 1.99, 3.9601, 15.68239201, 245.9374192, 60485.21414, 3658461130
Rounded Values: 2, 4, 16, 256, 65536, 4294967296
Re-Adjusted Calculations: 1.99499978, 3.980024122, 15.84059201, 250.9243552, 62963.03202, 3964343401
The difference: (Calculated - Actual value)
Rounded Value: 4294967296 - 3658461130 = 636506166
Re-Adjusted Calculations: 3964343401 - 3658461130= 305882271
The result that readjusted and recalculated for the initial value was 2X as close to the actual value as the one that didn't. Continually readjusting the initial value and re-calculating will, yes, be very process intensive for large sequences. It'd actually make more sense to simply re-start the calculation with the current point in time as the initial value, but tracking back like this will allow you to adjust the measurements to account for the measurement error. In the above example, it was possible to calculate that the difference was +/- 0.01 by subtracting 1.99499978 from 2. This error can then be plugged into the calculations to adjust for the measurement error.
The effect of small details on the resulting answer causes a huge problem of uncertanity and a huge margin of error that limit the prediction capabilities of anys simulation to just a few iterations. Take for example, the equation n = n^2 with a starting n value of 2
2, 4,16,256,65536,4294967296...
If the starting value was to n = 1.99, the sequence would be like...
1.99, 3.9601, 15.68239201, 245.9374192, 60485.21414, 3658461130....
See how that small change resulted in a very large difference??
Suppose that this calculation was conducted as an experiment. We knew the initial value to be something close to 2. After a few "turns" the value was measured to be 60485. I picked a number a bit far from the initial turn so that the change was measurable. Using these 3 pieces of information, 60485, n = n^2, turn = 5, we can find the initial value by simply changing the equation to n = squareroot(n). Using this, we get the sequence
60485, 245.9369838, 15.68237813, 3.960098247, 1.98999956
Ofcourse, there is the problem of loosing data while backtrackign (more chaos....). I'd suggest finding the midpoint between the measured initial value and the calculated initial value to find a better estimation of the initial value.
(2+1.98999956)/2 = 1.99499978.
If we were to use this newly computed value as the starting point, the sequence would look like:
1.99499978, 3.980024122, 15.84059201, 250.9243552, 62963.03202, 3964343401....
Looking back at all three of the sequences and the benefit of this method:
Actual Values: 1.99, 3.9601, 15.68239201, 245.9374192, 60485.21414, 3658461130
Rounded Values: 2, 4, 16, 256, 65536, 4294967296
Re-Adjusted Calculations: 1.99499978, 3.980024122, 15.84059201, 250.9243552, 62963.03202, 3964343401
The difference: (Calculated - Actual value)
Rounded Value: 4294967296 - 3658461130 = 636506166
Re-Adjusted Calculations: 3964343401 - 3658461130= 305882271
The result that readjusted and recalculated for the initial value was 2X as close to the actual value as the one that didn't. Continually readjusting the initial value and re-calculating will, yes, be very process intensive for large sequences. It'd actually make more sense to simply re-start the calculation with the current point in time as the initial value, but tracking back like this will allow you to adjust the measurements to account for the measurement error. In the above example, it was possible to calculate that the difference was +/- 0.01 by subtracting 1.99499978 from 2. This error can then be plugged into the calculations to adjust for the measurement error.
Monday, August 25, 2008
Pattern Analysis
This is a basic Pattern Analysis program I wrote. It's just version 0.03 so it's capabilities are very limited. Sorry for the formatting :(
Usage: Just type in one number at a time, pressing enter after each number. Type ? to calculate the next item in the list. The program clear it's memory and start over with a new sequence.
Capabilities: I can find patterns such as:
1,1,1,1,1,? (1)
1,2,3,? (4)
1,4,9,16,25,36,49,64,81,? (100) Square of each number found using spacing pattern.
7,19,37,61,91(127)
1,8,27,64,125,216(343) Cube of each number
Right now, It's fairly bad at exponents and it cant figure out the Fibonacci sequence either :(
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class PatternRecognition{
public static void main(String args[])
{
System.out.println("AI Pattern Analysis Module. V 0.03");
while(true){
ArrayList intSequence = new ArrayList();
String str = getInput();
while(!str.equals("?")){
int temp = (Integer) Integer.valueOf(str);
intSequence.add(temp);
str = getInput();
}
System.out.println("Next number will be "+nextInt(intSequence));
}
}
public static String getInput()
{
Scanner input = new Scanner(System.in);
System.out.print("# :");
String inputString = input.nextLine();
return inputString;
}
public static ArrayList nextInt(ArrayList list){
ArrayList nextInt = new ArrayList();
ArrayList spaces = getSpaces(list);
if(linear(spaces)){
nextInt.add(getNextLinear(list));
System.out.println("Linear pattern found");
}
if(true){
boolean done = false;
int counter = 0;
int lastAdd = 0;
while(!done){
counter++;
if(linear(spaces)){
done = true;
//return getNextLinear(list,spaces);
lastAdd+=(Integer) spaces.get(spaces.size()-1);
int lastInList = (Integer) list.get(list.size()-1);
nextInt.add(lastInList+=lastAdd);
System.out.println("Spacing pattern found");
}
else{
lastAdd+=(Integer) spaces.get(spaces.size()-1);
ArrayList sp2 = copyArray(spaces);
spaces = getSpaces(sp2);
}
}
}
int sequenceWidth = repeatingCheck(list);
if(sequenceWidth > 0){
// System.out.println("Sequence repeats every "+sequenceWidth+" digits.");
nextInt.add(getNextRepeating(list,sequenceWidth));
System.out.println("Sequential pattern found");
}
return nextInt;
}
public static ArrayList copyArray(ArrayList list){
ArrayList rtrn = new ArrayList();
for(int k = 0; k < list.size(); k++){
rtrn.add(list.get(k));
}
return rtrn;
}
public static ArrayList getSpaces(ArrayList list){
ArrayList spaces = new ArrayList();
int previous = 0;
for(int k = 0; k < list.size(); k++){
int i = (Integer) list.get(k);
if(k == 0){ previous = i;} // Initialize previous
else{
spaces.add(i-previous);
previous = i;
}
}
return spaces;
}
// Checks if it's a set of evenly spaced numbers
public static boolean linear(ArrayList list){
if(list.size() >= 1){
int reference = (Integer) list.get(0); // Initialize a default value for space
for(int k = 1; k < list.size(); k++){ // starts at 1 because 0 is init value
int i = (Integer) list.get(k);
if(i != reference){ return false;}
}
}
return true;
}
public static int getNextLinear(ArrayList list){
if(list.size() >= 2){
int i1 = (Integer) list.get(0);
int i2 = (Integer) list.get(1);
int difference = i2-i1;
int rtrn = (Integer) list.get(list.size()-1);
rtrn += difference;
return rtrn ;
}
else if(list.size() >= 1){return (Integer)list.get(0);}
else{return 0; }
}
public static int repeatingCheck(ArrayList list){
ArrayList sequence = new ArrayList();
boolean repeating = false;
boolean oneRepeat = true;
sequence.add(list.get(0));
for(int k = 1; k < list.size()-1; k++){
if(repeating(list,sequence)){
return k;
}
else{
sequence.add(list.get(k));
}
}
return 0;
}
public static boolean repeating(ArrayList list, ArrayList sequence){
int k2 = 0;
for(int k = 0; k < list.size(); k++){
int l = (Integer) list.get(k);
int s = (Integer) sequence.get(k2);
k2++;
if(k2 > sequence.size() -1){ k2=0; }
if(l != s){ return false; }
}
return true;
}
public static int getNextRepeating(ArrayList list, int sequenceWidth){
if(list.size() > sequenceWidth){
ArrayList sequence = new ArrayList();
for(int k = 0; k < sequenceWidth; k++){
sequence.add(list.get(k));
}
int k2 = 0;
for(int k = 0; k < list.size(); k++){
int l = (Integer) list.get(k);
int s = (Integer) sequence.get(k2);
k2++;
if(k2 > sequence.size() -1){ k2=0; }
}
return (Integer)sequence.get(k2);
}
return 0;
}
}
How can I improve this???
Usage: Just type in one number at a time, pressing enter after each number. Type ? to calculate the next item in the list. The program clear it's memory and start over with a new sequence.
Capabilities: I can find patterns such as:
1,1,1,1,1,? (1)
1,2,3,? (4)
1,4,9,16,25,36,49,64,81,? (100) Square of each number found using spacing pattern.
7,19,37,61,91(127)
1,8,27,64,125,216(343) Cube of each number
Right now, It's fairly bad at exponents and it cant figure out the Fibonacci sequence either :(
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class PatternRecognition{
public static void main(String args[])
{
System.out.println("AI Pattern Analysis Module. V 0.03");
while(true){
ArrayList intSequence = new ArrayList();
String str = getInput();
while(!str.equals("?")){
int temp = (Integer) Integer.valueOf(str);
intSequence.add(temp);
str = getInput();
}
System.out.println("Next number will be "+nextInt(intSequence));
}
}
public static String getInput()
{
Scanner input = new Scanner(System.in);
System.out.print("# :");
String inputString = input.nextLine();
return inputString;
}
public static ArrayList nextInt(ArrayList list){
ArrayList nextInt = new ArrayList();
ArrayList spaces = getSpaces(list);
if(linear(spaces)){
nextInt.add(getNextLinear(list));
System.out.println("Linear pattern found");
}
if(true){
boolean done = false;
int counter = 0;
int lastAdd = 0;
while(!done){
counter++;
if(linear(spaces)){
done = true;
//return getNextLinear(list,spaces);
lastAdd+=(Integer) spaces.get(spaces.size()-1);
int lastInList = (Integer) list.get(list.size()-1);
nextInt.add(lastInList+=lastAdd);
System.out.println("Spacing pattern found");
}
else{
lastAdd+=(Integer) spaces.get(spaces.size()-1);
ArrayList sp2 = copyArray(spaces);
spaces = getSpaces(sp2);
}
}
}
int sequenceWidth = repeatingCheck(list);
if(sequenceWidth > 0){
// System.out.println("Sequence repeats every "+sequenceWidth+" digits.");
nextInt.add(getNextRepeating(list,sequenceWidth));
System.out.println("Sequential pattern found");
}
return nextInt;
}
public static ArrayList copyArray(ArrayList list){
ArrayList rtrn = new ArrayList();
for(int k = 0; k < list.size(); k++){
rtrn.add(list.get(k));
}
return rtrn;
}
public static ArrayList getSpaces(ArrayList list){
ArrayList spaces = new ArrayList();
int previous = 0;
for(int k = 0; k < list.size(); k++){
int i = (Integer) list.get(k);
if(k == 0){ previous = i;} // Initialize previous
else{
spaces.add(i-previous);
previous = i;
}
}
return spaces;
}
// Checks if it's a set of evenly spaced numbers
public static boolean linear(ArrayList list){
if(list.size() >= 1){
int reference = (Integer) list.get(0); // Initialize a default value for space
for(int k = 1; k < list.size(); k++){ // starts at 1 because 0 is init value
int i = (Integer) list.get(k);
if(i != reference){ return false;}
}
}
return true;
}
public static int getNextLinear(ArrayList list){
if(list.size() >= 2){
int i1 = (Integer) list.get(0);
int i2 = (Integer) list.get(1);
int difference = i2-i1;
int rtrn = (Integer) list.get(list.size()-1);
rtrn += difference;
return rtrn ;
}
else if(list.size() >= 1){return (Integer)list.get(0);}
else{return 0; }
}
public static int repeatingCheck(ArrayList list){
ArrayList sequence = new ArrayList();
boolean repeating = false;
boolean oneRepeat = true;
sequence.add(list.get(0));
for(int k = 1; k < list.size()-1; k++){
if(repeating(list,sequence)){
return k;
}
else{
sequence.add(list.get(k));
}
}
return 0;
}
public static boolean repeating(ArrayList list, ArrayList sequence){
int k2 = 0;
for(int k = 0; k < list.size(); k++){
int l = (Integer) list.get(k);
int s = (Integer) sequence.get(k2);
k2++;
if(k2 > sequence.size() -1){ k2=0; }
if(l != s){ return false; }
}
return true;
}
public static int getNextRepeating(ArrayList list, int sequenceWidth){
if(list.size() > sequenceWidth){
ArrayList sequence = new ArrayList();
for(int k = 0; k < sequenceWidth; k++){
sequence.add(list.get(k));
}
int k2 = 0;
for(int k = 0; k < list.size(); k++){
int l = (Integer) list.get(k);
int s = (Integer) sequence.get(k2);
k2++;
if(k2 > sequence.size() -1){ k2=0; }
}
return (Integer)sequence.get(k2);
}
return 0;
}
}
How can I improve this???
Subscribe to:
Posts (Atom)