Search in sources :

Example 1 with NonNegativeConstraint

use of org.hipparchus.optim.linear.NonNegativeConstraint 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 2 with NonNegativeConstraint

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

the class LinearProgramming method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    try {
        if (ast.arg1().isList() && ast.arg2().isList() && ast.arg3().isList()) {
            double[] arg1D = ast.arg1().toDoubleVector();
            if (arg1D != null) {
                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++) {
                    if (arg2.get(i).isList()) {
                        arg2D = arg2.get(i).toDoubleVector();
                        if (arg2D == null) {
                            return F.NIL;
                        }
                        if (arg3.get(i).isList()) {
                            arg3D = arg3.get(i).toDoubleVector();
                            if (arg3D == null) {
                                return F.NIL;
                            }
                            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).evalReal();
                            if (sn != null) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
                            } else {
                                LOGGER.log(engine.getLogLevel(), "Numeric vector or number expected!");
                                return F.NIL;
                            }
                        }
                    } else {
                        LOGGER.log(engine.getLogLevel(), "Numeric vector expected!");
                        return F.NIL;
                    }
                }
                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 (MathIllegalArgumentException miae) {
        // `1`.
        return IOFunctions.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())), engine);
    } catch (MathRuntimeException mre) {
        LOGGER.log(engine.getLogLevel(), ast.topHead(), mre);
    }
    return F.NIL;
}
Also used : MathRuntimeException(org.hipparchus.exception.MathRuntimeException) LinearConstraintSet(org.hipparchus.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) PointValuePair(org.hipparchus.optim.PointValuePair) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) SimplexSolver(org.hipparchus.optim.linear.SimplexSolver) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) IAST(org.matheclipse.core.interfaces.IAST)

Example 3 with NonNegativeConstraint

use of org.hipparchus.optim.linear.NonNegativeConstraint 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)

Aggregations

LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 LinearConstraintSet (org.hipparchus.optim.linear.LinearConstraintSet)3 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)3 NonNegativeConstraint (org.hipparchus.optim.linear.NonNegativeConstraint)3 IAST (org.matheclipse.core.interfaces.IAST)3 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 ArrayList (java.util.ArrayList)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)1 PointValuePair (org.hipparchus.optim.PointValuePair)1 SimplexSolver (org.hipparchus.optim.linear.SimplexSolver)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1