Search in sources :

Example 1 with SPRTMethod

use of kr.ac.kaist.se.simulator.method.SPRTMethod in project SIMVA-SoS by SESoS.

the class RobotMain method main.

public static void main(String[] args) throws IOException {
    RobotScenario robot = new RobotScenario(0);
    Simulator sim = new Simulator(robot);
    BaseChecker checker = new BaseChecker();
    checker.init(sim.getScenario().getEndTick(), 3, BaseChecker.comparisonType.EQUAL_TO);
    SPRTMethod sprt = new SPRTMethod(0.05, 0.05, 0.01);
    sprt.setLessCheck();
    for (int j = 0; j < 10; j++) {
        String outputName = "robot_result/SIM_robot_" + (j + 1) + ".csv";
        CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputName), "UTF-8"), ',', '"');
        cw.writeNext(new String[] { "prob", "num_of_samples", "result" });
        ArrayList<RobotResult> resList = new ArrayList<>();
        for (int trial = 1; trial <= 200; trial++) {
            for (int i = 1; i < 100; i++) {
                double theta = 0.01 * i;
                sprt.setExpression(theta);
                while (!sprt.checkStopCondition()) {
                    sim.execute();
                    System.out.println(checker.evaluateSample(sim.getResult()));
                    int result = checker.evaluateSample(sim.getResult());
                    if (result == 1)
                        result = 0;
                    else
                        result = 1;
                    sprt.updateResult(result);
                    sim.reset();
                }
                // Result
                boolean h0 = sprt.getResult();
                int numSamples = sprt.getNumSamples();
                sprt.reset();
                if (h0)
                    System.out.print("T");
                else
                    System.out.print("F");
                if (resList.size() < 99)
                    resList.add(new RobotResult(h0, numSamples, theta));
                else {
                    RobotResult res = resList.get(i - 1);
                    res.updateResult(numSamples);
                    resList.set(i - 1, res);
                }
            }
            System.out.println();
        }
        for (RobotResult r : resList) {
            System.out.print(".");
            cw.writeNext(r.getArr());
        }
        System.out.println();
        cw.close();
    }
}
Also used : SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) ArrayList(java.util.ArrayList) CSVWriter(com.opencsv.CSVWriter) BaseChecker(kr.ac.kaist.se.mc.BaseChecker) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Simulator(kr.ac.kaist.se.simulator.Simulator)

Example 2 with SPRTMethod

use of kr.ac.kaist.se.simulator.method.SPRTMethod in project SIMVA-SoS by SESoS.

the class KIISEMain method main.

public static void main(String[] args) throws IOException {
    Constituent cs1 = new Constituent("CS1", 120);
    Constituent cs2 = new Constituent("CS2", 120);
    Constituent cs3 = new Constituent("CS3", 120);
    Action a1 = new Action("Action1", 2, 1);
    a1.setActionType(Action.TYPE.NORMAL);
    Action a2 = new Action("Action2", 2, 2);
    a2.setActionType(Action.TYPE.NORMAL);
    Action a3 = new Action("Action3", 3, 3);
    a3.setActionType(Action.TYPE.NORMAL);
    cs1.addCapability(a1, 1);
    cs1.addCapability(a2, 2);
    cs2.addCapability(a2, 2);
    cs2.addCapability(a3, 3);
    cs3.addCapability(a1, 1);
    cs3.addCapability(a3, 3);
    Constituent[] CSs = { cs1, cs2, cs3 };
    Action[] actions = { a1, a2, a3 };
    SoS sos = new SoS("SoS Manager", CSs, actions);
    Environment env = new Environment(CSs, actions);
    Simulator sim = new Simulator(CSs, sos, env);
    sim.setEndTick(300);
    int[] boundArr = { 120, 125, 130, 135, 140, 145, 150 };
    for (int bound : boundArr) {
        String outputName = "SIM_" + bound + ".csv";
        CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputName), "UTF-8"), ',', '"');
        cw.writeNext(new String[] { "prob", "num_of_samples", "execution_time", "min_tick", "max_tick", "result" });
        ArrayList<SMCResult> resList = new ArrayList<SMCResult>();
        System.out.println("----------------------------------------------------");
        System.out.println("SoS-level benefit is greater than " + bound + ".");
        BaseChecker checker = new BaseChecker();
        checker.init(10000, bound, BaseChecker.comparisonType.GREATER_THAN_AND_EQUAL_TO);
        SPRTMethod sprt = new SPRTMethod(0.01, 0.01, 0.005);
        for (int i = 1; i < 100; i++) {
            // theta
            double theta = 0.01 * i;
            long start = System.currentTimeMillis();
            sprt.setExpression(theta);
            while (!sprt.checkStopCondition()) {
                sim.execute();
                SIMResult res = sim.getResult();
                int checkResult = checker.evaluateSample(res);
                sprt.updateResult(checkResult);
            }
            // Result
            boolean h0 = sprt.getResult();
            int numSamples = sprt.getNumSamples();
            // System.out.print("SMC decides that your hypothesis is ");
            // if(h0)
            // {
            // System.out.println("accepted at " + theta + " / number of samples: " + numSamples);
            // }
            // else
            // {
            // System.out.println("not accepted at " + theta + " / number of samples: " + numSamples);
            // }
            // exec time
            long exec_time = System.currentTimeMillis() - start;
            int minTick = checker.getMinTick();
            int maxTick = checker.getMaxTick();
            sprt.reset();
            resList.add(new SMCResult(theta, numSamples, exec_time, minTick, maxTick, h0));
            if (h0)
                System.out.print("T");
            else
                System.out.print("F");
        }
        System.out.println();
        System.out.print("w");
        for (SMCResult r : resList) {
            System.out.print(".");
            cw.writeNext(r.getArr());
        }
        cw.close();
        resList.clear();
        System.out.println();
    }
    System.out.println("Finished");
}
Also used : ArrayList(java.util.ArrayList) SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) CSVWriter(com.opencsv.CSVWriter) SIMResult(kr.ac.kaist.se.simulator.SIMResult) BaseChecker(kr.ac.kaist.se.mc.BaseChecker) Environment(kr.ac.kaist.se.simulator.Environment) Simulator(kr.ac.kaist.se.simulator.Simulator)

Example 3 with SPRTMethod

use of kr.ac.kaist.se.simulator.method.SPRTMethod 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)

Example 4 with SPRTMethod

use of kr.ac.kaist.se.simulator.method.SPRTMethod 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.");
}
Also used : MCIScenario(mci.scenario.MCIScenario) SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) CheckerInterface(kr.ac.kaist.se.mc.CheckerInterface) SimpleDateFormat(java.text.SimpleDateFormat) RobotScenario(christian.RobotScenario)

Aggregations

SPRTMethod (kr.ac.kaist.se.simulator.method.SPRTMethod)4 CSVWriter (com.opencsv.CSVWriter)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 BaseChecker (kr.ac.kaist.se.mc.BaseChecker)2 Simulator (kr.ac.kaist.se.simulator.Simulator)2 RobotScenario (christian.RobotScenario)1 CheckerInterface (kr.ac.kaist.se.mc.CheckerInterface)1 Environment (kr.ac.kaist.se.simulator.Environment)1 SIMResult (kr.ac.kaist.se.simulator.SIMResult)1 SMCResult (mci.SMCResult)1 MCIScenario (mci.scenario.MCIScenario)1