
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
/*******************************************************************************
* Copyright (c) 2015 OpenDSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy