Search code examples
javaarrayssumleast-squarespattern-recognition

Least Regression Pattern Recognition Sum Error


I am working on a project that requires a pattern recognition program. I will receive a data file with 50000+ data points, and have to recognize if a certain pattern is present. If the sum of the square regression is above a certain value, I have the pattern, otherwise, I keep cycling to the end of the of the file. However, at around datapoint 1000 ~ 3500, the sum settles and does not change. I cannot figure out why it won't change.

public class Recognition {
    private static final double[] pattern = {102.0909091,
        ...
            -102};
    private static double[] temp = new double[161];
    private static int[] sums;
    private static final int threshold = 2107270;
    private static Scanner reader;
    private static PrintWriter writer;

    public static int[] rec(int[] array) {
        sums = new int[array.length];
        int[] solution = array;
        int sum = 0;
        for (int x = 0; x < 161; x++) {
            temp[x] = Math.pow((array[x] - pattern[x]), 2);
            solution[x] = 0;
            sums[x] = sumArray();
        }
        loop:
            for (int x = 161; x < array.length; x++) {
                sum = sumArray();
                if (sum > threshold) {
                    solution[x] = 1;
                    cycleArray();
                    sums[x] = sum;
                    continue loop;
                }
                sums[x] = sum;
                solution[x] = 0;
                cycleArray();
                temp[0] = Math.pow((array[x] - pattern[0]), 2);
            }
        return solution;
    }

    private static int sumArray() {
        int sum = 0;
        for (int x = 0; x < temp.length; x++) {
            sum += temp[x];
        }
        return sum;
    }

    private static void cycleArray() {
        for (int x = (temp.length - 1); x > 0; x--) {
            temp[x] = temp[x - 1];
        }
    }

    public static void main(String[] args0) throws FileNotFoundException {
        reader = new Scanner(new File("data1.txt");
        writer = new PrintWriter(new File("pattern.txt"));
        int[] data = new int[50000];
        int x = 0;
        while (reader.hasNext()) {
            data[x] = reader.nextInt();
            x++;
        }
        int[] solutions = rec(data);
        for (int y = 0; y < solutions.length; y++) {
            writer.printf("%d: %d, Running Sum: %d\n", y + 1, solutions[y], sums[y]);
            System.out.println(solutions[y]);
        }
    }
}

For clarification, the pattern length is 161 integers. If the pattern is recognized, then a 1 is outputted, otherwise a 0 is outputted. Any and all help is appreciated.


Solution

  • Here's my whole idea of it, and I dont get stuck except when i hit the max value

    public class PatternRecognition {
    
    private  double[] pattern = new double[161];
    private double[] temp = new double[161];
    private  int[] sums;
    private static final int threshold = 2107270;
    
    public int[] rec(int[] array) {
        sums = new int[array.length];
        int[] solution = array;
        int sum = 0;
        for (int x = 0; x < 161; x++) {
            temp[x] = Math.pow((array[x] - pattern[x]), 2);
            solution[x] = 0;
            sums[x] = sumArray(temp);
        }
            for (int x = 0; x < array.length-temp.length; x++) {
        for (int x2 =0; x2<temp.length ; x2++)
            temp[x2] = Math.pow((array[x+x2] - pattern[x2]), 2);
                sum = sumArray(temp);
                if (sum > threshold)
                    solution[x] = 1;
                else solution[x] = 0;
                sums[x] = sum;
            }
        return solution;
    }
    
    private int sumArray(double[] temp) {
        int sum = 0;
        for (int x = 0; x < temp.length; x++) {
            sum += temp[x];
        }
        return sum;
    }
    
    private void cycleArray() {
        for (int x = (temp.length - 1); x > 0; x--) {
            temp[x] = temp[x - 1];
        }
    }
    
    public static void main(String[] args0) {
    int[] data = new int[50000];
    int x = 0;
    while (x<data.length) {
        data[x] = x/2;
        x++;
    }
    PatternRecognition p=new PatternRecognition();
    x=0;
    while (x<p.pattern.length) {
        p.pattern[x] = x+1;
        x++;
    }
    int[] solutions = p.rec(data);
    for (int y = 0; y < solutions.length; y++) {
        System.out.printf("%d: %d, Running Sum: %d %d\n", y + 1, solutions[y], p.sums[y], Integer.MAX_VALUE);
        System.out.println(solutions[y]);
    }
    }
    
    }