Class GraphEnvironment<N extends AgentGraphNode,A extends AgentGraphArc,K>
- Direct Known Subclasses:
LongAgentsEnvironment
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<GraphAgent<N, A>> protected booleanprotected booleanprotected booleanprotected booleanprotected booleanprotected intprotected intprotected intprotected int -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of graph environment with these default settings: Agents collision tracking is enabled, seeisCollisionsTrackingEnabled()Callback positions are enabled, seeisCallbackPositionsEnabled()Cache of shortest paths is enabled, seepathsCacheEnabled()Advanced tree-based cache is disabled, seeminTreeCacheEnabledUsing of barrier arcs is disabled, seebarrierArcsEnabledGraphEnvironment(boolean collisionsTrackingEnabled, boolean callbackPositionsEnabled, boolean pathsCacheEnabled, boolean minTreeCacheEnabled, boolean barrierArcsEnabled) Creates a new instance of graph environment with the specified settings. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddAgentInternal(GraphAgent<N, A> agent) Creates a new graph arc between the nodes with the specified values.Creates two new graph arcs (one forward and one backward) between the nodes with the specified values.Creates a new graph node with the specified node value and adds it to the graph.protected doublecalculatePathWeight(GraphPath<N, A> path, GraphAgent<N, A> agent) voidList<GraphAgent<N, A>> Returns an unmodifiable list of all agents currently living in this environment.getArcs()Returns an unmodifiable list of all graph arcs.Returns an unmodifiable list of all graph arc values.doublegetArcWeight(A arcValue, K weightKey) Returns the weight of the specified arc used when calculating the shortest paths for agents' movements.getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range) getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, int maxNodesCount) getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, GraphAgent<N, A> agent) Returns the nodes for which the specified predicate returnstruelocated within the specified range from the specified source node.getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, GraphAgent<N, A> agent, BiFunction<A, K, Double> arcWeighter, BiFunction<N, K, Double> nodeWeighter) getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, GraphAgent<N, A> agent, BiFunction<A, K, Double> arcWeighter, BiFunction<N, K, Double> nodeWeighter, int maxNodesCount) getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, GraphAgent<N, A> agent, Map<Graph<N, A>.Node, Double> outNodeWeights, Set<Graph<N, A>.Arc> outArcsVisited) getClosestNodes(Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, Map<Graph<N, A>.Node, Double> outNodeWeights, Set<Graph<N, A>.Arc> outArcsVisited) getClosestNodes(GeometricGraphPosition<N, A> sourcePosition, Predicate<Graph<N, A>.Node> destNodeIndicator, double range) getClosestNodes(GeometricGraphPosition<N, A> sourcePosition, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, int maxNodesCount) doubledoublegetDestsAndWeights(GeometricGraphPosition<N, A> destPosition, K weightKey) getGraph()Returns the graph of this graph environment.getGraphArc(A arcValue) Returns graph arc (an instance ofGraph.Arc) containing the specified value.
getGraphNode(N nodeValue) Returns graph node (an instance ofGraph.Node) containing the specified value.
getNodes()Returns an unmodifiable list of all graph nodes.Returns an unmodifiable list of all graph node values.doublegetNodeWeight(N nodeValue, K weightKey) Returns the weight of the specified node used when calculating the shortest path for agents' movements.doublegetPartialArcWeight(A arcValue, double startAbsOffset, double endAbsOffset, double startRelOffset, double endRelOffset, K weightKey) Returns the weight of the fragment of the specified arc.protected AgentGraphPath<N, A> getSameOrReverseArcPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition, GraphAgent<N, A> agent) Returns the shortest path from the specified source node to the specified destination node for the specified agent, ornullif no such path exists.getShortestPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition) getShortestPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the specified destination position for the specified agent, ornullif no such path exists.getShortestPath(GeometricGraphPosition<N, A> sourcePosition, Predicate<Graph<N, A>.Node> destNodeIndicator) getShortestPath(GeometricGraphPosition<N, A> sourcePosition, Predicate<Graph<N, A>.Node> destNodeIndicator, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the closest node on which the specified predicate returnstrue, ornullif no such path exists or the predicate returnsfalsefor all nodes reachable from the source position.getShortestPath(N sourceNodeValue, N destNodeValue) getShortestPath(N sourceNodeValue, N destNodeValue, GraphAgent<N, A> agent) Returns the shortest path from the specified source node to the specified destination node for the specified agent, ornullif no such path exists.getShortestPathBetweenNodesInternal(Graph<N, A> graph, Graph<N, A>.Node sourceNode, Graph<N, A>.Node destNode, Function<Graph<N, A>.Arc, Double> arcWeighter, Function<Graph<N, A>.Node, Double> nodeWeighter, Map<Graph<N, A>.Node, Double> nodeWeights, Set<Graph<N, A>.Arc> arcsVisited, GraphAgent<N, A> agent) getShortestPathToClosest(GeometricGraphPosition<N, A> sourcePosition, List<GeometricGraphPosition<N, A>> destPositions, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the closest one of the specified destination positions for the specified agent, ornullif neither of the specified destination positions is reachable from the source position.getSourcesAndWeights(GeometricGraphPosition<N, A> sourcePosition, K weightKey) booleanbooleanvoidonArcWeightChanged(A arcValue) booleanChecks whether caching of the paths is enabled.protected voidremoveAgentInternal(GraphAgent<N, A> agent) protected AgentGraphPath<N, A> tryGetZeroLengthPathBetweenAdjacentPositions(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition) Tries to find and return a zero-length path between the two adjacent positions.weightKey(GraphAgent<N, A> agent) Returns the key that is used as additional parameter when calculating weights of nodes and arcs for the purposes of shortest paths calculation for the specified agent.
-
Field Details
-
collisionsTrackingEnabled
protected boolean collisionsTrackingEnabled -
callbackPositionsEnabled
protected boolean callbackPositionsEnabled -
pathsCacheEnabled
protected boolean pathsCacheEnabled -
minTreeCacheEnabled
protected boolean minTreeCacheEnabled -
barrierArcsEnabled
protected boolean barrierArcsEnabled -
pathsGotFromCache
protected int pathsGotFromCache -
pathsCalculated
protected int pathsCalculated -
pathsCalculatedFromTree
protected int pathsCalculatedFromTree -
pathsCalculatedFromDijkstra
protected int pathsCalculatedFromDijkstra -
graph
-
cachedShortestPathsWithAgents
protected Map<Pair<K,Pair<Graph<N extends AgentGraphNode, cachedShortestPathsWithAgentsA extends AgentGraphArc>.Node, Graph<N extends AgentGraphNode, A extends AgentGraphArc>.Node>>, CachedShortestPath<N extends AgentGraphNode, A extends AgentGraphArc>> -
cachedShortestPathsWithoutAgents
protected Map<Pair<Graph<N extends AgentGraphNode,A extends AgentGraphArc>.Node, cachedShortestPathsWithoutAgentsGraph<N extends AgentGraphNode, A extends AgentGraphArc>.Node>, CachedShortestPath<N extends AgentGraphNode, A extends AgentGraphArc>> -
cachedShortestPathsWeights
-
cachedMinTreesWithAgents
protected Map<Pair<K,Graph<N extends AgentGraphNode, cachedMinTreesWithAgentsA extends AgentGraphArc>.Node>, com.amalgamasimulation.graphagent.CachedMinTree<N extends AgentGraphNode, A extends AgentGraphArc>> -
cachedMinTreesWithoutAgents
protected Map<Graph<N extends AgentGraphNode,A extends AgentGraphArc>.Node, cachedMinTreesWithoutAgentscom.amalgamasimulation.graphagent.CachedMinTree<N extends AgentGraphNode, A extends AgentGraphArc>> -
barrierArcsCache
protected Map<Pair<Graph<N extends AgentGraphNode,A extends AgentGraphArc>.Node, barrierArcsCacheGraph<N extends AgentGraphNode, A extends AgentGraphArc>.Node>, Set<Graph<N extends AgentGraphNode, A extends AgentGraphArc>.Arc>> -
agents
-
-
Constructor Details
-
GraphEnvironment
public GraphEnvironment()Creates a new instance of graph environment with these default settings:- Agents collision tracking is enabled, see
isCollisionsTrackingEnabled() - Callback positions are enabled, see
isCallbackPositionsEnabled() - Cache of shortest paths is enabled, see
pathsCacheEnabled() - Advanced tree-based cache is disabled, see
minTreeCacheEnabled - Using of barrier arcs is disabled, see
barrierArcsEnabled
- Agents collision tracking is enabled, see
-
GraphEnvironment
public GraphEnvironment(boolean collisionsTrackingEnabled, boolean callbackPositionsEnabled, boolean pathsCacheEnabled, boolean minTreeCacheEnabled, boolean barrierArcsEnabled) Creates a new instance of graph environment with the specified settings.- Parameters:
collisionsTrackingEnabled- whether or not to enable tracking collisions of agents. Enabling this option can result in some loss of computational performance but would allow to useGraphAgent.onOtherAgentReached(GraphAgent, boolean)method to determine the collisions of agentscallbackPositionsEnabled- whether or not to enable callback positions. Callback positions allow to track the events of agents reaching certain positions in the graph. This functionality can slightly decrease computational performance in some cases so this parameter can be switched off if callback positions are not neededpathsCacheEnabled- whether or not to allow caching of shortest paths. Enabling caching significantly improves computational performance of big models so switching on this option is recommended in majority of cases. Users might wish to switch off this option if the weight of nodes and arcs change very often and there is little value in caching the shortest pathsminTreeCacheEnabled- whether or not to use the advanced cache based on keeping the minimal spanning trees of all shortest path searches. Might result in increase in performance in certain casesbarrierArcsEnabled- whether or not to use the barrier arcs
-
-
Method Details
-
getArcWeight
Returns the weight of the specified arc used when calculating the shortest paths for agents' movements. Can be overridden in subclasses. The default implementation returns the length of the specified arc, i.e.:return arcValue.getLength();- Parameters:
arcValue- specified arcweightKey- an additional parameter used to parameterize the calculation if necessary. For example, can be agent if the paths should be calculated differently for each agent- Returns:
- weight of this arc, must be non-negative
-
getNodeWeight
Returns the weight of the specified node used when calculating the shortest path for agents' movements. Can be overridden in subclasses. The default implementation returns 0, so by default nodes do not contribute to paths' weights.The default implementation is to return zero, i.e.:
return 0;- Parameters:
nodeValue- specified nodeweightKey- an additional parameter used to parameterize the calculation if necessary. For example, can be agent if the paths should be calculated differently for each agent- Returns:
- weight of this node, must be non-negative
-
getPartialArcWeight
public double getPartialArcWeight(A arcValue, double startAbsOffset, double endAbsOffset, double startRelOffset, double endRelOffset, K weightKey) Returns the weight of the fragment of the specified arc. The fragment of the arc is defined by absolute and relative offsets of its beginning and end.Overriding this method allows users, for example, to make certain fragments of arcs less preferable or forbidden for use in path finding.
The default implementation is to return the weight of the fragment, i.e.:
return getArcWeight(arcValue, weightKey) * Utils.zidz(endAbsOffset - startAbsOffset, arcValue.getLength());For more information about absolute and relative offsets, see documentation of
Polylineclass. See how the total weight of the arc is calculated:- Parameters:
arcValue- specified arcstartAbsOffset- absolute offset of the fragment's beginningendAbsOffset- absolute offset of the fragment's endstartRelOffset- relative offset of the fragment's beginningendRelOffset- relative offset of the fragment's endweightKey- an additional parameter used to parameterize the calculation if necessary. For example, can be agent if the paths should be calculated differently for each agent- Returns:
- weight of the fragment of the specified arc, must be non-negative
-
pathsCacheEnabled
public boolean pathsCacheEnabled()Checks whether caching of the paths is enabled. Enabling paths cache increases the speed of modeling but must be used with caution when the weight of nodes or arcs change as the simulation time goes. In this case, it is still possible to use paths caching butclearCache()method must be called when the weights of nodes or arcs change.Paths cache can be enabled or disabled in constructor of this class.
- Returns:
trueif caching of the paths is enabled,falseotherwise
-
weightKey
Returns the key that is used as additional parameter when calculating weights of nodes and arcs for the purposes of shortest paths calculation for the specified agent. Can be overridden in subclasses. Default implementation returnsnull, so all the agents have the same context while calculating their paths.An example of weight key can be some type of agent if paths need to be calculated differently for agent of different types
- Parameters:
agent- specified agent- Returns:
- weight key
-
addNode
Creates a new graph node with the specified node value and adds it to the graph.- Parameters:
nodeValue- value of the node being added- Returns:
Graph.Nodeinstance that was created and added to the graph
-
addArc
Creates a new graph arc between the nodes with the specified values. Assigns the specified value to the created arc and adds it to the graph.- Parameters:
sourceNodeValue- value of the source nodedestNodeValue- value of the destination nodearcValue- value of the arc being added- Returns:
Graph.Arcinstance that was created and added to the graph, ornullif no source or destination node with the specified values were found
-
getNodes
Returns an unmodifiable list of all graph nodes. The nodes are sorted according to the order they were added to the graph- Returns:
- unmodifiable list of
Graph.Nodeinstances
-
getNodeValues
Returns an unmodifiable list of all graph node values.- Returns:
- unmodifiable list of graph node values
-
getArcs
Returns an unmodifiable list of all graph arcs. The arc are sorted according to the order they were added to the graph- Returns:
- unmodifiable list of
Graph.Arcinstances
-
getArcValues
Returns an unmodifiable list of all graph arc values.- Returns:
- unmodifiable list of graph arc values
-
getAgents
Returns an unmodifiable list of all agents currently living in this environment.- Returns:
- unmodifiable list of all agents currently living in this environment
-
addArc
public Pair<Graph<N,A>.Arc, addArcGraph<N, A>.Arc> (N sourceNodeValue, N destNodeValue, A arcValue, A reverseArcValue) Creates two new graph arcs (one forward and one backward) between the nodes with the specified values. Assigns the specified values to the created arcs and adds them to the graph.- Parameters:
sourceNodeValue- value of the source node of the forward arcdestNodeValue- value of the destination node of the forward arcarcValue- value of the forward arc being addedreverseArcValue- value of the reverse arc being added- Returns:
- a
PairofGraph.Arcinstances that were created and added to the graph, ornullif no source or destination node with the specified values were found
-
getGraph
Returns the graph of this graph environment.- Returns:
- instance of
Graph
-
getShortestPathToClosest
public AgentGraphPath<N,A> getShortestPathToClosest(GeometricGraphPosition<N, A> sourcePosition, List<GeometricGraphPosition<N, A>> destPositions, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the closest one of the specified destination positions for the specified agent, ornullif neither of the specified destination positions is reachable from the source position.Shortest paths and "closeness" of the positions are determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic.- Parameters:
sourcePosition- the origin of path findingdestPositions- the list of possible destination positionsagent- the agent for which the path is being searched- Returns:
- an instance of
AgentGraphPathrepresenting the shortest path, ornull
-
getShortestPath
public AgentGraphPath<N,A> getShortestPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the specified destination position for the specified agent, ornullif no such path exists.Shortest path is determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic.- Parameters:
sourcePosition- the origin of path findingdestPosition- the destination of path findingagent- the agent for which the path is being searched- Returns:
- an instance of
AgentGraphPathrepresenting the shortest path, ornull
-
getSourcesAndWeights
protected List<Pair<GeometricGraphPosition<N,A>, getSourcesAndWeightsDouble>> (GeometricGraphPosition<N, A> sourcePosition, K weightKey) -
getDestsAndWeights
protected List<Pair<GeometricGraphPosition<N,A>, getDestsAndWeightsDouble>> (GeometricGraphPosition<N, A> destPosition, K weightKey) -
tryGetZeroLengthPathBetweenAdjacentPositions
protected AgentGraphPath<N,A> tryGetZeroLengthPathBetweenAdjacentPositions(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition) Tries to find and return a zero-length path between the two adjacent positions. Note that there can still be a non-zero-lenght path between the two positions, in such case this method will returnnullas it only searches for zero-length paths.- Parameters:
sourcePosition- source adjacent positiondestPosition- destination adjacent position- Returns:
- Found zero-length path, or
null, if no zero-length path exists.
-
getSameOrReverseArcPath
protected AgentGraphPath<N,A> getSameOrReverseArcPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition, GraphAgent<N, A> agent) -
calculatePathWeight
-
getGraphArc
Returns graph arc (an instance ofGraph.Arc) containing the specified value.
- Parameters:
arcValue- value for which to return the graph node- Returns:
- graph arc containing the specified value, null if there is no such graph arc
-
getGraphNode
Returns graph node (an instance ofGraph.Node) containing the specified value.
- Parameters:
nodeValue- value for which to return the graph node- Returns:
- graph node containing the specified value, null if there is no such graph node.
-
getShortestPath
public AgentGraphPath<N,A> getShortestPath(GeometricGraphPosition<N, A> sourcePosition, Predicate<Graph<N, A>.Node> destNodeIndicator, GraphAgent<N, A> agent) Returns the shortest path from the specified source position to the closest node on which the specified predicate returnstrue, ornullif no such path exists or the predicate returnsfalsefor all nodes reachable from the source position. The path is searched for the specified agent.Shortest path is determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic.- Parameters:
sourcePosition- the origin of path findingdestNodeIndicator- thePredicatewhich must returntrueto indicate that a node is a valid destinationagent- the agent for which the path is being searched- Returns:
- an instance of
AgentGraphPathrepresenting the shortest path, ornull
-
clearCache
public void clearCache() -
getCurrentCacheFactor
public double getCurrentCacheFactor() -
getClosestNodes
public List<Pair<Graph<N,A>.Node, getClosestNodesDouble>> (Graph<N, A>.Node sourceNode, Predicate<Graph<N, A>.Node> destNodeIndicator, double range, GraphAgent<N, A> agent) Returns the nodes for which the specified predicate returnstruelocated within the specified range from the specified source node. For every node, returns the weight of the shortest path from the specified source node to this node.The result is returned as a
ListofPairs. The first element of each pair is the node, and the second element is its weight. The elements in the returned list are sorted by weights in ascending order.The search is made with respect to the specified agent.
Note that the range is expressed in arcs' and nodes' weights, not just in distance along the graph. This means that the weight of the shortest path from the source node to any of the returned nodes is less than or equal to the specified range.
"Closeness" is determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic. -
getShortestPath
public AgentGraphPath<N,A> getShortestPath(Graph<N, A>.Node sourceNode, Graph<N, A>.Node destNode, GraphAgent<N, A> agent) Returns the shortest path from the specified source node to the specified destination node for the specified agent, ornullif no such path exists.Shortest path is determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic.- Parameters:
sourceNode- the origin of path findingdestNode- the destination of path findingagent- the agent for which the path is being searched- Returns:
- an instance of
AgentGraphPathrepresenting the shortest path, ornull
-
getShortestPath
public AgentGraphPath<N,A> getShortestPath(N sourceNodeValue, N destNodeValue, GraphAgent<N, A> agent) Returns the shortest path from the specified source node to the specified destination node for the specified agent, ornullif no such path exists. The nodes are specified by their values.Shortest path is determined using the weights provided in the methods:
The default implementation of these methods returns simple distance-based weights, however these methods can be overridden to customize the path finding logic.- Parameters:
sourceNodeValue- the value of the node which is the origin of path findingdestNodeValue- the value of the node which is the destination of path findingagent- the agent for which the path is being searched- Returns:
- an instance of
AgentGraphPathrepresenting the shortest path, ornull
-
getShortestPath
public AgentGraphPath<N,A> getShortestPath(GeometricGraphPosition<N, A> sourcePosition, GeometricGraphPosition<N, A> destPosition) -
getShortestPath
-
getShortestPath
-
getShortestPath
-
getClosestNodes
-
getClosestNodes
-
getClosestNodes
-
getClosestNodes
-
onArcWeightChanged
-
getClosestNodes
-
getCurrentDijsktraFactor
public double getCurrentDijsktraFactor() -
getClosestNodes
-
getClosestNodes
-
getClosestNodes
-
isCollisionsTrackingEnabled
public boolean isCollisionsTrackingEnabled() -
isCallbackPositionsEnabled
public boolean isCallbackPositionsEnabled() -
getShortestPathBetweenNodesInternal
protected GraphPath<N,A> getShortestPathBetweenNodesInternal(Graph<N, A> graph, Graph<N, A>.Node sourceNode, Graph<N, A>.Node destNode, Function<Graph<N, A>.Arc, Double> arcWeighter, Function<Graph<N, A>.Node, Double> nodeWeighter, Map<Graph<N, A>.Node, Double> nodeWeights, Set<Graph<N, A>.Arc> arcsVisited, GraphAgent<N, A> agent) -
addAgentInternal
-
removeAgentInternal
-