
org.cpsolver.ifs.model.GlobalConstraint Maven / Gradle / Ivy
package org.cpsolver.ifs.model;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.cpsolver.ifs.assignment.Assignment;
/**
* Generic global constraint.
*
* Global constraint is a {@link Constraint} that applies to all variables.
*
* @see Variable
* @see Model
* @see Constraint
* @see org.cpsolver.ifs.solver.Solver
*
* @author Tomas Muller
* @version IFS 1.3 (Iterative Forward Search)
* Copyright (C) 2006 - 2014 Tomas Muller
* [email protected]
* http://muller.unitime.org
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not see
* http://www.gnu.org/licenses/.
* @param Variable
* @param Value
*/
public abstract class GlobalConstraint, T extends Value> extends Constraint {
/** The list of variables of this constraint */
@Override
public List variables() {
return getModel().variables();
}
/** The list of variables of this constraint that are assigned */
@Override
public Collection assignedVariables(Assignment assignment) {
return assignment.assignedVariables();
}
/** The number of variables of this constraint that are assigned */
@Override
public int countAssignedVariables(Assignment assignment) {
return assignment.nrAssignedVariables();
}
/** Add a variable to this constraint */
@Override
public void addVariable(V variable) {
throw new RuntimeException("A variable cannot be added to a global constraint.");
}
/** Remove a variable from this constraint */
@Override
public void removeVariable(V variable) {
throw new RuntimeException("A variable cannot be removed from a global constraint.");
}
/**
* Given value is to be assigned to its variable. In this method, the
* constraint should unassigns all variables which are in conflict with the
* given assignment because of this constraint.
*/
@Override
public void assigned(Assignment assignment, long iteration, T value) {
HashSet conf = null;
if (isHard()) {
conf = new HashSet();
computeConflicts(assignment, value, conf);
}
if (constraintListeners() != null)
for (ConstraintListener listener : iConstraintListeners)
listener.constraintBeforeAssigned(assignment, iteration, this, value, conf);
if (conf != null) {
for (T conflictValue : conf) {
if (!conflictValue.equals(value))
assignment.unassign(iteration, conflictValue.variable());
}
}
if (constraintListeners() != null)
for (ConstraintListener listener : iConstraintListeners)
listener.constraintAfterAssigned(assignment, iteration, this, value, conf);
}
/**
* Given value is unassigned from its varable.
*/
@Override
public void unassigned(Assignment assignment, long iteration, T value) {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy