Fix inequality bugs

This commit is contained in:
Alex Birkett 2016-01-31 12:14:49 +01:00
parent 88307a9f06
commit 837748cb5b
6 changed files with 360 additions and 14 deletions

View File

@ -298,7 +298,7 @@ public class Symbolics {
} }
public static Constraint lessThanOrEqualTo(Term term, Expression expression) { public static Constraint lessThanOrEqualTo(Term term, Expression expression) {
return lessThanOrEqualTo(expression, term); return lessThanOrEqualTo(new Expression(term), expression);
} }
public static Constraint lessThanOrEqualTo(Term first, Term second) { public static Constraint lessThanOrEqualTo(Term first, Term second) {
@ -314,7 +314,7 @@ public class Symbolics {
} }
public static Constraint greaterThanOrEqualTo(Term term, Expression expression) { public static Constraint greaterThanOrEqualTo(Term term, Expression expression) {
return greaterThanOrEqualTo(expression, term); return greaterThanOrEqualTo(new Expression(term), expression);
} }
public static Constraint greaterThanOrEqualTo(Term first, Term second) { public static Constraint greaterThanOrEqualTo(Term first, Term second) {
@ -347,11 +347,11 @@ public class Symbolics {
} }
public static Constraint lessThanOrEqualTo(Variable variable, Expression expression) { public static Constraint lessThanOrEqualTo(Variable variable, Expression expression) {
return lessThanOrEqualTo(expression, variable); return lessThanOrEqualTo(new Term(variable), expression);
} }
public static Constraint lessThanOrEqualTo(Variable variable, Term term) { public static Constraint lessThanOrEqualTo(Variable variable, Term term) {
return lessThanOrEqualTo(term, variable); return lessThanOrEqualTo(new Term(variable), term);
} }
public static Constraint lessThanOrEqualTo(Variable first, Variable second) { public static Constraint lessThanOrEqualTo(Variable first, Variable second) {
@ -363,7 +363,7 @@ public class Symbolics {
} }
public static Constraint greaterThanOrEqualTo(Variable variable, Expression expression) { public static Constraint greaterThanOrEqualTo(Variable variable, Expression expression) {
return greaterThanOrEqualTo(expression, variable); return greaterThanOrEqualTo(new Term(variable), expression);
} }
public static Constraint greaterThanOrEqualTo(Variable variable, Term term) { public static Constraint greaterThanOrEqualTo(Variable variable, Term term) {
@ -392,27 +392,23 @@ public class Symbolics {
} }
public static Constraint lessThanOrEqualTo(double constant, Expression expression) { public static Constraint lessThanOrEqualTo(double constant, Expression expression) {
return lessThanOrEqualTo(expression, constant); return lessThanOrEqualTo(new Expression(constant), expression);
} }
public static Constraint lessThanOrEqualTo(double constant, Term term) { public static Constraint lessThanOrEqualTo(double constant, Term term) {
return lessThanOrEqualTo(term, constant); return lessThanOrEqualTo(constant, new Expression(term));
} }
public static Constraint lessThanOrEqualTo(double constant, Variable variable) { public static Constraint lessThanOrEqualTo(double constant, Variable variable) {
return lessThanOrEqualTo(variable, constant); return lessThanOrEqualTo(constant, new Term(variable));
}
public static Constraint greaterThanOrEqualTo(double constant, Expression expression) {
return greaterThanOrEqualTo(expression, constant);
} }
public static Constraint greaterThanOrEqualTo(double constant, Term term) { public static Constraint greaterThanOrEqualTo(double constant, Term term) {
return greaterThanOrEqualTo(term, constant); return greaterThanOrEqualTo(new Expression(constant), term);
} }
public static Constraint greaterThanOrEqualTo(double constant, Variable variable) { public static Constraint greaterThanOrEqualTo(double constant, Variable variable) {
return greaterThanOrEqualTo(variable, constant); return greaterThanOrEqualTo(constant, new Term(variable));
} }
// Constraint strength modifier // Constraint strength modifier

View File

@ -0,0 +1,66 @@
package no.birkett.kiwi.inequality;
import org.junit.Test;
import no.birkett.kiwi.DuplicateConstraintException;
import no.birkett.kiwi.Solver;
import no.birkett.kiwi.Symbolics;
import no.birkett.kiwi.UnsatisfiableConstraintException;
import no.birkett.kiwi.Variable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by alex on 31/01/16.
*/
public class ConstantVariableTest {
private static double EPSILON = 1.0e-8;
@Test
public void lessThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(100, x));
solver.updateVariables();
assertTrue(100 <= x.getValue());
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
assertEquals(x.getValue(), 110, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void lessThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(100, x));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 10));
solver.updateVariables();
}
@Test
public void greaterThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(100, x));
solver.updateVariables();
assertTrue(100 >= x.getValue());
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
assertEquals(x.getValue(), 90, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void greaterThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(100, x));
solver.updateVariables();
assertTrue(100 >= x.getValue());
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
}
}

View File

@ -0,0 +1,66 @@
package no.birkett.kiwi.inequality;
import org.junit.Test;
import no.birkett.kiwi.DuplicateConstraintException;
import no.birkett.kiwi.Expression;
import no.birkett.kiwi.Solver;
import no.birkett.kiwi.Symbolics;
import no.birkett.kiwi.UnsatisfiableConstraintException;
import no.birkett.kiwi.Variable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by alex on 31/01/16.
*/
public class ExpressionVariableTest {
private static double EPSILON = 1.0e-8;
@Test
public void lessThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(new Expression(100), x));
solver.updateVariables();
assertTrue(100 <= x.getValue());
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
assertEquals(x.getValue(), 110, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void lessThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(new Expression(100), x));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 10));
solver.updateVariables();
}
@Test
public void greaterThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(new Expression(100), x));
solver.updateVariables();
assertTrue(100 >= x.getValue());
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
assertEquals(x.getValue(), 90, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void greaterThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(new Expression(100), x));
solver.updateVariables();
assertTrue(100 >= x.getValue());
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
}
}

View File

@ -0,0 +1,65 @@
package no.birkett.kiwi.inequality;
import org.junit.Test;
import no.birkett.kiwi.DuplicateConstraintException;
import no.birkett.kiwi.Solver;
import no.birkett.kiwi.Symbolics;
import no.birkett.kiwi.UnsatisfiableConstraintException;
import no.birkett.kiwi.Variable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by alex on 31/01/16.
*/
public class VariableConstantTest {
private static double EPSILON = 1.0e-8;
@Test
public void lessThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, 100));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
assertEquals(x.getValue(), 90, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void lessThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, 100));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
}
@Test
public void greaterThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, 100));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
assertEquals(x.getValue(), 110, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void greaterThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, 100));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
}
}

View File

@ -0,0 +1,66 @@
package no.birkett.kiwi.inequality;
import org.junit.Test;
import no.birkett.kiwi.DuplicateConstraintException;
import no.birkett.kiwi.Expression;
import no.birkett.kiwi.Solver;
import no.birkett.kiwi.Symbolics;
import no.birkett.kiwi.UnsatisfiableConstraintException;
import no.birkett.kiwi.Variable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by alex on 31/01/16.
*/
public class VariableExpression {
private static double EPSILON = 1.0e-8;
@Test
public void lessThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, new Expression(100)));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
assertEquals(x.getValue(), 90, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void lessThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, new Expression(100)));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
}
@Test
public void greaterThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, new Expression(100)));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
assertEquals(x.getValue(), 110, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void greaterThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Variable x = new Variable("x");
Solver solver = new Solver();
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, 100));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
}
}

View File

@ -0,0 +1,87 @@
package no.birkett.kiwi.inequality;
import org.junit.Test;
import no.birkett.kiwi.DuplicateConstraintException;
import no.birkett.kiwi.Solver;
import no.birkett.kiwi.Symbolics;
import no.birkett.kiwi.UnsatisfiableConstraintException;
import no.birkett.kiwi.Variable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by alex on 31/01/16.
*/
public class VariableVariableTest {
private static double EPSILON = 1.0e-8;
@Test
public void lessThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Solver solver = new Solver();
Variable x = new Variable("x");
Variable y = new Variable("y");
solver.addConstraint(Symbolics.equals(y, 100));
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, y));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
assertEquals(x.getValue(), 90, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void lessThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Solver solver = new Solver();
Variable x = new Variable("x");
Variable y = new Variable("y");
solver.addConstraint(Symbolics.equals(y, 100));
solver.addConstraint(Symbolics.lessThanOrEqualTo(x, y));
solver.updateVariables();
assertTrue(x.getValue() <= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
}
@Test
public void greaterThanEqualTo() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Solver solver = new Solver();
Variable x = new Variable("x");
Variable y = new Variable("y");
solver.addConstraint(Symbolics.equals(y, 100));
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, y));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 110));
solver.updateVariables();
assertEquals(x.getValue(), 110, EPSILON);
}
@Test(expected = UnsatisfiableConstraintException.class)
public void greaterThanEqualToUnsatisfiable() throws DuplicateConstraintException, UnsatisfiableConstraintException {
Solver solver = new Solver();
Variable x = new Variable("x");
Variable y = new Variable("y");
solver.addConstraint(Symbolics.equals(y, 100));
solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, y));
solver.updateVariables();
assertTrue(x.getValue() >= 100);
solver.addConstraint(Symbolics.equals(x, 90));
solver.updateVariables();
}
}