LightJason - AgentSpeak(L++)
CPerronFrobenius.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.blas.matrix;
25 
26 import cern.colt.matrix.tdouble.DoubleMatrix1D;
27 import cern.colt.matrix.tdouble.DoubleMatrix2D;
28 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D;
29 import cern.jet.math.tdouble.DoubleMult;
37 
38 import javax.annotation.Nonnegative;
39 import javax.annotation.Nonnull;
40 import java.util.List;
41 import java.util.Random;
42 import java.util.concurrent.ThreadLocalRandom;
43 import java.util.stream.Collectors;
44 import java.util.stream.IntStream;
45 
46 
58 public final class CPerronFrobenius extends IAlgebra
59 {
63  private static final long serialVersionUID = -4686274043894517802L;
64 
69  {
70  super( 4 );
71  }
72 
73  @Nonnegative
74  @Override
75  public final int minimalArgumentNumber()
76  {
77  return 2;
78  }
79 
80  @Nonnull
81  @Override
82  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
83  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
84  {
85  final Random l_random = ThreadLocalRandom.current();
86  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
87 
88  // create eigenvectors
89  final List<DoubleMatrix1D> l_eigenvector = IntStream
90  .range( 0, l_arguments.size() - 1 )
91  .parallel()
92  .boxed()
93  .map( i -> new double[l_arguments.get( i ).<DoubleMatrix2D>raw().rows()] )
94  .map( DenseDoubleMatrix1D::new )
95  .peek( i -> IntStream.range( 0, Long.valueOf( i.size() ).intValue() ).forEach( j -> i.setQuick( j, l_random.nextDouble() ) ) )
96  .collect( Collectors.toList() );
97 
98  // run iteration
99  IntStream.range( 0, l_arguments.get( 0 ).<Number>raw().intValue() )
100  .forEach( i -> IntStream
101  .range( 0, l_arguments.size() )
102  .boxed()
103  .parallel()
104  .forEach( j ->
105  {
106  l_eigenvector.get( j ).assign( DENSEALGEBRA.mult( l_arguments.get( j + 1 ).<DoubleMatrix2D>raw(), l_eigenvector.get( j ) ) );
107  l_eigenvector.get( j ).assign( DoubleMult.div( DENSEALGEBRA.norm2( l_eigenvector.get( j ) ) ) );
108  } ) );
109 
110  l_eigenvector.stream()
111  .map( CRawTerm::from )
112  .forEach( p_return::add );
113 
114  return CFuzzyValue.from( true );
115  }
116 }
calculates the largest eigenvector with perron-frobenius theorem.
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
execution context with local data
Definition: IContext.java:42
static final DenseDoubleAlgebra DENSEALGEBRA
dense algebra
Definition: IAlgebra.java:39
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
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
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45