Search in sources :

Example 1 with SMCResult

use of mci.SMCResult in project SIMVA-SoS by SESoS.

the class Executor method Perform_Experiment.

public static void Perform_Experiment(NormalDistributor distributor, Simulator sim, String caseName, int bound) throws IOException {
    int endTick = sim.getScenario().getEndTick();
    Existence checker = new Existence();
    checker.init(endTick, bound, Existence.comparisonType.GREATER_THAN_AND_EQUAL_TO);
    System.out.println("==========================================\n" + "[ Simulation Description ]\n" + "Scenario: " + sim.getScenario().getDescription() + "\n" + "Checker: " + checker.getName() + "\n" + "Statement: " + checker.getDescription());
    System.out.println("==========================================\n" + "[ Simulation Log ]");
    long totalstart = System.currentTimeMillis();
    long totaltime = 0;
    int totalsamples = 0;
    for (double alpha_beta : ARR_ALPHA_BETA) {
        Date nowDate = new Date();
        SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String pre = transFormat.format(nowDate);
        for (int trial = 1; trial <= 3; trial++) {
            System.out.println("Trial " + trial + " is Started");
            checker.init(endTick, bound, Existence.comparisonType.GREATER_THAN_AND_EQUAL_TO);
            // 신뢰도 99%
            SPRTMethod sprt = new SPRTMethod(alpha_beta, alpha_beta, 0.01);
            ArrayList<SMCResult> resList = new ArrayList<>();
            for (int t = 80; t < 81; t++) {
                // theta
                double theta = 0.01 * t;
                long start = System.currentTimeMillis();
                sprt.setExpression(theta);
                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);
                }
                // Result
                boolean h0 = sprt.getResult();
                int numSamples = sprt.getNumSamples();
                // exec time
                long exec_time = System.currentTimeMillis() - start;
                int minTick = checker.getMinTick();
                int maxTick = checker.getMaxTick();
                totaltime += exec_time;
                totalsamples += numSamples;
                sprt.reset();
                resList.add(new SMCResult(theta, numSamples, exec_time, minTick, maxTick, h0));
                System.out.print("The statement is");
                if (h0) {
                    System.out.print(" TRUE");
                } else {
                    System.out.print(" FALSE");
                }
                System.out.print(" for theta: " + String.format("%.2f", theta));
                System.out.print(" by examining " + numSamples + " samples");
                System.out.println(" [Time to Decide: " + String.format("%.2f", exec_time / 1000.0) + " secs]");
            }
            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("Trial " + trial + " is Finished");
        }
    }
    System.out.println("==========================================\n" + "[ Simulation Result ]\n" + "Result: The statement is --- with a probability --- than --.\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.");
}
Also used : SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) CSVWriter(com.opencsv.CSVWriter) FileOutputStream(java.io.FileOutputStream) SMCResult(mci.SMCResult) OutputStreamWriter(java.io.OutputStreamWriter) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

CSVWriter (com.opencsv.CSVWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 SPRTMethod (kr.ac.kaist.se.simulator.method.SPRTMethod)1 SMCResult (mci.SMCResult)1