• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / learnlib / 6433387082

06 Oct 2023 03:10PM UTC coverage: 92.296% (-0.007%) from 92.303%
6433387082

push

github

mtf90
update Falk's developer id

11573 of 12539 relevant lines covered (92.3%)

1.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.61
/commons/util/src/main/java/de/learnlib/util/Experiment.java
1
/* Copyright (C) 2013-2023 TU Dortmund
2
 * This file is part of LearnLib, http://www.learnlib.de/.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package de.learnlib.util;
17

18
import de.learnlib.api.algorithm.LearningAlgorithm;
19
import de.learnlib.api.logging.LearnLogger;
20
import de.learnlib.api.oracle.EquivalenceOracle;
21
import de.learnlib.api.query.DefaultQuery;
22
import de.learnlib.filter.statistic.Counter;
23
import de.learnlib.util.statistics.SimpleProfiler;
24
import net.automatalib.automata.fsa.DFA;
25
import net.automatalib.automata.transducers.MealyMachine;
26
import net.automatalib.automata.transducers.MooreMachine;
27
import net.automatalib.words.Alphabet;
28
import net.automatalib.words.Word;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30

31
/**
32
 * runs a learning experiment.
33
 *
34
 * @param <A> the automaton type
35
 */
36
public class Experiment<A extends Object> {
37

38
    public static final String LEARNING_PROFILE_KEY = "Learning";
39
    public static final String COUNTEREXAMPLE_PROFILE_KEY = "Searching for counterexample";
40

41
    private static final LearnLogger LOGGER = LearnLogger.getLogger(Experiment.class);
2✔
42
    private final ExperimentImpl<?, ?> impl;
43
    private boolean logModels;
44
    private boolean profile;
45
    private final Counter rounds = new Counter("learning rounds", "#");
2✔
46
    private @Nullable A finalHypothesis;
47

48
    public <I, D> Experiment(LearningAlgorithm<? extends A, I, D> learningAlgorithm,
49
                             EquivalenceOracle<? super A, I, D> equivalenceAlgorithm,
50
                             Alphabet<I> inputs) {
2✔
51
        this.impl = new ExperimentImpl<>(learningAlgorithm, equivalenceAlgorithm, inputs);
2✔
52
    }
2✔
53

54
    public A run() {
55
        if (this.finalHypothesis != null) {
2✔
56
            throw new IllegalStateException("Experiment has already been run");
2✔
57
        }
58

59
        finalHypothesis = impl.run();
2✔
60
        return finalHypothesis;
2✔
61
    }
62

63
    public A getFinalHypothesis() {
64
        if (finalHypothesis == null) {
2✔
65
            throw new IllegalStateException("Experiment has not yet been run");
2✔
66
        }
67

68
        return finalHypothesis;
2✔
69
    }
70

71
    private void profileStart(String taskname) {
72
        if (profile) {
2✔
73
            SimpleProfiler.start(taskname);
2✔
74
        }
75
    }
2✔
76

77
    private void profileStop(String taskname) {
78
        if (profile) {
2✔
79
            SimpleProfiler.stop(taskname);
2✔
80
        }
81
    }
2✔
82

83
    /**
84
     * @param logModels
85
     *         flag whether models should be logged
86
     */
87
    public void setLogModels(boolean logModels) {
88
        this.logModels = logModels;
1✔
89
    }
1✔
90

91
    /**
92
     * @param profile
93
     *         flag whether learning process should be profiled
94
     */
95
    public void setProfile(boolean profile) {
96
        this.profile = profile;
2✔
97
    }
2✔
98

99
    /**
100
     * @return the rounds
101
     */
102
    public Counter getRounds() {
103
        return rounds;
1✔
104
    }
105

106
    private final class ExperimentImpl<I, D> {
2✔
107

108
        private final LearningAlgorithm<? extends A, I, D> learningAlgorithm;
109
        private final EquivalenceOracle<? super A, I, D> equivalenceAlgorithm;
110
        private final Alphabet<I> inputs;
111

112
        ExperimentImpl(LearningAlgorithm<? extends A, I, D> learningAlgorithm,
113
                       EquivalenceOracle<? super A, I, D> equivalenceAlgorithm,
114
                       Alphabet<I> inputs) {
2✔
115
            this.learningAlgorithm = learningAlgorithm;
2✔
116
            this.equivalenceAlgorithm = equivalenceAlgorithm;
2✔
117
            this.inputs = inputs;
2✔
118
        }
2✔
119

120
        public A run() {
121
            rounds.increment();
2✔
122
            LOGGER.logPhase("Starting round " + rounds.getCount());
2✔
123
            LOGGER.logPhase("Learning");
2✔
124

125
            profileStart(LEARNING_PROFILE_KEY);
2✔
126
            learningAlgorithm.startLearning();
2✔
127
            profileStop(LEARNING_PROFILE_KEY);
2✔
128

129
            while (true) {
130
                final A hyp = learningAlgorithm.getHypothesisModel();
2✔
131

132
                if (logModels) {
2✔
133
                    LOGGER.logModel(hyp);
1✔
134
                }
135

136
                LOGGER.logPhase("Searching for counterexample");
2✔
137

138
                profileStart(COUNTEREXAMPLE_PROFILE_KEY);
2✔
139
                DefaultQuery<I, D> ce = equivalenceAlgorithm.findCounterExample(hyp, inputs);
2✔
140
                profileStop(COUNTEREXAMPLE_PROFILE_KEY);
2✔
141

142
                if (ce == null) {
2✔
143
                    return hyp;
2✔
144
                }
145

146
                LOGGER.logCounterexample(ce.getInput().toString());
2✔
147

148
                // next round ...
149
                rounds.increment();
2✔
150
                LOGGER.logPhase("Starting round " + rounds.getCount());
2✔
151
                LOGGER.logPhase("Learning");
2✔
152

153
                profileStart(LEARNING_PROFILE_KEY);
2✔
154
                final boolean refined = learningAlgorithm.refineHypothesis(ce);
2✔
155
                profileStop(LEARNING_PROFILE_KEY);
2✔
156

157
                assert refined;
2✔
158
            }
2✔
159
        }
160
    }
161

162
    public static class DFAExperiment<I> extends Experiment<DFA<?, I>> {
163
        public DFAExperiment(LearningAlgorithm<? extends DFA<?, I>, I, Boolean> learningAlgorithm,
164
                             EquivalenceOracle<? super DFA<?, I>, I, Boolean> equivalenceAlgorithm,
165
                             Alphabet<I> inputs) {
166
            super(learningAlgorithm, equivalenceAlgorithm, inputs);
2✔
167
        }
2✔
168

169
    }
170

171
    public static class MealyExperiment<I, O> extends Experiment<MealyMachine<?, I, ?, O>> {
172

173
        public MealyExperiment(LearningAlgorithm<? extends MealyMachine<?, I, ?, O>, I, Word<O>> learningAlgorithm,
174
                               EquivalenceOracle<? super MealyMachine<?, I, ?, O>, I, Word<O>> equivalenceAlgorithm,
175
                               Alphabet<I> inputs) {
176
            super(learningAlgorithm, equivalenceAlgorithm, inputs);
1✔
177
        }
1✔
178

179
    }
180

181
    public static class MooreExperiment<I, O> extends Experiment<MooreMachine<?, I, ?, O>> {
182

183
        public MooreExperiment(LearningAlgorithm<? extends MooreMachine<?, I, ?, O>, I, Word<O>> learningAlgorithm,
184
                               EquivalenceOracle<? super MooreMachine<?, I, ?, O>, I, Word<O>> equivalenceAlgorithm,
185
                               Alphabet<I> inputs) {
186
            super(learningAlgorithm, equivalenceAlgorithm, inputs);
×
187
        }
×
188

189
    }
190

191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc