コード例 #1
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
 def test_neighboor_c4(self):
     y0 = (1, 1, 0)
     y1 = (1, 0, 0)
     y2 = (0, 1, 0)
     matrix = (y0, y1, y2,)
     moore = Moore(matrix, 2, 0)
     self.assertEqual(moore.neighboor(), 2)
コード例 #2
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
 def test_neighboor_c3(self):
     y0 = (1, 1, 0, 1, 0)
     y1 = (1, 0, 0, 1, 1)
     y2 = (0, 1, 0, 0, 0)
     y3 = (1, 0, 0, 1, 1)
     y4 = (1, 0, 0, 1, 0)
     matrix = (y0, y1, y2, y3, y4,)
     moore = Moore(matrix, 4, 0)
     self.assertEqual(moore.neighboor(), 1)
コード例 #3
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
 def test_neighboor_1(self):
     y0 = (1, 1, 1, 1, 1)
     y1 = (1, 1, 1, 1, 1)
     y2 = (1, 1, 1, 1, 1)
     y3 = (1, 1, 1, 1, 1)
     y4 = (1, 1, 1, 1, 1)
     matrix = (y0, y1, y2, y3, y4,)
     moore = Moore(matrix, 1, 2)
     self.assertEqual(moore.neighboor(), 8)
コード例 #4
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
 def test_neighboor_c5(self):
     y0 = (1, 0, 1, 0, 1)
     y1 = (0, 1, 0, 1, 0)
     y2 = (1, 0, 1, 0, 1)
     y3 = (0, 1, 0, 1, 0)
     y4 = (1, 0, 1, 0, 1)
     y5 = (0, 1, 0, 1, 0)
     matrix = (y0, y1, y2, y3, y4, y5,)
     moore = Moore(matrix, 5, 4)
     self.assertEqual(moore.neighboor(), 2)
コード例 #5
0
ファイル: test_moore.py プロジェクト: netsamir/Checkio-python
 def test_neighboor_c4(self):
     y0 = (1, 1, 0)
     y1 = (1, 0, 0)
     y2 = (0, 1, 0)
     matrix = (
         y0,
         y1,
         y2,
     )
     moore = Moore(matrix, 2, 0)
     self.assertEqual(moore.neighboor(), 2)
コード例 #6
0
ファイル: test_moore.py プロジェクト: netsamir/Checkio-python
 def test_neighboor_c1(self):
     y0 = (1, 1, 0, 1, 0)
     y1 = (1, 0, 0, 1, 1)
     y2 = (0, 1, 0, 0, 0)
     y3 = (1, 0, 0, 1, 1)
     y4 = (1, 0, 0, 1, 0)
     matrix = (
         y0,
         y1,
         y2,
         y3,
         y4,
     )
     moore = Moore(matrix, 4, 4)
     self.assertEqual(moore.neighboor(), 3)
コード例 #7
0
ファイル: test_moore.py プロジェクト: netsamir/Checkio-python
 def test_neighboor_1(self):
     y0 = (1, 1, 1, 1, 1)
     y1 = (1, 1, 1, 1, 1)
     y2 = (1, 1, 1, 1, 1)
     y3 = (1, 1, 1, 1, 1)
     y4 = (1, 1, 1, 1, 1)
     matrix = (
         y0,
         y1,
         y2,
         y3,
         y4,
     )
     moore = Moore(matrix, 1, 2)
     self.assertEqual(moore.neighboor(), 8)
コード例 #8
0
ファイル: test_moore.py プロジェクト: netsamir/Checkio-python
 def setUp(self):
     # input for the function
     y0 = (0, 0, 1, 0, 0)
     y1 = (0, 1, 0, 0, 0)
     y2 = (0, 1, 1, 0, 0)
     y3 = (0, 1, 0, 0, 1)
     y4 = (0, 0, 1, 0, 1)
     matrix = (
         y0,
         y1,
         y2,
         y3,
         y4,
     )
     self.moore = Moore(matrix, 1, 2)
コード例 #9
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
 def setUp(self):
     # input for the function
     y0 = (0, 0, 1, 0, 0)
     y1 = (0, 1, 0, 0, 0)
     y2 = (0, 1, 1, 0, 0)
     y3 = (0, 1, 0, 0, 1)
     y4 = (0, 0, 1, 0, 1)
     matrix = (y0, y1, y2, y3, y4,)
     self.moore = Moore(matrix, 1, 2)
コード例 #10
0
ファイル: test_moore.py プロジェクト: netsamir/python_study
class TestMoore(TestCase):
    def setUp(self):
        # input for the function
        y0 = (0, 0, 1, 0, 0)
        y1 = (0, 1, 0, 0, 0)
        y2 = (0, 1, 1, 0, 0)
        y3 = (0, 1, 0, 0, 1)
        y4 = (0, 0, 1, 0, 1)
        matrix = (y0, y1, y2, y3, y4,)
        self.moore = Moore(matrix, 1, 2)

    def test_chg2list(self):
        # intermediary result
        rine1 = [0, 0, 1, 0, 0]
        rine2 = [0, 1, 0, 0, 0]
        rine3 = [0, 1, 1, 0, 0]
        rine4 = [0, 1, 0, 0, 1]
        rine5 = [0, 0, 1, 0, 1]
        ratrix = [rine1, rine2, rine3, rine4, rine5]
        self.assertEqual(self.moore.matrix, ratrix)

    def test_fndcoor(self):
        coor = (1, 2)
        self.assertEqual(self.moore.coor, coor)

    def test_neighboor(self):
        self.assertEqual(self.moore.neighboor(), 4)

    def test_neighboor_0(self):
        y0 = (0, 0, 0, 0, 0)
        y1 = (0, 0, 0, 0, 0)
        y2 = (0, 0, 0, 0, 0)
        y3 = (0, 0, 0, 0, 0)
        y4 = (0, 0, 0, 0, 0)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 1, 2)
        self.assertEqual(moore.neighboor(), 0)

    def test_neighboor_1(self):
        y0 = (1, 1, 1, 1, 1)
        y1 = (1, 1, 1, 1, 1)
        y2 = (1, 1, 1, 1, 1)
        y3 = (1, 1, 1, 1, 1)
        y4 = (1, 1, 1, 1, 1)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 1, 2)
        self.assertEqual(moore.neighboor(), 8)

    def test_neighboor_c(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 0, 0)
        self.assertEqual(moore.neighboor(), 2)

    def test_neighboor_c1(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 4, 4)
        self.assertEqual(moore.neighboor(), 3)

    def test_neighboor_c2(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 0, 4)
        self.assertEqual(moore.neighboor(), 3)

    def test_neighboor_c3(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (y0, y1, y2, y3, y4,)
        moore = Moore(matrix, 4, 0)
        self.assertEqual(moore.neighboor(), 1)

    def test_neighboor_c4(self):
        y0 = (1, 1, 0)
        y1 = (1, 0, 0)
        y2 = (0, 1, 0)
        matrix = (y0, y1, y2,)
        moore = Moore(matrix, 2, 0)
        self.assertEqual(moore.neighboor(), 2)

    def test_neighboor_c5(self):
        y0 = (1, 0, 1, 0, 1)
        y1 = (0, 1, 0, 1, 0)
        y2 = (1, 0, 1, 0, 1)
        y3 = (0, 1, 0, 1, 0)
        y4 = (1, 0, 1, 0, 1)
        y5 = (0, 1, 0, 1, 0)
        matrix = (y0, y1, y2, y3, y4, y5,)
        moore = Moore(matrix, 5, 4)
        self.assertEqual(moore.neighboor(), 2)
コード例 #11
0
ファイル: test_moore.py プロジェクト: netsamir/Checkio-python
class TestMoore(TestCase):
    def setUp(self):
        # input for the function
        y0 = (0, 0, 1, 0, 0)
        y1 = (0, 1, 0, 0, 0)
        y2 = (0, 1, 1, 0, 0)
        y3 = (0, 1, 0, 0, 1)
        y4 = (0, 0, 1, 0, 1)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        self.moore = Moore(matrix, 1, 2)

    def test_chg2list(self):
        # intermediary result
        rine1 = [0, 0, 1, 0, 0]
        rine2 = [0, 1, 0, 0, 0]
        rine3 = [0, 1, 1, 0, 0]
        rine4 = [0, 1, 0, 0, 1]
        rine5 = [0, 0, 1, 0, 1]
        ratrix = [rine1, rine2, rine3, rine4, rine5]
        self.assertEqual(self.moore.matrix, ratrix)

    def test_fndcoor(self):
        coor = (1, 2)
        self.assertEqual(self.moore.coor, coor)

    def test_neighboor(self):
        self.assertEqual(self.moore.neighboor(), 4)

    def test_neighboor_0(self):
        y0 = (0, 0, 0, 0, 0)
        y1 = (0, 0, 0, 0, 0)
        y2 = (0, 0, 0, 0, 0)
        y3 = (0, 0, 0, 0, 0)
        y4 = (0, 0, 0, 0, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 1, 2)
        self.assertEqual(moore.neighboor(), 0)

    def test_neighboor_1(self):
        y0 = (1, 1, 1, 1, 1)
        y1 = (1, 1, 1, 1, 1)
        y2 = (1, 1, 1, 1, 1)
        y3 = (1, 1, 1, 1, 1)
        y4 = (1, 1, 1, 1, 1)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 1, 2)
        self.assertEqual(moore.neighboor(), 8)

    def test_neighboor_c(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 0, 0)
        self.assertEqual(moore.neighboor(), 2)

    def test_neighboor_c1(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 4, 4)
        self.assertEqual(moore.neighboor(), 3)

    def test_neighboor_c2(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 0, 4)
        self.assertEqual(moore.neighboor(), 3)

    def test_neighboor_c3(self):
        y0 = (1, 1, 0, 1, 0)
        y1 = (1, 0, 0, 1, 1)
        y2 = (0, 1, 0, 0, 0)
        y3 = (1, 0, 0, 1, 1)
        y4 = (1, 0, 0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
        )
        moore = Moore(matrix, 4, 0)
        self.assertEqual(moore.neighboor(), 1)

    def test_neighboor_c4(self):
        y0 = (1, 1, 0)
        y1 = (1, 0, 0)
        y2 = (0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
        )
        moore = Moore(matrix, 2, 0)
        self.assertEqual(moore.neighboor(), 2)

    def test_neighboor_c5(self):
        y0 = (1, 0, 1, 0, 1)
        y1 = (0, 1, 0, 1, 0)
        y2 = (1, 0, 1, 0, 1)
        y3 = (0, 1, 0, 1, 0)
        y4 = (1, 0, 1, 0, 1)
        y5 = (0, 1, 0, 1, 0)
        matrix = (
            y0,
            y1,
            y2,
            y3,
            y4,
            y5,
        )
        moore = Moore(matrix, 5, 4)
        self.assertEqual(moore.neighboor(), 2)
コード例 #12
0
ファイル: gen_moore.py プロジェクト: ggiorikas/FSM-learning
def makeRandomMoore(rseed, sCount, aSize, oSize):

    if not SILENT:
        if sCount < oSize:
            print OPT_NL + 'state count less than output alphabet size -- truncating the latter...'

    oSize = min(oSize, sCount)

    random.seed(rseed)

    inAlphabet = [str(i) for i in range(aSize)]
    outAlphabet = [str(i) for i in range(oSize)]

    #print inAlphabet
    #print outAlphabet

    if not SILENT:
        print OPT_NL + 'generating states...'

    states = ['q_%s' % i for i in range(sCount)]

    #print states

    init = states[0]

    #print init

    if not SILENT:
        print OPT_NL + 'assigning outputs...'

    gDict = {}

    for q in states:
        gDict[q] = outAlphabet[random.randint(0, oSize - 1)]

    shuffledStateIds = range(0, sCount)
    random.shuffle(shuffledStateIds)
    for (qid, o) in zip(shuffledStateIds, outAlphabet):
        gDict['q_%s' % qid] = o

    if not SILENT:
        print OPT_NL + 'adding transitions...'

    dDict = {}

    for q in states:
        for a in inAlphabet:
            dDict[(q, a)] = states[random.randint(0, sCount - 1)]

    stateIdChain = range(1, sCount)
    random.shuffle(stateIdChain)
    stateIdChain = [0] + stateIdChain + [0]

    for i in range(len(stateIdChain) - 1):
        a = inAlphabet[random.randint(0, aSize - 1)]
        q1 = 'q_%s' % stateIdChain[i]
        q2 = 'q_%s' % stateIdChain[i + 1]
        dDict[(q1, a)] = q2
        #print '(%s, %s) -> %s' % (q1, a, q2)

    ret = Moore(inAlphabet, outAlphabet, states, init, dDict, gDict)

    #assert isMinimal(ret)

    return ret
コード例 #13
0
def main():
    
    
    '''
    
    args: machine-id traces-id cs/tree/fixlen
    
    if cs:
        cspc
    if tree:
        total length            
    if fixlen:
        word count
        word len
    
    '''
        
    def validator(parsedOpts, optParams):

        subCommandCount = sum(1 for e in [parsedOpts.cs, parsedOpts.tree, parsedOpts.fixlen] if e)

        if subCommandCount != 1:

            return 'error: exactly one of cs, tree, fixlen must be selected'        
        
        if parsedOpts.cs or parsedOpts.tree:
            
            setattr(parsedOpts, 'train_or_test', 'train')            
                        
            if parsedOpts.size != None:
                return 'error: -sz/--size option can only be used with fixlen'
            
            if parsedOpts.word_length != None:
                return 'error: -wlen/--word-length option can only be used with fixlen'
            
            if parsedOpts.cs:
                
                setattr(parsedOpts, 'cs_or_random', 'cs')
                
                if parsedOpts.total_length != None:
                    return 'error: -tlen/--total-length option can only be used with tree'
                
                if parsedOpts.random_seed != None:
                    return 'error: -rs/--random-seed option cannot be used with cs'
                
                for opt in optParams['options']:
                    if opt[optParams['optionFormat'].index('name')] == '--random-seed':
                        opt[optParams['optionFormat'].index('required')] = 0
                
            else:
                
                setattr(parsedOpts, 'cs_or_random', 'random')

                if parsedOpts.cs_percentage != None:
                    return 'error: -cspc/--cs-percentage option can only be used with cs'
                
                for opt in optParams['options']:
                    if opt[optParams['optionFormat'].index('name')] == '--total-length':
                        opt[optParams['optionFormat'].index('required')] = 1
            
        else:
            
            setattr(parsedOpts, 'train_or_test', 'test')
                        
            if parsedOpts.total_length != None:
                return 'error: -tlen/--total-length option can only be used with tree'

            if parsedOpts.cs_percentage != None:
                return 'error: -cspc/--cs-percentage option can only be used with cs'
            
            for opt in optParams['options']:
                if opt[optParams['optionFormat'].index('name')] in ['--size', '--word-length']:
                    opt[optParams['optionFormat'].index('required')] = 1
            
        
        
    
    optionParameters = {
        
        'description' : 'generates input-output traces from a given Moore machine',
        
        'optionFormat' : ['name', 'aliases', 'value', 'description', 'required'],
        
        'options' : [
            ['--help', ['-h'], None, 'displays this message and exits', 0],
            
            ['--silent', ['-s'], None, 'run silently (do not print anything)', 0],

            ['--machine-id', ['-mid'], 
            {'type' : str, 'isValid' : lambda _ : True, 'errMsg' : ''},
            'path of the machine that will generate the traces', 1],
            
            ['--traces-id', ['-tid'], 
            {'type' : str, 'isValid' : lambda _ : True, 'errMsg' : ''},
            'file where the generated traces will be stored', 1],
            
            ['--random-seed', ['-rs'], 
            {'type' : int, 'isValid' : lambda _ : True, 
             'errMsg' : 'error: -rs/--random-seed must be an integer'},
            'random seed that will be used to generate the traces (use with tree / fixlen)', 1],
                        
            ['cs', [], None, 'generates characteristic sample', 0],

            ['--cs-percentage', ['-cspc'], 
            {'type' : int, 'isValid' : (lambda x : 1 <= x <= 100), 'errMsg' : ''},
            'percentage of the characteristic sample to be generated (use with cs)', 0],
            
            ['tree', [], None, 'generates random tree of words', 0],
            
            ['--total-length', ['-tlen'], 
            {'type' : int, 'isValid' : lambda n : n > 0, 
             'errMsg' : 'error: -tlen/--total-length must be a positive integer'},
            'total length of generated input traces in letters (use with tree)', 0],            
            
            ['fixlen', [], None, 'generates words of fixed length', 0],
            
            ['--size', ['-sz'], 
            {'type' : int, 'isValid' : lambda n : n > 0, 
             'errMsg' : 'error: -sz/--size must be a positive integer'},
            'input-output pair count in generated traces (use with fixlen)', 0],
            
            ['--word-length', ['-wlen'], 
            {'type' : int, 'isValid' : lambda n : n > 0, 
             'errMsg' : 'error: -wlen/--word-length must be a positive integer'},
            'input word length in generated traces (use with fixlen)', 0],
            
        ],

        'usageExamples' : '''
examples:

  # generate a characteristic sample of the (minimal) machine
  # stored in the 'moore1' file inside the 'fsm' folder and
  # save it in the 'cs1' file inside the 'tr' folder

  python gen_traces.py -mid fsm/moore1 -tid tr/cs1 cs
  
  
  # same as above but instead of a characteristic sample we generate a
  # tree of words of total input length 1337, using a random seed of 42

  python gen_traces.py -mid fsm/moore1 -tid tr/tree1 tree -rs 42 -tlen 1337
  
  
  # generate a set of 1337 input-output pairs, 
  # with each input word consisting of 128 letters
  
  python gen_traces.py -mid fsm/moore1 -tid tr/fixlen1 fixlen -rs 42 -sz 1337 -wlen 128
''',

        'preprocessors' : [
            cmd_parse.genericHelpHandler,
            validator
        ],
        
        'postprocessors' : [
            
        ]
    }
    
    args = sys.argv[1:]
    
    if not args:
        args = ['-h']
        
    args = cmd_parse.parse(args, optionParameters)
    
    if args == None:
        exit()
    
    global SILENT
    
    if args.silent:
        SILENT = True
    else:
        SILENT = False
    
    #print args

    # load moore machine

    if not SILENT:
        print OPT_NL + 'loading Moore machine from...'
        print getPath(args.machine_id)
    
    moore = Moore([], [1], [], 'q0', {}, {})
    #moore = moore.loadFromFile('out/%s' % args.machine_id)
    moore = moore.loadFromFile('%s' % args.machine_id)
    
    if args.train_or_test == 'train':
        
        config = ({'characteristic_sample' : True, 'cs_percentage' : args.cs_percentage} if args.cs_or_random == 'cs' 
                  else {'total_length' : args.total_length, 
                        'random_seed' : args.random_seed})
        
        if not SILENT: print OPT_NL + 'generating traces...'
        trainSample = generateTrainingSample(moore, config)
                
        #'''
        cs, csParts = generateCS(moore, 100)
        print OPT_NL + 'generated sample contains %s %% of CS(%s)' % (round(100 * sampleContainsSample(trainSample, cs), 2), len(cs))
        print OPT_NL + 'generated sample contains %s %% %s %% %s %% of shpf(%s), kernel(%s), cond2stuff(%s)' % tuple(
            [round(100 * e, 2) for e in sampleContainsCSParts(trainSample, *csParts)] + [len(e) for e in csParts])
        print OPT_NL + 'generated sample contains %s %% %s %% of states(%s), transitions(%s)' % tuple(
            [round(100 * e, 2) for e in sampleContainsStatesTransitions(trainSample, moore)] + [len(moore.Q), len(moore.dDict)])
        #'''
        
        if not SILENT: 
            print OPT_NL + 'storing %s traces to...' % len(trainSample)
            print getPath(args.traces_id)
        #saveMooreSample('out/%s' % args.traces_id, trainSample)
        #from pprint import pprint
        #pprint(trainSample)
        saveMooreSample('%s' % args.traces_id, trainSample)

        #saveMooreSampleAsJson('out/%s.json' % args.traces_id, trainSample)

    else:
        
        if not SILENT: print OPT_NL + 'generating traces...'
        testSample = generateTestingSample(moore, {'size' : args.size, 
                                  'word_length' : args.word_length,
                                  'random_seed' : args.random_seed})
        
        #'''
        cs, csParts = generateCS(moore, 100)
        print OPT_NL + 'generated sample contains %s %% of CS(%s)' % (round(100 * sampleContainsSample(testSample, cs), 2), len(cs))
        print OPT_NL + 'generated sample contains %s %% %s %% %s %% of shpf(%s), kernel(%s), cond2stuff(%s)' % tuple(
            [round(100 * e, 2) for e in sampleContainsCSParts(testSample, *csParts)] + [len(e) for e in csParts])
        print OPT_NL + 'generated sample contains %s %% %s %% of states(%s), transitions(%s)' % tuple(
            [round(100 * e, 2) for e in sampleContainsStatesTransitions(testSample, moore)] + [len(moore.Q), len(moore.dDict)])
        #'''
        
        if not SILENT: 
            print OPT_NL + 'storing %s traces to...' % len(testSample)
            print getPath(args.traces_id)
        #saveMooreSample('out/%s' % args.traces_id, testSample)
        saveMooreSample('%s' % args.traces_id, testSample)

        #saveMooreSampleAsJson('out/%s.json' % args.traces_id, testSample)

    if not SILENT: print OPT_NL + 'done!'