aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/rankingexpression/importer/NamingConstraintSolver.java
blob: 21cc6b27dade699c9e54cb1ebadc495f69cd8e2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.rankingexpression.importer;

import com.yahoo.collections.ListMap;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Solves a dimension naming constraint problem.
 *
 * @author lesters
 * @author bratseth
 */
class NamingConstraintSolver {

    private final ListMap<String, Integer> possibleAssignments;
    private final ListMap<DimensionRenamer.Arc, DimensionRenamer.Constraint> constraints;

    private int iterations = 0;
    private final int maxIterations;

    private NamingConstraintSolver(Set<String> dimensions,
                                   ListMap<DimensionRenamer.Arc, DimensionRenamer.Constraint> constraints,
                                   int maxIterations) {
        this.possibleAssignments = allPossibilities(dimensions);
        this.constraints = constraints;
        this.maxIterations = maxIterations;
    }

    /** Returns a list containing a list of all assignment possibilities for each of the given dimensions */
    private static ListMap<String, Integer> allPossibilities(Set<String> dimensions) {
        ListMap<String, Integer> all = new ListMap<>();
        for (String dimension : dimensions) {
            for (int i = 0; i < dimensions.size(); ++i)
                all.put(dimension, i);
        }
        return all;
    }

    /**
     * Try the solve the constraint problem given in the arguments, and put the result in renames.
     *
     * This is done by performing iterative arc consistency until we have found a solution.
     * After an initial iteration, the dimensions will have multiple
     * valid values. Find a single valid assignment by iteratively locking one
     * dimension after another, and running the arc consistency algorithm
     * multiple times.
     *
     * This requires having constraints that result in an absolute ordering:
     * equal, lessThan and greaterThan do that, but not necessarily notEqual
     * If that is needed, the algorithm needs to be adapted with a backtracking
     * (tree) search
     *
     * @return the solution in the form of the renames to perform
     */
    private Map<String, Integer> trySolve() {
        // TODO: Evaluate possible improved efficiency by using a heuristic such as min-conflicts

        Map<String, Integer> solution = new HashMap<>();
        for (String dimension : possibleAssignments.keySet()) {
            List<Integer> values = possibleAssignments.get(dimension);
            if (values.size() > 1) {
                if ( ! ac3()) return null;
                values.sort(Integer::compare);
                possibleAssignments.replace(dimension, values.get(0));
            }
            solution.put(dimension, possibleAssignments.get(dimension).get(0)); // Pick the first available solution
            if (iterations > maxIterations) return null;
        }
        return solution;
    }

    private boolean ac3() {
        Deque<DimensionRenamer.Arc> workList = new ArrayDeque<>(constraints.keySet());
        while ( ! workList.isEmpty()) {
            DimensionRenamer.Arc arc = workList.pop();
            iterations++;
            if (revise(arc)) {
                if (possibleAssignments.get(arc.from).isEmpty()) return false;

                for (DimensionRenamer.Arc constraint : constraints.keySet()) {
                    if (arc.from.equals(constraint.to) && !arc.to.equals(constraint.from))
                        workList.add(constraint);
                }
            }
        }
        return true;
    }

    private boolean revise(DimensionRenamer.Arc arc) {
        boolean revised = false;
        for (Iterator<Integer> fromIterator = possibleAssignments.get(arc.from).iterator(); fromIterator.hasNext(); ) {
            Integer from = fromIterator.next();
            boolean satisfied = false;
            for (Iterator<Integer> toIterator = possibleAssignments.get(arc.to).iterator(); toIterator.hasNext(); ) {
                Integer to = toIterator.next();
                if (constraints.get(arc).stream().allMatch(constraint -> constraint.test(from, to)))
                    satisfied = true;
            }
            if ( ! satisfied) {
                fromIterator.remove();
                revised = true;
            }
        }
        return revised;
    }

    /**
     * Attempts to solve the given naming problem. The input maps are never modified.
     *
     * @return the solution as a map from existing names to name ids represented as integers, or NULL
     *         if no solution could be found
     */
    public static Map<String, Integer> solve(Set<String> dimensions,
                                             ListMap<DimensionRenamer.Arc, DimensionRenamer.Constraint> constraints,
                                             int maxIterations) {
        return new NamingConstraintSolver(dimensions, constraints, maxIterations).trySolve();
    }

}