Search in sources :

Example 6 with Constraint

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

Example 7 with Constraint

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

the class ConstraintTest method equalityTestOfOperator.

@Test
public void equalityTestOfOperator() {
    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, true));
    Constraint c2 = new Constraint(Operator.GE, 2);
    c2.add(2, new Literal(object1, true));
    c2.add(2, new Literal(object2, true));
    Constraint c3 = new Constraint(Operator.EQ, 2);
    c3.add(2, new Literal(object1, true));
    c3.add(2, new Literal(object2, true));
    Assert.assertFalse(c1.equals(c2));
    Assert.assertFalse(c1.equals(c3));
    Assert.assertFalse(c2.equals(c3));
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Test(org.junit.Test)

Example 8 with Constraint

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

the class ConstraintTest method equalityTestOfRhs2.

@Test
public void equalityTestOfRhs2() {
    Constraint c1 = new Constraint(">=", 1);
    c1.add(2, new Literal("papa", true));
    Constraint c2 = new Constraint("<=", 1);
    c2.add(2, new Literal("lala", true));
    // returns true, should be false
    Assert.assertFalse(c1.equals(c2));
}
Also used : Constraint(org.opt4j.satdecoding.Constraint) Literal(org.opt4j.satdecoding.Literal) Test(org.junit.Test)

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