Added MonteCarlo to EvaluationTest

parent 7a34428f
Pipeline #111628 passed with stages
......@@ -2,17 +2,14 @@ package de.monticore.lang.monticar.generator.middleware.Simulation;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
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 java.util.*;
public class MonteCarloIntegration {
private static double[] averages;
private static double[] costs;
private static int iterations;
private static int numberOfClusters;
private double[] averages;
private double[] costs;
private int iterations;
private int numberOfClusters;
public MonteCarloIntegration(int iterations, int numberOfClusters){
this.iterations = iterations;
......@@ -21,11 +18,11 @@ public class MonteCarloIntegration {
this.costs = new double[this.iterations];
}
public static double[] getAverages() {
public double[] getAverages() {
return averages;
}
public static int getIterations() {
public int getIterations() {
return iterations;
}
......@@ -33,11 +30,11 @@ public class MonteCarloIntegration {
return numberOfClusters;
}
public static double[] getCosts(){
public double[] getCosts() {
return costs;
}
public static double simulate(EMAComponentInstanceSymbol componentInstanceSymbol) {
public double simulate(EMAComponentInstanceSymbol componentInstanceSymbol) {
//EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0;
......@@ -67,7 +64,7 @@ public class MonteCarloIntegration {
}
// getting the maximum value
public static double getMaxValue() {
public double getMaxValue() {
double maxValue = costs[0];
for (int i = 1; i < costs.length; i++) {
if (costs[i] > maxValue) {
......@@ -78,7 +75,7 @@ public class MonteCarloIntegration {
}
// getting the miniumum value
public static double getMinValue() {
public double getMinValue() {
double minValue = costs[0];
for (int i = 1; i < costs.length; i++) {
if (costs[i] < minValue) {
......
......@@ -5,21 +5,19 @@ 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;
private MonteCarloIntegration sim;
public MonteCarloResult(EMAComponentInstanceSymbol component, int iterations, int numberOfClustersMC) {
public MonteCarloResult(EMAComponentInstanceSymbol component, MonteCarloIntegration sim) {
this.component = component;
this.iterations = iterations;
this.numberOfClustersMC = numberOfClustersMC;
this.sim = sim;
}
public EMAComponentInstanceSymbol getComponent() {
......@@ -27,11 +25,11 @@ public class MonteCarloResult {
}
public int getNumberOfClustersMC() {
return numberOfClustersMC;
return sim.getNumberOfClusters();
}
public int getIterations() {
return iterations;
return sim.getIterations();
}
public void saveAsJson(String path, String filename) {
......@@ -39,18 +37,9 @@ public class MonteCarloResult {
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);
}
JsonObject result = createJsonResultObject();
jsonArray.add(result);
try {
file.getParentFile().mkdirs();
FileWriter fileWriter = new FileWriter(file);
......@@ -65,12 +54,12 @@ public class MonteCarloResult {
JsonObject result = new JsonObject();
result.addProperty("Iterations", this.getIterations());
result.addProperty("NumberOfClusters", this.getNumberOfClustersMC());
result.addProperty("MaxValueMC", MonteCarloIntegration.getMaxValue());
result.addProperty("MinValueMC", MonteCarloIntegration.getMinValue());
result.addProperty("MaxValueMC", sim.getMaxValue());
result.addProperty("MinValueMC", sim.getMinValue());
for (int i = 0; i < this.getIterations(); i++) {
result.addProperty("MCResult(" + (i + 1) + ")", MonteCarloIntegration.getAverages()[i]);
result.addProperty("MCResult(" + (i + 1) + ")", sim.getAverages()[i]);
}
result.addProperty("MCAverageResult", MonteCarloIntegration.getAverages()[this.getIterations()-1]);
result.addProperty("MCAverageResult", sim.getAverages()[this.getIterations() - 1]);
return result;
}
......
package de.monticore.lang.monticar.generator.middleware;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.middleware.Simulation.MonteCarloIntegration;
import de.monticore.lang.monticar.generator.middleware.Simulation.MonteCarloResult;
import de.monticore.lang.monticar.generator.middleware.cli.DistributedTargetGeneratorCli;
import de.monticore.lang.monticar.generator.middleware.clustering.FlattenArchitecture;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
@Ignore("Used for evaluation, nothing gets asserted")
public class EvaluationTest {
......@@ -37,7 +44,6 @@ public class EvaluationTest {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/supermarioSilhouette.json"});
}
@Ignore("Very long runtime(30min+)")
@Test
public void testDaimlerModel() {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/daimler.json"});
......@@ -47,4 +53,52 @@ public class EvaluationTest {
public void testDaimlerModelSilhouette() {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/daimlerSilhouette.json"});
}
@Test
public void testPacmanMonteCarlo() {
String modelPath = "src/test/resources/pacman/";
String compName = "de.rwth.pacman.heithoff2.controller";
String shortName = "pacman";
MonteCarloSimulation(modelPath, compName, shortName, 1000);
}
@Test
public void testAutopilotMonteCarlo() {
String modelPath = "src/test/resources/autopilot/";
String compName = "de.rwth.armin.modeling.autopilot.autopilot";
String shortName = "autopilot";
MonteCarloSimulation(modelPath, compName, shortName, 1000);
}
@Test
public void testSupermarioMonteCarlo() {
String modelPath = "src/test/resources/supermario/";
String compName = "de.rwth.supermario.haller.controller";
String shortName = "supermario";
MonteCarloSimulation(modelPath, compName, shortName, 1000);
}
@Test
public void testDaimlerMonteCarlo() {
String modelPath = "src/test/resources/daimlerModel/";
String compName = "daimler.v4.oeffentlicher_Demonstrator_FAS_v04";
String shortName = "daimler";
MonteCarloSimulation(modelPath, compName, shortName, 100);
}
protected void MonteCarloSimulation(String modelPath, String compName, String shortName, int iterations) {
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(modelPath);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve(compName, EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
//Random Clustering
System.out.println("Starting Simulation");
MonteCarloIntegration sim = new MonteCarloIntegration(iterations, 3);
sim.simulate(flattenedComponent);
MonteCarloResult res = new MonteCarloResult(componentInstanceSymbol, sim);
res.saveAsJson("target/evaluation/" + shortName + "MC/", "monteCarloResults.json");
}
}
......@@ -10,7 +10,7 @@ import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class MonteCarloIntegrationTest {
......@@ -47,9 +47,9 @@ public class MonteCarloIntegrationTest {
//Random Clustering
MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3);
double costRandom = MonteCarloIntegration.simulate(flattenedComponent);
double[] averages = MonteCarloIntegration.getAverages();
double[] costs = MonteCarloIntegration.getCosts();
double costRandom = sim.simulate(flattenedComponent);
double[] averages = sim.getAverages();
double[] costs = sim.getCosts();
assertTrue(averages[1]== (costs[0]+costs[1])/2);
assertTrue(averages[2]== (costs[0]+costs[1]+costs[2])/3);
......@@ -69,9 +69,9 @@ public class MonteCarloIntegrationTest {
//Random Clustering
MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3);
double costRandom = MonteCarloIntegration.simulate(flattenedComponent);
double costRandom = sim.simulate(flattenedComponent);
MonteCarloResult res = new MonteCarloResult(flattenedComponent, 1000, 3);
MonteCarloResult res = new MonteCarloResult(flattenedComponent, sim);
res.saveAsJson("target/evaluation/montecarlo", "autopilot.json");
......
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