Search in sources :

Example 1 with Literal

use of org.opt4j.satdecoding.Literal 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 2 with Literal

use of org.opt4j.satdecoding.Literal 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 3 with Literal

use of org.opt4j.satdecoding.Literal 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 4 with Literal

use of org.opt4j.satdecoding.Literal 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)

Example 5 with Literal

use of org.opt4j.satdecoding.Literal in project opt4j by felixreimann.

the class QueensSATDecoder method createConstraints.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.opt4j.sat.AbstractSATDecoder#createConstraints()
	 */
@Override
public Set<Constraint> createConstraints() {
    Set<Constraint> constraints = new HashSet<Constraint>();
    int size = problem.size();
    for (int i = 0; i < size; i++) {
        Constraint constraint = new Constraint("=", 1);
        for (int j = 0; j < size; j++) {
            constraint.add(new Literal(i * size + j, true));
        }
        constraints.add(constraint);
    }
    for (int j = 0; j < size; j++) {
        Constraint constraint = new Constraint("=", 1);
        for (int i = 0; i < size; i++) {
            constraint.add(new Literal(i * size + j, true));
        }
        constraints.add(constraint);
    }
    for (int k = -size + 1; k < size; k++) {
        // diagonal 1
        Constraint constraint = new Constraint("<=", 1);
        for (int j = 0; j < size; j++) {
            int i = k + j;
            if (0 <= i && i < size) {
                constraint.add(new Literal(i * size + j, true));
            }
        }
        constraints.add(constraint);
    }
    for (int k = 0; k < 2 * size - 1; k++) {
        // diagonal 2
        Constraint constraint = new Constraint("<=", 1);
        for (int j = 0; j < size; j++) {
            int i = k - j;
            if (0 <= i && i < size) {
                constraint.add(new Literal(i * size + j, true));
            }
        }
        constraints.add(constraint);
    }
    return constraints;
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Constraint(org.opt4j.satdecoding.Constraint) HashSet(java.util.HashSet)

Aggregations

Literal (org.opt4j.satdecoding.Literal)8 Constraint (org.opt4j.satdecoding.Constraint)7 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Random (java.util.Random)1 VecInt (org.sat4j.core.VecInt)1