def setup(): mapper = mapMaker.MapMaker(xMin, xMax, yMin, yMax, gridSquareSize) replannerSM = replanner.ReplannerWithDynamicMap(goalPoint) robot.behavior = \ sm.Cascade(sm.Parallel(sm.Cascade(sm.Parallel(mapper, sm.Wire()), replannerSM), sm.Wire()), move.MoveToDynamicPoint())
def getNextValues(self, state, inp): (s1, s2) = state self.ba1 = PureFunction(self.sm1_inst.getNextValues) self.ba2 = PureFunction(self.sm2_inst.getNextValues) self.double_bank = sm.Parallel(self.ba1, self.ba2) ((newS1, newS2), (o1, o2)) = self.double_bank.getNextValues((s1, s2), inp) if newS1 > newS2: return newS1 else: return newS2
reload(ffSkeleton) from secretMessage import secret # Set to True for verbose output on every step verbose = False # Rotated square points squarePoints = [util.Point(0.5, 0.5), util.Point(0.0, 1.0), util.Point(-0.5, 0.5), util.Point(0.0, 0.0)] goal_generator = ffSkeleton.FollowFigure(secret) # goal_generator = sm.Constant(util.Point(1.0, 0.5)) dynamic_move_machine = dynamicMoveToPointSkeleton.DynamicMoveToPoint() # Put your answer to step 1 here mySM = sm.Cascade(sm.Parallel(goal_generator, sm.Wire()), dynamic_move_machine) ###################################################################### ### ### Brain methods ### ###################################################################### def setup(): robot.gfx = gfx.RobotGraphics(drawSlimeTrail = True) robot.behavior = mySM def brainStart(): robot.behavior.start(traceTasks = robot.gfx.tasks(), verbose = verbose)
newState = state * 1.02 + inp - 100 else: newState = state * 1.02 return (newState, newState) class BA2(sm.SM): startState = 0 def getNextValues(self, state, inp): newState = state * 1.01 + inp return (newState, newState) ## return the max number of the two balance counter ba1 = BA1() ba2 = BA2() maxAccount = sm.Cascade(sm.Parallel(ba1, ba2), PureFunction(max)) print maxAccount.transduce([100,200,20,3000], verbose=True) ## deposit or withdraw >3000 choose BA1, else choose BA2 def depositB(inp): if abs(inp) > 3000: return (inp, 0) else: return (0, inp) swithAccount = sm.Cascade(sm.Cascade(PureFunction(depositB), sm.Parallel2(ba1, ba2)), PureFunction(sum)) ## all in state machine class Switch(sm.SM): startState = 0
def test_max(state, inp): (s1, s2) = state return sm.Parallel(BA1().getNextValues(s1, inp), BA2().getNextValues(s2, inp))
if inp != 0: newState += inp - 100 return (newState, newState) class BA2(sm.SM): startState = 0 def getNextValues(self, state, inp): newState = state * 1.01 + inp return (newState, newState) bank_one = BA1() bank_two = BA2() combined_bank = sm.Cascade(sm.Parallel(bank_one, bank_two), PureFunction(max)) def is_large_transaction(amount): return abs(amount) > 3000 def add(args): return args[0] + args[1] class SwitchMachine(sm.SM): startState = None def getNextValues(self, state, inp): if is_large_transaction(inp):
self.f = f def getNextValues(self,state,inp): return (state,self.f(inp)) # Part 1: Maximize # Make a state machine that computes the balances of both types of accounts # and outputs the maximum of the two balances # The input is a number # Start by constructing a state machine whose input is a number and whose # output is a tuple with two balances: # Then combine this machine with sm.PureFunction a1 = BA1() a2 = BA2() maxAccount = sm.Cascade(sm.Parallel(a1,a2),sm.PureFunction(max)) # maxAccount.transduce([1000,1500,2000,500],verbose = True) # Part 2: Investment # I put any deposit or withdrawal whose magnitude is > 3000 in the account1 # and all others in the account of type 2. On every step both bank accounts # should continue to earn relevant interest. The output should be the sum # of the balances in the two accounts. Implement this by composing the two # bank accoutns using sm.Parallel2 and cascading it with two simple machines # you implement using sm.PureFunction class Switcher(sm.SM): startState = None def getNextValues(self,state,inp): if abs(inp) > 3000: