net.sf.jmpi.solver.sat4j.SolverSAT4J Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmpi-solver-sat4j Show documentation
Show all versions of jmpi-solver-sat4j Show documentation
The solver interface for SAT4J
package net.sf.jmpi.solver.sat4j;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import net.sf.jmpi.main.MpConstraint;
import net.sf.jmpi.main.MpDirection;
import net.sf.jmpi.main.MpOperator;
import net.sf.jmpi.main.MpResult;
import net.sf.jmpi.main.MpResultImpl;
import net.sf.jmpi.main.MpVariable;
import net.sf.jmpi.main.expression.MpExpr;
import net.sf.jmpi.main.expression.MpExprTerm;
import net.sf.jmpi.solver.AbstractMpSolver;
import org.sat4j.core.Vec;
import org.sat4j.core.VecInt;
import org.sat4j.pb.ObjectiveFunction;
import org.sat4j.pb.PseudoOptDecorator;
import org.sat4j.pb.SolverFactory;
import org.sat4j.pb.core.PBSolverResolution;
import org.sat4j.specs.ContradictionException;
import org.sat4j.specs.IVec;
import org.sat4j.specs.IVecInt;
import org.sat4j.specs.TimeoutException;
public class SolverSAT4J extends AbstractMpSolver {
protected final PBSolverResolution solver;
protected int timeout = 3600 * 24;
public SolverSAT4J() {
solver = SolverFactory.newPBResMixedConstraintsObjective();
solver.newVar(100);
}
@Override
public MpResult solve() {
solver.setTimeout(timeout);
boolean feasible = false;
PseudoOptDecorator decorator = new PseudoOptDecorator(solver);
decorator.newVar(varNum.get());
try {
if (decorator.hasNoObjectiveFunction()) {
feasible = decorator.isSatisfiable();
} else {
while (decorator.admitABetterSolution()) {
feasible = true;
BigInteger rhs = toBigInteger(decorator.getObjectiveValue().intValue() - 1);
ObjectiveFunction lhs = decorator.getObjectiveFunction();
decorator.addPseudoBoolean(lhs.getVars(), lhs.getCoeffs(), false, rhs);
}
}
} catch (ContradictionException e) {
} catch (TimeoutException e) {
}
if (!feasible) {
return null;
} else {
MpResult result;
if (decorator.hasNoObjectiveFunction()) {
result = new MpResultImpl();
} else {
result = new MpResultImpl(decorator.getObjectiveValue().intValue()
* (optType == MpDirection.MAX ? -1 : 1));
}
for (Entry