Commit c3685b33 authored by dinhan93's avatar dinhan93

Final Version of Monte Carlo Simulation, JSON generation for the MC results

parent ba9d617f
......@@ -9,16 +9,37 @@ import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.Spe
import java.util.*;
public class MonteCarloIntegration {
private static double[] averages;
private static double[] costs;
private static int iterations;
private static int numberOfClusters;
public MonteCarloIntegration(int iterations, int numberOfClusters){
this.iterations = iterations;
this.numberOfClusters = numberOfClusters;
this.averages = new double[this.iterations];
this.costs = new double[this.iterations];
}
public static double[] getAverages() {
return averages;
}
// index 1: Spectral Clustering
// Index 2: Random Clustering
public static double simulate(int iterations, EMAComponentInstanceSymbol componentInstanceSymbol, int numberOfClusters) {
//EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
public static int getIterations() {
return iterations;
}
double sum = 0;
public int getNumberOfClusters() {
return numberOfClusters;
}
double[] costs = new double[iterations];
public static double[] getCosts(){
return costs;
}
public static double simulate(EMAComponentInstanceSymbol componentInstanceSymbol) {
//EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0;
for (int i = 0; i < iterations; i++) {
/*
......@@ -34,17 +55,8 @@ public class MonteCarloIntegration {
costs[i] = AutomaticClusteringHelper.getTypeCostHeuristic(componentInstanceSymbol, clusters);
//iterate through all clusters and add all cost of the ROS Tags between clusters
sum = getCostAtN(i+1, costs);
System.out.println("Average(" + (i + 1) + ")=" + sum);
/*
if(i==iterations-1){
System.out.println("Costlist: "+ Arrays.toString(costs));
}
*/
averages[i]=sum;
}
System.out.println("Final Result: " + sum);
System.out.println("Minimum: "+ getMinValue(costs));
System.out.println("Maximum: "+ getMaxValue(costs));
// Plot(x,y) would be Plot(iterations, getCostAtN(i+1
return sum;
}
......@@ -55,22 +67,22 @@ public class MonteCarloIntegration {
}
// getting the maximum value
public static double getMaxValue(double[] array) {
double maxValue = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > maxValue) {
maxValue = array[i];
public static double getMaxValue() {
double maxValue = costs[0];
for (int i = 1; i < costs.length; i++) {
if (costs[i] > maxValue) {
maxValue = costs[i];
}
}
return maxValue;
}
// getting the miniumum value
public static double getMinValue(double[] array) {
double minValue = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < minValue) {
minValue = array[i];
public static double getMinValue() {
double minValue = costs[0];
for (int i = 1; i < costs.length; i++) {
if (costs[i] < minValue) {
minValue = costs[i];
}
}
return minValue;
......@@ -115,11 +127,11 @@ public class MonteCarloIntegration {
}
// iteration step n, costs: Array of all costs before
public static double getCostAtN(int n, double[] costs) {
public static double getCostAtN(int n, double[] array) {
double sum = 0;
for (int i = 0; i < n ; i++) {
sum += costs[i];
sum += array[i];
}
return sum /n;
......
package de.monticore.lang.monticar.generator.middleware.Simulation;
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.se_rwth.commons.logging.Log;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MonteCarloResult {
private EMAComponentInstanceSymbol component;
private static int iterations;
private static int numberOfClustersMC;
public MonteCarloResult(EMAComponentInstanceSymbol component, int iterations, int numberOfClustersMC) {
this.component = component;
this.iterations = iterations;
this.numberOfClustersMC = numberOfClustersMC;
}
public EMAComponentInstanceSymbol getComponent() {
return component;
}
public int getNumberOfClustersMC() {
return numberOfClustersMC;
}
public int getIterations() {
return iterations;
}
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 {
file.getParentFile().mkdirs();
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("Iterations", this.getIterations());
result.addProperty("NumberOfClusters", this.getNumberOfClustersMC());
result.addProperty("MaxValueMC", MonteCarloIntegration.getMaxValue());
result.addProperty("MinValueMC", MonteCarloIntegration.getMinValue());
for (int i = 0; i < this.getIterations(); i++) {
result.addProperty("MCResult(" + (i + 1) + ")", MonteCarloIntegration.getAverages()[i]);
}
result.addProperty("MCAverageResult", MonteCarloIntegration.getAverages()[this.getIterations()-1]);
return result;
}
}
......@@ -2,19 +2,23 @@ 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.Ignore;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
public class MonteCarloIntegrationTest {
public static final String TEST_PATH = "src/test/resources/";
public static final String TEST_PATH_PACMAN = "src/test/resources/pacman/";
@Test
public void randomClusteringTest(){
......@@ -23,16 +27,16 @@ public class MonteCarloIntegrationTest {
//ClustersWithSingleConnection
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("clustering.clustersWithSingleConnection", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
List<Set<EMAComponentInstanceSymbol>> clusters = MonteCarloIntegration.randomClustering(flattenedComponent, 2);
assertTrue("Too many or less clusters created.", clusters.size() == 2);
assertTrue("Subcomponent 1 is not distributed evenly/correctly!", clusters.get(0).size()>=1);
assertTrue("Subcomponent 2 is not distributed evenly/correctly!",clusters.get(1).size()>=1);
}
@Ignore
@Test
public void monteCarloSimulationTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
......@@ -43,33 +47,14 @@ public class MonteCarloIntegrationTest {
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
// random clustering
double cost = MonteCarloIntegration.simulate(100, flattenedComponent, 2, 2);
// spectral clustering
double cost2 = MonteCarloIntegration.simulate(100, flattenedComponent, 2, 1);
//Random Clustering
MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3);
double costRandom = MonteCarloIntegration.simulate(flattenedComponent);
double[] averages = MonteCarloIntegration.getAverages();
double[] costs = MonteCarloIntegration.getCosts();
assertTrue(cost >= cost2);
assertTrue(averages[1]== (costs[0]+costs[1])/2);
assertTrue(averages[2]== (costs[0]+costs[1]+costs[2])/3);
assertTrue(averages[3]== (costs[0]+costs[1]+costs[2]+costs[3])/4);
}
@Test
public void monteCarloSimulationPacmanTest(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH_PACMAN);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("de.rwth.pacman.heithoff2.controller", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
// simulation with spectral Clustering
double costS = MonteCarloIntegration.simulate(10, flattenedComponent, 10, 1);
System.out.println("Cost Spectral Clustering: "+costS);
// simulation with random Clustering
double costR = MonteCarloIntegration.simulate(10, flattenedComponent, 10, 2);
System.out.println("Cost Random Clustering: "+costR);
assertTrue(costR >= costS);
}
}
\ 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