From: Mart Lubbers Date: Wed, 20 Jan 2016 11:09:13 +0000 (+0100) Subject: candy learner mooier gemaakt X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=a2e131b76775e9fde987642efb414bca58345c22;p=tt2015.git candy learner mooier gemaakt --- diff --git a/a4/code/src/learner/CandyLearner.java b/a4/code/src/learner/CandyLearner.java index 61be178..0035fd4 100644 --- a/a4/code/src/learner/CandyLearner.java +++ b/a4/code/src/learner/CandyLearner.java @@ -31,193 +31,202 @@ import de.learnlib.statistics.StatisticSUL; public class CandyLearner { - - public static int sutInterface_portNumber=7892; - + + public static int sutInterface_portNumber=7892; + /* - * The Adapter: needed in order to let LearnLib and the sut to communicate - * - */ - public static class SULAdapter implements SUL { - - // system under learning - private SutSocketWrapper sul = new SutSocketWrapper(sutInterface_portNumber); - - @Override - public void pre(){ - System.out.println("hoi"); - } - - @Override - public void post(){ - System.out.println("hoi"); - } - - // // reset the SUL - // @Override - // public void reset() { - // sul.sendReset(); - // } - - // execute one input on the SUL - @Override - public String step(String in) { - String output = sul.sendInput(in); - //System.out.println(in + ":" + output); - return output; - } - } - - public static void main(String[] args) throws NoSuchMethodException, IOException { - - // create alphabet - Alphabet inputs = new SimpleAlphabet<>(); - inputs.add("IREFUND"); - inputs.add("IBUTTONMARS"); - inputs.add("IBUTTONSNICKERS"); - inputs.add("IBUTTONBOUNTY"); - inputs.add("ICOIN10"); - inputs.add("ICOIN5"); - - // Instantiate the sut - SUL sul = new SULAdapter(); - - // oracle for counting queries wraps sul - StatisticSUL statisticSul = new ResetCounterSUL<>("membership queries", sul); - - SUL effectiveSul = statisticSul; - // use caching in order to avoid duplicate queries - effectiveSul = Caches.createSULCache(inputs, effectiveSul); - - SULOracle mqOracle = new SULOracle<>(effectiveSul); - - // create initial set of suffixes - List> suffixes = new ArrayList<>(); - suffixes.add(Word.fromSymbols("IREFUND")); - suffixes.add(Word.fromSymbols("IBUTTONMARS")); - suffixes.add(Word.fromSymbols("IBUTTONSNICKERS")); - suffixes.add(Word.fromSymbols("IBUTTONBOUNTY")); - suffixes.add(Word.fromSymbols("ICOIN10")); - suffixes.add(Word.fromSymbols("ICOIN5")); - - // construct L* instance (almost classic Mealy version) - // almost: we use words (Word) in cells of the table - // instead of single outputs. - MealyLearner lstar = - new ExtensibleLStarMealy<>( - inputs, // input alphabet - mqOracle, // mq oracle - suffixes, // initial suffixes - ObservationTableCEXHandlers.FIND_LINEAR_ALLSUFFIXES, // handling of counterexamples - ClosingStrategies.CLOSE_SHORTEST // choose row for closing the table - ); - - // create random words equivalence test - RandomWordsEQOracle, MealyMachine> randomWords = - new RandomWordsEQOracle, MealyMachine>( - mqOracle, - 3, // int minLength - 8, // int maxLength - 1000, // int maxTests - new Random(46346293) // make results reproducible - ); - - // create complete exploration equivalence test - CompleteExplorationEQOracle> completeOracle = - new CompleteExplorationEQOracle<>( - mqOracle, // a membership oracle - 5, // int minDepth - 7 // int maxDepth - ); - - // create equivalence oracle based on the W method - MealyWMethodEQOracle wOracle= - new MealyWMethodEQOracle<>( - 5, //int maxDepth - mqOracle // a membership oracle - ); - - - // construct a learning experiment from - // the learning algorithm and one of the equivalence oracles. - // The experiment will execute the main loop of - // active learning - MealyExperiment experiment = - new MealyExperiment<>( - lstar, - randomWords, // equivalence oracle, choose among [randomWords | completeOracle | wOracle] **remember to change their settings** - inputs // input alphabet - ); - - // turn off time profiling - experiment.setProfile(true); - - // enable logging of models - experiment.setLogModels(true); - - // run experiment - experiment.run(); - - // get learned model - MealyMachine result = experiment.getFinalHypothesis(); - - // report results - System.out.println("-------------------------------------------------------"); - - // profiling - System.out.println(SimpleProfiler.getResults()); - - // learning statistics - System.out.println(experiment.getRounds().getSummary()); - System.out.println(statisticSul.getStatisticalData().getSummary()); - - // model statistics - System.out.println("States: " + result.size()); - System.out.println("Sigma: " + inputs.size()); - - // show model - System.out.println(); - System.out.println("Model: "); - - GraphDOT.write(result, inputs, System.out); // may throw IOException! -// Writer w = DOT.createDotWriter(true); -// GraphDOT.write(result, inputs, w); -// w.close(); - - String filename = "/path/to/yourfolder/CandyMachine.dot"; - PrintStream writer = new PrintStream( - new FileOutputStream(filename)); - GraphDOT.write(result, inputs, writer); // may throw IOException! - - System.out.println(executeCommand("dot -Tpdf /path/to/yourfolder/CandyMachine.dot -o /path/to/yourfolder/CandyMachine.pdf")); - - - System.out.println("-------------------------------------------------------"); - - } - - // execute command, for translation from dot to pdf - public static String executeCommand(String command) { - + * The Adapter: needed in order to let LearnLib and the sut to communicate + * + */ + public static class SULAdapter implements SUL { + + // system under learning + private SutSocketWrapper sul = new SutSocketWrapper(sutInterface_portNumber); + + @Override + public void pre(){ + System.out.println("hoi"); + } + + @Override + public void post(){ + System.out.println("hoi"); + } + + // // reset the SUL + // @Override + // public void reset() { + // sul.sendReset(); + // } + + // execute one input on the SUL + @Override + public String step(String in) { + String output = sul.sendInput(in); + //System.out.println(in + ":" + output); + return output; + } + } + + public static void main(String[] args) + throws NoSuchMethodException, IOException { + + // create alphabet + Alphabet inputs = new SimpleAlphabet<>(); + inputs.add("IREFUND"); + inputs.add("IBUTTONMARS"); + inputs.add("IBUTTONSNICKERS"); + inputs.add("IBUTTONBOUNTY"); + inputs.add("ICOIN10"); + inputs.add("ICOIN5"); + + // Instantiate the sut + SUL sul = new SULAdapter(); + + // oracle for counting queries wraps sul + StatisticSUL statisticSul = + new ResetCounterSUL<>("membership queries", sul); + + SUL effectiveSul = statisticSul; + // use caching in order to avoid duplicate queries + effectiveSul = Caches.createSULCache(inputs, effectiveSul); + + SULOracle mqOracle = new SULOracle<>(effectiveSul); + + // create initial set of suffixes + List> suffixes = new ArrayList<>(); + suffixes.add(Word.fromSymbols("IREFUND")); + suffixes.add(Word.fromSymbols("IBUTTONMARS")); + suffixes.add(Word.fromSymbols("IBUTTONSNICKERS")); + suffixes.add(Word.fromSymbols("IBUTTONBOUNTY")); + suffixes.add(Word.fromSymbols("ICOIN10")); + suffixes.add(Word.fromSymbols("ICOIN5")); + + // construct L* instance (almost classic Mealy version) + // almost: we use words (Word) in cells of the table + // instead of single outputs. + MealyLearner lstar = + new ExtensibleLStarMealy<>( + inputs, // input alphabet + mqOracle, // mq oracle + suffixes, // initial suffixes + // handling of counterexamples + ObservationTableCEXHandlers.FIND_LINEAR_ALLSUFFIXES, + // choose row for closing the table + ClosingStrategies.CLOSE_SHORTEST + ); + + // create random words equivalence test + RandomWordsEQOracle< + String, Word, MealyMachine> randomWords = + new RandomWordsEQOracle< + String, Word, MealyMachine>( + mqOracle, + 3, // int minLength + 8, // int maxLength + 1000, // int maxTests + new Random(46346293) // make results reproducible + ); + + // create complete exploration equivalence test + CompleteExplorationEQOracle> completeOracle = + new CompleteExplorationEQOracle<>( + mqOracle, // a membership oracle + 5, // int minDepth + 7 // int maxDepth + ); + + // create equivalence oracle based on the W method + MealyWMethodEQOracle wOracle= + new MealyWMethodEQOracle<>( + 5, //int maxDepth + mqOracle // a membership oracle + ); + + + // construct a learning experiment from + // the learning algorithm and one of the equivalence oracles. + // The experiment will execute the main loop of + // active learning + MealyExperiment experiment = + new MealyExperiment<>( + lstar, + // equivalence oracle, choose among + // [randomWords | completeOracle | wOracle] + // **remember to change their settings** + //randomWords, + completeOracle, + inputs // input alphabet + ); + + // turn off time profiling + experiment.setProfile(true); + + // enable logging of models + experiment.setLogModels(true); + + // run experiment + experiment.run(); + + // get learned model + MealyMachine result = + experiment.getFinalHypothesis(); + + // report results + System.out.println("------------------------------------------------"); + + // profiling + System.out.println(SimpleProfiler.getResults()); + + // learning statistics + System.out.println(experiment.getRounds().getSummary()); + System.out.println(statisticSul.getStatisticalData().getSummary()); + + // model statistics + System.out.println("States: " + result.size()); + System.out.println("Sigma: " + inputs.size()); + + // show model + System.out.println(); + System.out.println("Model: "); + + GraphDOT.write(result, inputs, System.out); // may throw IOException! + //Writer w = DOT.createDotWriter(true); + //GraphDOT.write(result, inputs, w); + //w.close(); + + String filename = "CandyMachine.dot"; + PrintStream writer = new PrintStream( + new FileOutputStream(filename)); + GraphDOT.write(result, inputs, writer); // may throw IOException! + + System.out.println(executeCommand( + "dot -Tpdf /path/to/yourfolder/CandyMachine.dot " + + "-o /path/to/yourfolder/CandyMachine.pdf")); + + System.out.println("------------------------------------------------"); + } + + // execute command, for translation from dot to pdf + public static String executeCommand(String command) { StringBuffer output = new StringBuffer(); - + Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = - new BufferedReader(new InputStreamReader(p.getInputStream())); - - String line = ""; + new BufferedReader(new InputStreamReader(p.getInputStream())); + + String line = ""; while ((line = reader.readLine())!= null) { - output.append(line + "\n"); + output.append(line + "\n"); } - + } catch (Exception e) { e.printStackTrace(); } - + return output.toString(); - } }