Commit 6e120d74 authored by dinhan93's avatar dinhan93

Tests for Monte Carlo Simulation

parent b41df725
......@@ -14,14 +14,14 @@ import java.util.Set;
public class MonteCarloIntegration {
public double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol){
public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol){
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0;
for(long 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(1, componentInstanceSymbol.getSubComponents().size());
int randNumClusters = randomNumberInRange(2, componentInstanceSymbol.getSubComponents().size());
SpectralClusteringAlgorithm spectralClusteringAlgorithm = new SpectralClusteringAlgorithm();
Object[] params = new Object[]{SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS, randNumClusters};
......@@ -32,10 +32,11 @@ public class MonteCarloIntegration {
sum+=calculateCostOfClusters(componentInstanceSymbol, spectralClusters);
}
// return average costs of clustering with spectral
return sum/iterations;
}
public int randomNumberInRange(int min, int max) {
public static int randomNumberInRange(int min, int max) {
Random random = new Random();
return random.nextInt((max - min) + 1) + min;
}
......@@ -46,6 +47,8 @@ public class MonteCarloIntegration {
double sum = 0;
for(EMAConnectorInstanceSymbol con : connectors){
System.out.println("Connector size "+connectors.size());
System.out.println("Con: "+con);
// -1 = super comp
int sourceClusterLabel = -1;
int targetClusterLabel = -1;
......@@ -64,6 +67,7 @@ public class MonteCarloIntegration {
}
if(sourceClusterLabel != targetClusterLabel){
sum +=AutomaticClusteringHelper.getTypeCostHeuristic(con.getSourcePort());
System.out.println("CostSub: " + AutomaticClusteringHelper.getTypeCostHeuristic(con.getSourcePort()));
}
}
......
package de.monticore.lang.monticar.generator.middleware.Simulation;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
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 org.junit.Test;
import java.util.List;
import java.util.Set;
import static de.monticore.lang.monticar.generator.middleware.Simulation.MonteCarloIntegration.calculateCostOfClusters;
import static org.junit.Assert.*;
public class MonteCarloIntegrationTest {
public static final String TEST_PATH = "src/test/resources/";
@Test
public void costTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
//ClustersWithSingleConnection
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
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("Size of Nodes "+ componentInstanceSymbol.getSubComponents().size());
System.out.println("Cost: "+ cost);
assertTrue(cost == 10);
}
@Test
public void mcSimulationTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
//ClustersWithSingleConnection
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
double cost = MonteCarloIntegration.simulate(10, componentInstanceSymbol);
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