diff --git a/msi.gama.core/src/msi/gama/util/graph/GamaGraph.java b/msi.gama.core/src/msi/gama/util/graph/GamaGraph.java index ef0332d01a..5b1d5c4fc6 100644 --- a/msi.gama.core/src/msi/gama/util/graph/GamaGraph.java +++ b/msi.gama.core/src/msi/gama/util/graph/GamaGraph.java @@ -1,9 +1,8 @@ /******************************************************************************************************* * - * msi.gama.util.graph.GamaGraph.java, in plugin msi.gama.core, is part of the source code of the GAMA modeling and - * simulation platform (v. 1.8.1) + * GamaGraph.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * @@ -96,60 +95,152 @@ import msi.gaml.types.Types; import one.util.streamex.StreamEx; +/** + * The Class GamaGraph. + * + * @param + * the value type + * @param + * the element type + */ @SuppressWarnings ({ "unchecked", "rawtypes" }) public class GamaGraph implements IGraph { + /** The vertex map. */ protected final Map> vertexMap; + + /** The edge map. */ protected final Map> edgeMap; + + /** The directed. */ protected boolean directed; + + /** The edge based. */ protected boolean edgeBased; + + /** The agent edge. */ protected boolean agentEdge; + + /** The graph scope. */ protected final IScope graphScope; + + /** The type. */ protected final IContainerType type; + + /** The shortest path computed. */ protected Map, IList>> shortestPathComputed = null; + + /** The vertex relation. */ protected VertexRelationship vertexRelation; + + /** The shortest path matrix. */ protected GamaIntMatrix shortestPathMatrix = null; + /** The default node weight. */ protected static double DEFAULT_NODE_WEIGHT = 0.0; + /** + * The Enum shortestPathAlgorithm. + */ public enum shortestPathAlgorithm { + + /** The Floyd warshall. */ FloydWarshall, + + /** The Bellmann ford. */ BellmannFord, + + /** The Dijkstra. */ Dijkstra, + + /** The A star. */ AStar, + + /** The NBA star. */ NBAStar, + + /** The NBA star approx. */ NBAStarApprox, + + /** The Delta stepping. */ DeltaStepping, + + /** The CH bidirectional dijkstra. */ CHBidirectionalDijkstra, + + /** The Bidirectional dijkstra. */ BidirectionalDijkstra, + + /** The Transit node routing. */ TransitNodeRouting; } + /** + * The Enum kShortestPathAlgorithm. + */ public enum kShortestPathAlgorithm { - Yen, Bhandari; + + /** The Yen. */ + Yen, + /** The Bhandari. */ + Bhandari; } + /** The Constant ONLY_FOR_DIRECTED_GRAPH. */ public static final ImmutableList ONLY_FOR_DIRECTED_GRAPH = ImmutableList.of(kShortestPathAlgorithm.Bhandari); + + /** The save computed shortest paths. */ protected boolean saveComputedShortestPaths = true; + /** The edge species. */ protected ISpecies edgeSpecies; + + /** The path finding algo. */ protected shortestPathAlgorithm pathFindingAlgo = shortestPathAlgorithm.BidirectionalDijkstra; + + /** The k path finding algo. */ protected kShortestPathAlgorithm kPathFindingAlgo = kShortestPathAlgorithm.Yen; + + /** The optimizer. */ private FloydWarshallShortestPathsGAMA optimizer; + /** The listeners. */ private final LinkedList listeners = new LinkedList<>(); + /** The generated edges. */ private final Set generatedEdges = new LinkedHashSet<>(); + + /** The version. */ protected int version; + /** The vertex species. */ protected ISpecies vertexSpecies; + /** The contraction hierarchy BD. */ protected ContractionHierarchyBidirectionalDijkstra contractionHierarchyBD = null; + + /** The transit node routing. */ protected TransitNodeRoutingShortestPath transitNodeRouting = null; + + /** The linked J graph. */ protected AbstractBaseGraph linkedJGraph; + + /** The from linked gto edges. */ protected Map fromLinkedGtoEdges; + /** + * Instantiates a new gama graph. + * + * @param scope + * the scope + * @param directed + * the directed + * @param nodeType + * the node type + * @param vertexType + * the vertex type + */ public GamaGraph(final IScope scope, final boolean directed, final IType nodeType, final IType vertexType) { this.directed = directed; vertexMap = GamaMapFactory.create(); @@ -163,6 +254,26 @@ public GamaGraph(final IScope scope, final boolean directed, final IType nodeTyp type = Types.GRAPH.of(nodeType, vertexType); } + /** + * Instantiates a new gama graph. + * + * @param scope + * the scope + * @param edgesOrVertices + * the edges or vertices + * @param byEdge + * the by edge + * @param directed + * the directed + * @param rel + * the rel + * @param edgesSpecies + * the edges species + * @param nodeType + * the node type + * @param edgeType + * the edge type + */ public GamaGraph(final IScope scope, final IContainer edgesOrVertices, final boolean byEdge, final boolean directed, final VertexRelationship rel, final ISpecies edgesSpecies, final IType nodeType, final IType edgeType) { vertexMap = GamaMapFactory.create(); @@ -179,14 +290,36 @@ public GamaGraph(final IScope scope, final IContainer edgesOrVertices, final boo init(scope, edgesOrVertices, byEdge, directed, rel, edgesSpecies); } - public GamaGraph(final IScope scope, final IType nodeType, final IType vertexType) { + /** + * Instantiates a new gama graph. + * + * @param scope + * the scope + * @param nodeType + * the node type + * @param edgeType + * the edge type + */ + public GamaGraph(final IScope scope, final IType nodeType, final IType edgeType) { vertexMap = GamaMapFactory.create(); edgeMap = GamaMapFactory.create(); shortestPathComputed = new ConcurrentHashMap<>(); this.graphScope = scope; - type = Types.GRAPH.of(nodeType, vertexType); + type = Types.GRAPH.of(nodeType, edgeType); } + /** + * Instantiates a new gama graph. + * + * @param scope + * the scope + * @param graph + * the graph + * @param nodeS + * the node S + * @param edgeS + * the edge S + */ public GamaGraph(final IScope scope, final AbstractBaseGraph graph, final ISpecies nodeS, final ISpecies edgeS) { this(scope, nodeS == null ? Types.STRING : Types.AGENT, edgeS == null ? Types.STRING : Types.AGENT); @@ -233,6 +366,18 @@ public GamaGraph(final IScope scope, final AbstractBaseGraph graph, final IList nodes, final ISpecies edgeS) { this(scope, Types.get(nodes.get(0).getClass()), edgeS == null ? Types.STRING : Types.AGENT); @@ -267,10 +412,29 @@ public GamaGraph(final IScope scope, final AbstractBaseGraph vertices) { - for (final E p : vertices.iterable(scope)) { - addVertex(p); - } + for (final E p : vertices.iterable(scope)) { addVertex(p); } } + /** + * Builds the by edge. + * + * @param scope + * the scope + * @param edges + * the edges + */ protected void buildByEdge(final IScope scope, final IContainer edges) { if (edges != null) { for (final Object p : edges.iterable(scope)) { @@ -360,6 +552,16 @@ protected void buildByEdge(final IScope scope, final IContainer edges) { } } + /** + * Builds the by edge. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param tolerance + * the tolerance + */ protected void buildByEdge(final IScope scope, final IContainer vertices, final Double tolerance) { if (vertices != null) { for (final Object p : vertices.iterable(scope)) { @@ -374,12 +576,36 @@ protected void buildByEdge(final IScope scope, final IContainer vertices, final } } + /** + * Builds the by edge. + * + * @param scope + * the scope + * @param edges + * the edges + * @param vertices + * the vertices + */ protected void buildByEdge(final IScope scope, final IContainer edges, final IContainer vertices) {} + /** + * Gets the edge. + * + * @param e + * the e + * @return the edge + */ public _Edge getEdge(final Object e) { return edgeMap.get(e); } + /** + * Gets the vertex. + * + * @param v + * the v + * @return the vertex + */ public _Vertex getVertex(final Object v) { return vertexMap.get(v); } @@ -441,9 +667,7 @@ public void setValueAtIndex(final IScope scope, final Object index, public void addValues(final IScope scope, final Object index, final IContainer values) { // Index is not used here as it does not make sense for graphs (see #2985) if (values instanceof GamaGraph) { - for (final Object o : ((GamaGraph) values).edgeSet()) { - addEdge(o); - } + for (final Object o : ((GamaGraph) values).edgeSet()) { addEdge(o); } } else { for (final Object o : values.iterable(scope)) { if (o instanceof msi.gaml.operators.Graphs.GraphObjectToAdd) { @@ -486,9 +710,7 @@ public void removeIndex(final IScope scope, final Object index) { */ @Override public void removeIndexes(final IScope scope, final IContainer index) { - for (final Object pair : index.iterable(scope)) { - removeIndex(scope, pair); - } + for (final Object pair : index.iterable(scope)) { removeIndex(scope, pair); } } @Override @@ -496,9 +718,7 @@ public void removeValues(final IScope scope, final IContainer values) { if (values instanceof IGraph) { removeAllEdges(((IGraph) values).edgeSet()); } else { - for (final Object o : values.iterable(scope)) { - removeValue(scope, o); - } + for (final Object o : values.iterable(scope)) { removeValue(scope, o); } } } @@ -521,6 +741,15 @@ public Object addEdge(final Object v1, final Object v2) { return null; } + /** + * Creates the new edge object from vertices. + * + * @param v1 + * the v 1 + * @param v2 + * the v 2 + * @return the object + */ protected Object createNewEdgeObjectFromVertices(final Object v1, final Object v2) { if (getEdgeSpecies() == null) return generateEdgeObject(v1, v2); final IMap map = GamaMapFactory.create(); @@ -532,10 +761,26 @@ protected Object createNewEdgeObjectFromVertices(final Object v1, final Object v return generateEdgeAgent(initVal); } + /** + * Generate edge object. + * + * @param v1 + * the v 1 + * @param v2 + * the v 2 + * @return the object + */ protected Object generateEdgeObject(final Object v1, final Object v2) { return new GamaPair(v1, v2, getGamlType().getKeyType(), getGamlType().getKeyType()); } + /** + * Generate edge agent. + * + * @param attributes + * the attributes + * @return the i agent + */ protected IAgent generateEdgeAgent(final List> attributes) { final IAgent agent = graphScope.getAgent().getPopulationFor(getEdgeSpecies()) .createAgents(graphScope, 1, attributes, false, true).firstValue(graphScope); @@ -563,10 +808,32 @@ public boolean addEdge(final Object v1, final Object v2, final Object e) { } + /** + * New edge. + * + * @param e + * the e + * @param v1 + * the v 1 + * @param v2 + * the v 2 + * @return the edge + * @throws GamaRuntimeException + * the gama runtime exception + */ protected _Edge newEdge(final Object e, final Object v1, final Object v2) throws GamaRuntimeException { return new _Edge(this, e, v1, v2); } + /** + * New vertex. + * + * @param v + * the v + * @return the vertex + * @throws GamaRuntimeException + * the gama runtime exception + */ protected _Vertex newVertex(final Object v) throws GamaRuntimeException { return new _Vertex<>(this); } @@ -614,16 +881,16 @@ public boolean containsVertex(final Object v) { public Set edgeSet() { return edgeMap.keySet(); } + // + // @Override + // public Collection _internalEdgeSet() { + // return edgeMap.values(); + // } - @Override - public Collection _internalEdgeSet() { - return edgeMap.values(); - } - - @Override - public Collection _internalNodesSet() { - return edgeMap.values(); - } + // @Override + // public Collection _internalNodesSet() { + // return edgeMap.values(); + // } @Override public Map> _internalEdgeMap() { @@ -718,9 +985,7 @@ public Set outgoingEdgesOf(final Object vertex) { @Override public boolean removeAllEdges(final Collection edges) { boolean result = false; - for (final Object e : edges) { - result = result || removeEdge(e); - } + for (final Object e : edges) { result = result || removeEdge(e); } return result; } @@ -745,9 +1010,7 @@ public Set removeAllEdges(final Object v1, final Object v2) { @Override public boolean removeAllVertices(final Collection vertices) { boolean result = false; - for (final Object o : vertices.toArray()) { - result = result || removeVertex(o); - } + for (final Object o : vertices.toArray()) { result = result || removeVertex(o); } return result; } @@ -783,9 +1046,7 @@ public boolean removeVertex(final Object v) { if (!containsVertex(v)) return false; incVersion(); final Set edges = edgesOf(v); - for (final Object e : edges) { - removeEdge(e); - } + for (final Object e : edges) { removeEdge(e); } vertexMap.remove(v); dispatchEvent(graphScope, new GraphEvent(graphScope, this, this, null, v, GraphEventType.VERTEX_REMOVED)); @@ -813,16 +1074,25 @@ public Set vertexSet() { } @Override - public void setShortestPathAlgorithm(final String s) { - pathFindingAlgo = shortestPathAlgorithm.valueOf(s); - } + public void setShortestPathAlgorithm(final String s) { pathFindingAlgo = shortestPathAlgorithm.valueOf(s); } @Override - public void setKShortestPathAlgorithm(final String s) { - kPathFindingAlgo = kShortestPathAlgorithm.valueOf(s); - } + public void setKShortestPathAlgorithm(final String s) { kPathFindingAlgo = kShortestPathAlgorithm.valueOf(s); } // protected IPath pathFromEdges(final Object source, final Object + /** + * Path from edges. + * + * @param scope + * the scope + * @param source + * the source + * @param target + * the target + * @param edges + * the edges + * @return the i path + */ // target, final IList edges) { protected IPath> pathFromEdges(final IScope scope, final V source, final V target, final IList edges) { @@ -837,15 +1107,26 @@ public IPath> computeShortestPathBetween(final IScope scope, return pathFromEdges(scope, source, target, computeBestRouteBetween(scope, source, target)); } + /** + * Gets the shortest path. + * + * @param scope + * the scope + * @param algo + * the algo + * @param source + * the source + * @param target + * the target + * @return the shortest path + */ public IList getShortestPath(final IScope scope, final ShortestPathAlgorithm algo, final V source, final V target) { final GraphPath ph = algo.getPath(source, target); if (ph == null) return GamaListFactory.create(getGamlType().getContentType()); final List re = ph.getEdgeList(); - if (re == null) - return GamaListFactory.create(getGamlType().getContentType()); - else - return GamaListFactory.create(scope, getGamlType().getContentType(), re); + if (re == null) return GamaListFactory.create(getGamlType().getContentType()); + return GamaListFactory.create(scope, getGamlType().getContentType(), re); } @Override @@ -906,6 +1187,16 @@ public IList computeBestRouteBetween(final IScope scope, final V source, fina return spl; } + /** + * Save shortest paths. + * + * @param edges + * the edges + * @param source + * the source + * @param target + * the target + */ private void saveShortestPaths(final List edges, final V source, final V target) { V s = source; final IList> spl = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType())); @@ -947,6 +1238,23 @@ public IList>> computeKShortestPathsBetween(final IScop return paths; } + /** + * Ge kt shortest path. + * + * @param scope + * the scope + * @param algo + * the algo + * @param source + * the source + * @param target + * the target + * @param k + * the k + * @param useLinkedGraph + * the use linked graph + * @return the i list + */ public IList> geKtShortestPath(final IScope scope, final KShortestPathAlgorithm algo, final V source, final V target, final int k, final boolean useLinkedGraph) { final List> pathsJGT = algo.getPaths(useLinkedGraph ? source.toString() : source, @@ -959,9 +1267,7 @@ public IList> geKtShortestPath(final IScope scope, final KShortestPathA for (final GraphPath p : pathsJGT) { IList path = GamaListFactory.create(); if (useLinkedGraph) { - for (Object e : p.getEdgeList()) { - path.add((E) fromLinkedGtoEdges.get(e)); - } + for (Object e : p.getEdgeList()) { path.add((E) fromLinkedGtoEdges.get(e)); } } else { path.addAll(p.getEdgeList()); } @@ -995,6 +1301,9 @@ public IList> computeKBestRoutesBetween(final IScope scope, final V sou } + /** + * Generate graph. + */ void generateGraph() { if (linkedJGraph != null) return; fromLinkedGtoEdges = GamaMapFactory.create(); @@ -1003,9 +1312,7 @@ void generateGraph() { SupplierUtil.createDefaultWeightedEdgeSupplier(), true) : new DefaultUndirectedGraph(SupplierUtil.createStringSupplier(), SupplierUtil.createDefaultWeightedEdgeSupplier(), true); - for (Object v : getVertices()) { - linkedJGraph.addVertex(v.toString()); - } + for (Object v : getVertices()) { linkedJGraph.addVertex(v.toString()); } for (Object e : getEdges()) { String s = getEdgeSource(e).toString(); String t = getEdgeTarget(e).toString(); @@ -1125,14 +1432,10 @@ public IContainer reverse(final IScope scope) { } @Override - public IList getEdges() { - return GamaListFactory.wrap(getGamlType().getContentType(), edgeSet()); - } + public IList getEdges() { return GamaListFactory.wrap(getGamlType().getContentType(), edgeSet()); } @Override - public IList getVertices() { - return GamaListFactory.wrap(getGamlType().getKeyType(), vertexSet()); - } + public IList getVertices() { return GamaListFactory.wrap(getGamlType().getKeyType(), vertexSet()); } @Override public IList getSpanningTree(final IScope scope) { @@ -1148,9 +1451,7 @@ public IPath getCircuit(final IScope scope) { final List vertices = hamilton.getTour(this).getVertexList(); final int size = vertices.size(); final IList edges = GamaListFactory.create(getGamlType().getContentType()); - for (int i = 0; i < size - 1; i++) { - edges.add(this.getEdge(vertices.get(i), vertices.get(i + 1))); - } + for (int i = 0; i < size - 1; i++) { edges.add(this.getEdge(vertices.get(i), vertices.get(i + 1))); } return pathFromEdges(scope, null, null, edges); } @@ -1169,14 +1470,10 @@ public Boolean hasCycle() { } @Override - public boolean isDirected() { - return directed; - } + public boolean isDirected() { return directed; } @Override - public void setDirected(final boolean b) { - directed = b; - } + public void setDirected(final boolean b) { directed = b; } @Override public IGraph copy(final IScope scope) { @@ -1240,16 +1537,12 @@ public void removeListener(final IGraphEventListener listener) { public void dispatchEvent(final IScope scope, final GraphEvent event) { synchronized (listeners) { if (listeners.isEmpty()) return; - for (final IGraphEventListener l : listeners) { - l.receiveEvent(scope, event); - } + for (final IGraphEventListener l : listeners) { l.receiveEvent(scope, event); } } } @Override - public int getVersion() { - return version; - } + public int getVersion() { return version; } @Override public void setVersion(final int version) { @@ -1276,36 +1569,34 @@ public java.lang.Iterable iterable(final IScope scope) { public double computeWeight(final IPath gamaPath) { double result = 0; final List l = gamaPath.getEdgeList(); - for (final Object o : l) { - result += getEdgeWeight(o); - } + for (final Object o : l) { result += getEdgeWeight(o); } return result; } @Override public double computeTotalWeight() { double result = 0; - for (final Object o : edgeSet()) { - result += getEdgeWeight(o); - } - for (final Object o : vertexSet()) { - result += getVertexWeight(o); - } + for (final Object o : edgeSet()) { result += getEdgeWeight(o); } + for (final Object o : vertexSet()) { result += getVertexWeight(o); } return result; } + /** + * Re init path finder. + */ public void reInitPathFinder() { optimizer = null; } - public boolean isAgentEdge() { - return agentEdge; - } + /** + * Checks if is agent edge. + * + * @return true, if is agent edge + */ + public boolean isAgentEdge() { return agentEdge; } @Override - public boolean isSaveComputedShortestPaths() { - return saveComputedShortestPaths; - } + public boolean isSaveComputedShortestPaths() { return saveComputedShortestPaths; } @Override public void setSaveComputedShortestPaths(final boolean saveComputedShortestPaths) { @@ -1313,20 +1604,35 @@ public void setSaveComputedShortestPaths(final boolean saveComputedShortestPaths } @Override - public FloydWarshallShortestPathsGAMA getFloydWarshallShortestPaths() { - return optimizer; - } + public FloydWarshallShortestPathsGAMA getFloydWarshallShortestPaths() { return optimizer; } @Override public void setFloydWarshallShortestPaths(final FloydWarshallShortestPathsGAMA optimizer) { this.optimizer = optimizer; } + /** + * Load shortest paths. + * + * @param scope + * the scope + * @param matrix + * the matrix + */ public void loadShortestPaths(final IScope scope, final GamaMatrix matrix) { shortestPathMatrix = GamaIntMatrix.from(scope, matrix); } + /** + * Gets the shortest path from matrix. + * + * @param s + * the s + * @param t + * the t + * @return the shortest path from matrix + */ public IList getShortestPathFromMatrix(final V s, final V t) { final IList vertices = getVertices(); final IList edges = GamaListFactory.create(getGamlType().getContentType()); @@ -1342,9 +1648,7 @@ public IList getShortestPathFromMatrix(final V s, final V t) { final Set eds = this.getAllEdges(vs, vn); E edge = null; - for (final E ed : eds) { - if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } - } + for (final E ed : eds) { if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } } if (edge == null) return GamaListFactory.create(getGamlType().getContentType()); edges.add(edge); previous = next; @@ -1380,6 +1684,25 @@ public IList getShortestPathFromMatrix(final V s, final V t) { * spl); } } } */ + /** + * Gets the path. + * + * @param M + * the m + * @param vertices + * the vertices + * @param nbvertices + * the nbvertices + * @param v1 + * the v 1 + * @param vt + * the vt + * @param i + * the i + * @param j + * the j + * @return the path + */ public IList getPath(final int M[], final IList vertices, final int nbvertices, final Object v1, final Object vt, final int i, final int j) { // VertexPair vv = new VertexPair(v1, vt); @@ -1395,9 +1718,7 @@ public IList getPath(final int M[], final IList vertices, final int nbvertices, final Set eds = this.getAllEdges(vn, vc); E edge = null; - for (final E ed : eds) { - if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } - } + for (final E ed : eds) { if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } } if (edge == null) { break; } edges.add(0, edge); previous = next; @@ -1407,6 +1728,23 @@ public IList getPath(final int M[], final IList vertices, final int nbvertices, return edges; } + /** + * Save paths. + * + * @param M + * the m + * @param vertices + * the vertices + * @param nbvertices + * the nbvertices + * @param v1 + * the v 1 + * @param i + * the i + * @param t + * the t + * @return the i list + */ public IList savePaths(final int M[], final IList vertices, final int nbvertices, final Object v1, final int i, final int t) { IList edgesVertices = GamaListFactory.create(getGamlType().getContentType()); @@ -1424,9 +1762,7 @@ public IList savePaths(final int M[], final IList vertices, final int nbvertices final Set eds = this.getAllEdges(vn, vc); E edge = null; - for (final E ed : eds) { - if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } - } + for (final E ed : eds) { if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) { edge = ed; } } if (edge == null) { break; } edges.add(0, edge); previous = next; @@ -1444,18 +1780,21 @@ public IList savePaths(final int M[], final IList vertices, final int nbvertices return edgesVertices; } + /** + * Save shortest paths. + * + * @param scope + * the scope + * @return the gama int matrix + */ public GamaIntMatrix saveShortestPaths(final IScope scope) { final IMap indexVertices = GamaMapFactory.create(getGamlType().getKeyType(), Types.INT); final IList vertices = getVertices(); - for (int i = 0; i < vertexMap.size(); i++) { - indexVertices.put(vertices.get(i), i); - } + for (int i = 0; i < vertexMap.size(); i++) { indexVertices.put(vertices.get(i), i); } final GamaIntMatrix matrix = new GamaIntMatrix(vertices.size(), vertices.size()); for (int i = 0; i < vertices.size(); i++) { - for (int j = 0; j < vertices.size(); j++) { - matrix.set(scope, j, i, i); - } + for (int j = 0; j < vertices.size(); j++) { matrix.set(scope, j, i, i); } } if (optimizer != null) { for (int i = 0; i < vertices.size(); i++) { @@ -1508,6 +1847,21 @@ public GamaIntMatrix saveShortestPaths(final IScope scope) { } + /** + * Next vertice. + * + * @param scope + * the scope + * @param edge + * the edge + * @param source + * the source + * @param indexVertices + * the index vertices + * @param isDirected + * the is directed + * @return the integer + */ private Integer nextVertice(final IScope scope, final E edge, final V source, final IMap indexVertices, final boolean isDirected) { if (isDirected) return indexVertices.get(scope, (V) this.getEdgeTarget(edge)); @@ -1518,10 +1872,22 @@ private Integer nextVertice(final IScope scope, final E edge, final V source, fi return indexVertices.get(scope, (V) this.getEdgeSource(edge)); } - public Map, IList>> getShortestPathComputed() { - return shortestPathComputed; - } + /** + * Gets the shortest path computed. + * + * @return the shortest path computed + */ + public Map, IList>> getShortestPathComputed() { return shortestPathComputed; } + /** + * Gets the shortest path. + * + * @param s + * the s + * @param t + * the t + * @return the shortest path + */ public IList getShortestPath(final V s, final V t) { final Pair vp = new Pair<>(s, t); final IList> ppc = shortestPathComputed.get(vp); @@ -1529,9 +1895,12 @@ public IList getShortestPath(final V s, final V t) { return ppc.get(0); } - public Map> getVertexMap() { - return vertexMap; - } + /** + * Gets the vertex map. + * + * @return the vertex map + */ + public Map> getVertexMap() { return vertexMap; } /** * Method buildValue() @@ -1564,9 +1933,7 @@ public IContainer buildValues(fin final IContainer objects) { try (final Collector.AsList list = Collector.getList()) { if (!(objects instanceof msi.gaml.operators.Graphs.NodesToAdd)) { - for (final Object o : objects.iterable(scope)) { - list.add(buildValue(scope, o)); - } + for (final Object o : objects.iterable(scope)) { list.add(buildValue(scope, o)); } } else { for (final Object o : objects.iterable(scope)) { list.add(buildValue(scope, new msi.gaml.operators.Graphs.NodeToAdd(o))); @@ -1590,12 +1957,17 @@ public GamaPair buildIndex(final IScope scope, final Object object) { @Override public IContainer> buildIndexes(final IScope scope, final IContainer value) { final IList> result = GamaListFactory.create(Types.PAIR); - for (final Object o : value.iterable(scope)) { - result.add(buildIndex(scope, o)); - } + for (final Object o : value.iterable(scope)) { result.add(buildIndex(scope, o)); } return result; } + /** + * To matrix. + * + * @param scope + * the scope + * @return the gama float matrix + */ public GamaFloatMatrix toMatrix(final IScope scope) { final int nbVertices = this.getVertices().size(); if (nbVertices == 0) return null; @@ -1615,9 +1987,7 @@ public GamaFloatMatrix toMatrix(final IScope scope) { } @Override - public ISpecies getVertexSpecies() { - return vertexSpecies; - } + public ISpecies getVertexSpecies() { return vertexSpecies; } @Override public ISpecies getEdgeSpecies() { @@ -1628,13 +1998,17 @@ public ISpecies getEdgeSpecies() { return edgeSpecies; } + /** + * Dispose vertex. + * + * @param agent + * the agent + */ public void disposeVertex(final IAgent agent) { final Set edgesToModify = edgesOf(agent); removeVertex(agent); - for (final Object obj : edgesToModify) { - if (obj instanceof IAgent) { ((IAgent) obj).dispose(); } - } + for (final Object obj : edgesToModify) { if (obj instanceof IAgent) { ((IAgent) obj).dispose(); } } } @Override @@ -1644,9 +2018,7 @@ public V addVertex() { } @Override - public Supplier getEdgeSupplier() { - return null; - } + public Supplier getEdgeSupplier() { return null; } @Override public GraphType getType() { @@ -1655,8 +2027,6 @@ public GraphType getType() { } @Override - public Supplier getVertexSupplier() { - return null; - } + public Supplier getVertexSupplier() { return null; } } diff --git a/msi.gama.core/src/msi/gama/util/graph/IGraph.java b/msi.gama.core/src/msi/gama/util/graph/IGraph.java index 330123f1e9..f89434b5a9 100644 --- a/msi.gama.core/src/msi/gama/util/graph/IGraph.java +++ b/msi.gama.core/src/msi/gama/util/graph/IGraph.java @@ -1,19 +1,18 @@ /******************************************************************************************************* * - * msi.gama.util.graph.IGraph.java, in plugin msi.gama.core, is part of the source code of the GAMA modeling and - * simulation platform (v. 1.8.1) + * IGraph.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * ********************************************************************************************************/ package msi.gama.util.graph; -import java.util.Collection; import java.util.List; import java.util.Map; +import org.jgrapht.Graph; import msi.gama.metamodel.topology.graph.FloydWarshallShortestPathsGAMA; import msi.gama.precompiler.GamlAnnotations.doc; @@ -32,7 +31,6 @@ import msi.gaml.species.ISpecies; import msi.gaml.types.IType; -import org.jgrapht.Graph; /** * Written by drogoul Modified on 24 nov. 2011 * @@ -68,59 +66,190 @@ public interface IGraph extends IModifiableContainer, Graphs.GraphObjectToAdd>, IAddressableContainer, List>, Graph, IGraphEventProvider { + /** + * Gets the vertex weight. + * + * @param v + * the v + * @return the vertex weight + */ double getVertexWeight(final Object v); + /** + * Gets the weight of. + * + * @param v + * the v + * @return the weight of + */ Double getWeightOf(final Object v); + /** + * Sets the vertex weight. + * + * @param v + * the v + * @param weight + * the weight + */ void setVertexWeight(final Object v, final double weight); + /** + * Sets the weights. + * + * @param weights + * the weights + */ void setWeights(Map weights); - Collection _internalEdgeSet(); + // /** + // * Internal edge set. + // * + // * @return the collection + // */ + // Collection _internalEdgeSet(); - Collection _internalNodesSet(); + // Collection _internalNodesSet(); + /** + * Internal edge map. + * + * @return the map + */ Map> _internalEdgeMap(); + /** + * Internal vertex map. + * + * @return the map + */ Map> _internalVertexMap(); + /** + * Gets the edges. + * + * @return the edges + */ @getter ("edges") IList getEdges(); + /** + * Gets the vertices. + * + * @return the vertices + */ @getter ("vertices") IList getVertices(); + /** + * Gets the spanning tree. + * + * @param scope + * the scope + * @return the spanning tree + */ @getter ("spanning_tree") IList getSpanningTree(IScope scope); + /** + * Gets the circuit. + * + * @param scope + * the scope + * @return the circuit + */ @getter ("circuit") IPath> getCircuit(IScope scope); + /** + * Gets the connected. + * + * @return the connected + */ @getter ("connected") Boolean getConnected(); + /** + * Checks for cycle. + * + * @return the boolean + */ @getter ("has_cycle") Boolean hasCycle(); + /** + * Checks if is directed. + * + * @return true, if is directed + */ boolean isDirected(); + /** + * Sets the directed. + * + * @param b + * the new directed + */ void setDirected(final boolean b); + /** + * Adds the edge. + * + * @param p + * the p + * @return the object + */ Object addEdge(Object p); + /** + * Sets the shortest path algorithm. + * + * @param optiType + * the new shortest path algorithm + */ void setShortestPathAlgorithm(String optiType); + + /** + * Sets the k shortest path algorithm. + * + * @param optiType + * the new k shortest path algorithm + */ void setKShortestPathAlgorithm(String optiType); - - + /** + * Gets the floyd warshall shortest paths. + * + * @return the floyd warshall shortest paths + */ FloydWarshallShortestPathsGAMA getFloydWarshallShortestPaths(); + /** + * Sets the floyd warshall shortest paths. + * + * @param optimizer + * the optimizer + */ void setFloydWarshallShortestPaths(FloydWarshallShortestPathsGAMA optimizer); + /** + * Gets the version. + * + * @return the version + */ int getVersion(); + /** + * Sets the version. + * + * @param version + * the new version + */ void setVersion(int version); + /** + * Inc version. + */ void incVersion(); // FIXME Patrick: To check @@ -129,42 +258,177 @@ public interface IGraph // public abstract IList computeBestRouteBetween(final Object // source, final Object target); + /** + * Compute shortest path between. + * + * @param scope + * the scope + * @param source + * the source + * @param target + * the target + * @return the i path + */ IPath> computeShortestPathBetween(IScope scope, final Node source, final Node target); + /** + * Compute best route between. + * + * @param scope + * the scope + * @param source + * the source + * @param target + * the target + * @return the i list + */ IList computeBestRouteBetween(IScope scope, final Node source, final Node target); + /** + * Compute weight. + * + * @param gamaPath + * the gama path + * @return the double + */ double computeWeight(final IPath> gamaPath); + /** + * Compute total weight. + * + * @return the double + */ double computeTotalWeight(); + /** + * Checks if is save computed shortest paths. + * + * @return true, if is save computed shortest paths + */ boolean isSaveComputedShortestPaths(); + /** + * Sets the save computed shortest paths. + * + * @param saveComputedShortestPaths + * the new save computed shortest paths + */ void setSaveComputedShortestPaths(boolean saveComputedShortestPaths); + /** + * Compute K shortest paths between. + * + * @param scope + * the scope + * @param source + * the source + * @param target + * the target + * @param k + * the k + * @return the i list + */ IList>> computeKShortestPathsBetween(IScope scope, Node source, Node target, int k); + /** + * Compute K best routes between. + * + * @param scope + * the scope + * @param source + * the source + * @param target + * the target + * @param k + * the k + * @return the i list + */ IList> computeKBestRoutesBetween(IScope scope, final Node source, final Node target, int k); + /** + * Builds the value. + * + * @param scope + * the scope + * @param object + * the object + * @return the graphs. graph object to add + */ Graphs.GraphObjectToAdd buildValue(IScope scope, Object object); + /** + * Builds the values. + * + * @param scope + * the scope + * @param objects + * the objects + * @return the i container + */ IContainer buildValues(IScope scope, IContainer objects); + /** + * Builds the index. + * + * @param scope + * the scope + * @param object + * the object + * @return the gama pair + */ GamaPair buildIndex(IScope scope, Object object); + /** + * Builds the indexes. + * + * @param scope + * the scope + * @param value + * the value + * @return the i container + */ IContainer> buildIndexes(IScope scope, IContainer value); + /** + * Gets the vertex species. + * + * @return the vertex species + */ ISpecies getVertexSpecies(); + /** + * Gets the edge species. + * + * @return the edge species + */ ISpecies getEdgeSpecies(); + /** + * Contains. + * + * @param scope + * the scope + * @param o + * the o + * @return true, if successful + */ @Override default boolean contains(final IScope scope, final Object o) { - if (o instanceof GamaPair) { return Graphs.containsEdge(scope, this, (GamaPair) o); } + if (o instanceof GamaPair) return Graphs.containsEdge(scope, this, (GamaPair) o); return Graphs.containsEdge(scope, this, o); } + /** + * Contains key. + * + * @param scope + * the scope + * @param o + * the o + * @return true, if successful + */ @Override default boolean containsKey(final IScope scope, final Object o) { return Graphs.containsVertex(scope, this, o); diff --git a/msi.gama.core/src/msi/gaml/operators/Graphs.java b/msi.gama.core/src/msi/gaml/operators/Graphs.java index 9cfeee5a37..389f7d7b8c 100644 --- a/msi.gama.core/src/msi/gaml/operators/Graphs.java +++ b/msi.gama.core/src/msi/gaml/operators/Graphs.java @@ -1,9 +1,8 @@ /******************************************************************************************************* * - * msi.gaml.operators.Graphs.java, in plugin msi.gama.core, is part of the source code of the GAMA modeling and - * simulation platform (v. 1.8.1) + * Graphs.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * @@ -17,6 +16,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -51,10 +51,10 @@ import msi.gama.common.interfaces.IKeyword; import msi.gama.metamodel.agent.IAgent; import msi.gama.metamodel.shape.GamaPoint; - import msi.gama.metamodel.shape.IShape; import msi.gama.metamodel.topology.graph.GamaSpatialGraph; import msi.gama.metamodel.topology.graph.GamaSpatialGraph.VertexRelationship; +import msi.gama.metamodel.topology.graph.ISpatialGraph; import msi.gama.precompiler.GamlAnnotations.doc; import msi.gama.precompiler.GamlAnnotations.example; import msi.gama.precompiler.GamlAnnotations.no_test; @@ -77,6 +77,8 @@ import msi.gama.util.IList; import msi.gama.util.IMap; import msi.gama.util.graph.GamaGraph; +import msi.gama.util.graph.GamaGraph.kShortestPathAlgorithm; +import msi.gama.util.graph.GamaGraph.shortestPathAlgorithm; import msi.gama.util.graph.GraphAlgorithmsHandmade; import msi.gama.util.graph.GraphFromAgentContainerSynchronizer; import msi.gama.util.graph.IGraph; @@ -104,10 +106,20 @@ @SuppressWarnings ({ "unchecked", "rawtypes" }) public class Graphs { + /** + * The Class IntersectionRelation. + */ private static class IntersectionRelation implements VertexRelationship { + /** The tolerance. */ double tolerance; + /** + * Instantiates a new intersection relation. + * + * @param t + * the t + */ IntersectionRelation(final double t) { tolerance = t; } @@ -123,15 +135,21 @@ public boolean related(final IScope scope, final IShape p1, final IShape p2) { public boolean equivalent(final IScope scope, final IShape p1, final IShape p2) { return p1 == null ? p2 == null : p1.getGeometry().equals(p2.getGeometry()); } - }; + } + /** + * The Class GridNeighborsRelation. + */ private static class GridNeighborsRelation implements VertexRelationship { + /** + * Instantiates a new grid neighbors relation. + */ GridNeighborsRelation() {} @Override public boolean related(final IScope scope, final IShape p1, final IShape p2) { - if (!(p1 instanceof IGridAgent)) { return false; } + if (!(p1 instanceof IGridAgent)) return false; return ((IGridAgent) p1).getNeighbors(scope).contains(p2); } @@ -139,7 +157,7 @@ public boolean related(final IScope scope, final IShape p1, final IShape p2) { public boolean equivalent(final IScope scope, final IShape p1, final IShape p2) { return p1 == p2; } - }; + } // private static class IntersectionRelationLine implements // VertexRelationship { @@ -161,10 +179,20 @@ public boolean equivalent(final IScope scope, final IShape p1, final IShape p2) // // }; + /** + * The Class IntersectionRelationLineTriangle. + */ private static class IntersectionRelationLineTriangle implements VertexRelationship { + /** The optimized for triangulation. */ final boolean optimizedForTriangulation; + /** + * Instantiates a new intersection relation line triangle. + * + * @param optimizedForTriangulation + * the optimized for triangulation + */ IntersectionRelationLineTriangle(final boolean optimizedForTriangulation) { this.optimizedForTriangulation = optimizedForTriangulation; } @@ -176,20 +204,14 @@ public boolean related(final IScope scope, final IShape p1, final IShape p2) { final Coordinate[] coord1 = p1.getInnerGeometry().getCoordinates(); final Coordinate[] coord2 = p2.getInnerGeometry().getCoordinates(); - for (int i = 0; i < 3; i++) { - if (ArrayUtils.contains(coord2, coord1[i])) { - nb++; - } - } + for (int i = 0; i < 3; i++) { if (ArrayUtils.contains(coord2, coord1[i])) { nb++; } } return nb == 2; } try (ICollector cp = Collector.getSet()) { final GamaPoint[] lp1 = GeometryUtils.getPointsOf(p1); for (final GamaPoint pt : GeometryUtils.getPointsOf(p2)) { - if (ArrayUtils.contains(lp1, pt)) { - cp.add(pt); - } + if (ArrayUtils.contains(lp1, pt)) { cp.add(pt); } } return cp.size() == 2; @@ -198,16 +220,26 @@ public boolean related(final IScope scope, final IShape p1, final IShape p2) { @Override public boolean equivalent(final IScope scope, final IShape p1, final IShape p2) { - if (optimizedForTriangulation) { return p1 == p2; } + if (optimizedForTriangulation) return p1 == p2; return p1 == null ? p2 == null : p1.getGeometry().equals(p2.getGeometry()); } - }; + } + /** + * The Class DistanceRelation. + */ private static class DistanceRelation implements VertexRelationship { + /** The distance. */ double distance; + /** + * Instantiates a new distance relation. + * + * @param d + * the d + */ DistanceRelation(final double d) { distance = d; } @@ -241,15 +273,40 @@ public boolean equivalent(final IScope scope, final IShape p1, final IShape p2) public interface GraphObjectToAdd { + /** + * Gets the object. + * + * @return the object + */ Object getObject(); } + /** + * The Class EdgeToAdd. + */ public static class EdgeToAdd implements GraphObjectToAdd { + /** The target. */ public Object source, target; + + /** The object. */ public Object object; + + /** The weight. */ public Double weight; + /** + * Instantiates a new edge to add. + * + * @param source + * the source + * @param target + * the target + * @param object + * the object + * @param weight + * the weight + */ public EdgeToAdd(final Object source, final Object target, final Object object, final Double weight) { this.object = object; this.weight = weight; @@ -257,6 +314,18 @@ public EdgeToAdd(final Object source, final Object target, final Object object, this.target = target; } + /** + * Instantiates a new edge to add. + * + * @param source + * the source + * @param target + * the target + * @param object + * the object + * @param weight + * the weight + */ public EdgeToAdd(final Object source, final Object target, final Object object, final Integer weight) { this.object = object; this.weight = weight == null ? null : weight.doubleValue(); @@ -265,9 +334,7 @@ public EdgeToAdd(final Object source, final Object target, final Object object, } @Override - public Object getObject() { - return object; - } + public Object getObject() { return object; } /** * @param cast @@ -277,11 +344,25 @@ public EdgeToAdd(final Object o) { } } + /** + * The Class NodeToAdd. + */ public static class NodeToAdd implements GraphObjectToAdd { + /** The object. */ public Object object; + + /** The weight. */ public Double weight; + /** + * Instantiates a new node to add. + * + * @param object + * the object + * @param weight + * the weight + */ public NodeToAdd(final Object object, final Double weight) { this.object = object; this.weight = weight; @@ -295,12 +376,13 @@ public NodeToAdd(final Object o) { } @Override - public Object getObject() { - return object; - } + public Object getObject() { return object; } } + /** + * The Class NodesToAdd. + */ public static class NodesToAdd extends GamaList implements GraphObjectToAdd { /** @@ -308,25 +390,36 @@ public static class NodesToAdd extends GamaList implements Gra */ private static final long serialVersionUID = 1L; + /** + * Instantiates a new nodes to add. + */ public NodesToAdd() { super(0, Types.NO_TYPE); } + /** + * From. + * + * @param scope + * the scope + * @param object + * the object + * @return the nodes to add + */ public static NodesToAdd from(final IScope scope, final IContainer object) { final NodesToAdd n = new NodesToAdd(); - for (final Object o : object.iterable(scope)) { - n.add((GraphObjectToAdd) o); - } + for (final Object o : object.iterable(scope)) { n.add((GraphObjectToAdd) o); } return n; } @Override - public Object getObject() { - return this; - } + public Object getObject() { return this; } } + /** + * The Class EdgesToAdd. + */ public static class EdgesToAdd extends GamaList implements GraphObjectToAdd { /** @@ -334,25 +427,42 @@ public static class EdgesToAdd extends GamaList implements Gra */ private static final long serialVersionUID = 1L; + /** + * Instantiates a new edges to add. + */ public EdgesToAdd() { super(0, Types.NO_TYPE); } + /** + * From. + * + * @param scope + * the scope + * @param object + * the object + * @return the edges to add + */ public static EdgesToAdd from(final IScope scope, final IContainer object) { final EdgesToAdd n = new EdgesToAdd(); - for (final Object o : object.iterable(scope)) { - n.add((GraphObjectToAdd) o); - } + for (final Object o : object.iterable(scope)) { n.add((GraphObjectToAdd) o); } return n; } @Override - public Object getObject() { - return this; - } + public Object getObject() { return this; } } + /** + * Gets the agent from geom. + * + * @param path + * the path + * @param geom + * the geom + * @return the agent from geom + */ @operator ( value = "agent_from_geometry", type = IType.AGENT, @@ -370,7 +480,7 @@ public Object getObject() { see = "path") @no_test public static IAgent getAgentFromGeom(final IPath path, final IShape geom) { - if (path == null) { return null; } + if (path == null) return null; return (IAgent) path.getRealObject(geom); } @@ -384,6 +494,17 @@ public static IAgent getAgentFromGeom(final IPath path, final IShape geom) { * new IList(); } return path.getEdgeList(); } */ + /** + * Contains vertex. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the boolean + */ @operator ( value = "contains_vertex", type = IType.BOOL, @@ -398,13 +519,23 @@ public static IAgent getAgentFromGeom(final IPath path, final IShape geom) { equals = "true") }, see = { "contains_edge" }) public static Boolean containsVertex(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { + if (graph == null) throw GamaRuntimeException.error("In the contains_vertex operator, the graph should not be null!", scope); - } - if (vertex instanceof Graphs.NodeToAdd) { return graph.containsVertex(((Graphs.NodeToAdd) vertex).object); } + if (vertex instanceof Graphs.NodeToAdd) return graph.containsVertex(((Graphs.NodeToAdd) vertex).object); return graph.containsVertex(vertex); } + /** + * Contains edge. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the boolean + */ @operator ( value = "contains_edge", type = IType.BOOL, @@ -420,19 +551,27 @@ public static Boolean containsVertex(final IScope scope, final IGraph graph, fin equals = "true") }, see = { "contains_vertex" }) public static Boolean containsEdge(final IScope scope, final IGraph graph, final Object edge) { - if (graph == null) { throw GamaRuntimeException.error("graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("graph is nil", scope); if (edge instanceof Graphs.EdgeToAdd) { final Graphs.EdgeToAdd edge2 = (Graphs.EdgeToAdd) edge; - if (edge2.object != null) { - return graph.containsEdge(edge2.object); - } else if (edge2.source != null && edge2.target != null) { - return graph.containsEdge(edge2.source, edge2.target); - } + if (edge2.object != null) return graph.containsEdge(edge2.object); + if (edge2.source != null && edge2.target != null) return graph.containsEdge(edge2.source, edge2.target); } return graph.containsEdge(edge); } + /** + * Contains edge. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the boolean + */ @operator ( value = "contains_edge", type = IType.BOOL, @@ -449,10 +588,21 @@ public static Boolean containsEdge(final IScope scope, final IGraph graph, final @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "(g contains_edge ({10,5}::{20,3})) = true") public static Boolean containsEdge(final IScope scope, final IGraph graph, final GamaPair edge) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); return graph.containsEdge(edge.first(), edge.last()); } + /** + * Source of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the object + */ @operator ( value = "source_of", type = ITypeProvider.KEY_TYPE_AT_INDEX + 1, @@ -476,11 +626,22 @@ public static Boolean containsEdge(final IScope scope, final IGraph graph, final equals = "{1,5}") }, see = { "target_of" }) public static Object sourceOf(final IScope scope, final IGraph graph, final Object edge) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsEdge(edge)) { return graph.getEdgeSource(edge); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsEdge(edge)) return graph.getEdgeSource(edge); return null; } + /** + * Target of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the object + */ @operator ( value = "target_of", type = ITypeProvider.KEY_TYPE_AT_INDEX + 1, @@ -501,11 +662,22 @@ public static Object sourceOf(final IScope scope, final IGraph graph, final Obje equals = "{12,45}") }, see = "source_of") public static Object targetOf(final IScope scope, final IGraph graph, final Object edge) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsEdge(edge)) { return graph.getEdgeTarget(edge); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsEdge(edge)) return graph.getEdgeTarget(edge); return null; } + /** + * Weight of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the double + */ @operator ( value = "weight_of", category = { IOperatorCategory.GRAPH }, @@ -521,26 +693,33 @@ public static Object targetOf(final IScope scope, final IGraph graph, final Obje value = "graphFromMap weight_of(link({1,5},{12,45}))", equals = "1.0") }) public static Double weightOf(final IScope scope, final IGraph graph, final Object edge) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); if (edge instanceof Graphs.GraphObjectToAdd) { if (edge instanceof Graphs.EdgeToAdd) { final Graphs.EdgeToAdd edge2 = (Graphs.EdgeToAdd) edge; - if (edge2.object != null) { - return graph.getEdgeWeight(edge2.object); - } else if (edge2.source != null && edge2.target != null) { + if (edge2.object != null) return graph.getEdgeWeight(edge2.object); + if (edge2.source != null && edge2.target != null) { final Object edge3 = graph.getEdge(edge2.source, edge2.target); return graph.getEdgeWeight(edge3); } - } else if (edge instanceof Graphs.NodeToAdd) { - return graph.getVertexWeight(((Graphs.NodeToAdd) edge).object); - } + } else if (edge instanceof Graphs.NodeToAdd) return graph.getVertexWeight(((Graphs.NodeToAdd) edge).object); } - if (graph.containsEdge(edge)) { - return graph.getEdgeWeight(edge); - } else if (graph.containsVertex(edge)) { return graph.getVertexWeight(edge); } + if (graph.containsEdge(edge)) return graph.getEdgeWeight(edge); + if (graph.containsVertex(edge)) return graph.getVertexWeight(edge); return 1d; } + /** + * In edges of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the i list + */ @operator ( value = "in_edges_of", type = IType.LIST, @@ -560,13 +739,23 @@ public static Double weightOf(final IScope scope, final IGraph graph, final Obje @test ("graph g2 <- directed(as_edge_graph([ edge({10,5}, {30,30}), edge({30,30}, {80,35}), node ({30,30})]));\r\n" + "first(link({10,5},{30,30})) = first(g2 in_edges_of {30,30})") public static IList inEdgesOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return GamaListFactory.create(scope, graph.getGamlType().getContentType(), graph.incomingEdgesOf(vertex)); - } return GamaListFactory.create(graph.getGamlType().getContentType()); } + /** + * Edge between. + * + * @param scope + * the scope + * @param graph + * the graph + * @param verticePair + * the vertice pair + * @return the object + */ @operator ( value = "edge_between", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -582,13 +771,23 @@ public static IList inEdgesOf(final IScope scope, final IGraph graph, final Obje + "edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "(g edge_between ({10,5}::{20,3})) = g.edges[0]") public static Object EdgeBetween(final IScope scope, final IGraph graph, final GamaPair verticePair) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(verticePair.key) && graph.containsVertex(verticePair.value)) { + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(verticePair.key) && graph.containsVertex(verticePair.value)) return graph.getEdge(verticePair.key, verticePair.value); - } return null; } + /** + * In dregree of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the int + */ @operator ( value = "in_degree_of", type = IType.INT, @@ -607,11 +806,22 @@ public static Object EdgeBetween(final IScope scope, final IGraph graph, final G @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "(g in_degree_of ({20,3})) = 1") public static int inDregreeOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { return graph.inDegreeOf(vertex); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return graph.inDegreeOf(vertex); return 0; } + /** + * Out edges of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the i list + */ @operator ( value = "out_edges_of", type = IType.LIST, @@ -632,13 +842,23 @@ public static int inDregreeOf(final IScope scope, final IGraph graph, final Obje @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " list li <- g out_edges_of {10,5}; length(li) = 2") public static IList outEdgesOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return GamaListFactory.create(scope, graph.getGamlType().getContentType(), graph.outgoingEdgesOf(vertex)); - } return GamaListFactory.create(graph.getGamlType().getContentType()); } + /** + * Out dregree of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the int + */ @operator ( value = "out_degree_of", type = IType.INT, @@ -659,11 +879,22 @@ public static IList outEdgesOf(final IScope scope, final IGraph graph, final Obj @test ("graph g2 <- directed(as_edge_graph([ edge({30,30}, {10,5}), edge({30,30}, {80,35}), node ({30,30})]));\r\n" + "g2 out_degree_of {30,30} = 2") public static int outDregreeOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { return graph.outDegreeOf(vertex); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return graph.outDegreeOf(vertex); return 0; } + /** + * Degree of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the int + */ @operator ( value = "degree_of", type = IType.INT, @@ -682,11 +913,20 @@ public static int outDregreeOf(final IScope scope, final IGraph graph, final Obj @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " (g degree_of ({10,5})) = 3") public static int degreeOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { return graph.degreeOf(vertex); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return graph.degreeOf(vertex); return 0; } + /** + * Connected component of. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i list + */ @operator ( value = "connected_components_of", type = IType.LIST, @@ -705,7 +945,7 @@ public static int degreeOf(final IScope scope, final IGraph graph, final Object @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " list comp <- connected_components_of(g); " + " length(comp) = 2") public static IList connectedComponentOf(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); ConnectivityInspector ci; ci = new ConnectivityInspector(graph); @@ -713,10 +953,21 @@ public static IList connectedComponentOf(final IScope scope, final IGraph for (final Object obj : ci.connectedSets()) { results.add(GamaListFactory.create(scope, graph.getGamlType().getKeyType(), (Set) obj)); } - + return results; } + /** + * Connected component of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param edge + * the edge + * @return the i list + */ @operator ( value = "connected_components_of", type = IType.LIST, @@ -735,7 +986,7 @@ public static IList connectedComponentOf(final IScope scope, final IGraph @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " list comp <- connected_components_of(g, true); " + " length(comp) = 2") public static IList connectedComponentOf(final IScope scope, final IGraph graph, final boolean edge) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); ConnectivityInspector ci; ci = new ConnectivityInspector(graph); @@ -743,9 +994,7 @@ public static IList connectedComponentOf(final IScope scope, final IGraph for (final Object obj : ci.connectedSets()) { if (edge) { final IList edges = GamaListFactory.create(scope, graph.getGamlType().getContentType()); - for (final Object v : (Set) obj) { - edges.addAll(graph.edgesOf(v)); - } + for (final Object v : (Set) obj) { edges.addAll(graph.edgesOf(v)); } results.add(Containers.remove_duplicates(scope, edges)); @@ -756,6 +1005,15 @@ public static IList connectedComponentOf(final IScope scope, final IGraph return results; } + /** + * Reduce to mainconnected component of. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i graph + */ @operator ( value = "main_connected_component", type = IType.GRAPH, @@ -772,7 +1030,7 @@ public static IList connectedComponentOf(final IScope scope, final IGraph @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " length(main_connected_component(g)) = 5") public static IGraph ReduceToMainconnectedComponentOf(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final IList cc = connectedComponentOf(scope, graph); final IGraph newGraph = (IGraph) graph.copy(scope); @@ -787,14 +1045,21 @@ public static IGraph ReduceToMainconnectedComponentOf(final IScope scope, final if (mainCC != null) { final Set vs = graph.vertexSet(); vs.removeAll(mainCC); - for (final Object v : vs) { - newGraph.removeVertex(v); - } + for (final Object v : vs) { newGraph.removeVertex(v); } } return newGraph; } + /** + * Gets the maximal cliques. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the maximal cliques + */ @operator ( value = "maximal_cliques_of", type = IType.LIST, @@ -813,7 +1078,7 @@ public static IGraph ReduceToMainconnectedComponentOf(final IScope scope, final @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " maximal_cliques_of(g) = [[{10.0,5.0,0.0},{20.0,3.0,0.0}],[{30.0,30.0,0.0},{10.0,5.0,0.0}],[{20.0,3.0,0.0}],[{30.0,30.0,0.0},{80.0,35.0,0.0}],[{40.0,60.0,0.0},{80.0,35.0,0.0}],[{40.0,60.0,0.0}],[{50.0,50.0,0.0}]] ") public static IList getMaximalCliques(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final BronKerboschCliqueFinder cls = new BronKerboschCliqueFinder(graph); final IList results = GamaListFactory.create(Types.LIST); Iterator it = cls.iterator(); @@ -823,6 +1088,15 @@ public static IList getMaximalCliques(final IScope scope, final IGraph gr return results; } + /** + * Gets the biggest cliques. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the biggest cliques + */ @operator ( value = "biggest_cliques_of", type = IType.LIST, @@ -841,7 +1115,7 @@ public static IList getMaximalCliques(final IScope scope, final IGraph gr @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " biggest_cliques_of(g) = [[{10.0,5.0,0.0},{20.0,3.0,0.0}],[{30.0,30.0,0.0},{10.0,5.0,0.0}],[{30.0,30.0,0.0},{80.0,35.0,0.0}],[{40.0,60.0,0.0},{80.0,35.0,0.0}]] ") public static IList getBiggestCliques(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final BronKerboschCliqueFinder cls = new BronKerboschCliqueFinder(graph); final IList results = GamaListFactory.create(Types.LIST); @@ -852,6 +1126,15 @@ public static IList getBiggestCliques(final IScope scope, final IGraph gr return results; } + /** + * Nb cycles. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the int + */ @operator ( value = "nb_cycles", type = IType.INT, @@ -869,13 +1152,22 @@ public static IList getBiggestCliques(final IScope scope, final IGraph gr @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " nb_cycles(g) = 1 ") public static int nbCycles(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final int S = graph.vertexSet().size(); final int C = connectedComponentOf(scope, graph).size(); final int L = graph.edgeSet().size(); return L - S + C; } + /** + * Alpha index. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the double + */ @operator ( value = "alpha_index", category = { IOperatorCategory.GRAPH }, @@ -893,11 +1185,20 @@ public static int nbCycles(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " alpha_index(g) = 0.14285714285714285 ") public static double alphaIndex(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final int S = graph.vertexSet().size(); return nbCycles(scope, graph) / (2.0 * S - 5); } + /** + * Beta index. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the double + */ @operator ( value = "beta_index", category = { IOperatorCategory.GRAPH }, @@ -914,10 +1215,19 @@ public static double alphaIndex(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " beta_index(g) = 0.8333333333333334 ") public static double betaIndex(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); return (graph.edgeSet().size() + 0.0) / graph.vertexSet().size(); } + /** + * Gamma index. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the double + */ @operator ( value = "gamma_index", category = { IOperatorCategory.GRAPH }, @@ -934,10 +1244,19 @@ public static double betaIndex(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " gamma_index(g) = 0.7142857142857143 ") public static double gammaIndex(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); return graph.edgeSet().size() / (2.0 * graph.vertexSet().size() - 5); } + /** + * Connectivity index. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the double + */ @operator ( value = "connectivity_index", category = { IOperatorCategory.GRAPH }, @@ -954,12 +1273,21 @@ public static double gammaIndex(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " connectivity_index(g) = 0.8 ") public static double connectivityIndex(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final int S = graph.vertexSet().size(); final int C = connectedComponentOf(scope, graph).size(); return (S - C) / (S - 1.0); } + /** + * Betweenness centrality. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i map + */ @operator ( value = "betweenness_centrality", type = IType.MAP, @@ -978,35 +1306,25 @@ public static double connectivityIndex(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + " betweenness_centrality(g) = [{10.0,5.0,0.0}::5,{20.0,3.0,0.0}::0,{30.0,30.0,0.0}::2,{80.0,35.0,0.0}::4,{40.0,60.0,0.0}::0,{50.0,50.0,0.0}::0] ") public static IMap betweennessCentrality(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final IMap mapResult = GamaMapFactory.create(graph.getGamlType().getKeyType(), Types.INT); final IList vertices = Cast.asList(scope, graph.vertexSet()); - for (final Object v : vertices) { - mapResult.put(v, 0); - } + for (final Object v : vertices) { mapResult.put(v, 0); } final boolean directed = graph.isDirected(); for (int i = 0; i < vertices.size(); i++) { for (int j = directed ? 0 : i + 1; j < vertices.size(); j++) { final Object v1 = vertices.get(i); final Object v2 = vertices.get(j); - if (v1 == v2) { - continue; - } + if (v1 == v2) { continue; } final List edges = graph.computeBestRouteBetween(scope, v1, v2); - if (edges == null) { - continue; - } + if (edges == null) { continue; } Object vc = v1; for (final Object edge : edges) { Object node = graph.getEdgeTarget(edge); - - if (node == vc) { - node = graph.getEdgeSource(edge); - } - if (node != v2 && node != v1) { - mapResult.put(node, (Integer) mapResult.get(node) + 1); - } + + if (node == vc) { node = graph.getEdgeSource(edge); } + if (node != v2 && node != v1) { mapResult.put(node, (Integer) mapResult.get(node) + 1); } vc = node; } } @@ -1014,6 +1332,15 @@ public static IMap betweennessCentrality(final IScope scope, final IGraph graph) return mapResult; } + /** + * Edge betweenness. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i map + */ @operator ( value = "edge_betweenness", type = IType.MAP, @@ -1031,34 +1358,36 @@ public static IMap betweennessCentrality(final IScope scope, final IGraph graph) see = {}) @no_test public static IMap edgeBetweenness(final IScope scope, final IGraph graph) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - // DEBUG.OUT("result.getRaw() : " + result.getRaw()); + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); final IMap mapResult = GamaMapFactory.create(graph.getGamlType().getContentType(), Types.INT); - for (final Object v : graph.edgeSet()) { - mapResult.put(v, 0); - } + for (final Object v : graph.edgeSet()) { mapResult.put(v, 0); } final IList vertices = Cast.asList(scope, graph.vertexSet()); final boolean directed = graph.isDirected(); for (int i = 0; i < vertices.size(); i++) { for (int j = directed ? 0 : i + 1; j < vertices.size(); j++) { final Object v1 = vertices.get(i); final Object v2 = vertices.get(j); - if (v1 == v2) { - continue; - } + if (v1 == v2) { continue; } final List edges = graph.computeBestRouteBetween(scope, v1, v2); - if (edges == null) { - continue; - } - for (final Object edge : edges) { - mapResult.put(edge, (Integer) mapResult.get(edge) + 1); - } + if (edges == null) { continue; } + for (final Object edge : edges) { mapResult.put(edge, (Integer) mapResult.get(edge) + 1); } } } return mapResult; } + /** + * Neighbors of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the i list + */ @operator ( value = { "neighbors_of" }, type = IType.LIST, @@ -1079,14 +1408,23 @@ public static IMap edgeBetweenness(final IScope scope, final IGraph graph) { @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "(g neighbors_of ({10,5}) sort_by point(each)) = [{20.0,3.0,0.0},{30.0,30.0,0.0},{80.0,35.0,0.0}]") public static IList neighborsOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph == null) { throw GamaRuntimeException.error("The graph is nil", scope); } - if (graph.containsVertex(vertex)) { - return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), - org.jgrapht.Graphs.neighborListOf(graph, vertex)); - } + if (graph == null) throw GamaRuntimeException.error("The graph is nil", scope); + if (graph.containsVertex(vertex)) return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), + org.jgrapht.Graphs.neighborListOf(graph, vertex)); return GamaListFactory.create(graph.getGamlType().getKeyType()); } + /** + * Predecessors of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the i list + */ @operator ( value = "predecessors_of", type = IType.LIST, @@ -1110,13 +1448,22 @@ public static IList neighborsOf(final IScope scope, final IGraph graph, final Ob @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "g predecessors_of ({10,5}) = [{80.0,35.0,0.0}]") public static IList predecessorsOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph.containsVertex(vertex)) { - return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), - org.jgrapht.Graphs.predecessorListOf(graph, vertex)); - } + if (graph.containsVertex(vertex)) return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), + org.jgrapht.Graphs.predecessorListOf(graph, vertex)); return GamaListFactory.create(graph.getGamlType().getKeyType()); } + /** + * Successors of. + * + * @param scope + * the scope + * @param graph + * the graph + * @param vertex + * the vertex + * @return the i list + */ @operator ( value = "successors_of", content_type = ITypeProvider.KEY_TYPE_AT_INDEX + 1, @@ -1137,13 +1484,20 @@ public static IList predecessorsOf(final IScope scope, final IGraph graph, final @test ("graph g <- directed(as_edge_graph([edge({10,5}, {20,3}), edge({10,5}, {30,30}),edge({30,30}, {80,35}),edge({80,35}, {40,60}),edge({80,35}, {10,5}), node ({50,50})]));\r\n" + "g successors_of ({10,5}) = [{20.0,3.0,0.0},{30.0,30.0,0.0}]") public static IList successorsOf(final IScope scope, final IGraph graph, final Object vertex) { - if (graph.containsVertex(vertex)) { - return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), - org.jgrapht.Graphs.successorListOf(graph, vertex)); - } + if (graph.containsVertex(vertex)) return GamaListFactory.create(scope, graph.getGamlType().getKeyType(), + org.jgrapht.Graphs.successorListOf(graph, vertex)); return GamaListFactory.create(graph.getGamlType().getKeyType()); } + /** + * Spatial from edges. + * + * @param scope + * the scope + * @param edges + * the edges + * @return the i graph + */ @operator ( value = "as_edge_graph", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1172,7 +1526,20 @@ public static IGraph spatialFromEdges(final IScope scope, final IContainer edges return createdGraph; } - + + /** + * Spatial from vertices. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param tolerance + * the tolerance + * @param edgeSpecies + * the edge species + * @return the i graph + */ @operator ( value = "as_intersection_graph", content_type = IType.GEOMETRY, @@ -1196,6 +1563,17 @@ public static IGraph spatialFromVertices(final IScope scope, final IContainer ve return createdGraph; } + /** + * Spatial from edges. + * + * @param scope + * the scope + * @param edges + * the edges + * @param tolerance + * the tolerance + * @return the i graph + */ @operator ( value = "as_edge_graph", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1226,6 +1604,15 @@ public static IGraph spatialFromEdges(final IScope scope, final IContainer edges return createdGraph; } + /** + * Spatial from edges. + * + * @param scope + * the scope + * @param edges + * the edges + * @return the i graph + */ @operator ( value = "as_edge_graph", index_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1244,7 +1631,20 @@ public static IGraph spatialFromEdges(final IScope scope, final IMap edges) { // Edges are represented by pairs of vertex::vertex return GamaGraphType.from(scope, edges, true); - }@operator ( + } + + /** + * Spatial from edges. + * + * @param scope + * the scope + * @param edges + * the edges + * @param nodes + * the nodes + * @return the i graph + */ + @operator ( value = "as_edge_graph", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, index_type = IType.GEOMETRY, @@ -1257,9 +1657,20 @@ public static IGraph spatialFromEdges(final IScope scope, final IMap edges) { @test ("graph comp <- as_edge_graph([line([{1,5},{12,45}]),line([{12,45},{34,56}])], [{1,5},{12,45},{34,56}]);" + " ( ({1,5} in comp.vertices) and ({12,45} in comp.vertices) and ({34,56} in comp.vertices) ) ") public static IGraph spatialFromEdges(final IScope scope, final IContainer edges, final IContainer nodes) { - return new GamaSpatialGraph(edges, nodes, scope); + return new GamaSpatialGraph(edges, nodes, scope); } + /** + * Spatial from vertices. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param tolerance + * the tolerance + * @return the i graph + */ @operator ( value = "as_intersection_graph", content_type = IType.GEOMETRY, @@ -1283,42 +1694,74 @@ public static IGraph spatialFromVertices(final IScope scope, final IContainer ve return createdGraph; } + /** + * Spatial line intersection. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @return the i graph + */ public static IGraph spatialLineIntersection(final IScope scope, final IContainer vertices) { return new GamaSpatialGraph(vertices, false, false, new IntersectionRelationLineTriangle(true), null, scope, vertices.getGamlType().getContentType(), Types.GEOMETRY); } + /** + * Spatial line intersection triangle. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @return the i graph + */ public static IGraph spatialLineIntersectionTriangle(final IScope scope, final IContainer vertices) { final IGraph g = new GamaSpatialGraph(scope, vertices.getGamlType().getContentType(), Types.GEOMETRY); - for (final Object o : vertices.iterable(scope)) { - g.addVertex(o); - } + for (final Object o : vertices.iterable(scope)) { g.addVertex(o); } for (final Object o1 : vertices.iterable(scope)) { final Coordinate[] coord1 = ((IShape) o1).getInnerGeometry().getCoordinates(); for (final Object o2 : vertices.iterable(scope)) { final Coordinate[] coord2 = ((IShape) o2).getInnerGeometry().getCoordinates(); - if (o1 != o2 && lineInter(coord1, coord2)) { - g.addEdge(o1, o2); - } + if (o1 != o2 && lineInter(coord1, coord2)) { g.addEdge(o1, o2); } } } return g; } + /** + * Line inter. + * + * @param coord1 + * the coord 1 + * @param coord2 + * the coord 2 + * @return true, if successful + */ static boolean lineInter(final Coordinate[] coord1, final Coordinate[] coord2) { int nb = 0; for (int i = 0; i < 3; i++) { final Coordinate c1 = coord1[i]; for (int j = 0; j < 3; j++) { final Coordinate c2 = coord2[j]; - if (c1.x == c2.x && c1.y == c2.y) { - nb++; - } + if (c1.x == c2.x && c1.y == c2.y) { nb++; } } } return nb == 2; } + /** + * Spatial distance graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param distance + * the distance + * @return the i graph + */ @operator ( value = "as_distance_graph", content_type = IType.GEOMETRY, @@ -1343,6 +1786,15 @@ public static IGraph spatialDistanceGraph(final IScope scope, final IContainer v return createdGraph; } + /** + * Grid cells to graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @return the i graph + */ @operator ( value = "grid_cells_to_graph", content_type = IType.GEOMETRY, @@ -1361,13 +1813,21 @@ public static IGraph spatialDistanceGraph(final IScope scope, final IContainer v public static IGraph gridCellsToGraph(final IScope scope, final IContainer vertices) { final IGraph graph = new GamaSpatialGraph(vertices, false, false, new GridNeighborsRelation(), null, scope, vertices.getGamlType().getContentType(), Types.GEOMETRY); - for (final Object e : graph.edgeSet()) { - graph.setEdgeWeight(e, ((IShape) e).getPerimeter()); - } + for (final Object e : graph.edgeSet()) { graph.setEdgeWeight(e, ((IShape) e).getPerimeter()); } return graph; } - + /** + * Grid cells to graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param edgeSpecies + * the edge species + * @return the i graph + */ @operator ( value = "grid_cells_to_graph", content_type = IType.GEOMETRY, @@ -1378,19 +1838,29 @@ public static IGraph gridCellsToGraph(final IScope scope, final IContainer verti value = "creates a graph from a list of cells (operand). An edge is created between neighbors.", see = { "as_intersection_graph", "as_edge_graph" }) @no_test - public static IGraph gridCellsToGraph(final IScope scope, final IContainer vertices,final ISpecies edgeSpecies) { + public static IGraph gridCellsToGraph(final IScope scope, final IContainer vertices, final ISpecies edgeSpecies) { final IType edgeType = scope.getType(edgeSpecies.getName()); final IGraph createdGraph = new GamaSpatialGraph(vertices, false, false, new GridNeighborsRelation(), edgeSpecies, scope, vertices.getGamlType().getContentType(), edgeType); - for (final Object e : createdGraph.edgeSet()) { - createdGraph.setEdgeWeight(e, ((IShape) e).getPerimeter()); - } + for (final Object e : createdGraph.edgeSet()) { createdGraph.setEdgeWeight(e, ((IShape) e).getPerimeter()); } return createdGraph; } - + /** + * Spatial distance graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param distance + * the distance + * @param edgeSpecies + * the edge species + * @return the i graph + */ @operator ( value = "as_distance_graph", content_type = IType.GEOMETRY, @@ -1412,6 +1882,17 @@ public static IGraph spatialDistanceGraph(final IScope scope, final IContainer v return createdGraph; } + /** + * Spatial distance graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @param params + * the params + * @return the i graph + */ @operator ( value = "as_distance_graph", category = { IOperatorCategory.GRAPH }, @@ -1433,6 +1914,15 @@ public static IGraph spatialDistanceGraph(final IScope scope, final IContainer v return createdGraph; } + /** + * Spatial graph. + * + * @param scope + * the scope + * @param vertices + * the vertices + * @return the i graph + */ @operator ( value = "spatial_graph", index_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1448,6 +1938,51 @@ public static IGraph spatial_graph(final IScope scope, final IContainer vertices Types.GEOMETRY); } + /** + * As spatial graph. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i spatial graph + */ + @operator ( + value = "as_spatial_graph", + index_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, + category = { IOperatorCategory.GRAPH }, + concept = { IConcept.GRAPH, IConcept.GEOMETRY, IConcept.POINT }) + @doc ("Creates a spatial graph out of an arbitrary graph. If the argument is already a spatial graph, returns it unchanged. If it contains geometrical nodes or edges, they are kept unchanged") + public static ISpatialGraph as_spatial_graph(final IScope scope, final IGraph graph) { + if (graph instanceof ISpatialGraph) return (ISpatialGraph) graph; + ISpatialGraph result = new GamaSpatialGraph(scope, Types.GEOMETRY, Types.GEOMETRY); + Map map = GamaMapFactory.create(Types.NO_TYPE, Types.GEOMETRY); + IShape world = scope.getSimulation().getGeometry(); + graph.vertexSet().forEach(v -> { + IShape newV = v instanceof IShape ? (IShape) v : Spatial.Punctal.any_location_in(scope, world); + newV.setAttribute(IKeyword.VALUE, v); + map.put(v, newV); + result.addVertex(newV); + }); + graph.edgeSet().forEach(e -> { + Object source = graph.getEdgeSource(e); + Object target = graph.getEdgeTarget(e); + IShape begin = map.get(source); + IShape end = map.get(target); + result.addEdge(e instanceof IShape ? edge(begin, end, e) : edge(begin, end)); + }); + return result; + } + + /** + * Use cache for shortest paths. + * + * @param g + * the g + * @param useCache + * the use cache + * @return the i graph + */ @operator ( value = "use_cache", category = { IOperatorCategory.GRAPH, IOperatorCategory.PATH }, @@ -1461,6 +1996,13 @@ public static IGraph useCacheForShortestPaths(final IGraph g, final boolean useC return GamaGraphType.useChacheForShortestPath(g, useCache); } + /** + * As directed graph. + * + * @param g + * the g + * @return the i graph + */ @operator ( value = "directed", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1477,6 +2019,13 @@ public static IGraph asDirectedGraph(final IGraph g) { return GamaGraphType.asDirectedGraph(g); } + /** + * As undirected graph. + * + * @param g + * the g + * @return the i graph + */ @operator ( value = "undirected", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1493,6 +2042,17 @@ public static IGraph asUndirectedGraph(final IGraph g) { return GamaGraphType.asUndirectedGraph(g); } + /** + * With weights. + * + * @param scope + * the scope + * @param graph + * the graph + * @param weights + * the weights + * @return the i graph + */ @operator ( value = "with_weights", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1512,12 +2072,21 @@ public static IGraph asUndirectedGraph(final IGraph g) { public static IGraph withWeights(final IScope scope, final IGraph graph, final IMap weights) { graph.setWeights(weights); graph.incVersion(); - if (graph instanceof GamaSpatialGraph) { - ((GamaSpatialGraph) graph).reInitPathFinder(); - } + if (graph instanceof GamaSpatialGraph) { ((GamaSpatialGraph) graph).reInitPathFinder(); } return graph; } + /** + * With weights. + * + * @param scope + * the scope + * @param graph + * the graph + * @param weights + * the weights + * @return the i graph + */ @operator ( value = "with_weights", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1534,17 +2103,24 @@ public static IGraph withWeights(final IScope scope, final IGraph graph, final I // change overtime, which can create a problem somewhere... final IList edges = graph.getEdges(); final int n = edges.size(); - if (n != weights.size()) { return graph; } - for (int i = 0; i < n; i++) { - graph.setEdgeWeight(edges.get(i), Cast.asFloat(scope, weights.get(i))); - } + if (n != weights.size()) return graph; + for (int i = 0; i < n; i++) { graph.setEdgeWeight(edges.get(i), Cast.asFloat(scope, weights.get(i))); } graph.incVersion(); - if (graph instanceof GamaSpatialGraph) { - ((GamaSpatialGraph) graph).reInitPathFinder(); - } + if (graph instanceof GamaSpatialGraph) { ((GamaSpatialGraph) graph).reInitPathFinder(); } return graph; } - + + /** + * Sets the K shortest path algorithm. + * + * @param scope + * the scope + * @param graph + * the graph + * @param shortestpathAlgo + * the shortestpath algo + * @return the i graph + */ @operator ( value = "with_k_shortest_path_algorithm", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1558,19 +2134,28 @@ public static IGraph withWeights(final IScope scope, final IGraph graph, final I examples = @example ( value = "graphEpidemio <- graphEpidemio with_optimizer_type \"static\";", isExecutable = false)) - @no_test - public static IGraph setKShortestPathAlgorithm(final IScope scope, final IGraph graph, final String shortestpathAlgo) { + @no_test + public static IGraph setKShortestPathAlgorithm(final IScope scope, final IGraph graph, + final String shortestpathAlgo) { final List existingAlgo = Arrays.asList(GamaGraph.kShortestPathAlgorithm.values()).stream() - .map(a -> a.toString()).collect(Collectors.toList()); - if (existingAlgo.contains(shortestpathAlgo)) { - graph.setKShortestPathAlgorithm(shortestpathAlgo); - } else { - throw GamaRuntimeException.error("The K shortest paths algorithm " + shortestpathAlgo - + " does not exist. Possible K shortest paths algorithms: " + existingAlgo, scope); - } + .map(kShortestPathAlgorithm::toString).collect(Collectors.toList()); + if (!existingAlgo.contains(shortestpathAlgo)) throw GamaRuntimeException.error("The K shortest paths algorithm " + + shortestpathAlgo + " does not exist. Possible K shortest paths algorithms: " + existingAlgo, scope); + graph.setKShortestPathAlgorithm(shortestpathAlgo); return graph; } - + + /** + * Sets the shortest path algorithm. + * + * @param scope + * the scope + * @param graph + * the graph + * @param shortestpathAlgo + * the shortestpath algo + * @return the i graph + */ @operator ( value = "with_shortest_path_algorithm", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1584,19 +2169,28 @@ public static IGraph setKShortestPathAlgorithm(final IScope scope, final IGraph examples = @example ( value = "road_network <- road_network with_shortestpath_algorithm TransitNodeRouting;", isExecutable = false)) - @no_test - public static IGraph setShortestPathAlgorithm(final IScope scope, final IGraph graph, final String shortestpathAlgo) { + @no_test + public static IGraph setShortestPathAlgorithm(final IScope scope, final IGraph graph, + final String shortestpathAlgo) { final List existingAlgo = Arrays.asList(GamaGraph.shortestPathAlgorithm.values()).stream() - .map(a -> a.toString()).collect(Collectors.toList()); - if (existingAlgo.contains(shortestpathAlgo)) { - graph.setShortestPathAlgorithm(shortestpathAlgo); - } else { - throw GamaRuntimeException.error("The shortest path algorithm " + shortestpathAlgo - + " does not exist. Possible shortest path algorithms: " + existingAlgo, scope); - } + .map(shortestPathAlgorithm::toString).collect(Collectors.toList()); + if (!existingAlgo.contains(shortestpathAlgo)) throw GamaRuntimeException.error("The shortest path algorithm " + + shortestpathAlgo + " does not exist. Possible shortest path algorithms: " + existingAlgo, scope); + graph.setShortestPathAlgorithm(shortestpathAlgo); return graph; } + /** + * Sets the optimize type. + * + * @param scope + * the scope + * @param graph + * the graph + * @param optimizerType + * the optimizer type + * @return the i graph + */ @operator ( value = "with_optimizer_type", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1607,16 +2201,25 @@ public static IGraph setShortestPathAlgorithm(final IScope scope, final IGraph g @doc ( value = "changes the shortest path computation method of the given graph", deprecated = "with_shortestpath_algorithm instead", - comment = "the right-hand operand can be Djikstra, BidirectionalDijkstra, BellmannFord, FloydWarshall, Astar, NBAStar, NBAStarApprox, DeltaStepping, CHBidirectionalDijkstra, TransitNodeRouting to use the associated algorithm. ", - examples = @example ( + comment = "the right-hand operand can be Djikstra, BidirectionalDijkstra, BellmannFord, FloydWarshall, Astar, NBAStar, NBAStarApprox, DeltaStepping, CHBidirectionalDijkstra, TransitNodeRouting to use the associated algorithm. ", + examples = @example ( value = "road_network <- road_network with_optimizer_type TransitNodeRouting;", isExecutable = false), see = "set_verbose") @no_test (Reason.DEPRECATED) public static IGraph setOptimizeType(final IScope scope, final IGraph graph, final String optimizerType) { - return setShortestPathAlgorithm(scope,graph,optimizerType); + return setShortestPathAlgorithm(scope, graph, optimizerType); } + /** + * Adds the node. + * + * @param g + * the g + * @param node + * the node + * @return the i graph + */ @operator ( value = "add_node", type = IType.GRAPH, @@ -1640,6 +2243,15 @@ public static IGraph addNode(final IGraph g, final IShape node) { return g; } + /** + * Removes the node from. + * + * @param node + * the node + * @param g + * the g + * @return the i graph + */ @operator ( value = "remove_node_from", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1661,6 +2273,17 @@ public static IGraph removeNodeFrom(final IShape node, final IGraph g) { return g; } + /** + * Rewire graph. + * + * @param scope + * the scope + * @param g + * the g + * @param count + * the count + * @return the i graph + */ @operator ( value = "rewire_n", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1683,6 +2306,15 @@ public static IGraph rewireGraph(final IScope scope, final IGraph g, final Integ return g; } + /** + * Adds the edge. + * + * @param g + * the g + * @param nodes + * the nodes + * @return the i graph + */ @operator ( value = "add_edge", type = IType.GRAPH, @@ -1705,6 +2337,21 @@ public static IGraph addEdge(final IGraph g, final GamaPair nodes) { return g; } + /** + * Path between. + * + * @param scope + * the scope + * @param graph + * the graph + * @param source + * the source + * @param target + * the target + * @return the i path + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "path_between", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1721,12 +2368,25 @@ public static IGraph addEdge(final IGraph g, final GamaPair nodes) { + " length((path_between (g, {10,5}, {50,50}))) = 1 ") public static IPath path_between(final IScope scope, final IGraph graph, final IShape source, final IShape target) throws GamaRuntimeException { - if (graph instanceof GamaSpatialGraph) { - return Cast.asTopology(scope, graph).pathBetween(scope, source, target); - } + if (graph instanceof GamaSpatialGraph) return Cast.asTopology(scope, graph).pathBetween(scope, source, target); return graph.computeShortestPathBetween(scope, source, target); } + /** + * Kpaths between. + * + * @param scope + * the scope + * @param graph + * the graph + * @param sourTarg + * the sour targ + * @param k + * the k + * @return the i list + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "paths_between", type = IType.LIST, @@ -1752,10 +2412,25 @@ public static IPath path_between(final IScope scope, final IGraph graph, final I + " length((paths_between(g, {10,5}:: {80,35}, 2))) = 2") public static IList Kpaths_between(final IScope scope, final GamaGraph graph, final GamaPair sourTarg, final int k) throws GamaRuntimeException { - + return Cast.asTopology(scope, graph).KpathsBetween(scope, (IShape) sourTarg.key, (IShape) sourTarg.value, k); } + /** + * Max flow between. + * + * @param scope + * the scope + * @param graph + * the graph + * @param source + * the source + * @param sink + * the sink + * @return the i map + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "max_flow_between", type = IType.LIST, @@ -1777,6 +2452,19 @@ public static IMap maxFlowBetween(final IScope scope, final Gama return result; } + /** + * As path. + * + * @param scope + * the scope + * @param edgesNodes + * the edges nodes + * @param graph + * the graph + * @return the i path + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "as_path", type = IType.PATH, @@ -1797,7 +2485,19 @@ public static IPath as_path(final IScope scope, final IList edgesNodes, return path; } - + /** + * Prim shortest path file. + * + * @param scope + * the scope + * @param graph + * the graph + * @param matrix + * the matrix + * @return the i graph + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "load_shortest_paths", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1813,10 +2513,8 @@ public static IPath as_path(final IScope scope, final IList edgesNodes, @no_test public static IGraph primShortestPathFile(final IScope scope, final GamaGraph graph, final GamaMatrix matrix) throws GamaRuntimeException { - if (graph == null) { - throw GamaRuntimeException.error("In the load_shortest_paths operator, the graph should not be null!", - scope); - } + if (graph == null) throw GamaRuntimeException + .error("In the load_shortest_paths operator, the graph should not be null!", scope); // final int n = graph.vertexSet().size(); graph.loadShortestPaths(scope, matrix); return graph; @@ -1825,6 +2523,17 @@ public static IGraph primShortestPathFile(final IScope scope, final GamaGraph gr } + /** + * Prim all pair shortest paths. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the gama int matrix + * @throws GamaRuntimeException + * the gama runtime exception + */ @operator ( value = "all_pairs_shortest_path", type = IType.MATRIX, @@ -1840,13 +2549,30 @@ public static IGraph primShortestPathFile(final IScope scope, final GamaGraph gr @no_test public static GamaIntMatrix primAllPairShortestPaths(final IScope scope, final GamaGraph graph) throws GamaRuntimeException { - if (graph == null) { - throw GamaRuntimeException.error("In the all_pairs_shortest_paths operator, the graph should not be null!", - scope); - } + if (graph == null) throw GamaRuntimeException + .error("In the all_pairs_shortest_paths operator, the graph should not be null!", scope); return graph.saveShortestPaths(scope); } + /** + * Layout force. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param coeffForce + * the coeff force + * @param coolingRate + * the cooling rate + * @param maxIteration + * the max iteration + * @param criterion + * the criterion + * @return the i graph + */ @operator ( value = "layout_force", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1870,7 +2596,21 @@ public static IGraph layoutForce(final IScope scope, final GamaGraph graph, fina return graph; } - + /** + * Layout force FR. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param normalization_factor + * the normalization factor + * @param maxIteration + * the max iteration + * @return the i graph + */ @operator ( value = "layout_force_FR", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1886,13 +2626,31 @@ public static IGraph layoutForce(final IScope scope, final GamaGraph graph, fina + " max_iteration is the maximal number of iterations") @no_test public static IGraph layoutForceFR(final IScope scope, final GamaGraph graph, final IShape bounds, - final double normalization_factor, final int maxIteration) { - final FRLayoutAlgorithm2D sim = new FRLayoutAlgorithm2D(maxIteration,normalization_factor,scope.getSimulation().getRandomGenerator().getGenerator()); - LayoutModel2D model = toModel(graph,bounds); + final double normalization_factor, final int maxIteration) { + final FRLayoutAlgorithm2D sim = new FRLayoutAlgorithm2D(maxIteration, normalization_factor, + scope.getSimulation().getRandomGenerator().getGenerator()); + LayoutModel2D model = toModel(graph, bounds); sim.layout(graph, model); - return update_loc(graph,model); + return update_loc(graph, model); } - + + /** + * Indexed FR layout. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param theta + * the theta + * @param normalizationFactor + * the normalization factor + * @param maxIteration + * the max iteration + * @return the i graph + */ @operator ( value = "layout_force_FR_indexed", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1908,16 +2666,24 @@ public static IGraph layoutForceFR(final IScope scope, final GamaGraph graph, fi + " max_iteration is the maximal number of iterations") @no_test public static IGraph indexedFRLayout(final IScope scope, final GamaGraph graph, final IShape bounds, - final double theta, final double normalizationFactor, final int maxIteration) { - final IndexedFRLayoutAlgorithm2D sim = new IndexedFRLayoutAlgorithm2D(maxIteration,theta, normalizationFactor,scope.getSimulation().getRandomGenerator().getGenerator()); - LayoutModel2D model = toModel(graph,bounds); + final double theta, final double normalizationFactor, final int maxIteration) { + final IndexedFRLayoutAlgorithm2D sim = new IndexedFRLayoutAlgorithm2D(maxIteration, theta, normalizationFactor, + scope.getSimulation().getRandomGenerator().getGenerator()); + LayoutModel2D model = toModel(graph, bounds); sim.layout(graph, model); - return update_loc(graph,model); + return update_loc(graph, model); } - - - static IGraph update_loc(IGraph graph, LayoutModel2D model) { + /** + * Update loc. + * + * @param graph + * the graph + * @param model + * the model + * @return the i graph + */ + static IGraph update_loc(final IGraph graph, final LayoutModel2D model) { for (Object s : graph.vertexSet()) { if (s instanceof IShape) { Point2D pt = model.get(s); @@ -1926,18 +2692,45 @@ static IGraph update_loc(IGraph graph, LayoutModel2D model) { } return graph; } - + + /** + * To model. + * + * @param graph + * the graph + * @param bounds + * the bounds + * @return the layout model 2 D + */ static LayoutModel2D toModel(final GamaGraph graph, final IShape bounds) { Envelope3D env = bounds.getEnvelope(); - LayoutModel2D model = new MapLayoutModel2D<>(new Box2D(env.getMinY(), env.getMinY(), env.getWidth(), env.getHeight())); + LayoutModel2D model = + new MapLayoutModel2D<>(new Box2D(env.getMinY(), env.getMinY(), env.getWidth(), env.getHeight())); for (Object s : graph.vertexSet()) { if (s instanceof IShape) { - model.put(s, new Point2D(((IShape)s).getLocation().getX(), ((IShape)s).getLocation().getY())); + model.put(s, new Point2D(((IShape) s).getLocation().getX(), ((IShape) s).getLocation().getY())); } } return model; } - + + /** + * Layout force. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param coeffForce + * the coeff force + * @param coolingRate + * the cooling rate + * @param maxIteration + * the max iteration + * @return the i graph + */ @operator ( value = "layout_force", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1946,7 +2739,7 @@ static LayoutModel2D toModel(final GamaGraph graph, final IShape bounds) { category = { IOperatorCategory.GRAPH }, concept = { IConcept.GRAPH }) @doc ( - value = "layouts a GAMA graph using Force model (in a given spatial bound and given coeff_force, cooling_rate, and max_iteration parameters).", + value = "layouts a GAMA graph using Force model (in a given spatial bound and given coeff_force, cooling_rate, and max_iteration parameters).", special_cases = "usage: layoutForce(graph, bounds, coeff_force, cooling_rate, max_iteration). graph is the graph to which " + "applied the layout; bounds is the shape (geometry) in which the graph should be located; coeff_force is the coefficient used to compute the force, typical value is 0.4; " + "cooling rate is the decreasing coefficient of the temperature, typical value is 0.01; max_iteration is the maximal number of iterations" @@ -1960,6 +2753,19 @@ public static IGraph layoutForce(final IScope scope, final GamaGraph graph, fina return graph; } + /** + * Layout circle. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param shuffle + * the shuffle + * @return the i graph + */ @operator ( value = "layout_circle", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -1981,6 +2787,19 @@ public static IGraph layoutCircle(final IScope scope, final GamaGraph graph, fin return graph; } + /** + * Layout grid. + * + * @param scope + * the scope + * @param graph + * the graph + * @param bounds + * the bounds + * @param coeffSq + * the coeff sq + * @return the i graph + */ @operator ( value = "layout_grid", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2002,6 +2821,15 @@ public static IGraph layoutGrid(final IScope scope, final GamaGraph graph, final return graph; } + /** + * Adjacency matrix. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the gama float matrix + */ @operator ( value = "adjacency", category = { IOperatorCategory.GRAPH }, @@ -2013,6 +2841,15 @@ public static GamaFloatMatrix adjacencyMatrix(final IScope scope, final GamaGrap return graph.toMatrix(scope); } + /** + * Strahler number. + * + * @param scope + * the scope + * @param graph + * the graph + * @return the i map + */ @operator ( value = "strahler", content_type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2022,13 +2859,12 @@ public static GamaFloatMatrix adjacencyMatrix(final IScope scope, final GamaGrap @no_test public static IMap strahlerNumber(final IScope scope, final GamaGraph graph) { final IMap results = GamaMapFactory.create(Types.NO_TYPE, Types.INT); - if (graph == null || graph.isEmpty(scope)) { return results; } - - IGraph g = graph.getConnected() ? asDirectedGraph(graph): asDirectedGraph(ReduceToMainconnectedComponentOf(scope, graph)); - if ( g.hasCycle()) { - throw GamaRuntimeException - .error("Strahler number can only be computed for Tree (connected graph with no cycle)!", scope); - } + if (graph == null || graph.isEmpty(scope)) return results; + + IGraph g = graph.getConnected() ? asDirectedGraph(graph) + : asDirectedGraph(ReduceToMainconnectedComponentOf(scope, graph)); + if (g.hasCycle()) throw GamaRuntimeException + .error("Strahler number can only be computed for Tree (connected graph with no cycle)!", scope); List currentEdges = of(g.getEdges()).filter(a -> g.outDegreeOf(g.getEdgeTarget(a)) == 0).toList(); while (!currentEdges.isEmpty()) { @@ -2061,19 +2897,41 @@ public static IMap strahlerNumber(final IScope scope, final GamaGraph graph) { return results; } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @param weight + * the weight + * @return the object + */ @operator ( - value = "edge", - type = IType.NONE, // ITypeProvider.TYPE_AT_INDEX + 1, // FIXME This is false - category = { IOperatorCategory.GRAPH }) - @doc ( - value = "Allows to create a wrapper (of type unknown) that wraps two objects and indicates they should be considered as the source and the target of a new edge of a graph. The third (omissible) parameter indicates which weight this edge should have in the graph", - masterDoc = true, - comment = "Useful only in graph-related operations (addition, removal of edges, creation of graphs)") + value = "edge", + type = IType.NONE, // ITypeProvider.TYPE_AT_INDEX + 1, // FIXME This is false + category = { IOperatorCategory.GRAPH }) + @doc ( + value = "Allows to create a wrapper (of type unknown) that wraps two objects and indicates they should be considered as the source and the target of a new edge of a graph. The third (omissible) parameter indicates which weight this edge should have in the graph", + masterDoc = true, + comment = "Useful only in graph-related operations (addition, removal of edges, creation of graphs)") @no_test public static Object edge(final Object source, final Object target, final Double weight) { return edge(source, target, null, weight); } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = IType.NONE, // ITypeProvider.TYPE_AT_INDEX + 1, // FIXME This is @@ -2086,6 +2944,17 @@ public static Object edge(final Object source, final Object target, final Intege return edge(source, target, null, weight); } + /** + * Edge. + * + * @param pair + * the pair + * @param object + * the object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 2, @@ -2097,6 +2966,17 @@ public static Object edge(final GamaPair pair, final Object object, final Double return edge(pair.key, pair.value, object, weight); } + /** + * Edge. + * + * @param pair + * the pair + * @param object + * the object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 2, @@ -2108,6 +2988,15 @@ public static Object edge(final GamaPair pair, final Object object, final Intege return edge(pair.key, pair.value, object, weight); } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @return the object + */ @operator ( value = "edge", type = IType.NONE, // ITypeProvider.TYPE_AT_INDEX + 1, // FIXME this is @@ -2120,6 +3009,17 @@ public static Object edge(final Object source, final Object target) { return edge(source, target, null, (Double) null); } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @param object + * the object + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 3, @@ -2131,6 +3031,19 @@ public static Object edge(final Object source, final Object target, final Object return edge(source, target, object, (Double) null); } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @param object + * the object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 3, @@ -2142,6 +3055,19 @@ public static Object edge(final Object source, final Object target, final Object return new EdgeToAdd(source, target, object, weight); } + /** + * Edge. + * + * @param source + * the source + * @param target + * the target + * @param object + * the object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 3, @@ -2153,6 +3079,15 @@ public static Object edge(final Object source, final Object target, final Object return new EdgeToAdd(source, target, object, weight); } + /** + * Edge. + * + * @param edgeObject + * the edge object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 1, @@ -2164,6 +3099,15 @@ public static Object edge(final Object edgeObject, final Double weight) { return edge(null, null, edgeObject, weight); } + /** + * Edge. + * + * @param edgeObject + * the edge object + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 1, @@ -2175,6 +3119,15 @@ public static Object edge(final Object edgeObject, final Integer weight) { return edge(null, null, edgeObject, weight); } + /** + * Edge. + * + * @param pair + * the pair + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2186,6 +3139,15 @@ public static Object edge(final GamaPair pair, final Double weight) { return edge(pair.key, pair.value, null, weight); } + /** + * Edge. + * + * @param pair + * the pair + * @param weight + * the weight + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2197,6 +3159,13 @@ public static Object edge(final GamaPair pair, final Integer weight) { return edge(pair.key, pair.value, null, weight); } + /** + * Edge. + * + * @param object + * the object + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.TYPE_AT_INDEX + 1, @@ -2208,6 +3177,13 @@ public static Object edge(final Object object) { return edge(null, null, object, (Double) null); } + /** + * Edge. + * + * @param pair + * the pair + * @return the object + */ @operator ( value = "edge", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2219,6 +3195,15 @@ public static Object edge(final GamaPair pair) { return edge(pair.key, pair.value, null, (Double) null); } + /** + * Node. + * + * @param object + * the object + * @param weight + * the weight + * @return the object + */ @operator ( value = "node", type = ITypeProvider.TYPE_AT_INDEX + 1, @@ -2232,6 +3217,13 @@ public static Object node(final Object object, final Double weight) { return new NodeToAdd(object, weight); } + /** + * Node. + * + * @param nodeObject + * the node object + * @return the object + */ @operator ( value = "node", type = ITypeProvider.TYPE_AT_INDEX + 1, @@ -2243,6 +3235,15 @@ public static Object node(final Object nodeObject) { return node(nodeObject, null); } + /** + * Nodes. + * + * @param scope + * the scope + * @param nodes + * the nodes + * @return the i container + */ @operator ( value = "nodes", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2254,6 +3255,15 @@ public static IContainer nodes(final IScope scope, final IContainer nodes) { return NodesToAdd.from(scope, nodes); } + /** + * Edges. + * + * @param scope + * the scope + * @param nodes + * the nodes + * @return the i container + */ @operator ( value = "edges", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2273,7 +3283,87 @@ public static IContainer edges(final IScope scope, final IContainer nodes) { * @param parameters * @return */ - /*@operator ( + /* + * @operator ( value = "generate_barabasi_albert", concept = { IConcept.ALGORITHM }) + * + * @doc ( value = "returns a random scale-free network (following Barabasi-Albert (BA) model).", masterDoc = true, + * comment = + * "The Barabasi-Albert (BA) model is an algorithm for generating random scale-free networks using a preferential attachment mechanism. " + * + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." + + * "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" + * + "The map operand should includes following elements:", // usages = { @usage + * ("\"vertices_specy\": the species of vertices"), // @usage ("\"edges_species\": the species of edges"), // @usage + * ("\"size\": the graph will contain (size + 1) nodes"), // @usage + * ("\"m\": the number of edges added per novel node"), // @usage + * ("\"synchronized\": is the graph and the species of vertices and edges synchronized?") }, usages = { + * + * @usage (value = "\"vertices_specy\": the species of vertices; \"edges_species\": the species of edges ; " + + * "\"size\": the graph will contain (size + 1) nodes; " + "\"m\": the number of edges added per novel node; " + + * "\"synchronized\": is the graph and the species of vertices and edges synchronized?", examples = { @example ( + * value = "graph graphEpidemio <- generate_barabasi_albert(", isExecutable = false), + * + * @example ( value = " yourNodeSpecy,", isExecutable = false), + * + * @example ( value = " yourEdgeSpecy,", isExecutable = false), + * + * @example ( value = " 3,", isExecutable = false), + * + * @example ( value = " 5,", isExecutable = false), + * + * @example ( value = " true);", isExecutable = false) })}, see = { "generate_watts_strogatz" }) + * + * @no_test public static IGraph generateGraphstreamBarabasiAlbert(final IScope scope, final ISpecies + * vertices_specy, final ISpecies edges_species, final Integer size, final Integer m, final Boolean isSychronized) { + * + * BarabasiAlbertGraphGenerator gen = new BarabasiAlbertGraphGenerator<>(m, m, n, + * scope.getSimulation().getRandomGenerator()); return loadGraphWithGraphstreamFromGeneratorSource(scope, + * vertices_specy, edges_species, new BarabasiAlbertGenerator(m), size - 2 // nota: in // graphstream, two // nodes + * are already // created by // default., , isSychronized); + * + * } + * + * @operator ( value = "generate_barabasi_albert", concept = {}) + * + * @doc ( value = "returns a random scale-free network (following Barabasi-Albert (BA) model).", // comment = + * "The Barabasi-Albert (BA) model is an algorithm for generating random scale-free networks using a preferential attachment mechanism. " + * // + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." + * // + + * "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" + * // + "The map operand should includes following elements:", // usages = { @usage + * ("\"agents\": list of existing node agents"), // @usage ("\"edges_species\": the species of edges"), // @usage + * ("\"m0\": the graph will contain (size + 1) nodes"), // @usage + * ("\"m\": the number of edges added per novel node"), // @usage + * ("\"synchronized\": is the graph and the species of vertices and edges synchronized?") }, usages = { @usage ( + * value = "\"agents\": list of existing node agents; \"edges_species\": the species of edges; " + + * "\"size\": the graph will contain (size + 1) nodes; " + "\"m\": the number of edges added per novel node.", + * examples = { @example ( value = "graph graphEpidemio <- generate_barabasi_albert(", + * isExecutable = false), + * + * @example ( value = " yourListOfNodes,", isExecutable = false), + * + * @example ( value = " yourEdgeSpecy,", isExecutable = false), + * + * @example ( value = " 3,", isExecutable = false), + * + * @example ( value = " 5,", isExecutable = false), + * + * @example ( value = " true);", isExecutable = false) })}, see = { "generate_watts_strogatz" }) + * + * @no_test public static IGraph generateGraphstreamBarabasiAlbert(final IScope scope, final IContainer + * agents, final ISpecies edges_species, final Integer m, final Boolean isSychronized) { if (agents.isEmpty(scope)) + * { return null; } final IList nodes = GamaListFactory.create(Types.AGENT); + * nodes.addAll(agents.listValue(scope, Types.AGENT, false)); return + * loadGraphWithGraphstreamFromGeneratorSource(scope, nodes, edges_species, new BarabasiAlbertGenerator(m), + * nodes.size() - 2 // nota: in graphstream, two nodes are already // created by default., , isSychronized); + * + * } + * + * + * + * + */ + + @operator ( value = "generate_barabasi_albert", concept = { IConcept.ALGORITHM }) @doc ( @@ -2283,164 +3373,68 @@ public static IContainer edges(final IScope scope, final IContainer nodes) { + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." + "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" + "The map operand should includes following elements:", -// usages = { @usage ("\"vertices_specy\": the species of vertices"), -// @usage ("\"edges_species\": the species of edges"), -// @usage ("\"size\": the graph will contain (size + 1) nodes"), -// @usage ("\"m\": the number of edges added per novel node"), -// @usage ("\"synchronized\": is the graph and the species of vertices and edges synchronized?") }, - usages = { - @usage (value = "\"vertices_specy\": the species of vertices; \"edges_species\": the species of edges ; " - + "\"size\": the graph will contain (size + 1) nodes; " - + "\"m\": the number of edges added per novel node; " - + "\"synchronized\": is the graph and the species of vertices and edges synchronized?", - examples = { @example ( - value = "graph graphEpidemio <- generate_barabasi_albert(", - isExecutable = false), - @example ( - value = " yourNodeSpecy,", - isExecutable = false), - @example ( - value = " yourEdgeSpecy,", - isExecutable = false), - @example ( - value = " 3,", - isExecutable = false), - @example ( - value = " 5,", - isExecutable = false), - @example ( - value = " true);", - isExecutable = false) })}, - see = { "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphstreamBarabasiAlbert(final IScope scope, final ISpecies vertices_specy, - final ISpecies edges_species, final Integer size, final Integer m, final Boolean isSychronized) { - - BarabasiAlbertGraphGenerator gen = new BarabasiAlbertGraphGenerator<>(m, m, n, scope.getSimulation().getRandomGenerator()); - return loadGraphWithGraphstreamFromGeneratorSource(scope, vertices_specy, edges_species, - new BarabasiAlbertGenerator(m), size - 2 // nota: in - // graphstream, two - // nodes are already - // created by - // default., - , isSychronized); - - } - - @operator ( - value = "generate_barabasi_albert", - concept = {}) - @doc ( - value = "returns a random scale-free network (following Barabasi-Albert (BA) model).", -// comment = "The Barabasi-Albert (BA) model is an algorithm for generating random scale-free networks using a preferential attachment mechanism. " -// + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." -// + "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" -// + "The map operand should includes following elements:", -// usages = { @usage ("\"agents\": list of existing node agents"), -// @usage ("\"edges_species\": the species of edges"), -// @usage ("\"m0\": the graph will contain (size + 1) nodes"), -// @usage ("\"m\": the number of edges added per novel node"), -// @usage ("\"synchronized\": is the graph and the species of vertices and edges synchronized?") }, usages = { @usage ( - value = "\"agents\": list of existing node agents; \"edges_species\": the species of edges; " - + "\"size\": the graph will contain (size + 1) nodes; " - + "\"m\": the number of edges added per novel node.", - examples = { @example ( - value = "graph graphEpidemio <- generate_barabasi_albert(", - isExecutable = false), - @example ( - value = " yourListOfNodes,", - isExecutable = false), - @example ( - value = " yourEdgeSpecy,", - isExecutable = false), - @example ( - value = " 3,", - isExecutable = false), - @example ( - value = " 5,", - isExecutable = false), - @example ( - value = " true);", - isExecutable = false) })}, - see = { "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphstreamBarabasiAlbert(final IScope scope, final IContainer agents, - final ISpecies edges_species, final Integer m, final Boolean isSychronized) { - if (agents.isEmpty(scope)) { return null; } - final IList nodes = GamaListFactory.create(Types.AGENT); - nodes.addAll(agents.listValue(scope, Types.AGENT, false)); - return loadGraphWithGraphstreamFromGeneratorSource(scope, nodes, edges_species, new BarabasiAlbertGenerator(m), - nodes.size() - 2 // nota: in graphstream, two nodes are already - // created by default., - , isSychronized); + value = "\"nbInitNodes\": number of initial nodes; " + + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " + + "\"nbNodes\": final number of nodes; " + "\"directed\": is the graph directed or not; " + + "\"node_species\": the species of vertices; \"edges_species\": the species of edges", - } - - + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", + isExecutable = false), - -*/ - - - @operator ( - value = "generate_barabasi_albert", - concept = { IConcept.ALGORITHM }) - @doc ( - value = "returns a random scale-free network (following Barabasi-Albert (BA) model).", - masterDoc = true, - comment = "The Barabasi-Albert (BA) model is an algorithm for generating random scale-free networks using a preferential attachment mechanism. " - + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." - + "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" - + "The map operand should includes following elements:", - usages = { @usage ( - value = "\"nbInitNodes\": number of initial nodes; " - + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " - +"\"nbNodes\": final number of nodes; " - + "\"directed\": is the graph directed or not; " - +"\"node_species\": the species of vertices; \"edges_species\": the species of edges", - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 60,", - isExecutable = false), - @example ( - value = " 1,", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " myVertexSpecies,", - isExecutable = false), - @example ( - value = " myEdgeSpecies);", - isExecutable = false)})}, + @example ( + value = " 60,", + isExecutable = false), + @example ( + value = " 1,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " myVertexSpecies,", + isExecutable = false), + @example ( + value = " myEdgeSpecies);", + isExecutable = false) }) }, see = { "generate_watts_strogatz" }) @no_test - public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer initNbNodes ,final Integer nbEdgesAdded, final Integer nbNodes, final Boolean directed, final ISpecies node_species, + public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer initNbNodes, + final Integer nbEdgesAdded, final Integer nbNodes, final Boolean directed, final ISpecies node_species, final ISpecies edges_species) { - BarabasiAlbertGraphGenerator gen = new BarabasiAlbertGraphGenerator(initNbNodes, nbEdgesAdded, nbNodes, scope.getSimulation().getRandomGenerator().getGenerator()); - AbstractBaseGraph graph = - directed ? - new DirectedMultigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) : - new Multigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); - + BarabasiAlbertGraphGenerator gen = new BarabasiAlbertGraphGenerator(initNbNodes, nbEdgesAdded, nbNodes, + scope.getSimulation().getRandomGenerator().getGenerator()); + AbstractBaseGraph graph = directed + ? new DirectedMultigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) + : new Multigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); + gen.generateGraph(graph); return new GamaGraph<>(scope, graph, node_species, edges_species); - } - + + /** + * Generate graph barabasi albert. + * + * @param scope + * the scope + * @param InitNbNodes + * the init nb nodes + * @param nbEdgesAdded + * the nb edges added + * @param nbNodes + * the nb nodes + * @param directed + * the directed + * @param node_species + * the node species + * @return the i graph + */ @operator ( value = "generate_barabasi_albert", concept = { IConcept.ALGORITHM }) @@ -2451,40 +3445,54 @@ public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integ + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." + "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" + "The map operand should includes following elements:", - usages = { @usage ( - value = "\"nbInitNodes\": number of initial nodes; " - + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " - +"\"nbNodes\": final number of nodes; " - + "\"directed\": is the graph directed or not; " - +"\"node_species\": the species of vertices; \"edges_species\": the species of edges", - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 60,", - isExecutable = false), - @example ( - value = " 1,", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " myVertexSpecies);", - isExecutable = false)})}, + usages = { @usage ( + value = "\"nbInitNodes\": number of initial nodes; " + + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " + + "\"nbNodes\": final number of nodes; " + "\"directed\": is the graph directed or not; " + + "\"node_species\": the species of vertices; \"edges_species\": the species of edges", + + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", + isExecutable = false), + + @example ( + value = " 60,", + isExecutable = false), + @example ( + value = " 1,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " myVertexSpecies);", + isExecutable = false) }) }, see = { "generate_watts_strogatz" }) @no_test - public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer InitNbNodes, final Integer nbEdgesAdded,final Integer nbNodes, final Boolean directed, final ISpecies node_species) { - - return generateGraphBarabasiAlbert(scope,InitNbNodes,nbEdgesAdded,nbNodes,directed,node_species, null); + public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer InitNbNodes, + final Integer nbEdgesAdded, final Integer nbNodes, final Boolean directed, final ISpecies node_species) { + + return generateGraphBarabasiAlbert(scope, InitNbNodes, nbEdgesAdded, nbNodes, directed, node_species, null); } - - + + /** + * Generate graph barabasi albert. + * + * @param scope + * the scope + * @param InitNbNodes + * the init nb nodes + * @param nbEdgesAdded + * the nb edges added + * @param nbNodes + * the nb nodes + * @param directed + * the directed + * @return the i graph + */ @operator ( value = "generate_barabasi_albert", concept = { IConcept.ALGORITHM }) @@ -2495,37 +3503,54 @@ public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integ + "A scale-free network is a network whose degree distribution follows a power law, at least asymptotically." + "Such networks are widely observed in natural and human-made systems, including the Internet, the world wide web, citation networks, and some social networks. [From Wikipedia article]" + "The map operand should includes following elements:", - usages = { @usage ( - value = "\"nbInitNodes\": number of initial nodes; " - + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " - +"\"nbNodes\": final number of nodes; " - + "\"directed\": is the graph directed or not; ", - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 60,", - isExecutable = false), - @example ( - value = " 1,", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true);", - isExecutable = false)})}, + usages = { @usage ( + value = "\"nbInitNodes\": number of initial nodes; " + + "\"nbEdgesAdded\": number of edges of each new node added during the network growth; " + + "\"nbNodes\": final number of nodes; " + "\"directed\": is the graph directed or not; ", + + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", + isExecutable = false), + + @example ( + value = " 60,", + isExecutable = false), + @example ( + value = " 1,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true);", + isExecutable = false) }) }, see = { "generate_watts_strogatz" }) @no_test - public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer InitNbNodes, final Integer nbEdgesAdded,final Integer nbNodes, final Boolean directed) { - - return generateGraphBarabasiAlbert(scope,InitNbNodes,nbEdgesAdded,nbNodes,directed,null, null); + public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integer InitNbNodes, + final Integer nbEdgesAdded, final Integer nbNodes, final Boolean directed) { + + return generateGraphBarabasiAlbert(scope, InitNbNodes, nbEdgesAdded, nbNodes, directed, null, null); } - - - + + /** + * Generate graph watts strogatz. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param p + * the p + * @param k + * the k + * @param directed + * the directed + * @param node_species + * the node species + * @param edges_species + * the edges species + * @return the i graph + */ @operator ( value = "generate_watts_strogatz", concept = { IConcept.ALGORITHM }) @@ -2536,52 +3561,66 @@ public static IGraph generateGraphBarabasiAlbert(final IScope scope, final Integ + "A small-world network is a type of graph in which most nodes are not neighbors of one another, but most nodes can be reached from every other by a small number of hops or steps. [From Wikipedia article]" + "The map operand should includes following elements:", usages = { @usage ( - value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " - + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " - + "\"k\": the base degree of each node (k must be greater than 2 and even); " - + "\"directed\": is the graph directed or not; " - +"\"node_species\": the species of vertices; \"edges_species\": the species of edges", - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " 0.3,", - isExecutable = false), - @example ( - value = " 5,", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " myVertexSpecies,", + value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " + + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " + + "\"k\": the base degree of each node (k must be greater than 2 and even); " + + "\"directed\": is the graph directed or not; " + + "\"node_species\": the species of vertices; \"edges_species\": the species of edges", + + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", isExecutable = false), - @example ( - value = " myEdgeSpecies);", - isExecutable = false)})}, + + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " 0.3,", + isExecutable = false), + @example ( + value = " 5,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " myVertexSpecies,", + isExecutable = false), + @example ( + value = " myEdgeSpecies);", + isExecutable = false) }) }, see = { "generate_barabasi_albert" }) @no_test - public static IGraph generateGraphWattsStrogatz(final IScope scope,final Integer nbNodes, final Double p, final Integer k, - final Boolean directed, final ISpecies node_species, - final ISpecies edges_species) { - - WattsStrogatzGraphGenerator wsg = new WattsStrogatzGraphGenerator(nbNodes, k, p, false, scope.getSimulation().getRandomGenerator().getGenerator()); - AbstractBaseGraph graph = - directed ? - new DirectedMultigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) : - new Multigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); + public static IGraph generateGraphWattsStrogatz(final IScope scope, final Integer nbNodes, final Double p, + final Integer k, final Boolean directed, final ISpecies node_species, final ISpecies edges_species) { + + WattsStrogatzGraphGenerator wsg = new WattsStrogatzGraphGenerator(nbNodes, k, p, false, + scope.getSimulation().getRandomGenerator().getGenerator()); + AbstractBaseGraph graph = directed + ? new DirectedMultigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) + : new Multigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); wsg.generateGraph(graph); return new GamaGraph<>(scope, graph, node_species, edges_species); - + } - + + /** + * Generate graph watts strogatz. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param p + * the p + * @param k + * the k + * @param directed + * the directed + * @param node_species + * the node species + * @return the i graph + */ @operator ( value = "generate_watts_strogatz", concept = { IConcept.ALGORITHM }) @@ -2592,40 +3631,55 @@ public static IGraph generateGraphWattsStrogatz(final IScope scope,final Integer + "A small-world network is a type of graph in which most nodes are not neighbors of one another, but most nodes can be reached from every other by a small number of hops or steps. [From Wikipedia article]" + "The map operand should includes following elements:", usages = { @usage ( - value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " - + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " - + "\"k\": the base degree of each node (k must be greater than 2 and even); " - + "\"directed\": is the graph directed or not; " - +"\"node_species\": the species of vertices" , - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " 0.3,", - isExecutable = false), - @example ( - value = " 5,", - isExecutable = false), - @example ( - value = " true,", + value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " + + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " + + "\"k\": the base degree of each node (k must be greater than 2 and even); " + + "\"directed\": is the graph directed or not; " + + "\"node_species\": the species of vertices", + + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", isExecutable = false), - @example ( - value = " myVertexSpecies);", - isExecutable = false)})}, + + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " 0.3,", + isExecutable = false), + @example ( + value = " 5,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " myVertexSpecies);", + isExecutable = false) }) }, see = { "generate_barabasi_albert" }) @no_test - public static IGraph generateGraphWattsStrogatz(final IScope scope,final Integer nbNodes, final Double p, final Integer k, - final Boolean directed, final ISpecies node_species) { - - return generateGraphWattsStrogatz(scope,nbNodes,p,k,directed,node_species,null); - + public static IGraph generateGraphWattsStrogatz(final IScope scope, final Integer nbNodes, final Double p, + final Integer k, final Boolean directed, final ISpecies node_species) { + + return generateGraphWattsStrogatz(scope, nbNodes, p, k, directed, node_species, null); + } - + + /** + * Generate graph watts strogatz. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param p + * the p + * @param k + * the k + * @param directed + * the directed + * @return the i graph + */ @operator ( value = "generate_watts_strogatz", concept = { IConcept.ALGORITHM }) @@ -2636,288 +3690,384 @@ public static IGraph generateGraphWattsStrogatz(final IScope scope,final Integer + "A small-world network is a type of graph in which most nodes are not neighbors of one another, but most nodes can be reached from every other by a small number of hops or steps. [From Wikipedia article]" + "The map operand should includes following elements:", usages = { @usage ( - value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " - + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " - + "\"k\": the base degree of each node (k must be greater than 2 and even); " - + "\"directed\": is the graph directed or not", - - examples = { @example ( - value = "graph myGraph <- generate_watts_strogatz(", - isExecutable = false), - - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " 0.3,", - isExecutable = false), - @example ( - value = " 5,", + value = "\"nbNodes\": the graph will contain (size + 1) nodes (size must be greater than k); " + + "\"p\": probability to \"rewire\" an edge (so it must be between 0 and 1, the parameter is often called beta in the literature); " + + "\"k\": the base degree of each node (k must be greater than 2 and even); " + + "\"directed\": is the graph directed or not", + + examples = { @example ( + value = "graph myGraph <- generate_watts_strogatz(", isExecutable = false), - @example ( - value = " true);", - isExecutable = false)})}, + + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " 0.3,", + isExecutable = false), + @example ( + value = " 5,", + isExecutable = false), + @example ( + value = " true);", + isExecutable = false) }) }, see = { "generate_barabasi_albert" }) @no_test - public static IGraph generateGraphWattsStrogatz(final IScope scope,final Integer nbNodes, final Double p, final Integer k, - final Boolean directed) { - - return generateGraphWattsStrogatz(scope,nbNodes,p,k,directed,null,null); - - } + public static IGraph generateGraphWattsStrogatz(final IScope scope, final Integer nbNodes, final Double p, + final Integer k, final Boolean directed) { + return generateGraphWattsStrogatz(scope, nbNodes, p, k, directed, null, null); + } + + /** + * Generate graph random. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param nbEdges + * the nb edges + * @param directed + * the directed + * @param node_species + * the node species + * @param edges_species + * the edges species + * @return the i graph + */ @operator ( value = "generate_random_graph", concept = {}) @doc ( value = "returns a random graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes; \"edges_species\": the species of edges ", - examples = { @example ( - value = "graph myGraph <- generate_random_graph(", - isExecutable = false), - @example ( - value = " 50,", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " node_species,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes; \"edges_species\": the species of edges ", + examples = { @example ( + value = "graph myGraph <- generate_random_graph(", isExecutable = false), - @example ( - value = " edge_species);", - isExecutable = false) })}, + @example ( + value = " 50,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " node_species,", + isExecutable = false), + @example ( + value = " edge_species);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, final int nbEdges, final Boolean directed, final ISpecies node_species, final ISpecies edges_species) { - AbstractBaseGraph graph = - directed ? - new DirectedMultigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) : - new Multigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); - GnmRandomGraphGenerator gen = new GnmRandomGraphGenerator(nbNodes,nbEdges,scope.getSimulation().getSeed().longValue()); + @no_test + public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, final int nbEdges, + final Boolean directed, final ISpecies node_species, final ISpecies edges_species) { + AbstractBaseGraph graph = directed + ? new DirectedMultigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) + : new Multigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); + GnmRandomGraphGenerator gen = + new GnmRandomGraphGenerator(nbNodes, nbEdges, scope.getSimulation().getSeed().longValue()); gen.generateGraph(graph, null); - + return new GamaGraph<>(scope, graph, node_species, edges_species); - + } - + + /** + * Generate graph random. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param nbEdges + * the nb edges + * @param directed + * the directed + * @param node_species + * the node species + * @return the i graph + */ @operator ( value = "generate_random_graph", concept = {}) @doc ( value = "returns a random graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes", - examples = { @example ( - value = "graph myGraph <- generate_random_graph(", - isExecutable = false), - @example ( - value = " 50,", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes", + examples = { @example ( + value = "graph myGraph <- generate_random_graph(", isExecutable = false), - @example ( - value = " node_species);", - isExecutable = false)})}, + @example ( + value = " 50,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " node_species);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, int nbEdges, final Boolean directed, final ISpecies node_species) { - return generateGraphRandom(scope, nbNodes,nbEdges,directed, node_species,null); - + @no_test + public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, final int nbEdges, + final Boolean directed, final ISpecies node_species) { + return generateGraphRandom(scope, nbNodes, nbEdges, directed, node_species, null); + } - + + /** + * Generate graph random. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param nbEdges + * the nb edges + * @param directed + * the directed + * @return the i graph + */ @operator ( value = "generate_random_graph", concept = {}) @doc ( value = "returns a random graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not", - examples = { @example ( - value = "graph myGraph <- generate_random_graph(", - isExecutable = false), - @example ( - value = " 50,", - isExecutable = false), - @example ( - value = " 100,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"nbEdges\": number of edges to created;\"directed\": is the graph has to be directed or not", + examples = { @example ( + value = "graph myGraph <- generate_random_graph(", isExecutable = false), - @example ( - value = " true);", - isExecutable = false) })}, + @example ( + value = " 50,", + isExecutable = false), + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, final int nbEdges, final Boolean directed) { - return generateGraphRandom(scope, nbNodes, nbEdges,directed, (ISpecies)null, (ISpecies)null); - + @no_test + public static IGraph generateGraphRandom(final IScope scope, final int nbNodes, final int nbEdges, + final Boolean directed) { + return generateGraphRandom(scope, nbNodes, nbEdges, directed, (ISpecies) null, (ISpecies) null); + } - - /******************************/ - + @operator ( value = "generate_complete_graph", concept = {}) @doc ( value = "returns a fully connected graph.", - usages = { - @usage (value = "\"directed\": is the graph has to be directed or not;\"nodes\": the list of existing nodes; \"edges_species\": the species of edges ", - examples = { @example ( - value = "graph myGraph <- generate_complete_graph(", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " nodes,", + usages = { @usage ( + value = "\"directed\": is the graph has to be directed or not;\"nodes\": the list of existing nodes; \"edges_species\": the species of edges ", + examples = { @example ( + value = "graph myGraph <- generate_complete_graph(", isExecutable = false), - @example ( - value = " edge_species);", - isExecutable = false) })}, + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " nodes,", + isExecutable = false), + @example ( + value = " edge_species);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphComplete(final IScope scope, final Boolean directed, final IList nodes, final ISpecies edges_species) { - AbstractBaseGraph graph = - directed ? - new DirectedMultigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) : - new Multigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); - for (int i = 0; i < nodes.size(); i++) { - graph.addVertex(i + ""); - } + @no_test + public static IGraph generateGraphComplete(final IScope scope, final Boolean directed, final IList nodes, + final ISpecies edges_species) { + AbstractBaseGraph graph = directed + ? new DirectedMultigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) + : new Multigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); + for (int i = 0; i < nodes.size(); i++) { graph.addVertex(i + ""); } ComplementGraphGenerator gen = new ComplementGraphGenerator(graph); gen.generateGraph(graph, null); - - return new GamaGraph<>(scope, graph, nodes,edges_species); - + + return new GamaGraph<>(scope, graph, nodes, edges_species); + } - + + /** + * Generate graph complete. + * + * @param scope + * the scope + * @param directed + * the directed + * @param nodes + * the nodes + * @return the i graph + */ @operator ( value = "generate_complete_graph", concept = {}) @doc ( value = "returns a fully connected graph.", - usages = { - @usage (value = "\"directed\": is the graph has to be directed or not;\"nodes\": the list of existing nodes", - examples = { @example ( - value = "graph myGraph <- generate_complete_graph(", - isExecutable = false), - @example ( - value = " true,", + usages = { @usage ( + value = "\"directed\": is the graph has to be directed or not;\"nodes\": the list of existing nodes", + examples = { @example ( + value = "graph myGraph <- generate_complete_graph(", isExecutable = false), - @example ( - value = " nodes);", - isExecutable = false)})}, + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " nodes);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test + @no_test public static IGraph generateGraphComplete(final IScope scope, final Boolean directed, final IList nodes) { - return generateGraphComplete(scope, directed,nodes, null); + return generateGraphComplete(scope, directed, nodes, null); } - + + /** + * Generate graph complete. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param directed + * the directed + * @param node_species + * the node species + * @param edges_species + * the edges species + * @return the i graph + */ @operator ( value = "generate_complete_graph", concept = {}) @doc ( value = "returns a fully connected graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes; \"edges_species\": the species of edges ", - examples = { @example ( - value = "graph myGraph <- generate_complete_graph(", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", - isExecutable = false), - @example ( - value = " node_species,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes; \"edges_species\": the species of edges ", + examples = { @example ( + value = "graph myGraph <- generate_complete_graph(", isExecutable = false), - @example ( - value = " edge_species);", - isExecutable = false) })}, + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " node_species,", + isExecutable = false), + @example ( + value = " edge_species);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed, final ISpecies node_species, final ISpecies edges_species) { - AbstractBaseGraph graph = - directed ? - new DirectedMultigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) : - new Multigraph( - SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); - for (int i = 0; i < nbNodes;i++) { - graph.addVertex(""+i); - } + @no_test + public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed, + final ISpecies node_species, final ISpecies edges_species) { + AbstractBaseGraph graph = directed + ? new DirectedMultigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true) + : new Multigraph(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true); + for (int i = 0; i < nbNodes; i++) { graph.addVertex("" + i); } ComplementGraphGenerator gen = new ComplementGraphGenerator(graph); gen.generateGraph(graph, null); - + return new GamaGraph<>(scope, graph, node_species, edges_species); - + } - + + /** + * Generate graph complete. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param directed + * the directed + * @param node_species + * the node species + * @return the i graph + */ @operator ( value = "generate_complete_graph", concept = {}) @doc ( value = "returns a fully connected graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes", - examples = { @example ( - value = "graph myGraph <- generate_complete_graph(", - isExecutable = false), - @example ( - value = " 100,", - isExecutable = false), - @example ( - value = " true,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not;\"node_species\": the species of nodes", + examples = { @example ( + value = "graph myGraph <- generate_complete_graph(", isExecutable = false), - @example ( - value = " node_species);", - isExecutable = false)})}, + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true,", + isExecutable = false), + @example ( + value = " node_species);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed, final ISpecies node_species) { - return generateGraphComplete(scope, nbNodes,directed, node_species,null); - + @no_test + public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed, + final ISpecies node_species) { + return generateGraphComplete(scope, nbNodes, directed, node_species, null); + } - + + /** + * Generate graph complete. + * + * @param scope + * the scope + * @param nbNodes + * the nb nodes + * @param directed + * the directed + * @return the i graph + */ @operator ( value = "generate_complete_graph", concept = {}) @doc ( value = "returns a fully connected graph.", - usages = { - @usage (value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not", - examples = { @example ( - value = "graph myGraph <- generate_complete_graph(", - isExecutable = false), - @example ( - value = " 100,", + usages = { @usage ( + value = "\"nbNodes\": number of nodes to created;\"directed\": is the graph has to be directed or not", + examples = { @example ( + value = "graph myGraph <- generate_complete_graph(", isExecutable = false), - @example ( - value = " true);", - isExecutable = false) })}, + @example ( + value = " 100,", + isExecutable = false), + @example ( + value = " true);", + isExecutable = false) }) }, see = { "generate_barabasi_albert", "generate_watts_strogatz" }) - @no_test - public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed) { - return generateGraphComplete(scope, nbNodes,directed, (ISpecies)null, (ISpecies)null); - + @no_test + public static IGraph generateGraphComplete(final IScope scope, final int nbNodes, final Boolean directed) { + return generateGraphComplete(scope, nbNodes, directed, (ISpecies) null, (ISpecies) null); + } - + /** + * Girvan newman clustering. + * + * @param scope + * the scope + * @param graph + * the graph + * @param numCLusters + * the num C lusters + * @return the i list + */ @operator ( value = "girvan_newman_clustering", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2932,7 +4082,7 @@ public static IList GirvanNewmanClustering(final IScope scope, final IGraph grap emptyL.add((IGraph) graph.copy(scope)); return emptyL; } - + GirvanNewmanClustering clustering = new GirvanNewmanClustering(graph, numCLusters); Clustering clusters = clustering.getClustering(); IList clustersV = GamaListFactory.create(Types.LIST); @@ -2941,8 +4091,18 @@ public static IList GirvanNewmanClustering(final IScope scope, final IGraph grap } return clustersV; } - - + + /** + * K spanning tree clustering afl. + * + * @param scope + * the scope + * @param graph + * the graph + * @param numCLusters + * the num C lusters + * @return the i list + */ @operator ( value = "k_spanning_tree_clustering", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2959,7 +4119,7 @@ public static IList KSpanningTreeClusteringAfl(final IScope scope, final IGraph emptyL.add((IGraph) graph.copy(scope)); return emptyL; } - + KSpanningTreeClustering clustering = new KSpanningTreeClustering(graph, numCLusters); Clustering clusters = clustering.getClustering(); IList clustersV = GamaListFactory.create(Types.LIST); @@ -2968,7 +4128,18 @@ public static IList KSpanningTreeClusteringAfl(final IScope scope, final IGraph } return clustersV; } - + + /** + * Label propagation clustering agl. + * + * @param scope + * the scope + * @param graph + * the graph + * @param maxIteration + * the max iteration + * @return the i list + */ @operator ( value = "label_propagation_clustering", type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, @@ -2985,8 +4156,9 @@ public static IList LabelPropagationClusteringAgl(final IScope scope, final IGra emptyL.add((IGraph) graph.copy(scope)); return emptyL; } - - LabelPropagationClustering clustering= new LabelPropagationClustering(graph, maxIteration, scope.getSimulation().getRandomGenerator().getGenerator()); + + LabelPropagationClustering clustering = new LabelPropagationClustering(graph, maxIteration, + scope.getSimulation().getRandomGenerator().getGenerator()); Clustering clusters = clustering.getClustering(); IList clustersV = GamaListFactory.create(Types.LIST); for (Object s : clusters.getClusters()) { @@ -2994,7 +4166,5 @@ public static IList LabelPropagationClusteringAgl(final IScope scope, final IGra } return clustersV; } - - } diff --git a/msi.gama.models/models/GAML Syntax/Data Types And Structures/Graph.gaml b/msi.gama.models/models/GAML Syntax/Data Types And Structures/Graph.gaml index cd5e103d76..227e8da233 100644 --- a/msi.gama.models/models/GAML Syntax/Data Types And Structures/Graph.gaml +++ b/msi.gama.models/models/GAML Syntax/Data Types And Structures/Graph.gaml @@ -57,16 +57,16 @@ global { action random { write "- random graph : Erdős-Rényi = generate_random_graph(nb_nodes,nb_edges,directed,node_species,edge_species)"; do clean; - if no_species { g_graph <- generate_random_graph(nb_nodes,nb_nodes*av_degree,directed_graph);} - else if node_species_only { g_graph <- generate_random_graph(nb_nodes,nb_nodes*av_degree,directed_graph,regular_agent_node);} + if no_species { g_graph <- as_spatial_graph(generate_random_graph(nb_nodes,nb_nodes*av_degree,directed_graph));} + else if node_species_only { g_graph <- as_spatial_graph(generate_random_graph(nb_nodes,nb_nodes*av_degree,directed_graph,regular_agent_node));} else { - g_graph <- generate_random_graph( + g_graph <- as_spatial_graph(generate_random_graph( nb_nodes, // The number of nodes nb_nodes*av_degree, // The number of edges directed_graph, // directed graph regular_agent_node, // The species of nodes regular_agent_edge // The species of edges - ); + )); } @@ -77,21 +77,21 @@ global { * https://en.wikipedia.org/wiki/Barabási–Albert_model * */ - action scall_free { + action scale_free { write "- Scale-free : Barabási–Albert = generate_barabasi_albert(node_species, edge_species, nb_nodes, new_edges, synchronize)"; do clean; int new_edges_addition_per_node_introduction <- 4 > init_nb_nodes ? init_nb_nodes : 4; - if no_species {g_graph <- generate_barabasi_albert(init_nb_nodes,new_edges_addition_per_node_introduction,nb_nodes,directed_graph);} - else if node_species_only {g_graph <- generate_barabasi_albert(init_nb_nodes,new_edges_addition_per_node_introduction,nb_nodes,directed_graph,regular_agent_node);} + if no_species {g_graph <- as_spatial_graph(generate_barabasi_albert(init_nb_nodes,new_edges_addition_per_node_introduction,nb_nodes,directed_graph));} + else if node_species_only {g_graph <- as_spatial_graph(generate_barabasi_albert(init_nb_nodes,new_edges_addition_per_node_introduction,nb_nodes,directed_graph,regular_agent_node));} else { - g_graph <- generate_barabasi_albert( + g_graph <- as_spatial_graph(generate_barabasi_albert( init_nb_nodes, // The number of nodes in the graph new_edges_addition_per_node_introduction, // the number of edges created when a new node enter the graph nb_nodes, // The number of nodes in the graph directed_graph, //directed grah regular_agent_node, // The species of nodes regular_agent_edge // The species of edges - ); + )); } } @@ -105,17 +105,17 @@ global { do clean; float rewirering_probability <- 0.1; int fake_lattice_start_degree <- 4; // Even and more than 2 - if no_species {g_graph <- generate_watts_strogatz(nb_nodes,rewirering_probability,fake_lattice_start_degree,directed_graph);} - else if node_species_only {g_graph <- generate_watts_strogatz(nb_nodes,rewirering_probability,fake_lattice_start_degree,directed_graph,regular_agent_node);} + if no_species {g_graph <- as_spatial_graph(generate_watts_strogatz(nb_nodes,rewirering_probability,fake_lattice_start_degree,directed_graph));} + else if node_species_only {g_graph <- as_spatial_graph(generate_watts_strogatz(nb_nodes,rewirering_probability,fake_lattice_start_degree,directed_graph,regular_agent_node));} else { - g_graph <- generate_watts_strogatz( + g_graph <- as_spatial_graph(generate_watts_strogatz( nb_nodes, // The number of nodes rewirering_probability, // The probability to rewire a node in the generation process fake_lattice_start_degree, // The degree of node at start, before the rewirering process directed_graph, //is directed regular_agent_node, // The species of nodes regular_agent_edge // The species of edges - ); + )); } } @@ -125,15 +125,15 @@ global { action complete { write "- Complete = generate_complete_graph(node_species, edge_species, nb_node)"; do clean; - if no_species {g_graph <- generate_complete_graph(nb_nodes,directed_graph);} - else if node_species_only {g_graph <- generate_complete_graph(nb_nodes,directed_graph,regular_agent_node);} + if no_species {g_graph <- as_spatial_graph(generate_complete_graph(nb_nodes,directed_graph));} + else if node_species_only {g_graph <- as_spatial_graph(generate_complete_graph(nb_nodes,directed_graph,regular_agent_node));} else { - g_graph <- generate_complete_graph( + g_graph <- as_spatial_graph(generate_complete_graph( nb_nodes,// The number of nodes in the graph directed_graph, //is directed regular_agent_node, // The species of nodes regular_agent_edge // The species of edges - ); + )); } } @@ -385,7 +385,7 @@ experiment Graph type: gui { user_command "Create graphs" { switch graph_generator { match "Random" { ask world {do random();} } - match "Scall-free" { ask world { do scall_free(); } } + match "Scall-free" { ask world { do scale_free(); } } match "Small-world" { ask world { do small_world(); } } match "Complete" { ask world { do complete(); } } match "Distance" { ask world { do from_nodes(); } }