PointAnalyser.Main.java Source code

Java tutorial

Introduction

Here is the source code for PointAnalyser.Main.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package PointAnalyser;

import static CalculiConfigurator.RepresentationOptimiser.writeToCSV;
import QRSweeper.QRSweeper;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NumericToNominal;
import weka.classifiers.lazy.IBk;
import weka.filters.unsupervised.instance.RemoveMisclassified;

/**
 *
 * @author jxy
 */
public class Main {

    public static Point2D.Float stringToPoint(String s) {
        String x_s = s.split(":")[0];
        String y_s = s.split(":")[1];
        int x = Integer.valueOf(x_s);
        int y = Integer.valueOf(y_s);

        return new Point2D.Float(x, y);
    }

    static J48 tree;
    static IBk nn;
    static ConverterUtils.DataSource source;
    static Instances data;

    public static void trainC45Classifier() throws Exception {

        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        NumericToNominal nmf = new NumericToNominal();
        nmf.setInputFormat(data);
        data = Filter.useFilter(data, nmf);

        // build a c4.5 classifier
        String[] options = new String[1];
        options[0] = "-C 0.25 -M 2 -U"; // unpruned tree
        tree = new J48(); // new instance of tree
        tree.setOptions(options); // set the options
        tree.buildClassifier(data); // build classifier
        /*
                 RemoveMisclassified rm = new RemoveMisclassified();
                 rm.setInputFormat(data);
                 rm.setClassifier(tree);
                 rm.setNumFolds(10);
                 rm.setThreshold(0.1);
                 rm.setMaxIterations(0);
                 data = Filter.useFilter(data, rm);
            
                 tree = new J48();         // new instance of tree
                 tree.setOptions(options);     // set the options
                 tree.buildClassifier(data);   // build classifier
                 */
        // eval
        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(tree, data, 10, new Random(1));

        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
        System.out.println(eval.toClassDetailsString());

    }

    public static J48 getC45Classifier() {
        return tree;
    }

    public static int classifyC45Instance(ArrayList<String> inst) throws Exception {

        //   ConverterUtils.DataSource source = new ConverterUtils.DataSource("Z:\\\\shared from vm\\\\fifthset\\\\newmixed.csv");
        // Instances data = source.getDataSet();
        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        //  if (data.classIndex() == -1) {
        //      data.setClassIndex(data.numAttributes() - 1);
        //  }
        NumericToNominal nmf = new NumericToNominal();
        nmf.setInputFormat(data);
        // data = Filter.useFilter(data, nmf);

        // build a c4.5 classifier
        // eval
        //  Evaluation eval = new Evaluation(data);
        //eval.crossValidateModel(tree, data, 10, new Random(1));
        //  System.out.println(eval.toSummaryString());
        //  System.out.println(eval.toMatrixString());
        // System.out.println(eval.toClassDetailsString());
        //tree.classifyInstance(null)
        Attribute a = new Attribute("ring(dragoon)");
        Attribute b = new Attribute("ring(zerg1)");
        Attribute c = new Attribute("ring(zerg2)");
        Attribute d = new Attribute("ring(zerg3)");

        ArrayList<String> classVal = new ArrayList<String>();
        classVal.add("safe");
        Attribute e = new Attribute("@@class@@", classVal);

        ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
        attributeList.add(a);
        attributeList.add(b);
        attributeList.add(c);
        attributeList.add(d);
        attributeList.add(e);

        Instances dataSet = new Instances("TestInstances", attributeList, 0);
        dataSet = Filter.useFilter(dataSet, nmf);
        dataSet.setClassIndex(dataSet.numAttributes() - 1);
        DenseInstance ins = new DenseInstance(dataSet.numAttributes());
        dataSet.add(ins);
        ins.setDataset(dataSet);
        ins.setValue(a, Integer.valueOf(inst.get(0)));
        ins.setValue(b, Integer.valueOf(inst.get(1)));
        ins.setValue(c, Integer.valueOf(inst.get(2)));
        ins.setValue(d, Integer.valueOf(inst.get(3)));

        return (int) tree.classifyInstance(ins);
    }

    public static void trainNNClassifier() throws Exception {

        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        NumericToNominal nmf = new NumericToNominal();
        nmf.setInputFormat(data);
        data = Filter.useFilter(data, nmf);

        // build a c4.5 classifier
        String[] options = new String[1];
        // options[0] = "-K 1";            // unpruned tree
        nn = new IBk(); // new instance of tree
        //  nn.setCrossValidate(true);
        nn.setKNN(7);
        nn.setNearestNeighbourSearchAlgorithm(new weka.core.neighboursearch.KDTree(data));

        nn.setWindowSize(0);

        // nn.setOptions(options);     // set the options
        nn.buildClassifier(data); // build classifier
        // eval
        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(nn, data, 10, new Random(1));

        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
        System.out.println(eval.toClassDetailsString());

    }

    public static int classifyNNInstance(ArrayList<String> inst) throws Exception {
        NumericToNominal nmf = new NumericToNominal();
        nmf.setInputFormat(data);

        Attribute a = new Attribute("ring(dragoon)");
        Attribute b = new Attribute("ring(zerg1)");
        Attribute c = new Attribute("ring(zerg2)");
        Attribute d = new Attribute("ring(zerg3)");

        ArrayList<String> classVal = new ArrayList<String>();
        classVal.add("safe");
        Attribute e = new Attribute("@@class@@", classVal);

        ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
        attributeList.add(a);
        attributeList.add(b);
        attributeList.add(c);
        attributeList.add(d);
        attributeList.add(e);

        Instances dataSet = new Instances("TestInstances", attributeList, 0);
        dataSet = Filter.useFilter(dataSet, nmf);
        dataSet.setClassIndex(dataSet.numAttributes() - 1);
        DenseInstance ins = new DenseInstance(dataSet.numAttributes());
        dataSet.add(ins);
        ins.setDataset(dataSet);
        ins.setValue(a, Integer.valueOf(inst.get(0)));
        ins.setValue(b, Integer.valueOf(inst.get(1)));
        ins.setValue(c, Integer.valueOf(inst.get(2)));
        ins.setValue(d, Integer.valueOf(inst.get(3)));

        return (int) nn.classifyInstance(ins);
    }

    public static void main(String[] args) throws Exception {
        int star = 45;
        int ring = 128;
        int starStart = 45;
        source = new ConverterUtils.DataSource("Z:\\\\shared from vm\\\\fifthset\\\\newmixed-edited.csv");
        data = source.getDataSet();
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        NumericToNominal nmf = new NumericToNominal();
        nmf.setInputFormat(data);
        data = Filter.useFilter(data, nmf);

        // SET UP CLASSIFIERS
        trainNNClassifier();
        trainC45Classifier();

        String temp = "Z:\\\\shared from vm\\\\thirdset\\\\out.csv";

        QRSweeper q = new QRSweeper();
        ArrayList<String> qrState = q.configure("Z:\\shared from vm\\thirdset\\rawdata.csv", star, ring, starStart);

        writeToCSV(temp, qrState, q.labels);
        //System.out.println(q.actionStream);
        // ok so now lets get all the points out of the action stream
        ArrayList<Point2D.Float> movePoints = new ArrayList<Point2D.Float>();
        int idx = 0;
        for (String s : q.actionStream) {
            if (s.contains(":")) {

                Point2D.Float dragoon = q.stateStream.get(idx).get(0);
                Point2D.Float click = stringToPoint(s);

                movePoints.add(new Point2D.Float(dragoon.x + click.x, dragoon.y + click.y));
                ///  System.out.println("mp: " + s);

            } else {

            }
            idx++;
        }

        // so now, given a qr representation, and a new point, what are the qr relations between the stuff and the new point?
        // find the frame where an action was picked
        // find out it's qsrs in that frame
        for (String s : q.labels) {
            System.out.print("ring:" + s + ",");
        }
        System.out.println("");
        int frame = 0;
        ArrayList<String> monitoredQSRs = new ArrayList<String>();

        // this goes through actionstream because we need to know what the val is
        // at a particular frame
        for (String s : q.actionStream) {
            if (s.contains(":")) {
                // figure out the QSRs of the click at this point, and just print them
                Point2D.Float click = stringToPoint(s);
                Point2D.Float dragoon = q.stateStream.get(frame).get(0);
                String st = q.getQSRStateOf(new Point2D.Float(dragoon.x + click.x, dragoon.y + click.y), frame,
                        ring, star, ",");
                // System.out.println(st);
                monitoredQSRs.add(st);
            }
            frame++;
        }
        //q.dumpSafePoints(monitoredQSRs, ring, star, 16);
        PointStreamVisualiser v = new PointStreamVisualiser(q, monitoredQSRs, ring, star);
        v.launch();

    }

}