Search in sources :

Example 1 with LinearConstraint

use of org.hipparchus.optim.linear.LinearConstraint in project symja_android_library by axkr.

the class LinearProgramming method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    try {
        if (ast.arg1().isList() && ast.arg2().isList() && ast.arg3().isList()) {
            double[] arg1D = Expr2Object.toDoubleVector((IAST) ast.arg1());
            LinearObjectiveFunction f = new LinearObjectiveFunction(arg1D, 0);
            Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
            IAST arg2 = (IAST) ast.arg2();
            IAST arg3 = (IAST) ast.arg3();
            if (arg2.size() != arg3.size()) {
                return F.NIL;
            }
            double[] arg2D;
            double[] arg3D;
            for (int i = 1; i < arg2.size(); i++) {
                arg2D = Expr2Object.toDoubleVector((IAST) arg2.get(i));
                if (arg2.get(i).isList()) {
                    if (arg3.get(i).isList()) {
                        arg3D = Expr2Object.toDoubleVector((IAST) arg3.get(i));
                        if (arg3D.length >= 2) {
                            double val = arg3D[1];
                            if (val == 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.EQ, arg3D[0]));
                            } else if (val < 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.LEQ, arg3D[0]));
                            } else if (val > 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
                            }
                        } else if (arg3D.length == 1) {
                            constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
                        }
                    } else {
                        ISignedNumber sn = arg3.get(i).evalSignedNumber();
                        if (sn != null) {
                            constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
                        } else {
                            throw new WrongArgumentType(arg3, arg3.get(i), i, "Numeric vector or number expected!");
                        }
                    }
                } else {
                    throw new WrongArgumentType(ast, ast.get(i), i, "Numeric vector expected!");
                }
            }
            SimplexSolver solver = new SimplexSolver();
            // PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
            PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
            double[] values = solution.getPointRef();
            return F.List(values);
        }
    } catch (MathIllegalStateException oe) {
        throw new WrappedException(oe);
    // if (Config.SHOW_STACKTRACE) {
    // oe.printStackTrace();
    // }
    }
    return F.NIL;
}
Also used : WrappedException(org.matheclipse.core.eval.exception.WrappedException) LinearConstraintSet(org.hipparchus.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction) ArrayList(java.util.ArrayList) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) PointValuePair(org.hipparchus.optim.PointValuePair) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) SimplexSolver(org.hipparchus.optim.linear.SimplexSolver) IAST(org.matheclipse.core.interfaces.IAST)

Example 2 with LinearConstraint

use of org.hipparchus.optim.linear.LinearConstraint in project symja_android_library by axkr.

the class NMinimize method getConstraints.

protected static List<LinearConstraint> getConstraints(VariablesSet vars, IExpr listOfconstraints) {
    Expr2LP x2LP;
    List<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    IAST andAST = (IAST) listOfconstraints;
    for (int i = 1; i < andAST.size(); i++) {
        x2LP = new Expr2LP(andAST.get(i), vars);
        constraints.add(x2LP.expr2Constraint());
    }
    return constraints;
}
Also used : Expr2LP(org.matheclipse.core.convert.Expr2LP) ArrayList(java.util.ArrayList) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint)

Example 3 with LinearConstraint

use of org.hipparchus.optim.linear.LinearConstraint in project symja_android_library by axkr.

the class NMinimize method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (ast.arg1().isList() && ast.arg2().isList()) {
        IAST list1 = (IAST) ast.arg1();
        IAST list2 = (IAST) ast.arg2();
        VariablesSet vars = new VariablesSet(list2);
        if (list1.isAST2()) {
            IExpr function = list1.arg1();
            IExpr listOfconstraints = list1.arg2();
            if (listOfconstraints.isAnd()) {
                // lc1 && lc2 && lc3...
                LinearObjectiveFunction objectiveFunction = getObjectiveFunction(vars, function);
                List<LinearConstraint> constraints = getConstraints(vars, listOfconstraints);
                return simplexSolver(vars, objectiveFunction, objectiveFunction, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
            }
        }
    }
    return F.NIL;
}
Also used : LinearConstraintSet(org.hipparchus.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with LinearConstraint

use of org.hipparchus.optim.linear.LinearConstraint in project symja_android_library by axkr.

the class NMaximize method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (ast.arg1().isList() && ast.arg2().isList()) {
        IAST list1 = (IAST) ast.arg1();
        IAST list2 = (IAST) ast.arg2();
        VariablesSet vars = new VariablesSet(list2);
        if (list1.isAST2()) {
            IExpr function = list1.arg1();
            IExpr listOfconstraints = list1.arg2();
            if (listOfconstraints.isAnd()) {
                // lc1 && lc2 && lc3...
                LinearObjectiveFunction objectiveFunction = getObjectiveFunction(vars, function);
                List<LinearConstraint> constraints = getConstraints(vars, listOfconstraints);
                return simplexSolver(vars, objectiveFunction, objectiveFunction, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
            }
        }
    }
    return F.NIL;
}
Also used : LinearConstraintSet(org.hipparchus.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with LinearConstraint

use of org.hipparchus.optim.linear.LinearConstraint in project symja_android_library by axkr.

the class Expr2LP method expr2Constraint.

public LinearConstraint expr2Constraint() {
    double[] coefficients = new double[fVariables.size()];
    if (fExpr.isAST()) {
        IAST ast = (IAST) fExpr;
        if (ast.isAST(F.Equal, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.EQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.EQ, -1 * num.doubleValue());
        }
        if (ast.isAST(F.GreaterEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.GEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.GEQ, -1 * num.doubleValue());
        }
        if (ast.isAST(F.LessEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.LEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.LEQ, -1 * num.doubleValue());
        }
    }
    throw new WrongArgumentType(fExpr, "Conversion from expression to linear programming expression failed");
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)5 IAST (org.matheclipse.core.interfaces.IAST)5 NonNegativeConstraint (org.hipparchus.optim.linear.NonNegativeConstraint)4 LinearConstraintSet (org.hipparchus.optim.linear.LinearConstraintSet)3 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ArrayList (java.util.ArrayList)2 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)2 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)2 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)1 PointValuePair (org.hipparchus.optim.PointValuePair)1 SimplexSolver (org.hipparchus.optim.linear.SimplexSolver)1 Expr2LP (org.matheclipse.core.convert.Expr2LP)1 WrappedException (org.matheclipse.core.eval.exception.WrappedException)1