LightJason - AgentSpeak(L++)
CAdjacencyMatrix.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.graph;
25 
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;
44 
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;
52 import java.util.Map;
53 import java.util.stream.IntStream;
54 import java.util.stream.Stream;
55 
56 
78 public final class CAdjacencyMatrix extends IBuiltinAction
79 {
83  private static final long serialVersionUID = -2499068539684263946L;
84 
85  @Nonnegative
86  @Override
87  public final int minimalArgumentNumber()
88  {
89  return 1;
90  }
91 
92  @Nonnull
93  @Override
94  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
95  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
96  )
97  {
98  // --- filter parameters ---
99  final EType l_type = CCommon.flatten( p_argument )
100  .filter( i -> CCommon.rawvalueAssignableTo( i, String.class ) )
101  .findFirst()
102  .map( ITerm::<String>raw )
103  .map( EType::from )
104  .orElse( EType.SPARSE );
105 
106  final double l_defaultcost = CCommon.flatten( p_argument )
107  .filter( i -> CCommon.rawvalueAssignableTo( i, Number.class ) )
108  .findFirst()
109  .map( ITerm::<Number>raw )
110  .map( Number::doubleValue )
111  .orElse( 1D );
112 
113  final Map<?, Number> l_costmap = CCommon.flatten( p_argument )
114  .filter( i -> CCommon.rawvalueAssignableTo( i, Map.class ) )
115  .findFirst()
116  .map( ITerm::<Map<?, Number>>raw )
117  .orElseGet( Collections::emptyMap );
118 
119 
120  // --- filter graphs ---
121  CCommon.flatten( p_argument )
122  .filter( i -> CCommon.rawvalueAssignableTo( i, Graph.class ) )
123  .map( ITerm::<Graph<Object, Object>>raw )
124  .map( i -> CAdjacencyMatrix.apply( i, l_costmap, l_defaultcost, l_type ) )
125  .forEach( i ->
126  {
127  p_return.add( CRawTerm.from( i.getLeft() ) );
128  p_return.add( CRawTerm.from( i.getRight() ) );
129  } );
130 
131  return CFuzzyValue.from( true );
132  }
133 
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 )
145  {
146  // index map for matching vertex to index position within matrix
147  final Map<Object, Integer> l_index = new HashMap<>();
148 
149  // extract vertices from edges
150  p_graph.getEdges()
151  .stream()
152  .map( p_graph::getEndpoints )
153  .flatMap( i -> Stream.of( i.getFirst(), i.getSecond() ) )
154  .forEach( i -> l_index.putIfAbsent( i, 0 ) );
155 
156  // define for each vertex an index number in [0, size)
157  StreamUtils.zip(
158  l_index.keySet().stream(),
159  IntStream.range( 0, l_index.size() ).boxed(),
160  ImmutablePair::new
161  ).forEach( i -> l_index.put( i.getKey(), i.getValue() ) );
162 
163  final DoubleMatrix2D l_matrix;
164  switch ( p_type )
165  {
166  case SPARSE:
167  l_matrix = new SparseDoubleMatrix2D( l_index.size(), l_index.size() );
168  break;
169 
170  default:
171  l_matrix = new DenseDoubleMatrix2D( l_index.size(), l_index.size() );
172  }
173 
174  // map costs to matrix
175  p_graph.getEdges()
176  .stream()
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() ) )
181  ) );
182 
183  // on undirected graphs, add the transposefor cost duplicating
184  if ( p_graph instanceof UndirectedGraph<?, ?> )
185  l_matrix.assign( IAlgebra.DENSEALGEBRA.transpose( l_matrix ).copy(), DoubleFunctions.plus );
186 
187  return new ImmutablePair<>( l_matrix, new ArrayList<>( l_index.keySet() ) );
188  }
189 }
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
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
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
Definition: EType.java:53
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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
Definition: CRawTerm.java:45