
org.cpsolver.ifs.extension.ViolatedInitials Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cpsolver Show documentation
Show all versions of cpsolver Show documentation
The constraint solver library contains a local search based framework that allows
modeling of a problem using constraint programming primitives (variables, values, constraints).
The newest version!
package org.cpsolver.ifs.extension;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.cpsolver.ifs.assignment.Assignment;
import org.cpsolver.ifs.model.Constraint;
import org.cpsolver.ifs.model.Value;
import org.cpsolver.ifs.model.Variable;
import org.cpsolver.ifs.solver.Solver;
import org.cpsolver.ifs.util.DataProperties;
/**
* Computation of violated initial values (minimal perturbation problem).
*
* It is using {@link Constraint#isConsistent(Value, Value)} to find out what
* initial values (of different variables) cannot be assigned when an arbitrary
* value is assigned to a variable. This information is computed in advance,
* before the solver is executed. It is used for better estimation of
* perturbation penalty (see
* {@link org.cpsolver.ifs.perturbations.PerturbationsCounter}) when a value
* is to be assigned to a variable.
*
* @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 class ViolatedInitials, T extends Value> extends Extension {
private static org.apache.logging.log4j.Logger sLogger = org.apache.logging.log4j.LogManager.getLogger(ViolatedInitials.class);
private Map> iViolatedInitials = new HashMap>();
public ViolatedInitials(Solver solver, DataProperties properties) {
super(solver, properties);
}
/** Compute the violations between any value and all other initial values
* @param assignment current assignment
* @return true if initialized properly
**/
public boolean init(Assignment assignment) {
sLogger.info("Computation of violated initials enabled.");
for (V variable : getModel().variables()) {
if (variable.getInitialAssignment() == null)
continue;
for (Constraint constraint : variable.hardConstraints()) {
for (T value : conflictValues(assignment, constraint, variable.getInitialAssignment())) {
addViolatedInitial(value, variable.getInitialAssignment());
}
}
}
return true;
}
/** Initial values that cannot be assigned when the given value is assigned
* @param value given value
* @return list of initial values that cannot be assigned due to the given value
*
**/
public Set getViolatedInitials(T value) {
return iViolatedInitials.get(value);
}
private void addViolatedInitial(T value, T anotherValue) {
Set violations = iViolatedInitials.get(value);
if (violations == null) {
violations = new HashSet();
iViolatedInitials.put(value, violations);
}
violations.add(anotherValue);
}
private List conflictValues(Assignment assignment, Constraint constraint, T aValue) {
List ret = new ArrayList();
for (V variable : constraint.variables()) {
if (variable.equals(aValue.variable()))
continue;
if (assignment.getValue(variable) != null)
continue;
for (T value : variable.values(assignment)) {
if (!constraint.isConsistent(aValue, value))
ret.add(value);
}
}
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy