001package org.cpsolver.ifs.example.csp; 002 003import java.util.Random; 004import java.util.Set; 005 006import org.cpsolver.ifs.assignment.Assignment; 007import org.cpsolver.ifs.model.BinaryConstraint; 008 009 010/** 011 * CSP binary constraint. <br> 012 * <br> 013 * This class only implements the generation of a binary CSP constraint and the 014 * consistency check. 015 * 016 * @author Tomáš Müller 017 * @version IFS 1.3 (Iterative Forward Search)<br> 018 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 019 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 020 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 021 * <br> 022 * This library is free software; you can redistribute it and/or modify 023 * it under the terms of the GNU Lesser General Public License as 024 * published by the Free Software Foundation; either version 3 of the 025 * License, or (at your option) any later version. <br> 026 * <br> 027 * This library is distributed in the hope that it will be useful, but 028 * WITHOUT ANY WARRANTY; without even the implied warranty of 029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030 * Lesser General Public License for more details. <br> 031 * <br> 032 * You should have received a copy of the GNU Lesser General Public 033 * License along with this library; if not see 034 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 035 */ 036public class CSPBinaryConstraint extends BinaryConstraint<CSPVariable, CSPValue> { 037 private boolean iIsConsistent[][] = null; 038 private int iNrCompatiblePairs; 039 040 /** 041 * Constructor 042 * @param id constraint id 043 * @param nrCompatiblePairs 044 * number of compatible pairs of values in the constraint 045 */ 046 public CSPBinaryConstraint(int id, int nrCompatiblePairs) { 047 super(); 048 iId = id; 049 iNrCompatiblePairs = nrCompatiblePairs; 050 } 051 052 private void swap(int[][] allPairs, int first, int second) { 053 int[] a = allPairs[first]; 054 allPairs[first] = allPairs[second]; 055 allPairs[second] = a; 056 } 057 058 /** 059 * Initializes the constraint. Randomly generates the given number of 060 * compatible pairs of values. 061 * 062 * @param rndNumGen 063 * random number generator 064 */ 065 public void init(Random rndNumGen) { 066 int numberOfAllPairs = first().values(null).size() * second().values(null).size(); 067 int[][] allPairs = new int[numberOfAllPairs][]; 068 int idx = 0; 069 070 iIsConsistent = new boolean[first().values(null).size()][second().values(null).size()]; 071 072 for (CSPValue v1 : first().values(null)) { 073 for (CSPValue v2 : second().values(null)) { 074 iIsConsistent[(int) v1.toDouble()][(int) v2.toDouble()] = false; 075 allPairs[idx++] = new int[] { (int) v1.toDouble(), (int) v2.toDouble() }; 076 } 077 } 078 079 for (int i = 0; i < iNrCompatiblePairs; i++) { 080 swap(allPairs, i, i + (int) (rndNumGen.nextDouble() * (numberOfAllPairs - i))); 081 iIsConsistent[allPairs[i][0]][allPairs[i][1]] = true; 082 } 083 } 084 085 /** 086 * True if the pair of given values is compatible. 087 */ 088 @Override 089 public boolean isConsistent(CSPValue value1, CSPValue value2) { 090 if (value1 == null || value2 == null) 091 return true; 092 if (isFirst(value1.variable())) { 093 return iIsConsistent[(int) value1.toDouble()][(int) value2.toDouble()]; 094 } else { 095 return iIsConsistent[(int) value2.toDouble()][(int) value1.toDouble()]; 096 } 097 } 098 099 /** 100 * Add the other variable to the set of conflicts, if it is not compatible 101 * with the given value. 102 */ 103 @Override 104 public void computeConflicts(Assignment<CSPVariable, CSPValue> assignment, CSPValue aValue, Set<CSPValue> conflicts) { 105 if (isFirst(aValue.variable())) { 106 if (!isConsistent(aValue, assignment.getValue(second()))) { 107 conflicts.add(assignment.getValue(second())); 108 } 109 } else { 110 if (!isConsistent(assignment.getValue(first()), aValue)) { 111 conflicts.add(assignment.getValue(first())); 112 } 113 } 114 } 115 116 @Override 117 public String getName() { 118 return "C" + getId(); 119 } 120}