LightJason - AgentSpeak(L++)
CEquationConstraint.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.math.linearprogram;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.apache.commons.math3.optim.linear.LinearConstraint;
29 import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
35 
36 import javax.annotation.Nonnegative;
37 import javax.annotation.Nonnull;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.stream.Collectors;
41 
42 
59 public final class CEquationConstraint extends IConstraint
60 {
64  private static final long serialVersionUID = 3123101079239668634L;
65 
66 
67  @Nonnegative
68  @Override
69  public final int minimalArgumentNumber()
70  {
71  return 6;
72  }
73 
74  @Nonnull
75  @Override
76  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
77  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
78  {
79  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
80 
81  // create left-hand-side and right-hand-side with operator lists
82  final List<Number> l_lhs = StreamUtils.takeWhile( l_arguments.stream().skip( 1 ), i -> !CCommon.rawvalueAssignableTo( i, String.class ) )
83  .map( ITerm::<Number>raw )
84  .collect( Collectors.toList() );
85 
86  final List<ITerm> l_rhs = l_arguments.stream()
87  .skip( l_lhs.size() + 1 )
88  .collect( Collectors.toList() );
89 
90  // test content
91  if ( ( l_lhs.size() < 2 ) || ( l_rhs.size() < 3 ) || ( !CCommon.rawvalueAssignableTo( l_rhs.get( 0 ), String.class ) ) )
92  return CFuzzyValue.from( false );
93 
94  // create constraint
95  l_arguments.get( 0 ).<Pair<LinearObjectiveFunction, Collection<LinearConstraint>>>raw().getRight().add(
96  new LinearConstraint(
97 
98  // c_i values
99  l_lhs.stream()
100  .limit( l_lhs.size() - 1 )
101  .mapToDouble( Number::doubleValue )
102  .toArray(),
103 
104  // c_const value
105  l_lhs.get( l_lhs.size() - 1 )
106  .doubleValue(),
107 
108  // relation symbol
109  this.getRelation( l_rhs.get( 0 ).<String>raw() ),
110 
111  // r_i values
112  l_rhs.stream()
113  .limit( l_rhs.size() - 1 )
114  .skip( 1 )
115  .map( ITerm::<Number>raw )
116  .mapToDouble( Number::doubleValue )
117  .toArray(),
118 
119  // r_const value
120  l_rhs.get( l_rhs.size() - 1 )
121  .<Number>raw()
122  .doubleValue()
123 
124  )
125  );
126 
127  return CFuzzyValue.from( true );
128  }
129 
130 }
final Relationship getRelation( @Nonnull final String p_symbol)
returns the enum of a relationship by a string value
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
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
execution context with local data
Definition: IContext.java:42
result for an immutable fuzzy value