24 package org.lightjason.agentspeak.action.builtin.graph;
26 import cern.colt.matrix.tdouble.DoubleMatrix2D;
27 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
28 import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D;
29 import cern.jet.math.tdouble.DoubleFunctions;
30 import com.codepoetics.protonpack.StreamUtils;
31 import edu.uci.ics.jung.graph.Graph;
32 import edu.uci.ics.jung.graph.UndirectedGraph;
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.apache.commons.lang3.tuple.Pair;
45 import javax.annotation.Nonnegative;
46 import javax.annotation.Nonnull;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
53 import java.util.stream.IntStream;
54 import java.util.stream.Stream;
95 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return
102 .map(
ITerm::<String>raw )
109 .map(
ITerm::<Number>raw )
110 .map( Number::doubleValue )
116 .map(
ITerm::<Map<?, Number>>raw )
117 .orElseGet( Collections::emptyMap );
123 .map(
ITerm::<Graph<Object, Object>>raw )
143 private static Pair<DoubleMatrix2D, Collection<?>>
apply( @Nonnull
final Graph<Object, Object> p_graph, @Nonnull
final Map<?, Number> p_cost,
144 final double p_defaultcost, @Nonnull
final EType p_type )
147 final Map<Object, Integer> l_index =
new HashMap<>();
152 .map( p_graph::getEndpoints )
153 .flatMap( i -> Stream.of( i.getFirst(), i.getSecond() ) )
154 .forEach( i -> l_index.putIfAbsent( i, 0 ) );
158 l_index.keySet().stream(),
159 IntStream.range( 0, l_index.size() ).boxed(),
161 ).forEach( i -> l_index.put( i.getKey(), i.getValue() ) );
163 final DoubleMatrix2D l_matrix;
167 l_matrix =
new SparseDoubleMatrix2D( l_index.size(), l_index.size() );
171 l_matrix =
new DenseDoubleMatrix2D( l_index.size(), l_index.size() );
177 .map( i ->
new ImmutablePair<>( p_graph.getEndpoints( i ), p_cost.getOrDefault( i, p_defaultcost ).doubleValue() ) )
178 .forEach( i -> l_matrix.setQuick(
179 l_index.get( i.getLeft().getFirst() ), l_index.get( i.getLeft().getSecond() ),
180 i.getRight() + l_matrix.getQuick( l_index.get( i.getLeft().getFirst() ), l_index.get( i.getLeft().getSecond() ) )
184 if ( p_graph instanceof UndirectedGraph<?, ?> )
187 return new ImmutablePair<>( l_matrix,
new ArrayList<>( l_index.keySet() ) );
base class of build-in actions for setting name by package/classname (without prefix character) ...
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
final int minimalArgumentNumber()
minimum number of arguments
static final long serialVersionUID
serial id
execution context with local data
static final DenseDoubleAlgebra DENSEALGEBRA
dense algebra
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
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 EType from(final String p_name)
additional factory
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
creates from a graph the adjacency matrix.
blas algebra operations e.g.
static Pair< DoubleMatrix2D, Collection<?> > apply( @Nonnull final Graph< Object, Object > p_graph, @Nonnull final Map<?, Number > p_cost, final double p_defaultcost, @Nonnull final EType p_type)
converts a graph into an adjacency matrix
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
term structure for simple datatypes