completetesting.py 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from testing.basetesting import AbstractTester
import logging
import itertools

# Complete tester.
class CompleteTicTacToeTester(AbstractTester):

    def __init__(self, teacher, logger=None):
        # The tester will run test using the teacher
        self._teacher = teacher
        # upper bound: 9! (possible plays, also invalid ones)
        # according to wikipedia
33 34
        # Reduced to games of length 5 (maximum number of X in a bard)
        self._all_games = set(itertools.permutations(['0','1','2','3','4','5','6','7','8'],5))
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        self._logger = logger or logging.getLogger(__name__)

    # Search a counterexample to teacher ioco model
    def findCounterexample(self, model):
        self._teacher.reset()
        model.reset()
        ce = ()
        i = 0
        for i in self._all_games:
            ce = ()
            # Next game
            self._teacher.reset()
            model.reset()
            # play this game, if we receive END, then go to next game.
            for action in i:
                ce = ce + (action,)
                output = self._processInputs((action,))
                model.move(action)
                if output not in model.outputs():
                    self._logger.info("Found a counterexample: "
55 56
                                      + str(ce) + " output: "+str(output)+
                                      " not in " + str(model.outputs()))
57 58 59 60 61 62

                    self._teacher.reset()
                    model.reset()
                    # return counterexample trace and output obtained by
                    # testing
                    return ce, output
63 64
                #elif 'END' in output:
                #    continue
65 66 67 68 69 70 71 72
                else:
                    model.move(output)
                    ce = ce + (output,)

        return None, None

    def _processInputs(self, consecutiveInputs):
        if consecutiveInputs != ():
73
            output = self._teacher.oneOutputForTesting(consecutiveInputs)
74 75 76 77
            if output == None:
                return None
            return output
        return self._teacher.output()