use of org.opt4j.satdecoding.Constraint in project opt4j by felixreimann.
the class SAT4JSolver method solve.
/*
* (non-Javadoc)
*
* @see org.opt4j.sat.Solver#solve(org.opt4j.sat.Order)
*/
@Override
public synchronized Model solve(Order order) throws TimeoutException, ContradictionException {
if (!solverValid) {
throw new ContradictionException();
}
Constraint c = null;
while ((c = constraints.poll()) != null) {
addConstraintToSolver(c);
}
if (order instanceof VarOrder) {
VarOrder varOrder = (VarOrder) order;
VariableOrder o = new VariableOrder();
solver.setOrder(o);
for (Entry<Object, Double> entry : varOrder.getActivityEntrySet()) {
if (variables.containsKey(entry.getKey())) {
int var = variables.get(entry.getKey());
o.setVarActivity(var, entry.getValue());
}
}
for (Entry<Object, Boolean> entry : varOrder.getPhaseEntrySet()) {
if (variables.containsKey(entry.getKey())) {
int var = variables.get(entry.getKey());
o.setVarPhase(var, entry.getValue());
}
}
o.setVarInc(varOrder.getVarInc());
o.setVarDecay(varOrder.getVarDecay());
}
try {
boolean success = solver.isSatisfiable();
if (success) {
Model model = new Model();
for (Entry<Object, Integer> entry : variables.entrySet()) {
model.set(entry.getKey(), solver.model(entry.getValue()));
}
return model;
} else {
throw new ContradictionException();
}
} catch (org.sat4j.specs.TimeoutException e) {
throw new TimeoutException();
}
}
use of org.opt4j.satdecoding.Constraint in project opt4j by felixreimann.
the class ConstraintTest method equalityTestOfLiterals.
@Test
public void equalityTestOfLiterals() {
Object object1 = "literal1";
Object object2 = "literal2";
Constraint c1 = new Constraint(Operator.LE, 2);
c1.add(2, new Literal(object1, true));
c1.add(2, new Literal(object2, false));
Constraint c2 = new Constraint(Operator.LE, 2);
c2.add(2, new Literal(object1, true));
c2.add(2, new Literal(object2, true));
Assert.assertFalse(c1.equals(c2));
}
use of org.opt4j.satdecoding.Constraint in project opt4j by felixreimann.
the class ConstraintTest method equalityTestOfRhs.
@Test
public void equalityTestOfRhs() {
Object object1 = "literal1";
Object object2 = "literal2";
Constraint c1 = new Constraint(Operator.LE, 0);
c1.add(2, new Literal(object1, true));
c1.add(2, new Literal(object2, true));
Constraint c2 = new Constraint(Operator.LE, 2);
c2.add(2, new Literal(object1, true));
c2.add(2, new Literal(object2, true));
Assert.assertFalse(c1.equals(c2));
}
use of org.opt4j.satdecoding.Constraint in project opt4j by felixreimann.
the class KnapsackSATCreatorDecoder method createConstraints.
@Override
public Set<Constraint> createConstraints() {
Set<Constraint> constraints = new HashSet<Constraint>();
for (Knapsack knapsack : problem.getKnapsacks()) {
Constraint constraint = new Constraint(Operator.LE, (int) Math.floor(knapsack.getCapacity()));
for (Item item : problem.getItems()) {
constraint.add(knapsack.getWeight(item), new Literal(item, true));
}
constraints.add(constraint);
}
return constraints;
}
use of org.opt4j.satdecoding.Constraint in project opt4j by felixreimann.
the class MinOnesDecoder method createConstraints.
// Here you can set the constraints of your problem. In our case, we will
// randomly generate a problem as a 3SAT problem (3 literals per clause)
// with 1000 variables and 1000 clauses. This problem is known to be
// NP-complete. However, we hope that there exists at least one feasible
// solution (and with the seed 0 of random it does ;) ).
@Override
public Set<Constraint> createConstraints() {
Set<Constraint> constraints = new HashSet<Constraint>();
Random random = new Random(0);
for (int i = 0; i < 1000; i++) {
Constraint clause = new Constraint(">=", 1);
HashSet<Integer> vars = new HashSet<Integer>();
do {
vars.add(random.nextInt(1000));
} while (vars.size() < 3);
for (int n : vars) {
clause.add(new Literal(n, random.nextBoolean()));
}
constraints.add(clause);
}
return constraints;
}
Aggregations