def perfectObsNoiseModel(actualColor): """ :param actualColor: actual color in a location :returns: ``DDist`` over observed colors when in a room that has ``actualColor``. In this case, we observe the actual color with probability 1. """ return dist.DDist({actualColor: 1.0})
def perfectObsNoiseModel(actualColor): """ @param actualColor: actual color in a location @returns: C{DDist} over observed colors when in a room that has C{actualColor}. In this case, we observe the actual color with probability 1. """ return dist.DDist({actualColor: 1.0})
def perfectTransNoiseModel(nominalLoc, hallwayLength): """ @param nominalLoc: location that the robot would have ended up given perfect dynamics @param hallwayLength: length of the hallway @returns: distribution over resulting locations, modeling noisy execution of commands; in this case, the robot goes to the nominal location with probability 1.0 """ return dist.DDist({nominalLoc: 1.0})
def transition_model(self, s, a, p = 0.4): # Only randomness is in brv and brc after a bounce # 1- prob of negating nominal velocity if s == 'over': return dist.delta_dist('over') # Current state ((br, bc), (brv, bcv), pp, pv) = s # Nominal next ball state new_br = br + self.ball_speed*brv; new_brv = brv new_bc = bc + self.ball_speed*bcv; new_bcv = bcv # nominal paddle state, a is action (-1, 0, 1) new_pp = max(0, min(self.n-1, pp + a)) new_pv = a new_s = None hit_r = hit_c = False # bottom, top contacts if new_br < 0: new_br = 0; new_brv = 1; hit_r = True elif new_br >= self.n: new_br = self.n - 1; new_brv = -1; hit_r = True # back, front contacts if new_bc < 0: # back bounce new_bc = 0; new_bcv = 1; hit_c = True elif new_bc >= self.n: if self.paddle_hit(pp, new_pp, br, bc, new_br, new_bc): new_bc = self.n-1; new_bcv = -1; hit_c = True else: return dist.delta_dist('over') new_s = ((new_br, new_bc), (new_brv, new_bcv), new_pp, new_pv) if ((not hit_c) and (not hit_r)): return dist.delta_dist(new_s) elif hit_c: # also hit_c and hit_r if abs(new_brv) > 0: return dist.DDist({new_s: p, ((new_br, new_bc), (-new_brv, new_bcv), new_pp, new_pv) : 1-p}) else: return dist.DDist({new_s: p, ((new_br, new_bc), (-1, new_bcv), new_pp, new_pv) : 0.5*(1-p), ((new_br, new_bc), (1, new_bcv), new_pp, new_pv) : 0.5*(1-p)}) elif hit_r: return dist.DDist({new_s: p, ((new_br, new_bc), (new_brv, -new_bcv), new_pp, new_pv) : 1-p})
def noisyObsNoiseModel(actualColor): """ @param actualColor: actual color in a location @returns: C{DDist} over observed colors when in a room that has C{actualColor}. In this case, we observe the actual color with probability 0.8, and the remaining 0.2 probability is divided uniformly over the other possible colors in this world. """ d = {} for observedColor in possibleColors: if observedColor == actualColor: d[observedColor] = 0.8 else: d[observedColor] = 0.2 / (len(possibleColors) - 1) return dist.DDist(d)
def leftSlipTransNoiseModel(nominalLoc, hallwayLength): """ @param nominalLoc: location that the robot would have ended up given perfect dynamics @param hallwayLength: length of the hallway @returns: distribution over resulting locations, modeling noisy execution of commands; in this case, the robot goes to the nominal location with probability 0.9, and goes one step too far left with probability 0.1. If any of these locations are out of bounds, then the associated probability mass stays at C{nominalLoc}. """ d = {} dist.incrDictEntry(d, util.clip(loc - 1, 0, n - 1), 0.1) dist.incrDictEntry(d, util.clip(loc, 0, n - 1), 0.9) return dist.DDist(d)
def noisyTransNoiseModel(nominalLoc, hallwayLength): """ @param nominalLoc: location that the robot would have ended up given perfect dynamics @param hallwayLength: length of the hallway @returns: distribution over resulting locations, modeling noisy execution of commands; in this case, the robot goes to the nominal location with probability 0.8, goes one step too far left with probability 0.1, and goes one step too far right with probability 0.1. If any of these locations are out of bounds, then the associated probability mass goes is assigned to the boundary location (either 0 or C{hallwayLength-1}). """ d = {} dist.incrDictEntry(d, util.clip(nominalLoc - 1, 0, hallwayLength - 1), 0.1) dist.incrDictEntry(d, util.clip(nominalLoc, 0, hallwayLength - 1), 0.8) dist.incrDictEntry(d, util.clip(nominalLoc + 1, 0, hallwayLength - 1), 0.1) return dist.DDist(d)
def getNextValues(self, state, inp): """ @param state: Distribution over states of the subject machine, represented as a C{dist.Dist} object @param inp: A pair C{(o, a)} of the input and output of the subject machine on this time step. If this parameter is C{None}, then no update occurs and the state is returned, unchanged. """ if inp == None: return (state, state) (o, i) = inp total = 0 afterObs = state.d.copy() for s in state.support(): afterObs[s] *= self.model.observationDistribution(s).prob(o) total += afterObs[s] if total == 0: raise Exception('Observation '+str(o)+\ ' has 0 probability in all possible states.') # Note: afterObs is not normalized at this point; dividing # through by toal during the transition update new = {} tDist = self.model.transitionDistribution(i) for s in state.support(): tDistS = tDist(s) oldP = afterObs[s] / total for sPrime in tDistS.support(): dist.incrDictEntry(new, sPrime, tDistS.prob(sPrime) * oldP) dSPrime = dist.DDist(new) return (dSPrime, dSPrime)
def uGivenAS(a): #! pass return lambda s: dist.DDist({s: 1.0})
def oGivenS(s): #! pass if s == 'empty': return dist.DDist({'hit': falsePos, 'free': 1 - falsePos}) else: # occ return dist.DDist({'hit': 1 - falseNeg, 'free': falseNeg})
else: # occ return dist.DDist({'hit': 1 - falseNeg, 'free': falseNeg}) #! # Transition model: P(newState | s | a) def uGivenAS(a): #! pass return lambda s: dist.DDist({s: 1.0}) #! #!cellSSM = None # Your code here cellSSM = ssm.StochasticSM( dist.DDist({ 'occ': initPOcc, 'empty': 1 - initPOcc }), uGivenAS, oGivenS) #! class BayesGridMap(dynamicGridMap.DynamicGridMap): def squareColor(self, (xIndex, yIndex)): p = self.occProb((xIndex, yIndex)) if self.robotCanOccupy((xIndex, yIndex)): return colors.probToMapColor(p, colors.greenHue) elif self.occupied((xIndex, yIndex)): return 'black' else: return 'red'
# Observation model: P(obs | state) def oGivenS(s): #! pass if s == 'empty': return dist.DDist({'hit': falsePos, 'free': 1 - falsePos}) else: # occ return dist.DDist({'hit': 1 - falseNeg, 'free': falseNeg}) #! # Transition model: P(newState | s | a) def uGivenAS(a): #! pass return lambda s: dist.DDist({s: 1.0}) #! #!cellSSM = None # Your code here cellSSM = ssm.StochasticSM(dist.DDist({'occ': initPOcc, 'empty': 1 - initPOcc}), uGivenAS, oGivenS) #! class BayesGridMap(dynamicGridMap.DynamicGridMap): def squareColor(self, (xIndex, yIndex)): p = self.occProb((xIndex, yIndex)) if self.robotCanOccupy((xIndex,yIndex)): return colors.probToMapColor(p, colors.greenHue) elif self.occupied((xIndex, yIndex)): return 'black' else: return 'red'