Search in sources :

Example 1 with Constraint

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();
    }
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) ContradictionException(org.opt4j.satdecoding.ContradictionException) Constraint(org.opt4j.satdecoding.Constraint) BigInteger(java.math.BigInteger) Model(org.opt4j.satdecoding.Model) VarOrder(org.opt4j.satdecoding.VarOrder) TimeoutException(org.opt4j.satdecoding.TimeoutException)

Example 2 with Constraint

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));
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Test(org.junit.Test)

Example 3 with Constraint

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));
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Test(org.junit.Test)

Example 4 with Constraint

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;
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) HashSet(java.util.HashSet)

Example 5 with Constraint

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;
}
Also used : Random(java.util.Random) Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Constraint(org.opt4j.satdecoding.Constraint) HashSet(java.util.HashSet)

Aggregations

Constraint (org.opt4j.satdecoding.Constraint)8 Literal (org.opt4j.satdecoding.Literal)7 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 BigInteger (java.math.BigInteger)1 Random (java.util.Random)1 ContradictionException (org.opt4j.satdecoding.ContradictionException)1 Model (org.opt4j.satdecoding.Model)1 TimeoutException (org.opt4j.satdecoding.TimeoutException)1 VarOrder (org.opt4j.satdecoding.VarOrder)1