use of christian.RobotScenario in project SIMVA-SoS by SESoS.
the class Executor method Perform_Experiment.
public static void Perform_Experiment(NormalDistributor distributor, String params) throws IOException {
// Simulator sim, String caseName
String[] args = params.split(",");
// MCI
int scenarioType = 2;
BaseScenario bs = null;
Simulator sim = null;
if (args[0].equalsIgnoreCase("Robot")) {
bs = new RobotScenario(0);
} else if (args[0].equalsIgnoreCase("Robot_b1")) {
bs = new RobotScenario(1);
} else if (args[0].equalsIgnoreCase("Robot_b2")) {
bs = new RobotScenario(2);
} else if (args[0].equalsIgnoreCase("Robot_b3")) {
bs = new RobotScenario(3);
} else if (args[0].equalsIgnoreCase("Robot_b4")) {
bs = new RobotScenario(4);
} else if (args[0].equalsIgnoreCase("Robot_b5")) {
bs = new RobotScenario(5);
} else if (args[0].equalsIgnoreCase("MCI")) {
bs = new MCIScenario(Integer.parseInt(args[3]), Integer.parseInt(args[4]));
} else {
// undefined scenario
}
sim = new Simulator(bs);
sim.setDEBUG();
CheckerInterface checker = null;
if (args[0].startsWith("Robot")) {
checker = new RobotChecker();
// Robot
scenarioType = 1;
} else if (args[1].equalsIgnoreCase("Existence")) {
checker = new Existence();
} else if (args[1].equalsIgnoreCase("Absence")) {
checker = new Absence();
} else if (args[1].equalsIgnoreCase("Universality")) {
checker = new Universality();
} else if (args[1].equalsIgnoreCase("TransientStateProbability")) {
checker = new TransientStateProbability();
} else {
// Undefined Checker
}
checker.init(args);
System.out.println("==========================================\n" + "[ Simulation Description ]\n" + "Parameters: " + params + "\n" + "Scenario: " + sim.getScenario().getDescription() + "\n" + "Checker: " + checker.getName() + "\n" + "Statement: " + ANSI_RED + checker.getDescription() + ANSI_RESET);
System.out.println("==========================================\n" + "[ Simulation Log ]");
long totalstart = System.currentTimeMillis();
long totaltime = 0;
int totalsamples = 0;
String finalres = "True";
for (double alpha_beta : ARR_ALPHA_BETA) {
Date nowDate = new Date();
SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String pre = transFormat.format(nowDate);
checker.init(args);
// 신뢰도 99%
SPRTMethod sprt = new SPRTMethod(alpha_beta, alpha_beta, 0.01);
for (int t = 1; t <= 95; ) {
// theta
double theta = 0.01 * t;
int h0 = 0;
int h1 = 0;
int numThetaSamples = 0;
long exec_ThetaTime = 0;
for (int i = 0; i < 100; ) {
long start = System.currentTimeMillis();
sprt.setExpression(theta);
sprt.reset();
while (!sprt.checkStopCondition()) {
// Initialize Patient map
sim.getScenario().init();
// 매번 다른 distribution 이 필요함
ArrayList<Integer> list = new ArrayList<>();
list.clear();
list = distributor.getDistributionArray(sim.getScenario().getActionList().size());
sim.setActionPlan(list);
// sim.setEndTick(endTick);
sim.execute();
SIMResult res = sim.getResult();
int checkResult = checker.evaluateSample(res);
sprt.updateResult(checkResult);
// System.gc();
sim.reset();
sim.setActionPlan(list);
}
boolean res = sprt.getResult();
if (res) {
h0++;
// System.out.print("T");
} else {
h1++;
// System.out.print("F");
}
int numSamples = sprt.getNumSamples();
// exec time
long exec_time = System.currentTimeMillis() - start;
int minTick = checker.getMinTick();
int maxTick = checker.getMaxTick();
numThetaSamples += numSamples;
exec_ThetaTime += exec_time;
totalsamples += numSamples;
totaltime += exec_time;
// resList.add(new SMCResult(theta, numSamples, exec_time, minTick, maxTick, res));
if (scenarioType == 1)
i++;
else
i += 34;
}
System.out.print("The statement is");
if (h0 >= h1) {
System.out.print(" TRUE\t");
if (theta > Double.parseDouble(args[2]))
finalres = "False";
} else {
System.out.print(" FALSE\t");
if (theta <= Double.parseDouble(args[2]))
finalres = "False";
}
System.out.print("for theta: " + String.format("%.2f", theta));
if (scenarioType == 1)
System.out.print(" by examining " + (numThetaSamples / 100) + " samples\t");
else
System.out.print(" by examining " + (numThetaSamples / 3) + " samples\t");
// if (scenarioType == 1)
System.out.print("(T: " + String.format("%03d", h0) + ", F: " + String.format("%03d", h1) + ")\t");
System.out.println("[Time to Decide: " + String.format("%.2f", exec_ThetaTime / 1000.0) + " secs]");
if (scenarioType == 1)
t++;
else
t++;
}
// String outputName = caseName + "_result/" + pre + caseName + bound + "_" + String.format("%.3f", alpha_beta) + "t" + String.valueOf(trial) + ".csv";
// CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputName), "UTF-8"), ',', '"');
// cw.writeNext(new String[]{"prob", "num_of_samples", "execution_time", "result"});
//
// for (SMCResult r : resList) {
// System.out.print(".");
// cw.writeNext(r.getArr());
// }
// cw.close();
// resList.clear();
System.out.println();
}
System.out.println("==========================================\n" + "[ Simulation Result ]\n" + "Result: The statement <" + checker.getDescription() + "> is " + ANSI_RED + finalres + ANSI_RESET + " with a probability <= " + ANSI_RED + args[2] + ANSI_RESET + ".\n" + "Total Examined Samples: " + totalsamples + " samples\n" + "Total Time to Decide: " + String.format("%.2f", totaltime / 1000.0) + " secs\n" + "Total Elapsed Time: " + String.format("%.2f", (System.currentTimeMillis() - totalstart) / 1000.0) + " secs\n" + "==========================================\n" + "Finished.");
}
Aggregations