package figures;

import java.io.File;

import jimena.binaryrn.RegulatoryNetwork;
import jimena.calculationparameters.SSSearchParameters;
import jimena.simulation.StableSteadyState;
import jimena.simulationmethods.BooleCubeMethod;

public class PlotConvergence {

    /**
     * @param args
     */
    public static void main(String[] args) {
        RegulatoryNetwork network1 = new RegulatoryNetwork();
        network1.loadYEdFile(new File("I:\\Controllability\\FigureConvergence\\net1.graphml"));

        RegulatoryNetwork network2 = new RegulatoryNetwork();
        network2.loadYEdFile(new File("I:\\Controllability\\FigureConvergence\\net5.graphml"));

        // SSSearchParameters parameters = new StandardSSSSearchParameters(new SQUADMethod(), 0.04);
        SSSearchParameters parameters = new SSSearchParameters(new BooleCubeMethod(), 0.02, 2000, null, 1e-6, 2, 8, 1e-5);

        // TODO Auto-generated method stub
        int resolution = 99;

        double[][][] convergence1 = new double[resolution + 1][resolution + 1][2];
        for (int x1 = 0; x1 <= resolution; x1++) {
            double x1d = ((double) x1) / resolution;
            for (int x2 = 0; x2 <= resolution; x2++) {
                double x2d = ((double) x2) / resolution;
                network1.setValues(new double[] { x1d, x2d });
                StableSteadyState n = new StableSteadyState(network1, parameters);
                n.run();
                convergence1[x1][x2] = n.getResult();
            }
        }

        plot(convergence1);
        System.out.println();

        double[][][] convergence2 = new double[resolution + 1][resolution + 1][2];
        for (int x1 = 0; x1 <= resolution; x1++) {
            double x1d = ((double) x1) / resolution;
            for (int x2 = 0; x2 <= resolution; x2++) {
                double x2d = ((double) x2) / resolution;
                network2.setValues(new double[] { x1d, x2d });
                StableSteadyState n = new StableSteadyState(network2, parameters);
                n.run();
                convergence2[x1][x2] = n.getResult();
            }
        }

        plot(convergence2);
        System.out.println();

        double[][] convergenceDiff = new double[resolution + 1][resolution + 1];
        for (int x1 = 0; x1 <= resolution; x1++) {
            for (int x2 = 0; x2 <= resolution; x2++) {
                convergenceDiff[x1][x2] = ((convergence1[x1][x2][0] - convergence2[x1][x2][0])
                        * (convergence1[x1][x2][0] - convergence2[x1][x2][0]) + (convergence1[x1][x2][1] - convergence2[x1][x2][1])
                        * (convergence1[x1][x2][1] - convergence2[x1][x2][1]))/2;
            }
        }

        plot(convergenceDiff);
        System.out.println();
        
        double maxDiff = 0;
        for (int x1 = 0; x1 <= resolution; x1++) {
            for (int x2 = 0; x2 <= resolution; x2++) {
                maxDiff = Math.max(maxDiff, convergenceDiff[x1][x2]);
            }
        }
        System.out.println(maxDiff);

    }

    public static void plot(double[][][] convergence) {
        System.out.print("{");
        for (int x1 = 0; x1 < convergence.length; x1++) {
            System.out.print("{");
            for (int x2 = 0; x2 < convergence[x1].length; x2++) {
                if (convergence[x1][x2] != null) {
                    System.out.print(color(convergence[x1][x2][0], convergence[x1][x2][1]));
                } else {
                    System.out.print("Yellow");
                }
                if (x2 != convergence.length - 1) {
                    System.out.print(",");
                }
            }
            System.out.print("}");
            if (x1 != convergence.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("}");
    }

    public static void plot(double[][] convergence) {
        System.out.print("{");
        for (int x1 = 0; x1 < convergence.length; x1++) {
            System.out.print("{");
            for (int x2 = 0; x2 < convergence[x1].length; x2++) {
                System.out.print(math(convergence[x1][x2]));
                if (x2 != convergence.length - 1) {
                    System.out.print(",");
                }
            }
            System.out.print("}");
            if (x1 != convergence.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("}");
    }

    public static void plotCSV(double[][][] convergence) {
        for (int x1 = 0; x1 < convergence.length; x1++) {
            for (int x2 = 0; x2 < convergence[x1].length; x2++) {
                if (convergence[x1][x2] != null) {
                    System.out.print(color(convergence[x1][x2][0], convergence[x1][x2][1]));
                } else {
                    System.out.print("Yellow");
                }
                if (x2 != convergence.length - 1) {
                    System.out.print(",");
                }
            }
            if (x1 != convergence.length - 1) {
                System.out.print("\n");
            }
        }
    }

    public static String math(double input) {
        return (String.valueOf(input)).replaceAll("E", "\\*^");
    }

    public static String color(double x1, double x2) {
        return "RGBColor[" + math(x1 * 0.7) + "," + math(x2 * 0.7) + ",0]";
    }
}
