Commit be988f0a authored by Philipp Görick's avatar Philipp Görick

Add algorithm parameters for evaluation test. Add number of components...

Add algorithm parameters for evaluation test. Add number of components attribute to ClusteringResult. Generate json file from ClusteringResult for better evalutation.
parent 82fd676b
Pipeline #107448 failed with stages
in 24 minutes and 39 seconds
......@@ -111,6 +111,7 @@ public class DistributedTargetGenerator extends CMakeGenerator {
Optional<Integer> nOpt = clusteringParameters.getNumberOfClusters();
for(ClusteringResult c : clusteringResults){
String prefix = nOpt.isPresent() && !c.hasNumberOfClusters(nOpt.get()) ? "[IGNORED]" : "";
c.saveAsJson(generationTargetPath +"emam/", "clusteringResults.json");
System.out.println(prefix + "Score was " + c.getScore() + " for " + c.getParameters().toString());
}
......
package de.monticore.lang.monticar.generator.middleware.clustering;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.AlgorithmCliParameters;
import de.monticore.lang.monticar.generator.middleware.clustering.visualization.ModelVisualizer;
import de.monticore.lang.monticar.generator.middleware.impls.MiddlewareTagGenImpl;
import de.se_rwth.commons.logging.Log;
import org.graphstream.graph.Graph;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
......@@ -16,19 +24,26 @@ public class ClusteringResult {
private AlgorithmCliParameters parameters;
private List<Set<EMAComponentInstanceSymbol>> clustering;
private long durration;
private int componentNumber;
private ClusteringResult(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters, List<Set<EMAComponentInstanceSymbol>> clustering, long durration) {
private ClusteringResult(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters,
List<Set<EMAComponentInstanceSymbol>> clustering, long durration, int componentNumber) {
this.component = component;
this.parameters = parameters;
this.clustering = clustering;
this.durration = durration;
this.componentNumber = componentNumber;
}
public static ClusteringResult fromParameters(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters){
long startTime = System.currentTimeMillis();
List<Set<EMAComponentInstanceSymbol>> res = parameters.asClusteringAlgorithm().clusterWithState(component);
long endTime = System.currentTimeMillis();
return new ClusteringResult(component, parameters, res, endTime - startTime);
int componentNumber = 0;
for (Set<EMAComponentInstanceSymbol> cluster : res) {
componentNumber += cluster.size();
}
return new ClusteringResult(component, parameters, res, endTime - startTime, componentNumber);
}
public double getScore(){
......@@ -58,6 +73,10 @@ public class ClusteringResult {
return this.durration;
}
public int getComponentNumber() {
return componentNumber;
}
public boolean hasNumberOfClusters(int n){
return getNumberOfClusters() == n;
}
......@@ -82,4 +101,40 @@ public class ClusteringResult {
ModelVisualizer.saveGraphAsImage(g, path, fileName);
}
public void saveAsJson(String path,String filename){
JsonParser parser = new JsonParser();
File dir = new File(path);
File file = new File(dir, filename);
JsonArray jsonArray = new JsonArray();
if (file.exists() && !file.isDirectory()) {
try {
jsonArray = (JsonArray) parser.parse(new FileReader(file));
JsonObject result = createJsonResultObject();
jsonArray.add(result);
} catch (IOException e) {
Log.warn("Could not open clustering result file " + filename);
}
}else {
JsonObject result = createJsonResultObject();
jsonArray.add(result);
}
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(jsonArray.toString());
fileWriter.flush();
}catch (IOException io){
Log.warn("Could not write clustering results to json file " + filename);
}
}
private JsonObject createJsonResultObject() {
JsonObject result = new JsonObject();
result.addProperty("Algorithm", this.getParameters().toString());
result.addProperty("NumberOfClusters", this.getNumberOfClusters());
result.addProperty("Score", this.getScore());
result.addProperty("DurationInMs", this.getDurration());
result.addProperty("ComponentNumber", this.getComponentNumber());
return result;
}
}
\ No newline at end of file
......@@ -10,6 +10,20 @@
{
"name":"SpectralClustering",
"numberOfClusters":3
},
{
"name":"SpectralClustering",
"numberOfClusters":6
},
{
"name":"SpectralClustering",
"numberOfClusters":19
},
{
"name":"Markov"
},
{
"name":"AffinityPropagation"
}
]
}
......
......@@ -10,6 +10,17 @@
{
"name":"SpectralClustering",
"numberOfClusters":3
},{
"name":"SpectralClustering",
"numberOfClusters":25
},{
"name":"SpectralClustering",
"numberOfClusters":72
},{
"name":"Markov"
},
{
"name":"AffinityPropagation"
}
]
}
......
......@@ -10,6 +10,11 @@
{
"name":"SpectralClustering",
"numberOfClusters":3
}, {
"name":"Markov"
},
{
"name":"AffinityPropagation"
}
]
}
......
......@@ -10,6 +10,17 @@
{
"name":"SpectralClustering",
"numberOfClusters":3
},{
"name":"SpectralClustering",
"numberOfClusters":21
},{
"name":"SpectralClustering",
"numberOfClusters":39
},{
"name":"Markov"
},
{
"name":"AffinityPropagation"
}
]
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment