use of jp.ac.kobe_u.cs.cream.DefaultSolver in project symja_android_library by axkr.
the class Solve method integerSolve.
public static IAST integerSolve(final IAST list, final IAST variables) {
IAST result = F.List();
TreeMap<ISymbol, IntVariable> map = new TreeMap<ISymbol, IntVariable>();
// Create a constraint network
Network net = new Network();
for (int i = 1; i < variables.size(); i++) {
if (variables.get(i) instanceof ISymbol) {
map.put((ISymbol) variables.get(i), new IntVariable(net));
}
}
IAST temp;
IntVariable lhs;
IntVariable rhs;
for (int i = 1; i < list.size(); i++) {
if (list.get(i) instanceof IAST) {
temp = (IAST) list.get(i);
if (temp.isAST2()) {
lhs = integerVariable(net, map, temp.arg1());
rhs = integerVariable(net, map, temp.arg2());
if (temp.isAST(F.Equal, 3)) {
lhs.equals(rhs);
} else if (temp.isAST(F.Unequal, 3)) {
lhs.notEquals(rhs);
} else if (temp.isAST(F.Greater, 3)) {
lhs.gt(rhs);
} else if (temp.isAST(F.GreaterEqual, 3)) {
lhs.ge(rhs);
} else if (temp.isAST(F.LessEqual, 3)) {
lhs.le(rhs);
} else if (temp.isAST(F.Less, 3)) {
lhs.lt(rhs);
} else {
return null;
}
}
}
}
Solver solver = new DefaultSolver(net);
solver.findAll(new SolutionHandler() {
@Override
public void solved(Solver solver, Solution solution) {
if (solution != null) {
IAST temp = F.List();
for (Entry<ISymbol, IntVariable> entry : map.entrySet()) {
temp.append(F.Rule(entry.getKey(), F.integer(solution.getIntValue(entry.getValue()))));
}
result.append(temp);
}
}
}, 10000);
// }
return result;
}
Aggregations