Commit 225b7de4 authored by Charles Vernerey's avatar Charles Vernerey
Browse files

Add CoverSize tests

parent be09a04e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public class PatternUtil {
     * @param stream input stream of skypatterns
     * @return The list of patterns contained in the stream
     */
    public static List<Pattern> readPatternStream(InputStream stream, List<String> measuresId) throws IOException {
    public static List<Pattern> readPatternStream(InputStream stream) throws IOException {
        List<Pattern> skypatterns = new LinkedList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
            String line;
+70 −0
Original line number Diff line number Diff line
/*
 * This file is part of io.gitlab.chaver:data-mining (https://gitlab.com/chaver/data-mining)
 *
 * Copyright (c) 2022, IMT Atlantique
 *
 * Licensed under the MIT license.
 *
 * See LICENSE file in the project root for full license information.
 */
package io.gitlab.chaver.mining.patterns.constraints;

import io.gitlab.chaver.mining.patterns.io.DatReader;
import io.gitlab.chaver.mining.patterns.io.Database;
import io.gitlab.chaver.mining.patterns.io.Pattern;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.variables.BoolVar;
import org.chocosolver.solver.variables.IntVar;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static io.gitlab.chaver.mining.patterns.util.PatternUtil.readPatternStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CoverSizeTest {

    private final String resourcesPath = "src/test/resources/";

    private void testFindFrequentPatterns(String dataPath, Set<Pattern> expectedPatterns, int freqLB) throws IOException {
        Set<Pattern> filteredExpected = expectedPatterns.stream()
                .filter(p -> p.getMeasures()[0] >= freqLB)
                .collect(Collectors.toSet());
        Model model = new Model("frequent test");
        Database database = new DatReader(dataPath, 0, true).readFiles();
        IntVar freq = model.intVar("freq", freqLB, database.getNbTransactions());
        IntVar length = model.intVar("length", 1, database.getNbItems());
        BoolVar[] x = model.boolVarArray("x", database.getNbItems());
        model.sum(x, "=", length).post();
        model.post(new Constraint("Cover Size", new CoverSize(database, freq, x)));
        List<Solution> sols = model.getSolver().findAllSolutions();
        assertEquals(filteredExpected.size(), sols.size());
        for (Solution sol : sols) {
            int[] itemSave = IntStream.range(0, x.length)
                    .filter(i -> sol.getIntVal(x[i]) == 1)
                    .map(i -> database.getItems()[i])
                    .toArray();
            int freqSave = sol.getIntVal(freq);
            assertTrue(filteredExpected.contains(new Pattern(itemSave, new int[]{freqSave})));
        }
    }

    @Test
    public void testCoverSize() throws IOException {
        String dataPath = resourcesPath + "contextPasquier99/contextPasquier99.dat";
        String resPath = resourcesPath + "contextPasquier99/frequent.txt";
        Set<Pattern> expectedPatterns = new HashSet<>(readPatternStream(new FileInputStream(resPath)));
        for (int i = 2; i <= 4; i++) {
            testFindFrequentPatterns(dataPath, expectedPatterns, i);
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public abstract class PatternProblemTest {
    public abstract PatternProblem getProblem();

    private List<Pattern> readPatterns(String patternPath, List<String> measuresId) throws IOException {
        return readPatternStream(new FileInputStream(patternPath), measuresId);
        return readPatternStream(new FileInputStream(patternPath));
    }

    private void testEqualsPatterns(List<Pattern> p1, List<Pattern> p2) {
+5 −0
Original line number Diff line number Diff line
1 3 4
2 3 5
1 2 3 5
2 5
1 2 3 5
 No newline at end of file
+15 −0
Original line number Diff line number Diff line
1 : 3
2 : 4
3 : 4
5 : 4
1 2 : 2
1 2 3 : 2
1 2 5 : 2
1 2 3 5 : 2
1 3 : 3
1 3 5 : 2
1 5 : 2
2 3 : 3
2 5 : 4
2 3 5 : 3
3 5 : 3
 No newline at end of file