package edu.claflin.finder.algo.clustering.struct.infomap_struct;

import edu.claflin.finder.algo.clustering.struct.louvain_struct.LouvainAlgorithm;
import edu.claflin.finder.io.graph.SimpleGraphIO;
import edu.claflin.finder.logic.Graph;
import edu.claflin.finder.logic.Node;

import java.io.File;
import java.util.List;
import java.util.Map;

public class Tester {
    /*public static void main(String[] args) {
        String fname = "nine_triangles.txt"; // two_triangles.txt nine_triangles.txt
        SimpleGraphIO sgio = new SimpleGraphIO();
        Graph sourceGraph = sgio.parseGraph(new File("./Test Graphs/" + fname), true);

        int trials = 0;
        double total = 0;
        for (int i = 0; i < 50; i++) {
            Graph g = sourceGraph.copy();
            LouvainAlgorithm louvain = new LouvainAlgorithm(g);
            AvgBitLength avgBitLength = new AvgBitLength(g, louvain.getCommunities());
            double b = avgBitLength.avgBitLength();
            if (!Double.isNaN(b)) {
                trials++;
                total += b;
            }
        }
        System.out.println("Average bit length: " + (total / trials));
    }*/

    public static void main(String[] args) {
        String fname = "two_triangles.txt"; // two_triangles.txt nine_triangles.txt
        SimpleGraphIO sgio = new SimpleGraphIO();

        Graph sourceGraph = sgio.parseGraph(new File("./Test Graphs/" + fname), true);
        Graph g;
        Map<Integer, List<Node>> modules;
        AvgBitLength avgBitLength;
        int trials;
        double total;
        final int NUM_TRIALS = 50;

        // BAD
        trials = 0;
        total = 0;
        for (int i = 0; i < NUM_TRIALS; i++) {
            g = sourceGraph.copy();
            modules = Map.of(
                    1, List.of(g.getNode("5"), g.getNode("6")),
                    2, List.of(g.getNode("4"), g.getNode("1")),
                    3, List.of(g.getNode("2"), g.getNode("3"))
            );
            avgBitLength = new AvgBitLength(g, modules);
            double b = avgBitLength.avgBitLength();
            if (!Double.isNaN(b)) {
                trials++;
                total += b;
            }
        }
        System.out.println("BAD Average bit length: " + (total / trials));

        // OK
        trials = 0;
        total = 0;
        for (int i = 0; i < NUM_TRIALS; i++) {
            g = sourceGraph.copy();
            modules = Map.of(
                    1, List.of(g.getNode("5")),
                    2, List.of(g.getNode("4"), g.getNode("6")),
                    3, List.of(g.getNode("1"), g.getNode("2"), g.getNode("3"))
            );
            avgBitLength = new AvgBitLength(g, modules);
            double b = avgBitLength.avgBitLength();
            if (!Double.isNaN(b)) {
                trials++;
                total += b;
            }
        }
        System.out.println("OK Average bit length: " + (total / trials));

        // GOOD
        trials = 0;
        total = 0;
        for (int i = 0; i < NUM_TRIALS; i++) {
            g = sourceGraph.copy();
            LouvainAlgorithm louvain = new LouvainAlgorithm(g);
            modules = louvain.getCommunities();
            avgBitLength = new AvgBitLength(g, modules);
            double b = avgBitLength.avgBitLength();
            if (!Double.isNaN(b)) {
                trials++;
                total += b;
            }
        }
        System.out.println("GOOD Average bit length: " + (total / trials));
    }
}
