def testFSA(self): ''' Used to generate an fsa and display transition graph ''' fsaStates = self.gameStates.keys() #print fsaStates fsaAlphabet = list(self.gameAlphabet) fsaInitialState = fsaStates[0] fsaFinalStates = fsaStates[-1] if not (fsaInitialState in fsaStates) or not (fsaFinalStates in fsaStates): sys.stderr.write("Initial of final state not in state space") sys.stderr.write("Exiting function") sys.exit() #fsaInitialState = self.gameStates.keys()[4] #fsaFinalStates = self.gameStates.keys()[7] fsaTransitions = self.transitionTableGenerator() myFsa = FSA(fsaStates, fsaAlphabet, fsaTransitions, fsaInitialState, [fsaFinalStates]) myFsa.view() return fsaStates, fsaAlphabet, fsaTransitions
def testFSA(self): ''' Generate inputs required for Fsa ''' fsaStates = list(self.states.keys()) fsaAlphabet = self.alphabetList fsaInitialStates = self.initialStates fsaFinalStates = self.goalStates fsaTransitions = self.transitionTableGenerator() self.fsa = FSA(fsaStates, fsaAlphabet, fsaTransitions, fsaInitialStates[0], fsaFinalStates) #FSA processing self.fsa.view() return
def testFSA(self): ''' Used to generate an fsa and display transition graph ''' fsaStates = self.gameStates.keys() #print fsaStates fsaAlphabet = list(self.gameAlphabet) fsaInitialState = fsaStates[0] fsaFinalStates = fsaStates[-1] if not(fsaInitialState in fsaStates) or not(fsaFinalStates in fsaStates): sys.stderr.write("Initial of final state not in state space") sys.stderr.write("Exiting function") sys.exit() #fsaInitialState = self.gameStates.keys()[4] #fsaFinalStates = self.gameStates.keys()[7] fsaTransitions = self.transitionTableGenerator() myFsa = FSA(fsaStates, fsaAlphabet, fsaTransitions, fsaInitialState, [fsaFinalStates]) myFsa.view() return fsaStates, fsaAlphabet, fsaTransitions
class fsmIndiv(object): ''' Agent class that contains the semi-automata for an individual agent ''' def __init__(self, arenaDimensions=[2, 2], labelFunction=cpsFsmLabelFunctions.robotLabelFuction, initialPositions=[[0, 0]], goalPositions=[[1, 1]], agentName='robot', agentAlphabetInput=[['nn', 1, 5], ['ss', 1, 5], ['ee', 1, 5], ['ww', 1, 5], ['oo', 0, 1], ['ne', 1, 5], ['nw', 1, 5], ['se', 1, 5], ['sw', 1, 5]], drawTransitionGraph=False, grammarType='SL_1', agentType='KNOWN', defaultAction=None): ''' Constructor ''' self.arenaDimensions = arenaDimensions #Grid dimensions self.labelFunction = labelFunction #Labeling function self.totalStates = np.prod(arenaDimensions) #Total number of states self.maxTotalStates = 10000 #Maximum allowed states self.agentType = agentType #KNOWN or UNKNOWN dynamics # Checking if state size is too big # print "Number of states: ", arenaDimensions,"Total",self.totalStates if self.totalStates > self.maxTotalStates: sys.stderr.write("State space greater than %d" % self.totalStates) sys.stderr.write("Exiting function") sys.exit() self.goalStates = [] #List of goal states self.initialStates = [] #List of initial states self.agentName = agentName #Name of agent self.actionObj = fsmActions( arenaDimensions) #Generate related action class objects self.grammarObj = fsmGrammar( grammarType) #Generate related grammar object self.alphabetList = self.actionObj.generateActionList( agentAlphabetInput) #List of alphabets self.states = {} #List of states self.generateStates() #Initialize list of states self.setInitialFinalStates( initialPositions, goalPositions) #Setting initial and final states # Set default action if not given as input if not defaultAction: defaultAction = list(self.alphabetList)[0] self.agentWord = [] # initializing grammar of unknown agent if agentType == 'UNKNOWN' and len(self.alphabetList) < 1: raise "Unknown agent should be initialized with atleast 1 action" elif agentType == 'UNKNOWN': self.grammarObj.computeDefaultGrammar(defaultAction, self.alphabetList) self.addToAgentWord(defaultAction, init=True) if drawTransitionGraph: self.testFSA() self.prevGrammar = None return def addToAgentWord(self, inAct, init=False): '''Add characters/actions to the agent word or action history''' # Do not update agent word during initialization if not init: self.agentWord.append(inAct) # If unknown dynamics then update grammar if self.agentType == 'UNKNOWN': self.grammarObj.addMove(inAct) return def generateStates(self): ''' Generate states of Agent Semi-automata ''' for i in range(self.totalStates): agentPos = "".join( map(str, list(np.unravel_index(i, self.arenaDimensions)))) userID = [self.agentName] + [agentPos] machineID = self.generateMachineID(userID) self.states[machineID] = fsmState(userID, machineID, self.arenaDimensions) return def generateMachineID(self, stateUserID): ''' Generate a unique machineID string for each state ''' return stateUserID[0] + '_' + "".join(map(str, stateUserID[1:])) def setInitialFinalStates(self, initialPositions, goalPositions): ''' Assign labels to states initially and set goal states initially ''' for key in self.states.keys(): #Get name of proposition 'occupied' #propName = self.propositionNames[0] agentPosition = self.states[key].userID[1:] #Assign 'occupied' to occupied cell #self.states[key].assignStatePropositionGrid(propName, agentPosition) if agentPosition in goalPositions: self.states[key].setFinal() self.goalStates.append(key) if agentPosition in initialPositions: self.states[key].setInitial() self.initialStates.append(key) return def generateStateLabels(self, machineID, labelInit={}): return self.labelFunction(self, machineID, labelInit) def testFSA(self): ''' Generate inputs required for Fsa ''' fsaStates = list(self.states.keys()) fsaAlphabet = self.alphabetList fsaInitialStates = self.initialStates fsaFinalStates = self.goalStates fsaTransitions = self.transitionTableGenerator() self.fsa = FSA(fsaStates, fsaAlphabet, fsaTransitions, fsaInitialStates[0], fsaFinalStates) #FSA processing self.fsa.view() return def transitionTableGenerator(self): ''' Generate all transitions from states ''' fsaTransitions = [] for key in self.states.keys(): for i in self.alphabetList: currTransition = self.transitionGenerator(key, i) if currTransition != None: fsaTransitions.append(currTransition) return fsaTransitions def transitionGenerator(self, inStateID, actionID): ''' Generate transition for a specific state and action ''' outPos = self.actionObj.actionResult(actionID, self.states[inStateID].userID) if outPos != None: outState = [self.states[inStateID].userID[0]] + outPos outStateMachineID = self.generateMachineID(outState) return [inStateID, outStateMachineID, actionID] else: return None
from FSA import FSA # Define the dataset that makes up the sheeptalk input table sheeptalk_data = np.array((["q1", None, None], [None, "q2", None], [None, "q3", None], [None, "q3", "q4"], [None, None, None])) # Put data into a pandas df with row and column labels sheeptalk_table = pd.DataFrame(data=sheeptalk_data, index=["q0", "q1", "q2", "q3", "q4"], columns=["b", "a", "!"]) # Initialize the sheeptalk FSA and define states sheeptalk_FSA = FSA() sheeptalk_FSA.add_state("q0") sheeptalk_FSA.add_state("q1") sheeptalk_FSA.add_state("q2") sheeptalk_FSA.add_state("q3") sheeptalk_FSA.add_state("q4", final_state=1) sheeptalk_FSA.start_state = "q0" def main(): ''' Prompt the user for a string and then determine whether or not it is an example of sheeptalk. ''' print("Is it sheeptalk?")
# You also need to specify an initial and final state for this. # To see how to specify the states see the comments on how to generate # state labels mentioned above. To avoid accidentally trying to # draw a transition graph visualization with too many states, this # section gets disabled if the number of states is greater than 50. ################################################################### # Checking if number of states is less than 50 if len(gameStates)<=50: # Setting initial and final states gameInitialState = 'R00E11TR' gameFinalStates = 'R11E00TR' # Verifying if the initial state and final state of the transition system # is included in the statespace if not(gameInitialState in gameStates) or not(gameFinalStates in gameStates): sys.stderr.write("Initial of final state not in state space\n") sys.stderr.write("Exiting function") sys.exit() #Drawing transition graph myFsa = FSA(gameStates, gameAlphabet, gameTransitions, gameInitialState, [gameFinalStates]) myFsa.view() else: print "Number of states: ", len(gameStates) print "Too many states, visualization of transition graph disabled" ###### End of draw transition graph code #####################
# You also need to specify an initial and final state for this. # To see how to specify the states see the comments on how to generate # state labels mentioned above. To avoid accidentally trying to # draw a transition graph visualization with too many states, this # section gets disabled if the number of states is greater than 50. ################################################################### # Checking if number of states is less than 50 if len(gameStates) <= 50: # Setting initial and final states gameInitialState = 'R00E11TR' gameFinalStates = 'R11E00TR' # Verifying if the initial state and final state of the transition system # is included in the statespace if not (gameInitialState in gameStates) or not (gameFinalStates in gameStates): sys.stderr.write("Initial of final state not in state space\n") sys.stderr.write("Exiting function") sys.exit() #Drawing transition graph myFsa = FSA(gameStates, gameAlphabet, gameTransitions, gameInitialState, [gameFinalStates]) myFsa.view() else: print "Number of states: ", len(gameStates) print "Too many states, visualization of transition graph disabled" ###### End of draw transition graph code #####################