use of org.logicng.datastructures.Assignment in project symja_android_library by axkr.
the class BooleanFunctions method solveInstances.
/**
* Example: Create a list of rules in the form <code>
* {{a->False,b->True,c->False,d->False},{a->True,b->False,c->False,d->False},...}</code> for the
* variables <code>{a,b,c,d}</code>
*
* @param booleanExpression an expression build from symbols and boolean operators like <code>
* And, Or, Not, Xor, Nand, Nor, Implies, Equivalent,...</code>
* @param variables the possible variables. Example: <code>{a,b,c,d}</code>
* @param maximumNumberOfResults
* @return
*/
public static IAST solveInstances(IExpr booleanExpression, IAST variables, int maximumNumberOfResults) {
LogicFormula lf = new LogicFormula();
Variable[] vars = lf.ast2Variable(variables);
List<Assignment> assignments = logicNGSatisfiabilityInstances(booleanExpression, vars, lf, maximumNumberOfResults);
Object2IntMap<String> map = LogicFormula.name2Position(vars);
IASTAppendable list = F.ListAlloc(assignments.size());
for (int i = 0; i < assignments.size(); i++) {
if (i >= maximumNumberOfResults) {
break;
}
list.append(lf.literals2VariableList(assignments.get(i).literals(), map));
}
EvalAttributes.sort(list, Comparators.REVERSE_CANONICAL_COMPARATOR);
return list;
}
use of org.logicng.datastructures.Assignment in project symja_android_library by axkr.
the class BooleanFunctions method satisfiabilityInstances.
/**
* Use LogicNG MiniSAT method.
*
* <p>
* Example: Create a list of rules in the form <code>
* {{False,True,False,False},{True,False,False,False},...}</code> for the variables <code>
* {a,b,c,d}</code>
*
* @param booleanExpression an expression build from symbols and boolean operators like <code>
* And, Or, Not, Xor, Nand, Nor, Implies, Equivalent,...</code>
* @param variables the possible variables. Example: <code>{a,b,c,d}</code>
* @param maxChoices maximum number of choices, which satisfy the given boolean expression
* @return
*/
public static IAST satisfiabilityInstances(IExpr booleanExpression, IAST variables, int maxChoices) {
LogicFormula lf = new LogicFormula();
Variable[] vars = lf.ast2Variable(variables);
List<Assignment> assignments = logicNGSatisfiabilityInstances(booleanExpression, vars, lf, maxChoices);
Object2IntMap<String> map = LogicFormula.name2Position(vars);
IASTAppendable list = F.ListAlloc(assignments.size());
for (int i = 0; i < assignments.size(); i++) {
if (i >= maxChoices) {
break;
}
//
list.append(//
lf.literals2BooleanList(assignments.get(i).literals(), map));
}
EvalAttributes.sort(list, Comparators.REVERSE_CANONICAL_COMPARATOR);
return list;
}
Aggregations