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.Arrays; 028import java.util.Collections; 029import java.util.HashMap; 030import java.util.HashSet; 031import java.util.Map; 032import java.util.Set; 033 034import com.google.common.collect.Lists; 035import com.google.common.collect.Sets; 036 037import conexp.fx.core.collections.Pair; 038import conexp.fx.core.context.Concept; 039import conexp.fx.core.context.Implication; 040import conexp.fx.core.context.MatrixContext; 041import conexp.fx.core.importer.CXTImporter; 042 043public final class ProbExample { 044 045 private static Set<Implication<String, String>> backgroundImplications; 046 private static Map<String, Set<Integer>> elems; 047 private static Map<String, Double> probs; 048 049 private static final void e(final String s, final Integer... es) { 050 elems.put(s, Sets.newHashSet(es)); 051 } 052 053 private static final void p(final String s, final Double p) { 054 probs.put(s, p); 055 } 056 057 private static final void f(final String p, final String... cs) { 058 for (String c : Arrays.asList(cs)) 059 backgroundImplications.add(new Implication<String, String>(Collections.singleton(p), Collections.singleton(c))); 060 } 061 062 private static final void g(final String p, final String q, final String c) { 063 backgroundImplications.add(new Implication<String, String>(Sets.newHashSet(p, q), Collections.singleton(c))); 064 } 065 066 public static final void main(final String[] args) throws Exception { 067 elems = new HashMap<>(); 068// e("A", 2); 069// e("B", 3); 070// e("C", 1, 3); 071// e("D", 2, 3); 072// e("E", 1, 2, 3); 073 e("F", 2); 074 e("G", 3); 075 e("H", 1, 3); 076 e("I", 2, 3); 077 e("J", 1, 2, 3); 078 e("K", 2); 079 e("L", 3); 080 e("M", 1, 3); 081 e("N", 2, 3); 082 e("O", 1, 2, 3); 083 e("P", 2); 084 e("Q", 3); 085 e("R", 1, 3); 086 e("S", 2, 3); 087 e("T", 1, 2, 3); 088 e("U", 2); 089 e("V", 3); 090 e("W", 1, 3); 091 e("X", 2, 3); 092 e("Y", 1, 2, 3); 093 e("Z", 2); 094 e("0", 3); 095 e("1", 1, 3); 096 e("2", 2, 3); 097 e("3", 1, 2, 3); 098 099 probs = new HashMap<>(); 100// p("A", 0d); 101// p("B", 0d); 102// p("C", 0d); 103// p("D", 0d); 104// p("E", 0d); 105 p("F", 1d / 6d); 106 p("G", 1d / 6d); 107 p("H", 1d / 6d); 108 p("I", 1d / 6d); 109 p("J", 1d / 6d); 110 p("K", 1d / 3d); 111 p("L", 1d / 3d); 112 p("M", 1d / 3d); 113 p("N", 1d / 3d); 114 p("O", 1d / 3d); 115 p("P", 2d / 3d); 116 p("Q", 2d / 3d); 117 p("R", 2d / 3d); 118 p("S", 2d / 3d); 119 p("T", 2d / 3d); 120 p("U", 5d / 6d); 121 p("V", 5d / 6d); 122 p("W", 5d / 6d); 123 p("X", 5d / 6d); 124 p("Y", 5d / 6d); 125 p("Z", 1d); 126 p("0", 1d); 127 p("1", 1d); 128 p("2", 1d); 129 p("3", 1d); 130 131 backgroundImplications = new HashSet<>(); 132 133// f("3", "Y", "T", "O", "J", "E"); 134// f("2", "X", "S", "N", "I", "D"); 135// f("1", "W", "R", "M", "H", "C"); 136// f("0", "V", "Q", "L", "G", "B"); 137// f("Z", "U", "P", "K", "F", "A"); 138// 139// f("3", "2", "1", "0", "Z"); 140// f("Y", "X", "W", "V", "U"); 141// f("T", "S", "R", "Q", "P"); 142// f("O", "N", "M", "L", "K"); 143// f("J", "I", "H", "G", "F"); 144// f("E", "D", "C", "B", "A"); 145// 146// f("2", "0", "Z"); 147// f("X", "V", "U"); 148// f("S", "Q", "P"); 149// f("N", "L", "K"); 150// f("I", "G", "F"); 151// f("D", "B", "A"); 152// 153// f("1", "0"); 154// f("W", "V"); 155// f("R", "Q"); 156// f("M", "L"); 157// f("H", "G"); 158// f("C", "B"); 159 160 for (String p : elems.keySet()) 161 for (String q : elems.keySet()) 162 if (!p.equals(q)) 163 if (probs.get(p) >= probs.get(q)) 164 if (elems.get(p).containsAll(elems.get(q))) 165 f(p, q); 166 167// g("Z", "0", "2"); 168// g("Z", "V", "X"); 169// g("Z", "Q", "S"); 170// g("Z", "L", "N"); 171// g("Z", "G", "I"); 172// g("Z", "B", "D"); 173 174 for (String p : elems.keySet()) 175 for (String q : elems.keySet()) 176 if (!p.equals(q)) 177 for (String c : elems.keySet()) 178 if (!c.equals(p)) 179 if (!c.equals(q)) 180 // if (Collections.disjoint(elems.get(p), elems.get(q))) 181 if (probs.get(p) + probs.get(q) - 1d > 0d) 182 if (probs.get(c) == probs.get(p) + probs.get(q) - 1d) 183 if (elems.get(c).containsAll(Sets.union(elems.get(p), elems.get(q)))) 184 if (Sets.union(elems.get(p), elems.get(q)).containsAll(elems.get(c))) 185 g(p, q, c); 186 187 backgroundImplications.forEach(System.out::println); 188 189 final MatrixContext<String, String> cxt = 190 CXTImporter.read(new File("../../LaTeX/cla2015-prob-ijgs/example-scaling-2.cxt")); 191 final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> result2 = 192 NextClosures2.compute(cxt, backgroundImplications); 193 final Set<Implication<String, String>> impls2 = result2.second(); 194 impls2 195 .stream() 196 .map(i -> new Implication<String, String>(b(i.getPremise()), b(i.getConclusion()))) 197 .sequential() 198 .forEach(System.out::println); 199// final List<Set<Implication<String, String>>> foos = Lists.newArrayList(); 200// for (int k = 0; k < 100; k++) { 201// foos.add(Sets.newHashSet()); 202// impls2 203// .stream() 204// .map(i -> new Implication<String, String>(b(i.getPremise()), b(i.getConclusion()))) 205// .sequential() 206// .forEach(foos.get(k)::add); 207// } 208// for (int k = 0; k < 100; k++) 209// for (int l = 0; l < 100; l++) 210// if (k != l) { 211// System.out.println(foos.get(k).containsAll(foos.get(l))); 212// System.out.println(foos.get(l).containsAll(foos.get(k))); 213// Sets.difference(foos.get(k), foos.get(l)).forEach(System.out::println); 214// Sets.difference(foos.get(l), foos.get(k)).forEach(System.out::println); 215// System.out.println(); 216// } 217// 218// System.out 219// .println(ClosureOperator.fromImplications(backgroundImplications, false, true).closure(Sets.newHashSet("3"))); 220// System.out.println( 221// ClosureOperator.fromImplications(backgroundImplications, false, true).closure(Sets.newHashSet("1", "2", "Y"))); 222// final Pair<Set<Concept<String, String>>, Set<Implication<String, String>>> resultC = 223// NextClosures2C.compute(cxt, ClosureOperator.fromImplications(backgroundImplications, false, false)); 224// System.out.println(); 225// System.out.println(result2.first().containsAll(resultC.first())); 226// System.out.println(resultC.first().containsAll(result2.first())); 227// System.out.println(result2.second().containsAll(resultC.second())); 228// System.out.println(resultC.second().containsAll(result2.second())); 229// System.out.println(); 230// Sets.difference(result2.second(), resultC.second()).forEach(System.out::println); 231// System.out.println(); 232// Sets.difference(resultC.second(), result2.second()).forEach(System.out::println); 233// System.out.println(); 234 } 235 236 private static final Set<String> b(final Set<String> s) { 237// final HashSet<String> result = Sets.newHashSet(s); 238// final Set<String> r = Sets.newHashSet(s); 239 final ArrayList<String> r = Lists.newArrayList(s); 240 Collections.shuffle(r); 241 String a; 242 do { 243 a = null; 244 for (String x : r) { 245 if (a != null) 246 break; 247 for (String y : r) { 248 if (a != null) 249 break; 250 if (backgroundImplications.contains(new Implication<>(x, y))) { 251 a = y; 252 break; 253 } else 254 for (String z : r) 255 if (backgroundImplications.contains(new Implication<>(Sets.newHashSet(x, y), z))) { 256 a = z; 257 break; 258 } 259 } 260 } 261 r.remove(a); 262 } while (a != null); 263// result.removeAll(r); 264// return r; 265 return Sets.newHashSet(r); 266 } 267 268}