Skip to content

Commit

Permalink
Adjusted iLCD to check for newCommunties after Contraction.
Browse files Browse the repository at this point in the history
RUn with WebService now working.
Most problems came from the copy constructors not copying everything. CustomNode copy constructor now copies the full customnode instead of just the name. The key is important for persisting. OcdAlgorithmExecutor added an OR for iLCDAlgorithm so that the algorithm is not run on components.
  • Loading branch information
fsaintpreux committed May 10, 2024
1 parent 6165f7d commit 0a5ce08
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package i5.las2peer.services.ocd.algorithms;

import i5.las2peer.services.ocd.algorithms.utils.OcdAlgorithmException;
import i5.las2peer.services.ocd.graphs.Cover;
import i5.las2peer.services.ocd.graphs.CoverCreationLog;
import i5.las2peer.services.ocd.graphs.CoverCreationType;
import i5.las2peer.services.ocd.graphs.CustomGraph;
import i5.las2peer.services.ocd.graphs.GraphProcessor;
import i5.las2peer.services.ocd.graphs.*;
import i5.las2peer.services.ocd.metrics.ExecutionTime;
import i5.las2peer.services.ocd.metrics.OcdMetricException;
import i5.las2peer.services.ocd.utils.ExecutionStatus;
Expand Down Expand Up @@ -44,7 +40,7 @@ public Cover execute(CustomGraph graph, OcdAlgorithm algorithm, int componentNod
CustomGraph graphCopy = new CustomGraph(graph);
GraphProcessor processor = new GraphProcessor();
processor.makeCompatible(graphCopy, algorithm.compatibleGraphTypes());
if(algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.SIGNED_PROBABILISTIC_MIXTURE_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.WORD_CLUSTERING_REF_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.COST_FUNC_OPT_CLUSTERING_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.LOCAL_SPECTRAL_CLUSTERING_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.LOUVAIN_ALGORITHM.toString())){
if(algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.SIGNED_PROBABILISTIC_MIXTURE_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.WORD_CLUSTERING_REF_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.COST_FUNC_OPT_CLUSTERING_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.LOCAL_SPECTRAL_CLUSTERING_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.LOUVAIN_ALGORITHM.toString()) || algorithm.getAlgorithmType().toString().equalsIgnoreCase(CoverCreationType.ILCD_ALGORITHM.toString())){
ExecutionTime executionTime = new ExecutionTime();
//TODO: I think it should be detectOverlappingCommunities(graphCopy) - Tobias
Cover cover = algorithm.detectOverlappingCommunities(graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public Cover detectOverlappingCommunities(CustomGraph graph) throws OcdAlgorithm

if (graph instanceof DynamicGraph) {
// Set start timestamp
// List<DynamicInteraction> dynamicInteractions = ((DynamicGraph) graph).getDynamicInteractions();
String currentTimestep = ((DynamicGraph) graph).getDynamicInteractions().get(0).getDate();

//for (int i = 0; i < dynamicInteractions.size(); i++) {
// dynamicInteractions.get(i).
//}
for (DynamicInteraction dynamicInteraction: ((DynamicGraph) graph).getDynamicInteractions()) {
HashSet<iLCDCommunityAgent> modifiedCommunities = new HashSet<iLCDCommunityAgent>();
currentTimestep = dynamicInteraction.getDate();
Expand Down Expand Up @@ -146,7 +149,7 @@ public Cover detectOverlappingCommunities(CustomGraph graph) throws OcdAlgorithm
same for sourceNode
*/

iLCDCommunityAgent temp = new iLCDCommunityAgent(commonCommunity);
ArrayList<iLCDCommunityAgent> resultingCommunities = new ArrayList<>();
//Contraction
ArrayList<iLCDCommunityAgent> resultingCommunitiesSource = getContractionResult(commonCommunity, graphNodes.get(sourceName));
Expand All @@ -155,6 +158,33 @@ public Cover detectOverlappingCommunities(CustomGraph graph) throws OcdAlgorithm
}
modifiedCommunities.addAll(resultingCommunities);

// check if rejected nodes can form a new community
if(resultingCommunities.size() == 1){
ArrayList<iLCDNodeAgent> rejectedNodes = new ArrayList<>(temp.getNodes());
rejectedNodes.removeAll(resultingCommunities.get(0).getNodes());

for(iLCDNodeAgent rejectedNode: rejectedNodes) {
for(iLCDNodeAgent nodeInCom: rejectedNode.getNeighborsInCommunity(resultingCommunities.get(0))){
ArrayList<iLCDCommunityAgent> newCommunities = new ArrayList<iLCDCommunityAgent>();
// Choose birth based on the parameter
switch(min_C){
case 3:
newCommunities = birthCase3(rejectedNode, nodeInCom);
break;
case 4:
newCommunities = birthCase4(rejectedNode, nodeInCom);
break;
}

for(iLCDCommunityAgent newCommunity: newCommunities) {
newCommunity.setBirth(currentTimestep);
graphCommunities.put(newCommunity.getId(), newCommunity);
//TODO handle BIRTH event
modifiedCommunities.add(newCommunity);
}
}
}
}
// DEATH
for (iLCDCommunityAgent community: resultingCommunities) {
if (community.getNodes().size() < min_C) {
Expand Down Expand Up @@ -230,6 +260,7 @@ public Cover detectOverlappingCommunities(CustomGraph graph) throws OcdAlgorithm
}
}else {
System.out.println("Graph is static");
throw new OcdAlgorithmException("Graph is static");
}

Matrix community_matrix = getCommunityMatrix((DynamicGraph) graph, graphCommunities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public iLCDCommunityAgent() {
communityIndexer++;
}

public iLCDCommunityAgent(iLCDCommunityAgent community) {
this.nodes = community.getNodes();

}

public iLCDCommunityAgent(int id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void persist( ArangoDatabase db, DocumentCreateOptions opt) {
bd.addAttribute(propertiesColumnName, this.properties);
bd.addAttribute(coverKeyColumnName, this.cover.getKey());
Map<String, Double> membershipKeyMap = new HashMap<String, Double>();

for (Map.Entry<CustomNode, Double> entry : this.memberships.entrySet()) {
membershipKeyMap.put(entry.getKey().getKey(), entry.getValue()); //CustomNode Keys muessen bekannt sein
}
Expand All @@ -307,7 +307,6 @@ public static Community load(String key, Cover cover, ArangoDatabase db, Documen
c.name = bd.getAttribute(nameColumnName).toString();
c.color = Integer.parseInt(colorString);
c.properties = om.convertValue(objProperties, List.class);

// each customNode is assigned the stored belongingValue
for (Map.Entry<String, Double> entry : membershipKeyMap.entrySet()) {
String nodeKey = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public CustomGraph(CustomGraph graph) {
graph.creationMethod.getParameters());
this.creationMethod.setStatus(graph.creationMethod.getStatus());
this.customNodes = new HashMap<Integer, CustomNode>();

copyMappings(graph.customNodes, graph.customEdges, graph.nodeIds, graph.edgeIds);
this.userName = new String(graph.userName);
this.name = new String(graph.name);
Expand All @@ -320,6 +321,7 @@ public CustomGraph(CustomGraph graph) {
nodeIndexer = graph.nodeIndexer;
edgeIndexer = graph.edgeIndexer;
this.types = new HashSet<Integer>(graph.types);
this.properties = graph.getProperties();
}

//////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ protected CustomNode(){
*/
protected CustomNode(CustomNode customNode) {
this.name = customNode.name;
this.graph = customNode.graph;
this.key = customNode.key;
this.id = customNode.id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void addDynamicInteraction(Edge edge, String date, String action) {
@Override
public void persist(ArangoDatabase db, String transId) throws InterruptedException {
super.persist(db, transId);

for(DynamicInteraction dynIn: dynamicInteractions) {
dynIn.update(this);
}
Expand Down Expand Up @@ -160,7 +159,6 @@ public static DynamicGraph load(String key, ArangoDatabase db, String transId) {

// Load underlying CustomGraph
DynamicGraph graph = new DynamicGraph(CustomGraph.load(key, db, transId));

ArangoCollection collection = db.collection(collectionName);
DocumentReadOptions readOpt = new DocumentReadOptions().streamTransactionId(transId);
AqlQueryOptions queryOpt = new AqlQueryOptions().streamTransactionId(transId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public void persist(ArangoDatabase db, DocumentCreateOptions opt) {
bed.addAttribute(graphKeyColumnName, this.getGraph().getKey());
bed.addAttribute(dateColumnName, this.date);
bed.addAttribute(actionColumnName, this.action);

bed.setFrom(CustomNode.collectionName + "/" + this.getSource().getKey());

bed.setTo(CustomNode.collectionName + "/" + this.getTarget().getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ else if(weight == 2.5) {
assertEquals(1, persistedGraph.getTypes().size());
assertTrue(persistedGraph.getTypes().contains(GraphType.DIRECTED));
System.out.println("Types: " + graph.getTypes());

System.out.println(persistedGraph.getProperties());
queryResults = database.getGraphs(invalidGraphName);

assertEquals(0, queryResults.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void testPersist() {
DynamicGraph persistedGraphByKey = (DynamicGraph) database.getGraph(userName1, graph.getKey());
assertNotNull(persistedGraphByKey);


assertEquals(1, queryResults.size());

DynamicGraph persistedGraph = (DynamicGraph) queryResults.get(0);
Expand All @@ -82,7 +83,8 @@ public void testPersist() {
Node[] nodes = graph.nodes().toArray(Node[]::new);
for(int i=0; i<3; i++) {
Node node = nodes[i];
String name = persistedGraph.getNodeName(node);
System.out.println(persistedGraphByKey.getCustomNode(node).String());
String name = persistedGraphByKey.getNodeName(node);
System.out.println("Node: " + node.getIndex() + ", Name: " + persistedGraph.getNodeName(node));
assertTrue(nodeNames.contains(name));
nodeNames.remove(name);
Expand All @@ -109,12 +111,13 @@ else if(weight == 2.5) {
assertTrue(persistedGraph.getTypes().contains(GraphType.DYNAMIC));
System.out.println("Types: " + graph.getTypes());

assertEquals(2, persistedGraph.getDynamicInteractions().size());
System.out.println(persistedGraph.getDynamicInteractions().toString());
assertEquals(2, persistedGraphByKey.getDynamicInteractions().size());


System.out.println(graph.getDynamicInteractions().toString());
System.out.println(persistedGraphByKey.getDynamicInteractions().toString());
List<CustomGraph> queryResults2 = database.getGraphs(invalidGraphName);


assertEquals(0, queryResults2.size());

try {
Expand Down
2 changes: 1 addition & 1 deletion time_seed.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21112018
21112134

0 comments on commit 0a5ce08

Please sign in to comment.