Commit 61045900 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Added MonteCarlo to EvaluationTest

parent 7a34428f
Pipeline #111628 passed with stages
...@@ -2,17 +2,14 @@ package de.monticore.lang.monticar.generator.middleware.Simulation; ...@@ -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.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
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.algorithms.SpectralClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder;
import java.util.*; import java.util.*;
public class MonteCarloIntegration { public class MonteCarloIntegration {
private static double[] averages; private double[] averages;
private static double[] costs; private double[] costs;
private static int iterations; private int iterations;
private static int numberOfClusters; private int numberOfClusters;
public MonteCarloIntegration(int iterations, int numberOfClusters){ public MonteCarloIntegration(int iterations, int numberOfClusters){
this.iterations = iterations; this.iterations = iterations;
...@@ -21,11 +18,11 @@ public class MonteCarloIntegration { ...@@ -21,11 +18,11 @@ public class MonteCarloIntegration {
this.costs = new double[this.iterations]; this.costs = new double[this.iterations];
} }
public static double[] getAverages() { public double[] getAverages() {
return averages; return averages;
} }
public static int getIterations() { public int getIterations() {
return iterations; return iterations;
} }
...@@ -33,11 +30,11 @@ public class MonteCarloIntegration { ...@@ -33,11 +30,11 @@ public class MonteCarloIntegration {
return numberOfClusters; return numberOfClusters;
} }
public static double[] getCosts(){ public double[] getCosts() {
return costs; return costs;
} }
public static double simulate(EMAComponentInstanceSymbol componentInstanceSymbol) { public double simulate(EMAComponentInstanceSymbol componentInstanceSymbol) {
//EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol); //EMAComponentInstanceSymbol flattenedComponent = FlattenArchitecture.flattenArchitecture(componentInstanceSymbol);
double sum = 0; double sum = 0;
...@@ -67,7 +64,7 @@ public class MonteCarloIntegration { ...@@ -67,7 +64,7 @@ public class MonteCarloIntegration {
} }
// getting the maximum value // getting the maximum value
public static double getMaxValue() { public double getMaxValue() {
double maxValue = costs[0]; double maxValue = costs[0];
for (int i = 1; i < costs.length; i++) { for (int i = 1; i < costs.length; i++) {
if (costs[i] > maxValue) { if (costs[i] > maxValue) {
...@@ -78,7 +75,7 @@ public class MonteCarloIntegration { ...@@ -78,7 +75,7 @@ public class MonteCarloIntegration {
} }
// getting the miniumum value // getting the miniumum value
public static double getMinValue() { public double getMinValue() {
double minValue = costs[0]; double minValue = costs[0];
for (int i = 1; i < costs.length; i++) { for (int i = 1; i < costs.length; i++) {
if (costs[i] < minValue) { if (costs[i] < minValue) {
......
...@@ -5,21 +5,19 @@ import com.google.gson.JsonObject; ...@@ -5,21 +5,19 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol; import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.se_rwth.commons.logging.Log; import de.se_rwth.commons.logging.Log;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
public class MonteCarloResult { public class MonteCarloResult {
private EMAComponentInstanceSymbol component; private EMAComponentInstanceSymbol component;
private static int iterations; private MonteCarloIntegration sim;
private static int numberOfClustersMC;
public MonteCarloResult(EMAComponentInstanceSymbol component, int iterations, int numberOfClustersMC) { public MonteCarloResult(EMAComponentInstanceSymbol component, MonteCarloIntegration sim) {
this.component = component; this.component = component;
this.iterations = iterations; this.sim = sim;
this.numberOfClustersMC = numberOfClustersMC;
} }
public EMAComponentInstanceSymbol getComponent() { public EMAComponentInstanceSymbol getComponent() {
...@@ -27,11 +25,11 @@ public class MonteCarloResult { ...@@ -27,11 +25,11 @@ public class MonteCarloResult {
} }
public int getNumberOfClustersMC() { public int getNumberOfClustersMC() {
return numberOfClustersMC; return sim.getNumberOfClusters();
} }
public int getIterations() { public int getIterations() {
return iterations; return sim.getIterations();
} }
public void saveAsJson(String path, String filename) { public void saveAsJson(String path, String filename) {
...@@ -39,18 +37,9 @@ public class MonteCarloResult { ...@@ -39,18 +37,9 @@ public class MonteCarloResult {
File dir = new File(path); File dir = new File(path);
File file = new File(dir, filename); File file = new File(dir, filename);
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
if (file.exists() && !file.isDirectory()) { JsonObject result = createJsonResultObject();
try { jsonArray.add(result);
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 { try {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
FileWriter fileWriter = new FileWriter(file); FileWriter fileWriter = new FileWriter(file);
...@@ -65,12 +54,12 @@ public class MonteCarloResult { ...@@ -65,12 +54,12 @@ public class MonteCarloResult {
JsonObject result = new JsonObject(); JsonObject result = new JsonObject();
result.addProperty("Iterations", this.getIterations()); result.addProperty("Iterations", this.getIterations());
result.addProperty("NumberOfClusters", this.getNumberOfClustersMC()); result.addProperty("NumberOfClusters", this.getNumberOfClustersMC());
result.addProperty("MaxValueMC", MonteCarloIntegration.getMaxValue()); result.addProperty("MaxValueMC", sim.getMaxValue());
result.addProperty("MinValueMC", MonteCarloIntegration.getMinValue()); result.addProperty("MinValueMC", sim.getMinValue());
for (int i = 0; i < this.getIterations(); i++) { 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; return result;
} }
......
package de.monticore.lang.monticar.generator.middleware; 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.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.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertNotNull;
@Ignore("Used for evaluation, nothing gets asserted") @Ignore("Used for evaluation, nothing gets asserted")
public class EvaluationTest { public class EvaluationTest {
...@@ -37,7 +44,6 @@ public class EvaluationTest { ...@@ -37,7 +44,6 @@ public class EvaluationTest {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/supermarioSilhouette.json"}); DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/supermarioSilhouette.json"});
} }
@Ignore("Very long runtime(30min+)")
@Test @Test
public void testDaimlerModel() { public void testDaimlerModel() {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/daimler.json"}); DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/daimler.json"});
...@@ -47,4 +53,52 @@ public class EvaluationTest { ...@@ -47,4 +53,52 @@ public class EvaluationTest {
public void testDaimlerModelSilhouette() { public void testDaimlerModelSilhouette() {
DistributedTargetGeneratorCli.main(new String[]{"./src/test/resources/config/evaluation/daimlerSilhouette.json"}); 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; ...@@ -10,7 +10,7 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.*; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class MonteCarloIntegrationTest { public class MonteCarloIntegrationTest {
...@@ -47,9 +47,9 @@ public class MonteCarloIntegrationTest { ...@@ -47,9 +47,9 @@ public class MonteCarloIntegrationTest {
//Random Clustering //Random Clustering
MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3); MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3);
double costRandom = MonteCarloIntegration.simulate(flattenedComponent); double costRandom = sim.simulate(flattenedComponent);
double[] averages = MonteCarloIntegration.getAverages(); double[] averages = sim.getAverages();
double[] costs = MonteCarloIntegration.getCosts(); double[] costs = sim.getCosts();
assertTrue(averages[1]== (costs[0]+costs[1])/2); assertTrue(averages[1]== (costs[0]+costs[1])/2);
assertTrue(averages[2]== (costs[0]+costs[1]+costs[2])/3); assertTrue(averages[2]== (costs[0]+costs[1]+costs[2])/3);
...@@ -69,9 +69,9 @@ public class MonteCarloIntegrationTest { ...@@ -69,9 +69,9 @@ public class MonteCarloIntegrationTest {
//Random Clustering //Random Clustering
MonteCarloIntegration sim = new MonteCarloIntegration(1000, 3); 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"); 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