def makeSim(hallwayColors, legalInputs, obsNoise, dynamics, transNoise, title='sim', initialDist=None): """ Make an instance of the simulator with noisy motion and sensing models. @param hallwayColors: A list of the colors of the rooms in the hallway, from left to right. @param legalInputs: A list of the possible action commands @param obsNoise: conditional distribution specifying the probability of observing a color given the actual color of the room @param dynamics: function that takes the robot's current location, action, and hallwaylength, and returns its nominal new location @param transNoise: P(actualResultingLocation | nominalResultingLoc) represented as a function from ideal location to the actual location the robot will end up in @param title: String specifying title for simulator window """ n = len(hallwayColors) if not initialDist: initialDist = dist.UniformDist(range(n)) worldSM = ssm.StochasticSM(initialDist, makeTransitionModel(dynamics, transNoise, n), makeObservationModel(hallwayColors, obsNoise)) return makeSESwithGUI(worldSM, hallwayColors, legalInputs, verbose=True, title=title, initBelief=initialDist)
def transitionGivenState(s): # A uniform distribution we mix in to handle teleportation transUniform = dist.UniformDist(range(numStates)) return dist.MixtureDist(dist.triangleDist(\ util.clip(s+a, 0, numStates-1), transDiscTriangleWidth, 0, numStates-1), transUniform, 1 - teleportProb)
def hallSE(hallwayColors, legalInputs, obsNoise, dynamics, transNoise, initialDist=None, verbose=True): n = len(hallwayColors) if not initialDist: initialDist = dist.UniformDist(range(n)) worldSM = ssm.StochasticSM(initialDist, makeTransitionModel(dynamics, transNoise, n), makeObservationModel(hallwayColors, obsNoise)) return se.StateEstimator(worldSM, verbose)
def wrapWindowUI(m, worldColors, legalInputs, windowName='Belief', initBelief=None): """ @param m: A machine created by applying C{se.makeStateEstimationSimulation} to a hallway world, which take movement commands as input and generates as output structures of the form C{(b, (o, a))}, where C{b} is a belief state, C{a} is the action command, and C{o} is the observable output generated by the world. @param worldColors: A list of the colors of the rooms in the hallway, from left to right. @returns: A composite machine that prompts the user for input to, and graphically displays the output of C{m} on each step. """ def drawWorld(size): y = 0 for x in range(size): window.drawRect((x, y), (x + 1, y + 1), color=worldColors[x]) def processOutput(stuff): (dist, (o, a)) = stuff drawWorld(dim) drawBelief(dist, window, dim) return (o, a) def processInput(stuff): if stuff == 'quit': print 'Taking action 0 before quitting' return 0 else: return int(stuff) dim = len(worldColors) if not initBelief: initBelief = dist.UniformDist(range(dim)) ydim = 1 window = DrawingWindow(dim * 50, ydim * 50 + 10, -0.2, dim + 0.2, -0.2, ydim + 0.2, windowName) drawWorld(dim) drawBelief(initBelief, window, dim) return sm.Cascade( sm.Cascade( sm.Cascade(TextInputSM(legalInputs), sm.PureFunction(processInput)), m), sm.PureFunction(processOutput))
def makeRobotNavModel(ideal, xMin, xMax, numStates, numObservations): #! startDistribution = None # redefine this """ Create a model of a robot navigating in a 1 dimensional world with a single sonar. @param ideal: list of ideal sonar readings @param xMin: minimum x coordinate for center of robot @param xMax: maximum x coordinate for center of robot @param numStates: number of discrete states into which to divide the range of x coordinates @param numObservations: number of discrete observations into which to divide the range of good sonar observations, between 0 and C{goodSonarRange} @returns: an instance of {\tt ssm.StochasticSM} that describes the dynamics of the world """ # make initial distribution over states startDistribution = dist.UniformDist(range(numStates)) ###################################################################### ### Define observation model ###################################################################### # real width of triangle distribution, in meters # obsTriangleWidth = sonarStDev * 3 obsTriangleWidth = .05 obsDiscTriangleWidth = max(2, int(obsTriangleWidth * \ (numObservations / sonarDist.sonarMax))) # Part of distribution common to all observations obsBackground = dist.MixtureDist(dist.UniformDist(range(numObservations)), dist.DeltaDist(numObservations - 1), 0.5) #! def observationModel(ix): # ix is a discrete location of the robot # return a distribution over observations in that state #! pass return dist.MixtureDist( dist.triangleDist(ideal[ix], obsDiscTriangleWidth, 0, numObservations), obsBackground, 0.9) ###################################################################### ### Define transition model ###################################################################### # real width of triangle distribution, in meters transTriangleWidth = odoStDev * 3 transDiscTriangleWidth = max( 2, int(transTriangleWidth * (numStates / (xMax - xMin)))) transDiscTriangleWidth = 2 teleportProb = 0 #! def transitionModel(a): # a is a discrete action # returns a conditional probability distribution on the next state # given the previous state #! pass def transitionGivenState(s): # A uniform distribution we mix in to handle teleportation transUniform = dist.UniformDist(range(numStates)) return dist.MixtureDist(dist.triangleDist(\ util.clip(s+a, 0, numStates-1), transDiscTriangleWidth, 0, numStates-1), transUniform, 1 - teleportProb) return transitionGivenState ###################################################################### ### Create and return SSM ###################################################################### #! return ssm.StochasticSM(startDistribution, transitionModel, observationModel)