LightJason - AgentSpeak(L++)
TestCActionMathLinearprogram.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
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;
41 
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;
49 
53 public final class TestCActionMathLinearprogram extends IBaseTest
54 {
58  private ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> m_linearprogram;
59 
60 
64  @Before
65  public final void initialize()
66  {
67  m_linearprogram = new ImmutablePair<>( new LinearObjectiveFunction( new double[]{}, 0.0 ), new HashSet<LinearConstraint>() );
68  }
69 
70 
71 
75  @Test
76  public final void create()
77  {
78  final List<ITerm> l_return = new ArrayList<>();
79 
80  new CCreate().execute(
81  false, IContext.EMPTYPLAN,
82  Stream.of( 2, 3, 4, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
83  l_return
84  );
85 
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() );
89  }
90 
94  @Test
95  public final void valueconstraint()
96  {
98  false, IContext.EMPTYPLAN,
99  Stream.of( m_linearprogram, 2.0, 2.0, 12.0, 19.0, "=", 11.0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
100  Collections.emptyList()
101  );
102 
103  Assert.assertEquals( m_linearprogram.getRight().size(), 1 );
104  Assert.assertEquals( 11.0, m_linearprogram.getRight().iterator().next().getValue(), 0 );
105  }
106 
107 
111  @Test
112  public final void equationconstraint()
113  {
114  final LinearConstraint l_result = new LinearConstraint( new double[]{2, 7, 12}, 19.0, Relationship.EQ, new double[]{1, 2, 3}, 5.0 );
115 
117  false, IContext.EMPTYPLAN,
118  Stream.of( m_linearprogram, 2, 7, 12, 19.0, "=", 1, 2, 3, 5.0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
119  Collections.emptyList()
120  );
121 
122 
123  Assert.assertEquals( m_linearprogram.getRight().size(), 1 );
124  Assert.assertArrayEquals(
125  Stream.of(
126  l_result.getValue(),
127  l_result.getRelationship(),
128  l_result.getCoefficients()
129  ).toArray(),
130 
131  Stream.of(
132  m_linearprogram.getRight().iterator().next().getValue(),
133  m_linearprogram.getRight().iterator().next().getRelationship(),
134  m_linearprogram.getRight().iterator().next().getCoefficients()
135  ).toArray()
136  );
137  }
138 
142  @Test
143  public final void solvemaximize()
144  {
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>()
149  );
150 
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 ) );
155 
156 
157  new CSolve().execute(
158  false, IContext.EMPTYPLAN,
159  Stream.of( l_linearprogrammax, "maximize", "non-negative" ).map( CRawTerm::from ).collect( Collectors.toList() ),
160  l_return
161  );
162 
163  Assert.assertArrayEquals(
164  l_return.stream().map( ITerm::raw ).toArray(),
165  Stream.of( 12.333333333333332, 2, 2.4047619047619047, 1.0238095238095237 ).toArray()
166  );
167  }
168 
172  @Test
173  public final void solveminimize()
174  {
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>()
179  );
180 
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 ) );
185 
186  new CSolve().execute(
187  false, IContext.EMPTYPLAN,
188  Stream.of( l_linearprogrammin, "minimize", "non-negative" ).map( CRawTerm::from ).collect( Collectors.toList() ),
189  l_return
190  );
191 
192  Assert.assertArrayEquals(
193  l_return.stream().map( ITerm::raw ).toArray(),
194  Stream.of( 30.38235294117647, 2, 2.676470588235294, 2.3823529411764706 ).toArray()
195  );
196  }
197 
198 }
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
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.
execution context with local data
Definition: IContext.java:42
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
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
ImmutablePair< LinearObjectiveFunction, Collection< LinearConstraint > > m_linearprogram
testing linear program
term structure for simple datatypes
Definition: CRawTerm.java:45