|
| 1 | +// CSP.java |
| 2 | +// From Classic Computer Science Problems in Java Chapter 3 |
| 3 | +// Copyright 2020 David Kopec |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package chapter3; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +public class CSP<V, D> { |
| 25 | + private List<V> variables; |
| 26 | + private Map<V, List<D>> domains; |
| 27 | + private Map<V, List<Constraint<V, D>>> constraints; |
| 28 | + |
| 29 | + public CSP(List<V> variables, Map<V, List<D>> domains) { |
| 30 | + this.variables = variables; |
| 31 | + this.domains = domains; |
| 32 | + constraints = new HashMap<>(); |
| 33 | + for (V variable : variables) { |
| 34 | + constraints.put(variable, new ArrayList<>()); |
| 35 | + if (!domains.containsKey(variable)) { |
| 36 | + throw new IllegalArgumentException("Every variable should have a domain assigned to it."); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public void addConstraint(Constraint<V, D> constraint) { |
| 42 | + for (V variable : constraint.variables) { |
| 43 | + if (!variables.contains(variable)) { |
| 44 | + throw new IllegalArgumentException("Variable in constraint not in CSP"); |
| 45 | + } else { |
| 46 | + constraints.get(variable).add(constraint); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Check if the value assignment is consistent by checking all constraints |
| 52 | + // for the given variable against it |
| 53 | + public boolean consistent(V variable, Map<V, D> assignment) { |
| 54 | + for (Constraint<V, D> constraint : constraints.get(variable)) { |
| 55 | + if (!constraint.satisfied(assignment)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + public Map<V, D> backtrackingSearch(Map<V, D> assignment) { |
| 63 | + // assignment is complete if every variable is assigned (our base case) |
| 64 | + if (assignment.size() == variables.size()) { |
| 65 | + return assignment; |
| 66 | + } |
| 67 | + // get the first variable in the CSP but not in the assignment |
| 68 | + V unassigned = variables.stream().filter(v -> (!assignment.containsKey(v))).findFirst().get(); |
| 69 | + // get the every possible domain value of the first unassigned variable |
| 70 | + for (D value : domains.get(unassigned)) { |
| 71 | + // shallow copy of assignment that we can change |
| 72 | + Map<V, D> localAssignment = new HashMap<>(assignment); |
| 73 | + localAssignment.put(unassigned, value); |
| 74 | + // if we're still consistent, we recurse (continue) |
| 75 | + if (consistent(unassigned, localAssignment)) { |
| 76 | + Map<V, D> result = backtrackingSearch(localAssignment); |
| 77 | + // if we didn't find the result, we will end up backtracking |
| 78 | + if (result != null) { |
| 79 | + return result; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + // helper for backtrackingSearch when nothing known yet |
| 87 | + public Map<V, D> backtrackingSearch() { |
| 88 | + return backtrackingSearch(new HashMap<>()); |
| 89 | + } |
| 90 | +} |
0 commit comments