Search in sources :

Example 1 with IntVariable

use of jp.ac.kobe_u.cs.cream.IntVariable in project symja_android_library by axkr.

the class Solve method integerVariable.

public static IntVariable integerVariable(Network net, TreeMap<ISymbol, IntVariable> map, IExpr expr) throws ArithmeticException {
    if (expr instanceof ISymbol) {
        return map.get(expr);
    }
    if (expr instanceof IInteger) {
        // throws ArithmeticException
        int value = ((IInteger) expr).toInt();
        return new IntVariable(net, value);
    }
    if (expr.isPlus()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.add(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isTimes()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.multiply(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isPower()) {
        IAST ast = (IAST) expr;
        if (ast.arg2().isInteger()) {
            int value = ((IInteger) ast.arg2()).toInt();
            if (value > 0) {
                IntVariable result = integerVariable(net, map, ast.arg1());
                for (int i = 1; i < value; i++) {
                    result = result.multiply(integerVariable(net, map, ast.arg1()));
                }
                return result;
            }
        }
    } else if (expr.isASTSizeGE(F.Max, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.max(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isASTSizeGE(F.Min, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.min(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isAbs()) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).abs();
    } else if (expr.isAST(F.Sign, 2)) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).sign();
    }
    throw new WrongArgumentType(expr, "for Solve(..., Integers)");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IntVariable(jp.ac.kobe_u.cs.cream.IntVariable)

Example 2 with IntVariable

use of jp.ac.kobe_u.cs.cream.IntVariable 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

IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)2 IAST (org.matheclipse.core.interfaces.IAST)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 DefaultSolver (jp.ac.kobe_u.cs.cream.DefaultSolver)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 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 IInteger (org.matheclipse.core.interfaces.IInteger)1