net.sf.opendse.optimization.encoding.common.ConstraintNormalization Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendse-optimization Show documentation
Show all versions of opendse-optimization Show documentation
The optimization module of OpenDSE
/**
* OpenDSE 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.
*
* OpenDSE 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 OpenDSE. If not, see http://www.gnu.org/licenses/.
*/
package net.sf.opendse.optimization.encoding.common;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.opt4j.satdecoding.Constraint;
import org.opt4j.satdecoding.Literal;
import org.opt4j.satdecoding.Term;
import org.opt4j.satdecoding.Constraint.Operator;
/**
* The {@code ConstraintNormalization} normalizes constraints.
*
* @author Martin Lukasiewycz
*
*/
class ConstraintNormalization {
public void normalize(Constraint constraint) {
switch (constraint.getOperator()) {
case LE:
inverse(constraint);
unify(constraint);
positive(constraint);
sorting(constraint);
trimming(constraint);
gcd(constraint);
break;
case GE:
unify(constraint);
positive(constraint);
sorting(constraint);
trimming(constraint);
gcd(constraint);
break;
default: // EQ
throw new IllegalArgumentException("Cannot normalize equality constraints");
}
}
private void inverse(Constraint constraint) {
assert (constraint.getOperator() == Operator.LE);
List terms = new ArrayList(constraint.size());
for (Term term : constraint) {
int coeff = -term.getCoefficient();
Literal lit = term.getLiteral();
terms.add(new Term(coeff, lit));
}
constraint.setRhs(-constraint.getRhs());
constraint.setOperator(Operator.GE);
constraint.clear();
constraint.addAll(terms);
}
private void positive(Constraint constraint) {
int rhs = constraint.getRhs();
for (int i = 0; i < constraint.size(); i++) {
Term term = constraint.get(i);
int coeff = term.getCoefficient();
if (coeff < 0) {
Term t = new Term(-coeff, term.getLiteral().negate());
constraint.set(i, t);
rhs -= coeff;
} else if (coeff == 0) {
constraint.remove(i);
i--;
}
}
constraint.setRhs(rhs);
if (rhs <= 0) {
// trivially satisfied
constraint.clear();
}
}
private void unify(Constraint constraint) {
assert (constraint.getOperator() == Operator.GE);
Set