Commit dbf5b788 authored by dinhan93's avatar dinhan93

Monte Carlo with Random Clustering, Spectral Clustering Option and Testcases

parent 8447d8de
Pipeline #109074 canceled with stages
...@@ -10,30 +10,36 @@ import java.util.*; ...@@ -10,30 +10,36 @@ import java.util.*;
public class MonteCarloIntegration { public class MonteCarloIntegration {
public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol, int numberOfClusters){ // index 1: Spectral Clustering
// Index 2: Random Clustering
public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol, int numberOfClusters, int index){
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol); EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0; double sum = 0;
for(int i = 0; i<iterations; i++){ if(index == 1){
for(int i = 0; i<iterations; i++){
// This would be with Spectral Clustering
SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm();
Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, numberOfClusters};
List<Set<EMAComponentInstanceSymbol>> clusters = spectralClusteringAlgorithm.cluster(flattenedComponent, params);
/* //iterate through all clusters and add all cost of the ROS Tags between clusters
// This would be with Spectral Clustering sum += AutomaticClusteringHelper.getTypeCostHeuristic(flattenedComponent, clusters);
SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm(); }
Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, numberOfClusters};
List<Set<EMAComponentInstanceSymbol>> clusters = spectralClusteringAlgorithm.cluster(flattenedComponent, params);
*/
// 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
sum += AutomaticClusteringHelper.getTypeCostHeuristic(flattenedComponent, clusters);
} }
else if(index == 2) {
for (int j = 0; j < iterations; j++) {
// 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
sum += AutomaticClusteringHelper.getTypeCostHeuristic(flattenedComponent, clusters);
}
}
// return average costs of clustering with spectral // return average costs of clustering with spectral
double res = sum/iterations; double res = sum/iterations;
System.out.println("Result: " + res); System.out.println("Average Costs: " + res);
return res; return res;
} }
...@@ -58,8 +64,6 @@ public class MonteCarloIntegration { ...@@ -58,8 +64,6 @@ public class MonteCarloIntegration {
arrayListSubComponent.add(subcomp); arrayListSubComponent.add(subcomp);
} }
System.out.println("ArrayList: " + arrayListSubComponent);
// Distribute randomly! // Distribute randomly!
// First of all, give each of the clusters one element randomly // First of all, give each of the clusters one element randomly
for(int j = 0; j < clusters.size(); j++){ for(int j = 0; j < clusters.size(); j++){
......
...@@ -2,10 +2,6 @@ package de.monticore.lang.monticar.generator.middleware.Simulation; ...@@ -2,10 +2,6 @@ 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.monticar.generator.middleware.AbstractSymtabTest; import de.monticore.lang.monticar.generator.middleware.AbstractSymtabTest;
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.algorithms.SpectralClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder;
import de.monticore.lang.tagging._symboltable.TaggingResolver; import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Test; import org.junit.Test;
...@@ -17,6 +13,7 @@ import static org.junit.Assert.*; ...@@ -17,6 +13,7 @@ import static org.junit.Assert.*;
public class MonteCarloIntegrationTest { public class MonteCarloIntegrationTest {
public static final String TEST_PATH = "src/test/resources/"; public static final String TEST_PATH = "src/test/resources/";
public static final String TEST_PATH_PACMAN = "src/test/resources/pacman/";
@Test @Test
public void costRandomClustering(){ public void costRandomClustering(){
...@@ -26,21 +23,13 @@ public class MonteCarloIntegrationTest { ...@@ -26,21 +23,13 @@ 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);
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); List<Set<EMAComponentInstanceSymbol>> clusters = MonteCarloIntegration.randomClustering(componentInstanceSymbol, 2);
assertTrue("Too many or less clusters created.", clusters.size() == 2);
System.out.println(" ------------------"); assertTrue("Subcomponent 1 is not distributed evenly/correctly!", clusters.get(0).size()>=1);
System.out.println(" "); assertTrue("Subcomponent 2 is not distributed evenly/correctly!",clusters.get(1).size()>=1);
System.out.println("Clusters: \n" + clusters);
assertTrue(clusters.size() == 2);
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,8 +38,24 @@ public class MonteCarloIntegrationTest { ...@@ -49,8 +38,24 @@ 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(100, componentInstanceSymbol, 2); EMAComponentInstanceSymbol componentInstanceSymbol2 = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
System.out.println("Cost: "+cost); assertNotNull(componentInstanceSymbol);
// random clustering
double cost = MonteCarloIntegration.simulate(100, componentInstanceSymbol, 2, 2);
assertTrue(cost >= 10); assertTrue(cost >= 10);
} }
@Test
public void mcSimulationPacmanTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH_PACMAN);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("de.rwth.pacman.heithoff2.controller", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
// simulation with random Clustering
double cost = MonteCarloIntegration.simulate(10, componentInstanceSymbol, 10, 2);
}
} }
\ 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