001package conexp.fx.core.algorithm.nextclosures;
002
003/*-
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.io.File;
026import java.util.Set;
027import java.util.stream.Collectors;
028
029import conexp.fx.core.algorithm.nextclosures.NextClosures1.Result;
030import conexp.fx.core.collections.Pair;
031import conexp.fx.core.context.Concept;
032import conexp.fx.core.context.Implication;
033import conexp.fx.core.context.MatrixContext;
034import conexp.fx.core.importer.CXTImporter;
035import conexp.fx.core.util.Meter;
036
037public final class Benchmark {
038
039  private static final boolean compare(
040      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r,
041      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> s) {
042    return r.x().size() == s.x().size() && r.x().containsAll(s.x()) && s.x().containsAll(r.x())
043        && r.y().size() == s.y().size() && r.y().containsAll(s.y()) && s.y().containsAll(r.y());
044  }
045
046  private static final boolean compareSem(
047      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r,
048      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> s) {
049    return r.x().size() == s.x().size() && r.x().containsAll(s.x()) && s.x().containsAll(r.x())
050        && Implication.equivalent(r.y(), s.y());
051  }
052
053  public static void main(String[] args) throws Exception {
054    for (int i = 0; i < 10; i++) {
055      final Meter<Long> meter = Meter.newNanoStopWatch();
056      final MatrixContext<String, String> cxt =
057          CXTImporter.read(new File("../../Data/Contexts/random/o1000a20d10.cxt"));
058      System.out.println("Import:                  " + meter.measureAndFormat());
059      meter.reset();
060      final Result<String, String> x = NextClosures1.compute(cxt, false);
061      System.out.println("NextClosures1:           " + meter.measureAndFormat());
062      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> x0 = Pair.of(
063          x.concepts,
064          x.implications
065              .entrySet()
066              .parallelStream()
067              .map(e -> new Implication<String, String>(e.getKey(), e.getValue()))
068              .collect(Collectors.toSet()));
069      meter.reset();
070      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r0 = NextClosures2.compute(cxt);
071      System.out.println("NextClosures2:           " + meter.measureAndFormat());
072//      meter.reset();
073//      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r1 =
074//          NextClosures2Bit.bitBitCompute(cxt);
075//      System.out.println("NextClosures2BitBit:     " + meter.measureAndFormat());
076//      meter.reset();
077//      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> s1 =
078//          NextClosures2BitNew.bitBitCompute(cxt);
079//      System.out.println("NextClosures2BitBitNew:     " + meter.measureAndFormat());
080      meter.reset();
081      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r2 = NextClosures2Bit.bitCompute(cxt);
082      System.out.println("NextClosures2Bit:        " + meter.measureAndFormat());
083//      meter.reset();
084//      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> s2 =
085//          NextClosures2BitNew.bitCompute(cxt);
086//      System.out.println("NextClosures2BitNew:        " + meter.measureAndFormat());
087//      meter.reset();
088//      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r3 = NextClosures2Bit
089//          .bitCleanedCompute(cxt, Executors.newWorkStealingPool(), __ -> {}, __ -> {}, __ -> {}, __ -> {}, () -> false);
090//      System.out.println("NextClosures2BitCleaned: " + meter.measureAndFormat());
091//      meter.reset();
092//      final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> r4 = NextClosures2Bit
093//          .bitReducedCompute(cxt, Executors.newWorkStealingPool(), __ -> {}, __ -> {}, __ -> {}, __ -> {}, () -> false);
094//      System.out.println("NextClosures2BitReduced: " + meter.measureAndFormat());
095      System.out.println("############################################");
096      System.out.println(compare(r0, x0) + " : " + compareSem(r0, x0));
097      System.out.println(compare(x0, r2) + " : " + compareSem(x0, r2));
098//      System.out.println(compare(r1, s1) + " : " + compareSem(r1, s1));
099//      System.out.println(r1.y().size() + " : " + s1.y().size());
100//      System.out.println(Sets.difference(r1.y(), s1.y()));
101//      System.out.println(Sets.difference(s1.y(), r1.y()));
102//      System.out.println(compare(r1, r2) + " : " + compareSem(r1, r2));
103//      System.out.println(compare(r1, s2) + " : " + compareSem(r1, s2));
104//      System.out.println(r1.y().size() + " : " + s2.y().size());
105//      System.out.println(Sets.difference(r1.y(), s2.y()));
106//      System.out.println(Sets.difference(s2.y(), r1.y()));
107//      System.out.println(compareSem(r0, r3));
108//      System.out.println(compareSem(r0, r4));
109      System.out.println("############################################");
110      System.out.println();
111    }
112  }
113
114}