24 package org.lightjason.agentspeak.action.builtin;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.commons.math3.optim.linear.LinearConstraint;
28 import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
29 import org.apache.commons.math3.optim.linear.Relationship;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.stream.Collectors;
48 import java.util.stream.Stream;
58 private ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>>
m_linearprogram;
67 m_linearprogram =
new ImmutablePair<>(
new LinearObjectiveFunction(
new double[]{}, 0.0 ),
new HashSet<LinearConstraint>() );
78 final List<ITerm> l_return =
new ArrayList<>();
82 Stream.of( 2, 3, 4, 2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
86 Assert.assertEquals( l_return.size(), 1 );
87 Assert.assertNotNull( l_return.get( 0 ).<ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>>>raw().getLeft() );
88 Assert.assertTrue( l_return.get( 0 ).<ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>>>raw().getRight().isEmpty() );
99 Stream.of( m_linearprogram, 2.0, 2.0, 12.0, 19.0,
"=", 11.0 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
100 Collections.emptyList()
103 Assert.assertEquals( m_linearprogram.getRight().size(), 1 );
104 Assert.assertEquals( 11.0, m_linearprogram.getRight().iterator().next().getValue(), 0 );
114 final LinearConstraint l_result =
new LinearConstraint(
new double[]{2, 7, 12}, 19.0, Relationship.EQ,
new double[]{1, 2, 3}, 5.0 );
118 Stream.of( m_linearprogram, 2, 7, 12, 19.0,
"=", 1, 2, 3, 5.0 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
119 Collections.emptyList()
123 Assert.assertEquals( m_linearprogram.getRight().size(), 1 );
124 Assert.assertArrayEquals(
127 l_result.getRelationship(),
128 l_result.getCoefficients()
132 m_linearprogram.getRight().iterator().next().getValue(),
133 m_linearprogram.getRight().iterator().next().getRelationship(),
134 m_linearprogram.getRight().iterator().next().getCoefficients()
145 final List<ITerm> l_return =
new ArrayList<>();
146 final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammax =
new ImmutablePair<>(
147 new LinearObjectiveFunction(
new double[]{3, 5}, 0.0 ),
148 new HashSet<LinearConstraint>()
151 l_linearprogrammax.getRight().add(
new LinearConstraint(
new double[] {2, 8}, Relationship.LEQ, 13 ) );
152 l_linearprogrammax.getRight().add(
new LinearConstraint(
new double[] {5, -1}, Relationship.LEQ, 11 ) );
153 l_linearprogrammax.getRight().add(
new LinearConstraint(
new double[] {1, 0}, Relationship.GEQ, 0 ) );
154 l_linearprogrammax.getRight().add(
new LinearConstraint(
new double[] {0, 1}, Relationship.GEQ, 0 ) );
159 Stream.of( l_linearprogrammax,
"maximize",
"non-negative" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
163 Assert.assertArrayEquals(
164 l_return.stream().map(
ITerm::raw ).toArray(),
165 Stream.of( 12.333333333333332, 2, 2.4047619047619047, 1.0238095238095237 ).toArray()
175 final List<ITerm> l_return =
new ArrayList<>();
176 final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammin =
new ImmutablePair<>(
177 new LinearObjectiveFunction(
new double[]{-2, 15}, 0.0 ),
178 new HashSet<LinearConstraint>()
181 l_linearprogrammin.getRight().add(
new LinearConstraint(
new double[] {-6, 8}, Relationship.GEQ, 3 ) );
182 l_linearprogrammin.getRight().add(
new LinearConstraint(
new double[] {5, -1}, Relationship.GEQ, 11 ) );
183 l_linearprogrammin.getRight().add(
new LinearConstraint(
new double[] {1, 0}, Relationship.GEQ, 0 ) );
184 l_linearprogrammin.getRight().add(
new LinearConstraint(
new double[] {0, 1}, Relationship.GEQ, 0 ) );
188 Stream.of( l_linearprogrammin,
"minimize",
"non-negative" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
192 Assert.assertArrayEquals(
193 l_return.stream().map(
ITerm::raw ).toArray(),
194 Stream.of( 30.38235294117647, 2, 2.676470588235294, 2.3823529411764706 ).toArray()
base test class with helpers
add a linear value constraint to the LP.
action to create a linear program.
IContext EMPTYPLAN
empty context with plan
final void valueconstraint()
test value constraint
test math linear program functions
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)
defines a plan-body operation
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)
defines a plan-body operation
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)
defines a plan-body operation
solves the linear program and returns the solution.
add a linear equation constraint to the LP.
execution context with local data
final void solvemaximize()
test solve maximum
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)
defines a plan-body operation
solver of matrix-equation.
final void initialize()
initialize
< T > T raw()
cast to any raw value type
final void solveminimize()
test solve minimize
final void equationconstraint()
test equation constraint
final void create()
test create
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
ImmutablePair< LinearObjectiveFunction, Collection< LinearConstraint > > m_linearprogram
testing linear program
term structure for simple datatypes