Search in sources :

Example 1 with Network

use of jp.ac.kobe_u.cs.cream.Network 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;
}
Also used : DefaultSolver(jp.ac.kobe_u.cs.cream.DefaultSolver) Solver(jp.ac.kobe_u.cs.cream.Solver) ISymbol(org.matheclipse.core.interfaces.ISymbol) SolutionHandler(jp.ac.kobe_u.cs.cream.SolutionHandler) TreeMap(java.util.TreeMap) IntVariable(jp.ac.kobe_u.cs.cream.IntVariable) Entry(java.util.Map.Entry) DefaultSolver(jp.ac.kobe_u.cs.cream.DefaultSolver) Network(jp.ac.kobe_u.cs.cream.Network) IAST(org.matheclipse.core.interfaces.IAST) Solution(jp.ac.kobe_u.cs.cream.Solution)

Aggregations

Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 DefaultSolver (jp.ac.kobe_u.cs.cream.DefaultSolver)1 IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)1 Network (jp.ac.kobe_u.cs.cream.Network)1 Solution (jp.ac.kobe_u.cs.cream.Solution)1 SolutionHandler (jp.ac.kobe_u.cs.cream.SolutionHandler)1 Solver (jp.ac.kobe_u.cs.cream.Solver)1 IAST (org.matheclipse.core.interfaces.IAST)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1