Commit 8447d8de authored by dinhan93's avatar dinhan93

Monte Carlo with Random Clustering

parent c62b9ac2
Pipeline #109071 canceled with stages
package de.monticore.lang.monticar.generator.middleware.Simulation; package de.monticore.lang.monticar.generator.middleware.Simulation;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol; import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAConnectorInstanceSymbol;
import de.monticore.lang.monticar.generator.middleware.clustering.AutomaticClusteringHelper; import de.monticore.lang.monticar.generator.middleware.clustering.AutomaticClusteringHelper;
import de.monticore.lang.monticar.generator.middleware.clustering.FlattenArchitecture; import de.monticore.lang.monticar.generator.middleware.clustering.FlattenArchitecture;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringAlgorithm; import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder; import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class MonteCarloIntegration { public class MonteCarloIntegration {
public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol){ public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol, int numberOfClusters){
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol); EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0; double sum = 0;
for(int i = 0; i<iterations; i++){ for(int i = 0; i<iterations; i++){
// Cluster with Spectral + save the cost: Parameter(Number of clusters, data)
int randNumClusters = randomNumberInRange(2, componentInstanceSymbol.getSubComponents().size());
/*
// This would be with Spectral Clustering
SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm(); SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm();
Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, randNumClusters}; Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, numberOfClusters};
List<Set<EMAComponentInstanceSymbol>> spectralClusters = spectralClusteringAlgorithm.cluster(flattenedComponent, params); List<Set<EMAComponentInstanceSymbol>> clusters = spectralClusteringAlgorithm.cluster(flattenedComponent, params);
AutomaticClusteringHelper.annotateComponentWithRosTagsForClusters(flattenedComponent, spectralClusters); */
// Let's random cluster the model
List<Set<EMAComponentInstanceSymbol>> clusters = randomClustering(flattenedComponent, numberOfClusters);
//iterate through all clusters and add all cost of the ROS Tags between clusters //iterate through all clusters and add all cost of the ROS Tags between clusters
sum+=calculateCostOfClusters(componentInstanceSymbol, spectralClusters); sum += AutomaticClusteringHelper.getTypeCostHeuristic(flattenedComponent, clusters);
} }
// return average costs of clustering with spectral // return average costs of clustering with spectral
return sum/iterations; double res = sum/iterations;
System.out.println("Result: " + res);
return res;
} }
public static int randomNumberInRange(int min, int max) { public static int randomNumberInRange(int min, int max) {
...@@ -41,37 +42,44 @@ public class MonteCarloIntegration { ...@@ -41,37 +42,44 @@ public class MonteCarloIntegration {
return random.nextInt((max - min) + 1) + min; return random.nextInt((max - min) + 1) + min;
} }
public static double calculateCostOfClusters(EMAComponentInstanceSymbol componentInstanceSymbol, List<Set<EMAComponentInstanceSymbol>> clusters) { public static List<Set<EMAComponentInstanceSymbol>> randomClustering(EMAComponentInstanceSymbol componentInstanceSymbol, int numberOfClusters){
Collection<EMAConnectorInstanceSymbol> connectors = componentInstanceSymbol.getConnectorInstances(); List<Set<EMAComponentInstanceSymbol>> clusters = new ArrayList<>();
double sum = 0; for(int i = 0; i<numberOfClusters; i++){
clusters.add(new HashSet<>());
}
for(EMAConnectorInstanceSymbol con : connectors){ // All subcomponents of the Symbol
System.out.println("Connector size "+connectors.size()); Collection<EMAComponentInstanceSymbol> subcomponents = componentInstanceSymbol.getSubComponents();
System.out.println("Con: "+con);
// -1 = super comp
int sourceClusterLabel = -1;
int targetClusterLabel = -1;
EMAComponentInstanceSymbol sourceComp = con.getSourcePort().getComponentInstance(); // Put subcomponents into an ArrayList
EMAComponentInstanceSymbol targetComp = con.getTargetPort().getComponentInstance(); ArrayList<EMAComponentInstanceSymbol> arrayListSubComponent = new ArrayList<>();
for (EMAComponentInstanceSymbol subcomp : subcomponents) {
arrayListSubComponent.add(subcomp);
}
for(int i = 0; i < clusters.size(); i++){ System.out.println("ArrayList: " + arrayListSubComponent);
if(clusters.get(i).contains(sourceComp)){
sourceClusterLabel = i;
}
if(clusters.get(i).contains(targetComp)){ // Distribute randomly!
targetClusterLabel = i; // First of all, give each of the clusters one element randomly
} for(int j = 0; j < clusters.size(); j++){
} if(!arrayListSubComponent.isEmpty()) {
if(sourceClusterLabel != targetClusterLabel){ int randN = randomNumberInRange(0, arrayListSubComponent.size()-1);
sum +=AutomaticClusteringHelper.getTypeCostHeuristic(con.getSourcePort()); clusters.get(j).add(arrayListSubComponent.get(randN));
System.out.println("CostSub: " + AutomaticClusteringHelper.getTypeCostHeuristic(con.getSourcePort())); arrayListSubComponent.remove(randN);
} }
}
int numberOfSubcomponents = arrayListSubComponent.size();
// Then, a random element is assigned to a random cluster until every element is assigned
for(int h = 0; h<numberOfSubcomponents; h++){
int randElement = randomNumberInRange(0,arrayListSubComponent.size()-1);
int randCluster = randomNumberInRange(0,clusters.size()-1);
clusters.get(randCluster).add(arrayListSubComponent.get(randElement));
arrayListSubComponent.remove(randElement);
} }
return sum; return clusters;
} }
} }
\ No newline at end of file
...@@ -12,7 +12,6 @@ import org.junit.Test; ...@@ -12,7 +12,6 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static de.monticore.lang.monticar.generator.middleware.Simulation.MonteCarloIntegration.calculateCostOfClusters;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class MonteCarloIntegrationTest { public class MonteCarloIntegrationTest {
...@@ -20,27 +19,28 @@ public class MonteCarloIntegrationTest { ...@@ -20,27 +19,28 @@ public class MonteCarloIntegrationTest {
public static final String TEST_PATH = "src/test/resources/"; public static final String TEST_PATH = "src/test/resources/";
@Test @Test
public void costTest(){ public void costRandomClustering(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH); TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
//ClustersWithSingleConnection //ClustersWithSingleConnection
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null); EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol); assertNotNull(componentInstanceSymbol);
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol); System.out.println("Size of Subcomponents: " + componentInstanceSymbol.getSubComponents().size());
System.out.println("Components: \n" + componentInstanceSymbol);
//System.out.println(componentInstanceSymbol);
List<Set<EMAComponentInstanceSymbol>> clusters = MonteCarloIntegration.randomClustering(componentInstanceSymbol, 2);
SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm();
Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, 3};
List<Set<EMAComponentInstanceSymbol>> spectralClusters = spectralClusteringAlgorithm.cluster(flattenedComponent, params);
AutomaticClusteringHelper.annotateComponentWithRosTagsForClusters(flattenedComponent, spectralClusters);
double cost = calculateCostOfClusters(componentInstanceSymbol, spectralClusters); System.out.println(" ------------------");
System.out.println("Size of Nodes "+ componentInstanceSymbol.getSubComponents().size()); System.out.println(" ");
System.out.println("Cost: "+ cost); System.out.println("Clusters: \n" + clusters);
assertTrue(clusters.size() == 2);
assertTrue(cost == 10); assertTrue(clusters.get(0).size()>=1);
assertTrue(clusters.get(1).size()>=1);
} }
@Test @Test
public void mcSimulationTest(){ public void mcSimulationTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH); TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
...@@ -49,7 +49,8 @@ public class MonteCarloIntegrationTest { ...@@ -49,7 +49,8 @@ public class MonteCarloIntegrationTest {
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null); EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol); assertNotNull(componentInstanceSymbol);
double cost = MonteCarloIntegration.simulate(10, componentInstanceSymbol); double cost = MonteCarloIntegration.simulate(100, componentInstanceSymbol, 2);
assertTrue(cost == 10); System.out.println("Cost: "+cost);
assertTrue(cost >= 10);
} }
} }
\ No newline at end of file
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