Search in sources :

Example 1 with BaseChecker

use of kr.ac.kaist.se.mc.BaseChecker 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 BaseChecker

use of kr.ac.kaist.se.mc.BaseChecker 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)

Aggregations

CSVWriter (com.opencsv.CSVWriter)2 ArrayList (java.util.ArrayList)2 BaseChecker (kr.ac.kaist.se.mc.BaseChecker)2 Simulator (kr.ac.kaist.se.simulator.Simulator)2 SPRTMethod (kr.ac.kaist.se.simulator.method.SPRTMethod)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Environment (kr.ac.kaist.se.simulator.Environment)1 SIMResult (kr.ac.kaist.se.simulator.SIMResult)1