Example usage for org.apache.commons.math3.optim.linear LinearConstraint LinearConstraint

List of usage examples for org.apache.commons.math3.optim.linear LinearConstraint LinearConstraint

Introduction

In this page you can find the example usage for org.apache.commons.math3.optim.linear LinearConstraint LinearConstraint.

Prototype

public LinearConstraint(final RealVector coefficients, final Relationship relationship, final double value) 

Source Link

Document

Build a constraint involving a single linear equation.

Usage

From source file:org.lightjason.agentspeak.action.builtin.math.linearprogram.CValueConstraint.java

@Nonnull
@Override//from  ww  w .  j a  va 2 s .  c  om
public final IFuzzyValue<Boolean> execute(final boolean p_parallel, @Nonnull final IContext p_context,
        @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return) {
    final List<ITerm> l_arguments = CCommon.flatten(p_argument).collect(Collectors.toList());

    // create linear constraint based on a value
    l_arguments.get(0).<Pair<LinearObjectiveFunction, Collection<LinearConstraint>>>raw().getRight()
            .add(new LinearConstraint(
                    l_arguments.stream().limit(l_arguments.size() - 2).skip(1)
                            .mapToDouble(i -> i.<Number>raw().doubleValue()).toArray(),
                    this.getRelation(l_arguments.get(l_arguments.size() - 2).<String>raw()),
                    l_arguments.get(l_arguments.size() - 1).<Number>raw().doubleValue()));

    return CFuzzyValue.from(true);
}

From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathLinearprogram.java

/**
 * test solve maximum/*w  w w  . j  av  a 2  s .com*/
 */
@Test
public final void solvemaximize() {
    final List<ITerm> l_return = new ArrayList<>();
    final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammax = new ImmutablePair<>(
            new LinearObjectiveFunction(new double[] { 3, 5 }, 0.0), new HashSet<LinearConstraint>());

    l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 2, 8 }, Relationship.LEQ, 13));
    l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 5, -1 }, Relationship.LEQ, 11));
    l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 0));
    l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0));

    new CSolve().execute(false, IContext.EMPTYPLAN, Stream.of(l_linearprogrammax, "maximize", "non-negative")
            .map(CRawTerm::from).collect(Collectors.toList()), l_return);

    Assert.assertArrayEquals(l_return.stream().map(ITerm::raw).toArray(),
            Stream.of(12.333333333333332, 2, 2.4047619047619047, 1.0238095238095237).toArray());
}

From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathLinearprogram.java

/**
 * test solve minimize//w ww.  j av a 2  s .c  o  m
 */
@Test
public final void solveminimize() {
    final List<ITerm> l_return = new ArrayList<>();
    final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammin = new ImmutablePair<>(
            new LinearObjectiveFunction(new double[] { -2, 15 }, 0.0), new HashSet<LinearConstraint>());

    l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { -6, 8 }, Relationship.GEQ, 3));
    l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 5, -1 }, Relationship.GEQ, 11));
    l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 0));
    l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0));

    new CSolve().execute(false, IContext.EMPTYPLAN, Stream.of(l_linearprogrammin, "minimize", "non-negative")
            .map(CRawTerm::from).collect(Collectors.toList()), l_return);

    Assert.assertArrayEquals(l_return.stream().map(ITerm::raw).toArray(),
            Stream.of(30.38235294117647, 2, 2.676470588235294, 2.3823529411764706).toArray());
}

From source file:uk.ac.tgac.metaopt.external.Apache.java

protected void addDecisionVariables(Collection<LinearConstraint> constraints, Problem problem) {

    List<Variable> variables = problem.getVariables();

    // Add restriction constraint
    for (int i = 0; i < variables.size(); i++) {

        Variable var = variables.get(i);

        double[] coefficients = new double[variables.size()];

        for (int j = 0; j < variables.size(); j++) {

            coefficients[j] = i == j ? 1.0 : 0.0;
        }/*from w  w  w  .j  av  a 2 s  .  c o  m*/

        Bounds.BoundType boundType = var.getBounds().getBoundType();

        if (boundType == Bounds.BoundType.FREE) {
            // Do nothing in this case
        } else if (boundType == Bounds.BoundType.LOWER) {
            constraints.add(new LinearConstraint(coefficients, Relationship.GEQ, var.getBounds().getLower()));
        } else if (boundType == Bounds.BoundType.UPPER) {
            constraints.add(new LinearConstraint(coefficients, Relationship.LEQ, var.getBounds().getUpper()));
        } else if (boundType == Bounds.BoundType.DOUBLE) {
            constraints.add(new LinearConstraint(coefficients, Relationship.GEQ, var.getBounds().getLower()));
            constraints.add(new LinearConstraint(coefficients, Relationship.LEQ, var.getBounds().getUpper()));
        } else if (boundType == Bounds.BoundType.FIXED) {
            constraints.add(new LinearConstraint(coefficients, Relationship.EQ, var.getBounds().getLower()));
        } else {
            throw new IllegalArgumentException("Unknown bound type encountered: " + boundType.toString());
        }
    }
}

From source file:uk.ac.tgac.metaopt.external.Apache.java

protected void addRegularConstraints(Collection<LinearConstraint> apacheConstraints, Problem problem) {

    for (Constraint constraint : problem.getConstraints()) {

        double[] coefficients = new double[problem.getNbVariables()];

        List<LinearTerm> terms = constraint.getExpression().getLinearTerms();

        for (LinearTerm term : terms) {
            for (int j = 0; j < problem.getNbVariables(); j++) {

                if (problem.getVariables().get(j).getName().equals(term.getVariable().getName())) {
                    coefficients[j] = term.getCoefficient();
                }//from  www.  ja  va  2  s .  com
            }
        }

        Relationship relationship = this.convertRelationship(constraint.getRelation());

        apacheConstraints.add(new LinearConstraint(coefficients, relationship,
                constraint.getValue() - constraint.getExpression().getConstant()));
    }
}