def improvePolicy(policy,values, discountFactor): stable = True # all locations in the grid and all possible moves alllocations = [ (x,y) for x in range(11) for y in range(11)] moves = [(0,0),(0,1),(0,-1),(1,0),(-1,0)] # loop over possible states for predloc in alllocations: for preyloc in alllocations: if predloc == preyloc: continue prey = Prey(*preyloc) oldPolicy = policy[(predloc,preyloc)] bestPolicy = (0,0) bestVal = 0 # calculate greedy policy according to values for predMove in moves: newPredloc = ((predloc[0] + predMove[0])%11,(predloc[1] + predMove[1])%11) preySum = 0 if newPredloc == preyloc : preySum += 10.0 else: for preyProb, newPreyloc in prey.expand(newPredloc): preySum += preyProb * discountFactor * values[(newPredloc,newPreyloc)] if bestVal <= preySum: bestVal = preySum bestPolicy = predMove policy[(predloc,preyloc)]=bestPolicy # keep track of whether the policy is adjusted if oldPolicy != bestPolicy: stable = False return policy, stable
def __init__(self, x, y, width=0, height=0, angle=0, speed=5): Prey.__init__(self, x, y, width, height, angle, speed) self.radius = 5 self.set_dimension(self.radius * 2, self.radius * 2) self.set_speed(speed) self.randomize_angle()
def __init__(self,x,y): self.radiuses = 4 Prey.__init__(self, x, y, self._width, self._height, angle = None , speed= 10) self.randomize_angle() # self._image = PhotoImage(file='x-wing_fighter.gif') self._image = PhotoImage(file='tie-fighter.jpg')
def initialize_animats(initial_list, object_list, scale, number, speed, kind): for i in range(0, number): if (kind == 'prey'): animat = Prey(prey_path, scale, speed, RL.QLearn) overlap = False for j in range(0, len(initial_list)): if (doRectsOverlap(animat.get_rect(), initial_list[j].get_rect())): overlap = True break if (overlap): i = i - 1 else: object_list.append(animat) all_sprites.add(animat) initial_list.append(animat) else: animat = Predator(predator_path, scale, speed, RL.QLearn) overlap = False for j in range(0, len(initial_list)): if (doRectsOverlap(animat.get_rect(), initial_list[j].get_rect())): overlap = True break if (overlap): i = i - 1 else: object_list.append(animat) all_sprites.add(animat) initial_list.append(animat)
def __init__(self, x, y, width=0, height=0, angle=0, speed=5): Prey.__init__(self, x, y, width, height, angle, speed) self.radius = 5 self._image = PhotoImage(file="ufo.gif") self.randomize_angle() self.set_dimension(self._image.width(), self._image.height()) self.set_speed(speed)
def update(self): x = random() y = random() if x <= .3: Floater.randomize_angle(self) if ((self._speed + y - .5) >= 3) and (self._speed+y-.5) <= 7: self._speed = self._speed + y - .5 Prey.move(self)
def update(self, speed): Prey.move(self) Prey.wall_bounce(self) if random() <= .3: speed_mult,rad_mult = uniform(-.5,.5),uniform(-.5,.5) while self._speed + speed_mult > 3 and self._speed + speed_mult < 7: self._speed += speed_mult self._angle += rad_mult
def __init__(self, x, y): self.randomize_angle() Prey.__init__(self, x, y, width=self.radius * 2, height=self.radius * 2, angle=self.get_angle(), speed=5)
def __init__(self, x, y): self.randomize_angle() Prey.__init__(self, x, y, width=Floater.radius * 2, height=Floater.radius * 2, angle=self._angle, speed=5)
def valueIteration(discountFactor): # all locations in grid alllocations = [ (x,y) for x in range(11) for y in range(11)] # initialize values values = {} bestMoves = {} for predloc in alllocations: for preyloc in alllocations: if preyloc != predloc: values[(predloc,preyloc)] = 0 agent = Agent(0,0) deltas = [] epsilon = 0.01 delta = 1 numIt = 0 # perform value iteration according to pseud-code while delta > epsilon: delta = 0 newValues = {} # loop over all states for predloc in alllocations: for preyloc in alllocations: if predloc == preyloc: continue agent.setLocation(predloc) prey = Prey(*preyloc) temp = values[(predloc,preyloc)] # find optimal value according to current values bestVal = 0 bestMove = (0,0) for prob, predMove in agent.getMoveList(): preySum = 0 newPredloc = ((predloc[0] + predMove[0])%11,(predloc[1] + predMove[1])%11) if newPredloc == preyloc : preySum += 10.0 else: for preyProb, newPreyloc in prey.expand(newPredloc): preySum += preyProb * discountFactor * values[(newPredloc,newPreyloc)] if bestVal <= preySum: bestVal = preySum bestMove = predMove newValues[(predloc,preyloc)] = bestVal bestMoves[(predloc,preyloc)] = bestMove delta = max(delta, np.abs(bestVal - temp)) values = newValues deltas.append(delta) numIt+=1 # greedy policy to the optimal values computed above def policy(state): predloc, preyloc = state agent.setLocation(predloc) prey = Prey(*preyloc) return bestMoves[(predloc,preyloc)] return numIt, values, policy
def valueIteration(): alldiffs = [ (x,y) for x in range(-5,6) for y in range(-5,6)] alldiffs.remove((0,0)) # the relative positions vary from -5 up to 5, in both dimensions values = {} for x in range(-5,6): for y in range(-5,6): values[(x,y)] = 0 bestMoves = {} agent = Agent(0,0) deltas = [] discountFactor = 0.8 epsilon = 0.01 delta = 1 while delta > epsilon: delta = 0 newValues = {} for diff in alldiffs: # we place the predator in the middle of the world, # we are allowed to do this, since the positions are encoded relatively predloc = (5,5) preyloc = (predloc[0]+diff[0],predloc[1]+diff[1]) curKey = rewriteStates(predloc,preyloc) agent.setLocation(predloc) prey = Prey(*preyloc) temp = values[curKey] bestVal = 0 bestMove = (0,0) for prob, predMove in agent.getMoveList(): preySum = 0 newPredloc = agent.locAfterMove(predMove) if newPredloc == preyloc : preySum += 10.0 else: for preyProb, newPreyloc in prey.expand(newPredloc): # using rewriteStates we use relative positions preySum += preyProb * discountFactor * values[rewriteStates(newPredloc,newPreyloc)] if bestVal <= preySum: bestVal = preySum bestMove = predMove newValues[curKey] = bestVal bestMoves[curKey] = bestMove delta = max(delta, np.abs(bestVal - temp)) values = newValues deltas.append(delta) def policy(state): predloc, preyloc = state agent.setLocation(predloc) prey = Prey(*preyloc) return bestMoves[rewriteStates(predloc,preyloc)] return policy
def update(self): probability = random() if probability <= 0.3: self._floater_speed() self._floater_angle() while not self._valid_speed(): self._floater_speed() Prey.update(self)
def update(self, model): if random.uniform(0, 1) <= .3: self._angle += round(random.uniform(-.5, .5), 1) self._speed += round(random.uniform(-.5, .5), 1) if self._speed < 3: self._speed = 3 elif self._speed > 7: self._speed = 7 Prey.move(self)
def valueFunction(): # all locations on the grid alllocations = [ (x,y) for x in range(11) for y in range(11)] # initialize value function values = {} for predloc in alllocations: for preyloc in alllocations: if preyloc != predloc: values[(predloc,preyloc)] = 0 # predator which is placed in the top-left agent = Agent(0,0) discountFactor = 0.8 epsilon = 0.01 delta = 1 numIt = 0 while delta > epsilon: delta = 0 newValues = {} # sweep over all possible states for predloc in alllocations: for preyloc in alllocations: if predloc == preyloc: continue # place predator and prey at location agent.setLocation(predloc) prey = Prey(*preyloc) # temp is previous value of state temp = values[(predloc,preyloc)] moveSum = 0 # iterates over each actionthe agent can take # and the probability of the action according to the policy for prob, newPredloc in agent.expand(): preySum = 0 # absorbing state if newPredloc == preyloc : preySum += 10.0 else: # iterates over the states which the action can lead to, and their probability (stochastic) for preyProb, newPreyloc in prey.expand(newPredloc): # part of update rule (sum over s') preySum += preyProb * discountFactor * values[(newPredloc,newPreyloc)] # part of update rule (sum over a) moveSum += prob * preySum # policy evaluation update newValues[(predloc,preyloc)] = moveSum delta = max(delta, np.abs(moveSum - temp)) values = newValues numIt += 1 return values, numIt
def __init__(self, x, y, width, height, speed, angle, color): Prey.__init__(self, x, y, width, height, angle, speed) self._x = x self._y = y self._width = width * 2 self._height = height * 2 self._speed = speed self._angle = angle self._color = color Prey.randomize_angle(self)
def single_sim(): width = 1000 height = 1000 n_flock = 1000 n_pred = 10 elapsed = 0 flock = [ Prey(*np.random.rand(2) * 1000, width, height, np.mod(_, 2)) for _ in range(n_flock) ] predators = [ Predator(*np.random.rand(2) * 1000, width, height, np.mod(_, 2)) for _ in range(n_pred) ] while flock and predators and len(flock) < 1500 and elapsed < 10: pointmap = create_map(flock) locations = np.array(list(pointmap.keys())) tree = spatial.KDTree(locations, 15) all = flock + predators for boid in all: boid.apply_behaviour(flock, tree, locations, pointmap, predators, elapsed) for boid in all: boid.update() boid.edges() print(len(flock), ",", len(predators), ",", elapsed) elapsed += 1
def new_prey(): if (len(preyList) < 9): x = random.randrange((window_size[0] - 40)) y = random.randrange((window_size[1] - 40)) while (y > 300): y = random.randrange(window_size[1]) preyList.append(Prey(pygame.image.load("images/prey.png").convert(), [x, y]))
def single_sim(value): a_list = [] n_pred_vec = [] n_prey_vec = [] time_vec = [] width = 1000 height = 1000 n_flock = 600 n_pred = 25 elapsed = 0 flock = [ Prey(*np.random.rand(2) * 1000, width, height) for _ in range(n_flock) ] predators = [ Predator(*np.random.rand(2) * 1000, width, height) for _ in range(n_pred) ] _thread.start_new_thread(input_thread, (a_list, )) while flock and predators and len( flock) < 2000 and elapsed < len_sim and not a_list: pointmap = create_map(flock) locations = np.array(list(pointmap.keys())) tree = spatial.KDTree(locations, 15) all = flock + predators for boid in all: boid.apply_behaviour(flock, tree, locations, pointmap, predators) for boid in all: boid.update() boid.edges() print(len(flock), ",", len(predators), ",", elapsed) n_prey_vec.append(len(flock)) n_pred_vec.append(len(predators)) time_vec.append(elapsed) elapsed += 1 #plt.title("Populations vs time") #plt.xlabel("Elapsed time (#)") #plt.ylabel("# of individuals") #plt.plot(time_vec,n_prey_vec, label='Preys') #plt.plot(time_vec,n_pred_vec, label='Predators') #plt.legend() #plt.savefig('sim_{}.pdf'.format(value)) #plt.close() return n_prey_vec, n_pred_vec
def evaluatePolicy(policy, discountFactor, values=None): # all locations on the grid alllocations = [ (x,y) for x in range(11) for y in range(11)] # initialize values if None is given if values is None: values = {} for predloc in alllocations: for preyloc in alllocations: if preyloc != predloc: values[(predloc,preyloc)] = 0 delta = 1 numIt = 0 # perform update values according to given pseudo-code while delta > epsilon: delta = 0 newValues = {} # will be filled with new values # loop over all states for predloc in alllocations: for preyloc in alllocations: if predloc == preyloc: # impossible state continue prey = Prey(*preyloc) temp = values[(predloc,preyloc)] predMove = policy[(predloc,preyloc)] # make move according to policy newPredloc = ((predloc[0] + predMove[0])%11,(predloc[1] + predMove[1])%11) preySum = 0 # calculate discounted sum if newPredloc == preyloc : preySum += 10.0 # game ends after this else: for preyProb, newPreyloc in prey.expand(newPredloc): preySum += preyProb * discountFactor * values[(newPredloc,newPreyloc)] newValues[(predloc,preyloc)] = preySum delta = max(delta, np.abs(preySum - temp)) values = newValues numIt +=1 return values, numIt
def testInputs(): window = GraphWin("CS81 Final Project",700,700) window.setBackground("blue") preyList = [] p1 = Prey(0,350,350,pi,window) p2 = Prey(1,340,350,3*pi/2,window) p3 = Prey(2,370,350,pi/2,window) p4 = Prey(3,350,445,pi,window) preyList.append(p1) preyList.append(p2) preyList.append(p3) preyList.append(p4) shark = Predator(350,250,pi/2,window) print p1.calculateInputs(preyList,shark) window.getMouse() window.close()
def Start_Evaluation(self, pp, pb): #run simulator without pausing self.sim = pyrosim.Simulator(eval_time=c.evalTime, play_blind=pb, play_paused=pp) #create instance of the robot class #sends a random value between -1 and 1 for each iteration self.predator = Predator(self.sim, self.predatorGenome) self.prey = Prey(self.sim, self.preyGenome) #start the simulator self.sim.start()
def main(predator_breed_time=6, predator_starve_time=3, initial_predators=10, prey_breed_time=3, initial_prey=50, size=10, ticks=10): '''Initialization of the simulation''' # Initialize class variables Predator.set_breed_time(predator_breed_time) Predator.set_starve_time(predator_starve_time) Prey.set_breed_time(prey_breed_time) seed = int(input("Enter seed for randomness: ")) random.seed(seed) isle = Island(size) put_animals_at_random(isle, Prey, initial_prey) put_animals_at_random(isle, Predator, initial_predators) print(isle) # The simulation is carried out 'ticks' times for _ in range(ticks): for x in range(size): for y in range(size): animal = isle.animal(x, y) if isinstance(animal, Animal): if isinstance(animal, Predator): # a predator can eat animal.eat() animal.breed() if isinstance(animal, Prey): animal.breed(2) animal.move() animal.clock_tick() isle.clear_all_moved_flags() print(isle)
def Start_Evaluation(self, env, pp, pb): #run simulator without pausing self.sim = pyrosim.Simulator(eval_time=c.evalTime, play_blind=pb, play_paused=pp) env.Send_To(self.sim) #create instance of the robot class #sends a random value between -1 and 1 for each iteration self.robot = Prey(self.sim, self.genome) self.collided = self.sim.assign_collision(self.robot, env.source) #start the simulator self.sim.start()
def update(self, model): Pulsator.update(self, model) nearbyPrey = model.find(lambda x: isinstance(x, Prey) and self. distance((x.get_location())) <= 200) toSort = [] for prey in nearbyPrey: toSort.append((prey, self.distance((prey.get_location())))) if toSort != []: prey_to_chase = min(toSort, key=lambda x: x[1]) xp, yp = Prey.get_location(prey_to_chase[0]) xh, yh = self.get_location() diff_y = yp - yh diff_x = xp - xh self.set_angle(atan2(diff_y, diff_x)) Mobile_Simulton.move(self)
def start(self): prev = "" while 1: resp = self.s.recv(1024) + prev if '\n' not in resp: prev = resp continue resp = resp.split('\n') currResp = resp[0] resp.pop(0) prev = '\n'.join(resp) # print currResp if 'done' in currResp: break if 'sendname' in currResp: self.sendOutput(self.teamname) continue if 'hunter' in currResp: self.playerType = 'hunter' self.flag = 0 continue elif 'prey' in currResp: self.playerType = 'prey' self.flag = 0 continue currResp = currResp.split(' ') currResp = self.parseInput(currResp) if self.flag == 0: self.flag = 1 self.player = Hunter( currResp) if self.playerType == 'hunter' else Prey( currResp) self.sendOutput(self.parseOutput(self.player.move(currResp))) self.s.close()
def update(self, model): if random() > .8: speed = 5 * random() if speed < 3: speed = 3 elif speed > 7: speed = 7 angle = random() - random() if angle > 0.5: angle = 0.5 elif angle < -0.5: angle = 0.5 Prey.set_speed(self, speed) Prey.set_angle(self, Prey.get_angle(self) + angle) Prey.move(self)
def main(WIDTH=21, HEIGHT=10, PREDATORS=8): mygame = Kernel() myscheduler = DummyScheduler() mygame.addAgent(myscheduler, "Scheduler") mymesh = Mesh(WIDTH, HEIGHT) mygame.addAgent(mymesh, "Mesh") anAgent = Prey() mygame.addAgent(anAgent, "Prey") mymesh.addAgent(mygame.getAgentId(anAgent)) for i in range(PREDATORS): anAgent = Predator() mygame.addAgent(anAgent, "Predator%s" % i) mymesh.addAgent(mygame.getAgentId(anAgent)) while (Kernel.instance != None): time.sleep(3)
def main(): '''main execution func''' game = SteeringBehavioursGame("SteeringBehaviours") for _ in range(1): pos = Vector2(randrange(0, 800), randrange(0, 800)) vel = Vector2(randrange(0, 800), randrange(0, 800)).normalized agent0 = Prey("max", pos, vel) pos = Vector2(randrange(0, 800), randrange(0, 800)) vel = Vector2(randrange(0, 800), randrange(0, 800)).normalized agent1 = Prey("donray", pos, vel) pos = Vector2(randrange(0, 800), randrange(0, 800)) vel = Vector2(randrange(0, 800), randrange(0, 800)) agent2 = Predator("trent", pos, vel, [agent0, agent1]) agent0.set_target(agent2) agent1.set_target(agent2) game.addtobatch(agent0) game.addtobatch(agent1) game.addtobatch(agent2) game.run()
def __init__(self,x,y): Prey.__init__(self,x,y,2*Floater.radius,2*Floater.radius,Floater.angle,Floater.speed) self.randomize_angle()
def __init__(self,x,y): self._image = PhotoImage(file='asteroid.gif') Prey.__init__(self,x,y,self._image.width(),self._image.height(),random.random()*math.pi*2,5)
def __init__(self,x,y): Prey.__init__(self,x,y,2*Ball.radius,2*Ball.radius,Ball.angle,Ball.speed) self.randomize_angle()
def __init__(self, x, y): self.time_count = 0 self.randomize_angle() Prey.__init__(self, x, y, 15, 15, self.get_angle(), 4)
def __init__(self, x, y): Prey.__init__(self, x, y, 2 * Floater.radius, 2 * Floater.radius, 0 , 5) self._image = PhotoImage(file='ufo.gif') self.randomize_angle()
def __init__(self,x,y): Circle.__init__(self, x, y, 5, 'Blue') Prey.__init__(self, x, y, 2*self.radius, 2*self.radius, 0, self.radius) self.randomize_angle()
def __init__(self, x, y): Prey.__init__(self, x, y, Floater.radius * 2, Floater.radius * 2, random(), 5)
def update(self, speed): Prey.move(self) Prey.wall_bounce(self)
def __init__(self, x, y, width = 10, height = 10): Prey.__init__(self, x, y, width, height, 0, 5) self.randomize_angle() self._color = "blue"
def __init__(self, x, y): Prey.__init__(self, x, y, Floater.radius*2, Floater.radius*2, 0, 5) self.randomize_angle()
def __init__(self, x, y): Prey.__init__(self, x, y, 10, 10, 0, 5) self.randomize_angle()
def __init__(self,x,y): Prey.__init__(self,x,y,2*Ball.radius,2*Ball.radius,0,5) self.randomize_angle()
def __init__(self, x, y): self._image = PhotoImage(file='ufo.gif') ball = Prey(x, y, 10, 10, 1, 5) ball.randomize_angle() ang = ball.get_angle() Prey.__init__(self, x, y, 10, 10, ang, 5)
def __init__(self, x, y): self._image = PhotoImage(file='ufo.gif') Prey.__init__(self, x, y, 10, 10, 0, 5) self.randomize_angle()
def __init__(self, x, y): self.randomize_angle() Prey.__init__(self, x, y, 10, 10, self.get_angle(), 5)
def __init__(self, x, y): Prey.__init__(self, x, y, 10, 10, 0, speed=5) self.randomize_angle()
def __init__(self,x,y): self.randomize_angle() Prey.__init__(self, x, y, 2*5, 2*5, self._angle, 5)
def __init__(self, x, y): Prey.__init__(self,x,y,self.radius*2,self.radius*2, 1 ,5) self.randomize_angle()
def __init__(self, x, y): self._image = PhotoImage(file='ufo.gif') self.randomize_angle() Prey.__init__(self, x, y, self._image.width(), self._image.height(), self.get_angle(), 5)
def __init__(self, x, y): Prey.__init__(self,x, y, 2 * self.radius, 2 * self.radius, 1 ,20) self.randomize_angle() self.colors = ['red', 'orange', 'pink', 'yellow', 'green', 'purple'] self.current_color = 'pink' self.count = 0
def __init__(self, x, y): Prey.randomize_angle(self) Prey.__init__(self, x, y, 10, 10, Prey.get_angle(self), 5)
def __init__(self,x,y): self._image = PhotoImage(file='ufo.gif') Prey.__init__(self,x,y,self._image.width(),self._image.height(),0,5) self.randomize_angle()