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.ArrayList;
027import java.util.Collections;
028import java.util.Set;
029import java.util.stream.Collectors;
030
031import com.google.common.collect.Lists;
032import com.google.common.collect.Sets;
033
034import conexp.fx.core.collections.Collections3;
035import conexp.fx.core.context.Concept;
036import conexp.fx.core.context.Implication;
037import conexp.fx.core.context.MatrixContext;
038import conexp.fx.core.importer.CXTImporter;
039import conexp.fx.core.math.SetClosureOperator;
040
041public final class ProbDLExample {
042
043  private static Set<Implication<String, String>> bimp;
044
045  public static final void main(final String[] args) throws Exception {
046    final File file = new File("../../LaTeX/cla2015-prob-ijgs/example-pdl-induced.cxt");
047    final MatrixContext<String, String> cxt = CXTImporter.read(file);
048    bimp = Sets.newHashSet(
049        new Implication<>("$\\bot$", "$A_1$"),
050        new Implication<>("$\\bot$", "$A_2$"),
051        new Implication<>("$\\bot$", "$A_3$"),
052        new Implication<>("$\\bot$", "$r_1:C_1$"),
053        new Implication<>("$\\bot$", "$r_1:C_7$"),
054        new Implication<>("$\\bot$", "$r_2:C_1$"),
055        new Implication<>("$\\bot$", "$r_2:C_7$"),
056        new Implication<>("$r_1:C_1$", "$r_1:C_3$"),
057        new Implication<>("$r_1:C_3$", "$r_1:C_5$"),
058        new Implication<>("$r_1:C_5$", "$r_1:C_2$"),
059        new Implication<>("$r_1:C_7$", "$r_1:C_6$"),
060        new Implication<>("$r_1:C_6$", "$r_1:C_4$"),
061        new Implication<>("$r_1:C_6$", "$r_1:C_5$"),
062        new Implication<>("$r_1:C_4$", "$r_1:C_2$"),
063        new Implication<>("$r_2:C_1$", "$r_2:C_3$"),
064        new Implication<>("$r_2:C_3$", "$r_2:C_5$"),
065        new Implication<>("$r_2:C_5$", "$r_2:C_2$"),
066        new Implication<>("$r_2:C_7$", "$r_2:C_6$"),
067        new Implication<>("$r_2:C_6$", "$r_2:C_4$"),
068        new Implication<>("$r_2:C_6$", "$r_2:C_5$"),
069        new Implication<>("$r_2:C_4$", "$r_2:C_2$"));
070    final Set<Double> probs =
071        Sets.powerSet(Sets.newHashSet(1d / 2d, 1d / 3d, 1d / 6d)).stream().map(Collections3::sum).collect(
072            Collectors.toSet());
073    probs.remove(0d);
074    probs.forEach(System.out::println);
075    final Set<Implication<String, String>> i = NextClosures2.compute(cxt, bimp).second();
076    i.stream().map(ProbDLExample::minimize).forEach(System.out::println);
077    NextClosures2.compute(cxt).first().stream().map(Concept::getIntent).forEach(System.out::println);
078
079    final Set<Implication<String, String>> j =
080        NextClosures2C.compute(cxt, SetClosureOperator.fromImplications(bimp)).second();
081    System.out.println(i.size() == j.size());
082    System.out.println(i.equals(j));
083    System.out.println(Implication.equivalent(i, j));
084  }
085
086  private static final Implication<String, String> minimize(final Implication<String, String> impl) {
087    return new Implication<>(b(impl.getPremise()), b(impl.getConclusion()));
088  }
089
090  private static final Set<String> b(final Set<String> s) {
091//  final HashSet<String> result = Sets.newHashSet(s);
092//  final Set<String> r = Sets.newHashSet(s);
093    final ArrayList<String> r = Lists.newArrayList(s);
094    Collections.shuffle(r);
095    String a;
096    do {
097      a = null;
098      for (String x : r)
099        if (SetClosureOperator
100            .fromImplications(bimp, false, true)
101            .closure(Sets.difference(Sets.newHashSet(r), Collections.singleton(x)))
102            .contains(x)) {
103          a = x;
104          break;
105        }
106      r.remove(a);
107    } while (a != null);
108//  result.removeAll(r);
109//  return r;
110    return Sets.newHashSet(r);
111  }
112
113}