LightJason - AgentSpeak(L++)
CCreateDistribution.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.statistic;
25 
26 import org.apache.commons.math3.distribution.AbstractRealDistribution;
27 import org.apache.commons.math3.distribution.BetaDistribution;
28 import org.apache.commons.math3.distribution.CauchyDistribution;
29 import org.apache.commons.math3.distribution.ChiSquaredDistribution;
30 import org.apache.commons.math3.distribution.ExponentialDistribution;
31 import org.apache.commons.math3.distribution.FDistribution;
32 import org.apache.commons.math3.distribution.GammaDistribution;
33 import org.apache.commons.math3.distribution.GumbelDistribution;
34 import org.apache.commons.math3.distribution.LaplaceDistribution;
35 import org.apache.commons.math3.distribution.LevyDistribution;
36 import org.apache.commons.math3.distribution.LogNormalDistribution;
37 import org.apache.commons.math3.distribution.LogisticDistribution;
38 import org.apache.commons.math3.distribution.NakagamiDistribution;
39 import org.apache.commons.math3.distribution.NormalDistribution;
40 import org.apache.commons.math3.distribution.ParetoDistribution;
41 import org.apache.commons.math3.distribution.TDistribution;
42 import org.apache.commons.math3.distribution.TriangularDistribution;
43 import org.apache.commons.math3.distribution.UniformRealDistribution;
44 import org.apache.commons.math3.distribution.WeibullDistribution;
45 import org.apache.commons.math3.random.ISAACRandom;
46 import org.apache.commons.math3.random.JDKRandomGenerator;
47 import org.apache.commons.math3.random.MersenneTwister;
48 import org.apache.commons.math3.random.RandomGenerator;
49 import org.apache.commons.math3.random.SynchronizedRandomGenerator;
50 import org.apache.commons.math3.random.Well1024a;
51 import org.apache.commons.math3.random.Well19937a;
52 import org.apache.commons.math3.random.Well19937c;
53 import org.apache.commons.math3.random.Well44497a;
54 import org.apache.commons.math3.random.Well44497b;
55 import org.apache.commons.math3.random.Well512a;
64 
65 import javax.annotation.Nonnegative;
66 import javax.annotation.Nonnull;
67 import java.util.AbstractMap;
68 import java.util.Arrays;
69 import java.util.Collections;
70 import java.util.List;
71 import java.util.Locale;
72 import java.util.Set;
73 import java.util.stream.Collectors;
74 import java.util.stream.IntStream;
75 
76 
143 public final class CCreateDistribution extends IBuiltinAction
144 {
148  private static final long serialVersionUID = 614460992147593598L;
149 
154  {
155  super( 3 );
156  }
157 
158  @Nonnegative
159  @Override
160  public final int minimalArgumentNumber()
161  {
162  return 1;
163  }
164 
165  @Nonnull
166  @Override
167  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
168  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
169  {
170  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
171 
172  IntStream.range( 0, l_arguments.size() )
173  .filter( i -> CCommon.rawvalueAssignableTo( l_arguments.get( i ), String.class ) )
174  .mapToObj( i -> new AbstractMap.SimpleImmutableEntry<>( i, l_arguments.get( i ).<String>raw() ) )
175  .filter( i -> EDistribution.exist( i.getValue() ) )
176  .map( i -> new AbstractMap.SimpleImmutableEntry<>( i.getKey(), EDistribution.from( i.getValue() ) ) )
177  .map( i ->
178  {
179 
180  // check if next argument to the distribution name a generator name
181  final int l_skip;
182  final EGenerator l_generator;
183 
184  if ( ( i.getKey() < l_arguments.size() - 1 ) && ( CCommon.rawvalueAssignableTo( l_arguments.get( i.getKey() + 1 ), String.class ) ) )
185  {
186  l_skip = 1;
187  l_generator = EGenerator.from( l_arguments.get( i.getKey() + 1 ).<String>raw() );
188  }
189  else
190  {
191  l_skip = 0;
192  l_generator = EGenerator.MERSENNETWISTER;
193  }
194 
195  // generate distribution object, arguments after distribution are the initialize parameter
196  return i.getValue()
197  .get(
198  l_generator.get(),
199  l_arguments.stream()
200  .skip( i.getKey() + 1 + l_skip )
201  .limit( i.getValue().getArgumentNumber() )
202  .map( ITerm::<Number>raw )
203  .mapToDouble( Number::doubleValue )
204  .toArray()
205  );
206 
207  } )
208  .map( CRawTerm::from )
209  .forEach( p_return::add );
210 
211  return CFuzzyValue.from( true );
212  }
213 
214 
218  private enum EDistribution
219  {
220  BETA( 2 ),
221  CAUCHY( 2 ),
222  CHISQUARE( 1 ),
224  F( 2 ),
225  GAMMA( 2 ),
226  GUMBLE( 2 ),
227  LAPLACE( 2 ),
228  LEVY( 2 ),
229  LOGISTIC( 2 ),
230  LOGNORMAL( 2 ),
231  NAKAGAMI( 2 ),
232  NORMAL( 2 ),
233  PARETO( 2 ),
234  T( 1 ),
236  UNIFORM( 2 ),
237  WEIBULL( 2 );
238 
242  private static final Set<String> NAMES = Collections.unmodifiableSet(
243  Arrays.stream( EDistribution.values() )
244  .map( i -> i.name().toUpperCase( Locale.ROOT ) )
245  .collect( Collectors.toSet() )
246  );
247 
251  private final int m_arguments;
252 
258  EDistribution( final int p_arguments )
259  {
260  m_arguments = p_arguments;
261  }
262 
269  @Nonnull
270  public static EDistribution from( @Nonnull final String p_value )
271  {
272  return EDistribution.valueOf( p_value.trim().toUpperCase( Locale.ROOT ) );
273  }
274 
281  public static boolean exist( @Nonnull final String p_value )
282  {
283  return NAMES.contains( p_value.trim().toUpperCase( Locale.ROOT ) );
284  }
285 
291  public final int getArgumentNumber()
292  {
293  return m_arguments;
294  }
295 
303  @Nonnull
304  public final AbstractRealDistribution get( @Nonnull final RandomGenerator p_generator, final double[] p_arguments )
305  {
306  switch ( this )
307  {
308  case BETA:
309  return new BetaDistribution( p_generator, p_arguments[0], p_arguments[1] );
310 
311  case CAUCHY:
312  return new CauchyDistribution( p_generator, p_arguments[0], p_arguments[1] );
313 
314  case CHISQUARE:
315  return new ChiSquaredDistribution( p_generator, p_arguments[0] );
316 
317  case EXPONENTIAL:
318  return new ExponentialDistribution( p_generator, p_arguments[0] );
319 
320  case F:
321  return new FDistribution( p_generator, p_arguments[0], p_arguments[1] );
322 
323  case GAMMA:
324  return new GammaDistribution( p_generator, p_arguments[0], p_arguments[1] );
325 
326  case GUMBLE:
327  return new GumbelDistribution( p_generator, p_arguments[0], p_arguments[1] );
328 
329  case LAPLACE:
330  return new LaplaceDistribution( p_generator, p_arguments[0], p_arguments[1] );
331 
332  case LEVY:
333  return new LevyDistribution( p_generator, p_arguments[0], p_arguments[1] );
334 
335  case LOGISTIC:
336  return new LogisticDistribution( p_generator, p_arguments[0], p_arguments[1] );
337 
338  case LOGNORMAL:
339  return new LogNormalDistribution( p_generator, p_arguments[0], p_arguments[1] );
340 
341  case NAKAGAMI:
342  return new NakagamiDistribution(
343  p_generator, p_arguments[0], p_arguments[1],
344  NakagamiDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY
345  );
346 
347  case NORMAL:
348  return new NormalDistribution( p_generator, p_arguments[0], p_arguments[1] );
349 
350  case PARETO:
351  return new ParetoDistribution( p_generator, p_arguments[0], p_arguments[1] );
352 
353  case T:
354  return new TDistribution( p_generator, p_arguments[0] );
355 
356  case TRIANGULAR:
357  return new TriangularDistribution( p_generator, p_arguments[0], p_arguments[1], p_arguments[2] );
358 
359  case UNIFORM:
360  return new UniformRealDistribution( p_generator, p_arguments[0], p_arguments[1] );
361 
362  case WEIBULL:
363  return new WeibullDistribution( p_generator, p_arguments[0], p_arguments[1] );
364 
365  default:
366  throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "unknown", this ) );
367  }
368  }
369  }
370 
374  private enum EGenerator
375  {
394 
401  @Nonnull
402  public static EGenerator from( @Nonnull final String p_value )
403  {
404  return EGenerator.valueOf( p_value.trim().toUpperCase( Locale.ROOT ) );
405  }
406 
412  @Nonnull
413  public final RandomGenerator get()
414  {
415  switch ( this )
416  {
417  case MERSENNETWISTER:
418  return new MersenneTwister();
419 
420  case SYNCHRONIZEDMERSENNETWISTER:
421  return new SynchronizedRandomGenerator( new MersenneTwister() );
422 
423  case ISAAC:
424  return new ISAACRandom();
425 
426  case SYNCHRONIZEDISAAC:
427  return new SynchronizedRandomGenerator( new ISAACRandom() );
428 
429  case INTERNAL:
430  return new JDKRandomGenerator();
431 
432  case SYNCHRONIZEDINTERNAL:
433  return new SynchronizedRandomGenerator( new JDKRandomGenerator() );
434 
435  case WELL512A:
436  return new Well512a();
437 
438  case SYNCHRONIZEDWELL512A:
439  return new SynchronizedRandomGenerator( new Well512a() );
440 
441  case WELL1024A:
442  return new Well1024a();
443 
444  case SYNCHRONIZEDWELL1024A:
445  return new SynchronizedRandomGenerator( new Well1024a() );
446 
447  case WELL19937A:
448  return new Well19937a();
449 
450  case SYNCHRONIZEDWELL19937A:
451  return new SynchronizedRandomGenerator( new Well19937a() );
452 
453  case WELL19937C:
454  return new Well19937c();
455 
456  case SYNCHRONIZEDWELL19937C:
457  return new SynchronizedRandomGenerator( new Well19937c() );
458 
459  case WELL4449A:
460  return new Well44497a();
461 
462  case SYNCHRONIZEDWELL4449A:
463  return new SynchronizedRandomGenerator( new Well44497a() );
464 
465  case WELL44497B:
466  return new Well44497b();
467 
468  case SYNCHRONIZEDWELL44497B:
469  return new SynchronizedRandomGenerator( new Well44497b() );
470 
471  default:
472  throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "unknown", this ) );
473  }
474  }
475  }
476 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static EGenerator from( @Nonnull final String p_value)
additional factory
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
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
execution context with local data
Definition: IContext.java:42
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
static EDistribution from( @Nonnull final String p_value)
additional factory
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static boolean exist( @Nonnull final String p_value)
checks if a name exists within the enum
term structure for simple datatypes
Definition: CRawTerm.java:45