Commit 1c483936 authored by Dinh-An Ho's avatar Dinh-An Ho
Browse files

Merge remote-tracking branch 'origin/ML_clustering' into ML_clustering

# Conflicts:
#	src/main/java/de/monticore/lang/monticar/generator/middleware/Simulation/MonteCarloIntegration.java
parents c3685b33 b165a3be
Pipeline #111097 passed with stages
in 15 minutes and 58 seconds
...@@ -105,7 +105,8 @@ Clustering Parameters: ...@@ -105,7 +105,8 @@ Clustering Parameters:
| numberOfClusters | int | ❓ | Number of clusters the subcomponents should be divided into<br> Overrides numberOfClusters in algorithmParameters | | numberOfClusters | int | ❓ | Number of clusters the subcomponents should be divided into<br> Overrides numberOfClusters in algorithmParameters |
| flatten | bool | ❓ | Replace all components with their subcomponents execpt when it is atomic or the flatten level is reached | | flatten | bool | ❓ | Replace all components with their subcomponents execpt when it is atomic or the flatten level is reached |
| flattenLevel | int | ❓ | Maximal level of component flattening | | flattenLevel | int | ❓ | Maximal level of component flattening |
| chooseBy | String | ❓ | Strategie to choose from the resulting clusterings<br> bestWithFittingN(Default): if numberOfClusters is set, all results with a different number of clusters are ignored<br> bestOverall: ignore numberOfClusters, choose result with best score | | metric | String | ❓ | Metric to evaluate the quality of the resulting clusters. Available: "CommunicationCost"(Default), "Silhouette"|
| chooseBy | String | ❓ | Strategy to choose from the resulting clusterings<br> bestWithFittingN(Default): if numberOfClusters is set, all results with a different number of clusters are ignored<br> bestOverall: ignore numberOfClusters, choose result with best score |
| algorithmParameters | List<Object> | ❓ | Used to specify which algorithms(and their parameters) are used for clustering | | algorithmParameters | List<Object> | ❓ | Used to specify which algorithms(and their parameters) are used for clustering |
There are 4 different Clustering Algorithms with distinct parameters There are 4 different Clustering Algorithms with distinct parameters
......
...@@ -108,7 +108,7 @@ public class DistributedTargetGenerator extends CMakeGenerator { ...@@ -108,7 +108,7 @@ public class DistributedTargetGenerator extends CMakeGenerator {
//Cluster //Cluster
if(clusteringParameters.getAlgorithmParameters().size() > 0) { if(clusteringParameters.getAlgorithmParameters().size() > 0) {
clusteringResults = AutomaticClusteringHelper.executeClusteringFromParams(componentInstanceSymbol, clusteringParameters.getAlgorithmParameters()); clusteringResults = ClusteringResultList.fromParametersList(componentInstanceSymbol, clusteringParameters.getAlgorithmParameters(), clusteringParameters.getMetric());
Optional<Integer> nOpt = clusteringParameters.getNumberOfClusters(); Optional<Integer> nOpt = clusteringParameters.getNumberOfClusters();
for(ClusteringResult c : clusteringResults){ for(ClusteringResult c : clusteringResults){
String prefix = nOpt.isPresent() && !c.hasNumberOfClusters(nOpt.get()) ? "[IGNORED]" : ""; String prefix = nOpt.isPresent() && !c.hasNumberOfClusters(nOpt.get()) ? "[IGNORED]" : "";
......
...@@ -10,6 +10,7 @@ import java.io.FileReader; ...@@ -10,6 +10,7 @@ import java.io.FileReader;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class CliParametersLoader { public class CliParametersLoader {
...@@ -45,17 +46,19 @@ public class CliParametersLoader { ...@@ -45,17 +46,19 @@ public class CliParametersLoader {
.create(); .create();
JsonDeserializer<DynamicSpectralClusteringCliParameters> desCliParameters = new StrictJsonDeserializer<>(Arrays.asList("modelsDir","outputDir","rootModel","generators","emadlBackend","writeTagFile","clusteringParameters"), delegateGson); JsonDeserializer<CliParameters> desCliParameters = new StrictJsonDeserializer<>(Arrays.asList("emadlBackend","writeTagFile","clusteringParameters","modelsDir","outputDir","rootModel","generators"), delegateGson);
JsonDeserializer<ClusteringParameters> desClustering = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","flatten","flattenLevel","chooseBy","algorithmParameters"), delegateGson); JsonDeserializer<ClusteringParameters> desClustering = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","flatten","flattenLevel","chooseBy","algorithmParameters", "metric"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desSpectral = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","l","sigma"), delegateGson); JsonDeserializer<DynamicSpectralClusteringCliParameters> desSpectral = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","l","sigma"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desMarkov = new StrictJsonDeserializer<>(Arrays.asList("max_residual","gamma_exp","loop_gain","zero_max"), delegateGson); JsonDeserializer<DynamicMarkovCliParameters> desMarkov = new StrictJsonDeserializer<>(Arrays.asList("max_residual","gamma_exp","loop_gain","zero_max"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desDBScan = new StrictJsonDeserializer<>(Arrays.asList("min_pts","radius"), delegateGson); JsonDeserializer<DynamicDBScanCliParameters> desDBScan = new StrictJsonDeserializer<>(Arrays.asList("min_pts","radius"), delegateGson);
JsonDeserializer<DynamicAffinityPropagationCliParameters> desAff = new StrictJsonDeserializer<>(Collections.emptyList(), delegateGson);
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(DynamicSpectralClusteringCliParameters.class, desSpectral) .registerTypeAdapter(DynamicSpectralClusteringCliParameters.class, desSpectral)
.registerTypeAdapter(DynamicMarkovCliParameters.class, desMarkov) .registerTypeAdapter(DynamicMarkovCliParameters.class, desMarkov)
.registerTypeAdapter(DynamicDBScanCliParameters.class, desDBScan) .registerTypeAdapter(DynamicDBScanCliParameters.class, desDBScan)
.registerTypeAdapter(DynamicAffinityPropagationCliParameters.class, desAff)
.registerTypeAdapter(CliParameters.class, desCliParameters) .registerTypeAdapter(CliParameters.class, desCliParameters)
.registerTypeAdapter(ClusteringParameters.class, desClustering) .registerTypeAdapter(ClusteringParameters.class, desClustering)
.create(); .create();
......
package de.monticore.lang.monticar.generator.middleware.cli; package de.monticore.lang.monticar.generator.middleware.cli;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.AlgorithmCliParameters; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.AlgorithmCliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.SpectralClusteringCliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicAlgorithmCliParameters; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicAlgorithmCliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicParameter; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicParameter;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicSpectralClusteringCliParameters; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicSpectralClusteringCliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.ListParameter; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.ListParameter;
import de.monticore.lang.monticar.generator.middleware.clustering.qualityMetric.Metric;
import de.monticore.lang.monticar.generator.middleware.clustering.qualityMetric.MetricType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -18,6 +19,7 @@ public class ClusteringParameters { ...@@ -18,6 +19,7 @@ public class ClusteringParameters {
private Integer flattenLevel; private Integer flattenLevel;
private ResultChoosingStrategy chooseBy = ResultChoosingStrategy.bestWithFittingN; private ResultChoosingStrategy chooseBy = ResultChoosingStrategy.bestWithFittingN;
private List<DynamicAlgorithmCliParameters> algorithmParameters = new ArrayList<>(); private List<DynamicAlgorithmCliParameters> algorithmParameters = new ArrayList<>();
private MetricType metric;
public ClusteringParameters() { public ClusteringParameters() {
} }
...@@ -54,4 +56,12 @@ public class ClusteringParameters { ...@@ -54,4 +56,12 @@ public class ClusteringParameters {
public Optional<Integer> getFlattenLevel() { public Optional<Integer> getFlattenLevel() {
return Optional.ofNullable(flattenLevel); return Optional.ofNullable(flattenLevel);
} }
public Metric getMetric() {
if(metric == null){
return MetricType.CommunicationCost.toMetric();
}else{
return metric.toMetric();
}
}
} }
...@@ -2,6 +2,8 @@ package de.monticore.lang.monticar.generator.middleware.cli.algorithms; ...@@ -2,6 +2,8 @@ package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm; import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import java.util.Optional;
public abstract class AlgorithmCliParameters { public abstract class AlgorithmCliParameters {
public static final String TYPE_SPECTRAL_CLUSTERING = "SpectralClustering"; public static final String TYPE_SPECTRAL_CLUSTERING = "SpectralClustering";
public static final String TYPE_UNKOWN = "Unkown"; public static final String TYPE_UNKOWN = "Unkown";
...@@ -22,4 +24,8 @@ public abstract class AlgorithmCliParameters { ...@@ -22,4 +24,8 @@ public abstract class AlgorithmCliParameters {
public abstract Object[] asAlgorithmArgs(); public abstract Object[] asAlgorithmArgs();
public abstract boolean isValid(); public abstract boolean isValid();
public Optional<Integer> expectedClusterCount(){
return Optional.empty();
}
} }
...@@ -68,6 +68,11 @@ public class SpectralClusteringCliParameters extends AlgorithmCliParameters { ...@@ -68,6 +68,11 @@ public class SpectralClusteringCliParameters extends AlgorithmCliParameters {
return numberOfClusters != null; return numberOfClusters != null;
} }
@Override
public Optional<Integer> expectedClusterCount() {
return Optional.of(getNumberOfClusters().get());
}
public Optional<Integer> getNumberOfClusters() { public Optional<Integer> getNumberOfClusters() {
return Optional.ofNullable(numberOfClusters); return Optional.ofNullable(numberOfClusters);
} }
......
...@@ -7,7 +7,6 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instance ...@@ -7,7 +7,6 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instance
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol; import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.monticore.lang.math._ast.ASTNumberExpression; import de.monticore.lang.math._ast.ASTNumberExpression;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType; import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.AlgorithmCliParameters;
import de.monticore.lang.monticar.ts.MCTypeSymbol; import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference; import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference; import de.monticore.lang.monticar.ts.references.MCTypeReference;
...@@ -25,7 +24,6 @@ import java.util.stream.Collectors; ...@@ -25,7 +24,6 @@ import java.util.stream.Collectors;
public class AutomaticClusteringHelper { public class AutomaticClusteringHelper {
// public static double[][] createAdjacencyMatrix(List<ExpandedComponentInstanceSymbol> subcomps, Collection<ConnectorSymbol> connectors, Map<String, Integer> subcompLabels) {
public static double[][] createAdjacencyMatrix(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors, Map<String, Integer> subcompLabels) { public static double[][] createAdjacencyMatrix(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors, Map<String, Integer> subcompLabels) {
// Nodes = subcomponents // Nodes = subcomponents
// Verts = connectors between subcomponents // Verts = connectors between subcomponents
...@@ -36,18 +34,18 @@ public class AutomaticClusteringHelper { ...@@ -36,18 +34,18 @@ public class AutomaticClusteringHelper {
EMAComponentInstanceSymbol sourceCompOpt = con.getSourcePort().getComponentInstance(); EMAComponentInstanceSymbol sourceCompOpt = con.getSourcePort().getComponentInstance();
EMAComponentInstanceSymbol targetCompOpt = con.getTargetPort().getComponentInstance(); EMAComponentInstanceSymbol targetCompOpt = con.getTargetPort().getComponentInstance();
int index1 = subcompLabels.get(sourceCompOpt.getFullName()); int index1 = subcompLabels.get(sourceCompOpt.getFullName());
int index2 = subcompLabels.get(targetCompOpt.getFullName()); int index2 = subcompLabels.get(targetCompOpt.getFullName());
res[index1][index2] += getTypeCostHeuristic(con.getSourcePort()); res[index1][index2] += getTypeCostHeuristic(con.getSourcePort());
res[index2][index1] += getTypeCostHeuristic(con.getSourcePort()); res[index2][index1] += getTypeCostHeuristic(con.getSourcePort());
}); });
return res; return res;
} }
public static double[][] guaranteedConnectedAdjacencyMatrix(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors, Map<String, Integer> subcompLabels){ public static double[][] guaranteedConnectedAdjacencyMatrix(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors, Map<String, Integer> subcompLabels) {
double[][] res = createAdjacencyMatrix(subcomps, connectors, subcompLabels); double[][] res = createAdjacencyMatrix(subcomps, connectors, subcompLabels);
...@@ -56,7 +54,7 @@ public class AutomaticClusteringHelper { ...@@ -56,7 +54,7 @@ public class AutomaticClusteringHelper {
double max = 0; double max = 0;
for (double[] doubles : res) { for (double[] doubles : res) {
for (double adj : doubles) { for (double adj : doubles) {
if(adj > max){ if (adj > max) {
max = adj; max = adj;
} }
} }
...@@ -70,7 +68,7 @@ public class AutomaticClusteringHelper { ...@@ -70,7 +68,7 @@ public class AutomaticClusteringHelper {
for (Integer a : representativeLabels) { for (Integer a : representativeLabels) {
for (Integer b : representativeLabels) { for (Integer b : representativeLabels) {
if(!a.equals(b)){ if (!a.equals(b)) {
res[a][b] = unconnectedCost; res[a][b] = unconnectedCost;
} }
} }
...@@ -78,32 +76,60 @@ public class AutomaticClusteringHelper { ...@@ -78,32 +76,60 @@ public class AutomaticClusteringHelper {
return res; return res;
} }
public static double[][] getDistanceMatrix(double[][] adjacencyMatrix) {
public static List<Set<EMAComponentInstanceSymbol>> getConnectedSubcomponentSets(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors){ //Uses Floyd–Warshall
double[][] res = new double[adjacencyMatrix.length][adjacencyMatrix[0].length];
for (int i = 0; i < adjacencyMatrix.length; i++) {
for (int j = 0; j < adjacencyMatrix[0].length; j++) {
if (i != j) {
double curVal = adjacencyMatrix[i][j];
res[i][j] = Math.abs(curVal) <= 0.00000001d ? Double.MAX_VALUE : curVal;
} else {
res[i][i] = 0d;
}
}
}
for (int k = 0; k < adjacencyMatrix.length; k++) {
for (int i = 0; i < adjacencyMatrix.length; i++) {
for (int j = 0; j < adjacencyMatrix.length; j++) {
if (res[i][j] > res[i][k] + res[k][j]) {
res[i][j] = res[i][k] + res[k][j];
}
}
}
}
return res;
}
public static List<Set<EMAComponentInstanceSymbol>> getConnectedSubcomponentSets(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors) {
Graph<EMAComponentInstanceSymbol, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); Graph<EMAComponentInstanceSymbol, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
subcomps.forEach(graph::addVertex); subcomps.forEach(graph::addVertex);
connectors.stream() connectors.stream()
.filter(c -> subcomps.contains(c.getSourcePort().getComponentInstance())) .filter(c -> subcomps.contains(c.getSourcePort().getComponentInstance()))
.filter(c -> subcomps.contains(c.getTargetPort().getComponentInstance())) .filter(c -> subcomps.contains(c.getTargetPort().getComponentInstance()))
.forEach(c -> graph.addEdge(c.getSourcePort().getComponentInstance(), c.getTargetPort().getComponentInstance())); .forEach(c -> graph.addEdge(c.getSourcePort().getComponentInstance(), c.getTargetPort().getComponentInstance()));
ConnectivityInspector<EMAComponentInstanceSymbol, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(graph); ConnectivityInspector<EMAComponentInstanceSymbol, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(graph);
return connectivityInspector.connectedSets(); return connectivityInspector.connectedSets();
} }
public static double[][] adjacencyMatrix2transitionMatrix(double[][] adjacencyMatrix) { public static double[][] adjacencyMatrix2transitionMatrix(double[][] adjacencyMatrix) {
double[][] transitionMatrix= adjacencyMatrix; double[][] transitionMatrix = adjacencyMatrix;
int degree; int degree;
for(int i = 0; i < adjacencyMatrix[0].length; i++) { for (int i = 0; i < adjacencyMatrix[0].length; i++) {
degree= 0; degree = 0;
for(int j = 0; j < adjacencyMatrix[0].length; j++) { for (int j = 0; j < adjacencyMatrix[0].length; j++) {
if (adjacencyMatrix[i][j] == 1) degree++; if (adjacencyMatrix[i][j] == 1) degree++;
} }
for(int j = 0; j < adjacencyMatrix[0].length; j++) { for (int j = 0; j < adjacencyMatrix[0].length; j++) {
if (adjacencyMatrix[i][j] == 1) transitionMatrix[i][j] = 1.0/degree; if (adjacencyMatrix[i][j] == 1) transitionMatrix[i][j] = 1.0 / degree;
} }
} }
...@@ -113,18 +139,18 @@ public class AutomaticClusteringHelper { ...@@ -113,18 +139,18 @@ public class AutomaticClusteringHelper {
// generic matrix normalizer // generic matrix normalizer
public static double[][] normalizeMatrix(double[][] matrix) { public static double[][] normalizeMatrix(double[][] matrix) {
double[][] normalizedMatrix= matrix; double[][] normalizedMatrix = matrix;
double normalizer; double normalizer;
double sum; double sum;
for(int i = 0; i < matrix[0].length; i++) { for (int i = 0; i < matrix[0].length; i++) {
normalizer= 0; normalizer = 0;
sum= 0; sum = 0;
for(int j = 0; j < matrix[0].length; j++) { for (int j = 0; j < matrix[0].length; j++) {
sum+= normalizedMatrix[i][j]; sum += normalizedMatrix[i][j];
} }
if (sum>0) normalizer= 1.0/sum; if (sum > 0) normalizer = 1.0 / sum;
for(int j = 0; j < matrix[0].length; j++) { for (int j = 0; j < matrix[0].length; j++) {
normalizedMatrix[i][j] = matrix[i][j] * normalizer; normalizedMatrix[i][j] = matrix[i][j] * normalizer;
} }
} }
...@@ -135,11 +161,11 @@ public class AutomaticClusteringHelper { ...@@ -135,11 +161,11 @@ public class AutomaticClusteringHelper {
// calculate the inverse probabilities of a transition matrix // calculate the inverse probabilities of a transition matrix
// (regard zero as immutable zero probability) // (regard zero as immutable zero probability)
public static double[][] inverseProbabilitiesMatrix(double[][] matrix) { public static double[][] inverseProbabilitiesMatrix(double[][] matrix) {
double[][] inverseProbabilityMatrix= matrix; double[][] inverseProbabilityMatrix = matrix;
for(int i = 0; i < matrix[0].length; i++) { for (int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix[0].length; j++) { for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] > 0) inverseProbabilityMatrix[i][j] = 1.0/matrix[i][j]; if (matrix[i][j] > 0) inverseProbabilityMatrix[i][j] = 1.0 / matrix[i][j];
} }
} }
...@@ -163,17 +189,17 @@ public class AutomaticClusteringHelper { ...@@ -163,17 +189,17 @@ public class AutomaticClusteringHelper {
EMAComponentInstanceSymbol sourceComp = con.getSourcePort().getComponentInstance(); EMAComponentInstanceSymbol sourceComp = con.getSourcePort().getComponentInstance();
EMAComponentInstanceSymbol targetComp = con.getTargetPort().getComponentInstance(); EMAComponentInstanceSymbol targetComp = con.getTargetPort().getComponentInstance();
for(int i = 0; i < clusters.size(); i++){ for (int i = 0; i < clusters.size(); i++) {
if(clusters.get(i).contains(sourceComp)){ if (clusters.get(i).contains(sourceComp)) {
sourceClusterLabel = i; sourceClusterLabel = i;
} }
if(clusters.get(i).contains(targetComp)){ if (clusters.get(i).contains(targetComp)) {
targetClusterLabel = i; targetClusterLabel = i;
} }
} }
if(sourceClusterLabel != targetClusterLabel){ if (sourceClusterLabel != targetClusterLabel) {
con.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol()); con.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol());
con.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol()); con.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol());
} }
...@@ -182,7 +208,7 @@ public class AutomaticClusteringHelper { ...@@ -182,7 +208,7 @@ public class AutomaticClusteringHelper {
} }
public static double getTypeCostHeuristic(EMAComponentInstanceSymbol componentInstanceSymbol, List<Set<EMAComponentInstanceSymbol>> clustering){ public static double getTypeCostHeuristic(EMAComponentInstanceSymbol componentInstanceSymbol, List<Set<EMAComponentInstanceSymbol>> clustering) {
List<EMAConnectorInstanceSymbol> interClusterConnectors = getInterClusterConnectors(componentInstanceSymbol, clustering); List<EMAConnectorInstanceSymbol> interClusterConnectors = getInterClusterConnectors(componentInstanceSymbol, clustering);
return interClusterConnectors.stream() return interClusterConnectors.stream()
...@@ -221,18 +247,18 @@ public class AutomaticClusteringHelper { ...@@ -221,18 +247,18 @@ public class AutomaticClusteringHelper {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static double getTypeCostHeuristic(EMAPortSymbol port){ public static double getTypeCostHeuristic(EMAPortSymbol port) {
return getTypeCostHeuristic(port.getTypeReference()); return getTypeCostHeuristic(port.getTypeReference());
} }
public static double getTypeCostHeuristic(MCTypeReference<? extends MCTypeSymbol> typeReference) { public static double getTypeCostHeuristic(MCTypeReference<? extends MCTypeSymbol> typeReference) {
if (typeReference.getName().equals("CommonMatrixType")){ if (typeReference.getName().equals("CommonMatrixType")) {
double value = getTypeCostHeuristicHelper( double value = getTypeCostHeuristicHelper(
((ASTCommonMatrixType)((MCASTTypeSymbolReference)typeReference).getAstType()).getElementType().getName()); ((ASTCommonMatrixType) ((MCASTTypeSymbolReference) typeReference).getAstType()).getElementType().getName());
double res = 0; double res = 0;
List<ASTExpression> vectors = ((ASTCommonMatrixType) ((MCASTTypeSymbolReference) typeReference). List<ASTExpression> vectors = ((ASTCommonMatrixType) ((MCASTTypeSymbolReference) typeReference).
getAstType()).getDimension().getDimensionList(); getAstType()).getDimension().getDimensionList();
for (ASTExpression expression : vectors){ for (ASTExpression expression : vectors) {
if (((ASTNumberExpression) expression).getNumberWithUnit().getNumber().isPresent()) { if (((ASTNumberExpression) expression).getNumberWithUnit().getNumber().isPresent()) {
res += value * ((ASTNumberExpression) expression).getNumberWithUnit().getNumber().get(); res += value * ((ASTNumberExpression) expression).getNumberWithUnit().getNumber().get();
} }
...@@ -244,10 +270,11 @@ public class AutomaticClusteringHelper { ...@@ -244,10 +270,11 @@ public class AutomaticClusteringHelper {
} }
private static double getTypeCostHeuristicHelper(String name) { private static double getTypeCostHeuristicHelper(String name) {
// use cost in bytes as used by ROS(http://wiki.ros.org/msg#Field_Types)
double bool = 1; double bool = 1;
double z = 5; double z = 4;
double q = 10; double q = 8;
double c = 20; double c = 16;
switch (name) { switch (name) {
case "B": case "B":
return bool; return bool;
...@@ -262,13 +289,4 @@ public class AutomaticClusteringHelper { ...@@ -262,13 +289,4 @@ public class AutomaticClusteringHelper {
} }
public static ClusteringResultList executeClusteringFromParams(EMAComponentInstanceSymbol emaComponentInstance, List<AlgorithmCliParameters> algoParams) {
ClusteringResultList res = new ClusteringResultList();
for (int i = 0; i < algoParams.size(); i++) {
System.out.println("Clustering with algorithm " + (i+1) + "/" + algoParams.size() + ": " +algoParams.get(i).toString());
res.add(ClusteringResult.fromParameters(emaComponentInstance, algoParams.get(i)));
}
return res;
}
} }
...@@ -7,12 +7,12 @@ import java.util.Set; ...@@ -7,12 +7,12 @@ import java.util.Set;
// product if for clustering factory // product if for clustering factory
public interface ClusteringAlgorithm { public interface ClusteringAlgorithm {
List<Set<EMAComponentInstanceSymbol>> cluster(EMAComponentInstanceSymbol component, Object... args); List<Set<EMAComponentInstanceSymbol>> cluster(ClusteringInput clusteringInput, Object... args);
//TODO: add arguments as typed state of the algorithms(instead of untyped) //TODO: add arguments as typed state of the algorithms(instead of untyped)
default List<Set<EMAComponentInstanceSymbol>> clusterWithState(EMAComponentInstanceSymbol component){ default List<Set<EMAComponentInstanceSymbol>> clusterWithState(ClusteringInput clusteringInput){
Object[] args = getArgs(); Object[] args = getArgs();
return cluster(component, args);