Skip to content

Commit

Permalink
resolved empty lhs in linear terms
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhglass committed Jul 3, 2017
1 parent 36a3b51 commit b197f8c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
6 changes: 1 addition & 5 deletions src/main/java/org/jreliability/bdd/BDDConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected void eliminateZeroCoefficients() {
* values.
*/
protected void gcd() {
try {
if(!lhs.isEmpty()){
int gcd = lhs.get(0).getCoefficient();
for (Literal<T> literal : lhs) {
int coefficient = literal.getCoefficient();
Expand All @@ -173,11 +173,7 @@ protected void gcd() {
int newCoefficient = coefficient / gcd;
literal.setCoefficient(newCoefficient);
}
} catch (IndexOutOfBoundsException e) {
// do unless lhs is empty, i.e., 0
// this may be a result of applying eliminateZeroCoefficients()
}

}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/jreliability/bdd/BDDTTRF.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public BDDTTRF(BDDProvider<T> provider) {
* booleanfunction.Term, org.apache.commons.collections15.Transformer)
*/
@Override
public ReliabilityFunction convert(Term term,
Transformer<T, ReliabilityFunction> functionTransformer) {
public ReliabilityFunction convert(Term term, Transformer<T, ReliabilityFunction> functionTransformer) {
return convert(term, functionTransformer, null);
}

Expand All @@ -80,8 +79,7 @@ public ReliabilityFunction convert(Term term,
* org.apache.commons.collections15.Predicate)
*/
@Override
public ReliabilityFunction convert(Term term,
Transformer<T, ReliabilityFunction> functionTransformer,
public ReliabilityFunction convert(Term term, Transformer<T, ReliabilityFunction> functionTransformer,
Predicate<T> existsPredicate) {
BDD<T> bdd = convertToBDD(term, existsPredicate);
return convert(bdd, functionTransformer);
Expand All @@ -98,10 +96,8 @@ public ReliabilityFunction convert(Term term,
* @return a reliability function from the given bdd and function
* functionTransformer
*/
public ReliabilityFunction convert(BDD<T> bdd,
Transformer<T, ReliabilityFunction> functionTransformer) {
BDDReliabilityFunction<T> function = new BDDReliabilityFunction<T>(bdd,
functionTransformer);
public ReliabilityFunction convert(BDD<T> bdd, Transformer<T, ReliabilityFunction> functionTransformer) {
BDDReliabilityFunction<T> function = new BDDReliabilityFunction<T>(bdd, functionTransformer);
bdd.free();
return function;
}
Expand Down Expand Up @@ -174,8 +170,7 @@ protected BDD<T> transform(Term term) {
NOTTerm notTerm = (NOTTerm) term;
bdd = transformNOT(notTerm);
} else {
throw new IllegalArgumentException(
"Unknown Term class in boolean function.");
throw new IllegalArgumentException("Unknown Term class in boolean function.");
}
return bdd;
}
Expand Down Expand Up @@ -232,7 +227,7 @@ protected BDD<T> transformLinear(LinearTerm term) {
}
Comparator comparator = term.getComparator();
int rhs = term.getRHS();
return BDDs.getBDD(coefficients, bdds, comparator, rhs);
return BDDs.getBDD(coefficients, bdds, comparator, rhs, provider);
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/jreliability/bdd/BDDs.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ public static <T> Set<BDD<T>> getNodes(T t, BDD<T> bdd) {
* the right hand side value
* @return the BDD representing this linear constraint
*/
public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearTerm.Comparator comp, int rhs) {
public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearTerm.Comparator comp, int rhs, BDDProvider<T> provider) {
assert (coeffs.size() == vars.size());

BDD<T> result;

switch (comp) {
case EQUAL:
BDD<T> ge = getBDD(coeffs, vars, LinearTerm.Comparator.GREATEREQUAL, rhs);
BDD<T> le = getBDD(coeffs, vars, LinearTerm.Comparator.LESSEQUAL, rhs);
BDD<T> ge = getBDD(coeffs, vars, LinearTerm.Comparator.GREATEREQUAL, rhs, provider);
BDD<T> le = getBDD(coeffs, vars, LinearTerm.Comparator.LESSEQUAL, rhs, provider);
ge.andWith(le);
result = ge;
break;
case GREATER:
result = getBDD(coeffs, vars, LinearTerm.Comparator.GREATEREQUAL, rhs + 1);
result = getBDD(coeffs, vars, LinearTerm.Comparator.GREATEREQUAL, rhs + 1, provider);
break;
case GREATEREQUAL:
List<Literal<T>> lits = new ArrayList<>();
Expand All @@ -120,25 +120,24 @@ public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearT
* If 0 >= rhs, return true BDD; else return false BDD
*/
if(constraint.getLhs().isEmpty()) {
BDDProvider<T> provider = lits.get(0).getVariable().getProvider();
if(0 >= constraint.getRhs()) {
result = provider.one();
} else {
result = provider.zero();
}
} else{
result = getConstraintBDD(constraint);
result = getConstraintBDD(constraint, provider);
}
break;
case LESS:
result = getBDD(coeffs, vars, LinearTerm.Comparator.LESSEQUAL, rhs - 1);
result = getBDD(coeffs, vars, LinearTerm.Comparator.LESSEQUAL, rhs - 1, provider);
break;
case LESSEQUAL:
List<Integer> negativeCoeffs = new ArrayList<>();
for (int c : coeffs) {
negativeCoeffs.add(-c);
}
result = getBDD(negativeCoeffs, vars, LinearTerm.Comparator.GREATEREQUAL, -rhs);
result = getBDD(negativeCoeffs, vars, LinearTerm.Comparator.GREATEREQUAL, -rhs, provider);
break;
default:
throw new IllegalArgumentException("Unknown comparator in LinearTerm: " + comp);
Expand All @@ -156,7 +155,7 @@ public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearT
* the greater-equal constraint
* @return the bdd representation of the given constraint
*/
protected static <T> BDD<T> getConstraintBDD(BDDConstraint<T> constraint) {
protected static <T> BDD<T> getConstraintBDD(BDDConstraint<T> constraint, BDDProvider<T> provider) {
List<Literal<T>> literals = constraint.getLhs();

Collections.sort(literals, new Comparator<Literal<T>>() {
Expand All @@ -167,7 +166,6 @@ public int compare(Literal<T> o1, Literal<T> o2) {
});

int materialLeft = 0;
BDDProvider<T> provider = literals.get(0).getVariable().getProvider();
for (Literal<T> literal : literals) {
int coefficient = literal.getCoefficient();
materialLeft += coefficient;
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/jreliability/bdd/AbstractBDDOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public void testGetBDDGreaterEqual() {
BDD<String> a = provider.get("a");
BDD<String> b = provider.get("b");
BDD<String> c = provider.get("c");
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.GREATEREQUAL, 2);
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.GREATEREQUAL, 2, provider);

BDD<String> ref1 = a.and(b);
BDD<String> ref2 = b.and(c);
Expand All @@ -419,7 +419,7 @@ public void testGetBDDGreater() {
BDD<String> a = provider.get("a");
BDD<String> b = provider.get("b");
BDD<String> c = provider.get("c");
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.GREATER, 2);
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.GREATER, 2, provider);

BDD<String> ref1 = a.and(b).and(c);
Assert.assertEquals(test, ref1);
Expand All @@ -437,7 +437,7 @@ public void testGetBDDLess() {
BDD<String> a = provider.get("a");
BDD<String> b = provider.get("b");
BDD<String> c = provider.get("c");
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.LESS, 1);
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.LESS, 1, provider);

BDD<String> ref1 = a.not().and(b.not()).and(c.not());
Assert.assertEquals(test, ref1);
Expand All @@ -455,7 +455,7 @@ public void testGetBDDLessEqual() {
BDD<String> a = provider.get("a");
BDD<String> b = provider.get("b");
BDD<String> c = provider.get("c");
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.LESSEQUAL, 0);
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.LESSEQUAL, 0, provider);

BDD<String> ref1 = a.not().and(b.not()).and(c.not());
Assert.assertEquals(test, ref1);
Expand All @@ -473,7 +473,7 @@ public void testGetBDDEqual() {
BDD<String> a = provider.get("a");
BDD<String> b = provider.get("b");
BDD<String> c = provider.get("c");
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.EQUAL, 1);
BDD<String> test = BDDs.getBDD(Arrays.asList(1, 1, 1), Arrays.asList(a, b, c), Comparator.EQUAL, 1, provider);

BDD<String> ref1 = a.and(b.not()).and(c.not());
BDD<String> ref2 = a.not().and(b).and(c.not());
Expand Down

0 comments on commit b197f8c

Please sign in to comment.