Example #1
0
def get_flocking_heading(agent_name, neighbours):
    # initialise the heading vector for this agent
    heading_vector = [0, 0, 0]

    separation_heading_vector = get_separation_heading(agent_name, neighbours)
    cohesion_heading_vector = get_cohesion_heading(agent_name, neighbours)
    alignment_heading_vector = get_alignment_heading(neighbours)

    # apply the weighting to each heading vector and then add them altogether
    # to get the overall heading vector
    global separation_weight
    global cohesion_weight
    global alignment_weight
    separation_heading_vector = vectorMath.vector_scale(
        separation_heading_vector, separation_weight)
    cohesion_heading_vector = vectorMath.vector_scale(cohesion_heading_vector,
                                                      cohesion_weight)
    alignment_heading_vector = vectorMath.vector_scale(
        alignment_heading_vector, alignment_weight)
    heading_vector = vectorMath.vector_add(heading_vector,
                                           separation_heading_vector)
    heading_vector = vectorMath.vector_add(heading_vector,
                                           cohesion_heading_vector)
    heading_vector = vectorMath.vector_add(heading_vector,
                                           alignment_heading_vector)

    return heading_vector
Example #2
0
def get_cohesion_heading(agent_name, neighbours):
    # initialise the heading vector that will hold the separation heading
    cohesion_heading_vector = [0, 0, 0]

    # get the number of neighbouring agents in the neighbours list [find the length of the list]
    number_of_neighbours = len(neighbours)
    # IF we have 1 or more agents in the neighbours list
    if number_of_neighbours > 0:
        # get the position of the agent called agent_name
        agent_pos = agent_name.get_agent_position()
        # initialise a a vector to hold the average of all the neighbours positions to [0, 0, 0]
        neighbours_average_vector = [0, 0, 0]
        #FOR every neighbour in the neighbours list
        for neighbour in neighbours:
            # get the neighbours position
            neighbour_pos = neighbour.get_agent_position()
            # add it to the vector to hold the average positions
            neighbours_average_vector = vectorMath.vector_add(
                neighbours_average_vector, neighbour_pos)
        # calculate the averaged neighbours position
        neighbours_average_vector[
            0] = neighbours_average_vector[0] / number_of_neighbours
        neighbours_average_vector[
            1] = neighbours_average_vector[1] / number_of_neighbours
        neighbours_average_vector[
            2] = neighbours_average_vector[2] / number_of_neighbours
        # calculate the cohesion_heading_vector from the position of the agent called agent_name to the averaged neighbours position
        cohesion_heading_vector = vectorMath.get_vector_between_points(
            agent_pos, neighbours_average_vector)
        # normalise the cohesion_heading_vector
        cohesion_heading_vector = vectorMath.vector_normalise(
            cohesion_heading_vector)
    return cohesion_heading_vector
Example #3
0
def get_separation_heading(agent_name, neighbours):
    # initialise the heading vector that will hold the separation heading
    separation_heading_vector = [0, 0, 0]
    heading_vector = [0, 0, 0]

    # get the number of neighbouring agents in the neighbours list [find the length of the list]
    number_of_neighbours = len(neighbours)
    # IF we have 1 or more agents in the neighbours list
    if number_of_neighbours > 0:
        # get the position of the agent called agent_name
        agent_pos = agent_name.get_agent_position()
        # FOR every neighbour in the neighbours list
        for neighbour in neighbours:
            # get the position of the neighbour
            neighbour_pos = neighbour.get_agent_position()
            # calculate the heading vector from the neighbours position to the position of our agent
            heading_vector = vectorMath.get_vector_between_points(
                neighbour_pos, agent_pos)
            # normalise the heading vector to give it a size of 1 [needed so averaging the vector will work as expected
            heading_vector = vectorMath.vector_normalise(heading_vector)
            # add this heading vector to the separation_heading_vector
            separation_heading_vector = vectorMath.vector_add(
                separation_heading_vector, heading_vector)
        # calculate the averaged separation_heading_vector
        separation_heading_vector[
            0] = separation_heading_vector[0] / number_of_neighbours
        separation_heading_vector[
            1] = separation_heading_vector[1] / number_of_neighbours
        separation_heading_vector[
            2] = separation_heading_vector[2] / number_of_neighbours
        # normalise the separation_heading_vector
        separation_heading_vector = vectorMath.vector_normalise(
            separation_heading_vector)

    return separation_heading_vector
Example #4
0
def get_alignment_heading(neighbours):
    # initialise the heading vector that will hold the alignment heading
    alignment_heading_vector = [0, 0, 0]
    neighbour_vector = [0, 0, 0]
    # get the number of neighbouring agents in the neighbours list [find the length of the list]
    number_of_neighbours = len(neighbours)
    # IF we have 1 or more agents in the neighbours list
    if number_of_neighbours > 0:
        # FOR every neighbour in the neighbours list
        for neighbour in neighbours:
            # get the heading of the neighbour as a vector
            neighbour_vector = neighbour.get_agent_heading_vector()
            # add this heading to the alignment_heading_vector
            alignment_heading_vector = vectorMath.vector_add(
                alignment_heading_vector, neighbour_vector)
        # calculate the averaged alignment_heading_vector
        alignment_heading_vector[
            0] = alignment_heading_vector[0] / number_of_neighbours
        alignment_heading_vector[
            1] = alignment_heading_vector[1] / number_of_neighbours
        alignment_heading_vector[
            2] = alignment_heading_vector[2] / number_of_neighbours
        # normalise alignment vector
        alignment_heading_vector = vectorMath.vector_normalise(
            alignment_heading_vector)

    return alignment_heading_vector
Example #5
0
def get_cohesion_heading(agent_name, neighbours):
	# initialise the heading vector that will hold the separation heading
	cohesion_heading_vector = [0, 0, 0]

	# get the number of neighbouring agents in the neighbours list [find the length of the list]
	number_of_neighbours = len(neighbours)
	# IF we have 1 or more agents in the neighbours list
	if number_of_neighbours > 0:
		# get the position of the agent called agent_name
		agent_pos = agent_name.get_agent_position()
		# initialise a a vector to hold the average of all the neighbours positions to [0, 0, 0]
		neighbours_average_vector = [0, 0, 0]
		#FOR every neighbour in the neighbours list
		for neighbour in neighbours:
			# get the neighbours position
			neighbour_pos = neighbour.get_agent_position()
			# add it to the vector to hold the average positions
			neighbours_average_vector = vectorMath.vector_add(neighbours_average_vector, neighbour_pos)
		# calculate the averaged neighbours position
		neighbours_average_vector[0] = neighbours_average_vector[0] / number_of_neighbours
		neighbours_average_vector[1] = neighbours_average_vector[1] / number_of_neighbours
		neighbours_average_vector[2] = neighbours_average_vector[2] / number_of_neighbours
		# calculate the cohesion_heading_vector from the position of the agent called agent_name to the averaged neighbours position
		cohesion_heading_vector = vectorMath.get_vector_between_points(agent_pos, neighbours_average_vector)
		# normalise the cohesion_heading_vector
		cohesion_heading_vector = vectorMath.vector_normalise(cohesion_heading_vector)
	return cohesion_heading_vector
Example #6
0
def get_separation_heading(agent_name, neighbours):
	# initialise the heading vector that will hold the separation heading
	separation_heading_vector = [0, 0, 0]
	heading_vector = [0, 0, 0]

	# get the number of neighbouring agents in the neighbours list [find the length of the list]
	number_of_neighbours = len(neighbours)
	# IF we have 1 or more agents in the neighbours list
	if number_of_neighbours > 0:
		# get the position of the agent called agent_name
		agent_pos = agent_name.get_agent_position()
		# FOR every neighbour in the neighbours list
		for neighbour in neighbours:
			# get the position of the neighbour
			neighbour_pos = neighbour.get_agent_position()
			# calculate the heading vector from the neighbours position to the position of our agent
			heading_vector = vectorMath.get_vector_between_points(neighbour_pos, agent_pos)
			# normalise the heading vector to give it a size of 1 [needed so averaging the vector will work as expected
			heading_vector = vectorMath.vector_normalise(heading_vector)
			# add this heading vector to the separation_heading_vector
			separation_heading_vector = vectorMath.vector_add(separation_heading_vector, heading_vector)
		# calculate the averaged separation_heading_vector
		separation_heading_vector[0] = separation_heading_vector[0] / number_of_neighbours
		separation_heading_vector[1] = separation_heading_vector[1] / number_of_neighbours
		separation_heading_vector[2] = separation_heading_vector[2] / number_of_neighbours
		# normalise the separation_heading_vector
		separation_heading_vector = vectorMath.vector_normalise(separation_heading_vector)
	
	return separation_heading_vector
Example #7
0
def get_flocking_heading(agent_name, neighbours):
	# initialise the heading vector for this agent
	heading_vector = [0, 0, 0]
	
	separation_heading_vector = get_separation_heading(agent_name, neighbours)
	cohesion_heading_vector = get_cohesion_heading(agent_name, neighbours)
	alignment_heading_vector = get_alignment_heading(neighbours)
	
	# apply the weighting to each heading vector and then add them altogether
	# to get the overall heading vector
	global separation_weight
	global cohesion_weight
	global alignment_weight
	separation_heading_vector = vectorMath.vector_scale(separation_heading_vector, separation_weight)
	cohesion_heading_vector = vectorMath.vector_scale(cohesion_heading_vector, cohesion_weight)
	alignment_heading_vector = vectorMath.vector_scale(alignment_heading_vector, alignment_weight)
	heading_vector = vectorMath.vector_add(heading_vector, separation_heading_vector)
	heading_vector = vectorMath.vector_add(heading_vector, cohesion_heading_vector)
	heading_vector = vectorMath.vector_add(heading_vector, alignment_heading_vector)
	
	return heading_vector
Example #8
0
def do_flocking_behaviour(agent):
	# initialise our heading to 0
	agent_heading_vector = [0, 0, 0]
	#find all the neighbours of this agent
	neighbours = find_agents_within_distance(agent,agent_near_distance)
	#only get a new heading if there any agents close by
	
	if len(neighbours):
		agent_heading_vector = get_flocking_heading(agent, neighbours)
	#even if we have no neighbours we still want to move towards the goal
	goal_heading_vector = get_goal_heading(agent)
	global goal_weight
	goal_heading_vector = vectorMath.vector_scale(goal_heading_vector, goal_weight)
	agent_heading_vector = vectorMath.vector_add(agent_heading_vector, goal_heading_vector)
	#we now have the desired heading
	agent_heading_angle = vectorMath.get_heading_angle_from_vector(agent_heading_vector)
	##print agent,agent_heading_angle
	
	#agent.turn_towards_heading(agent_heading_angle)# decided not to use this
	agent.set_agent_heading(agent_heading_angle) #used this instead. better.
Example #9
0
def get_alignment_heading(neighbours):
	# initialise the heading vector that will hold the alignment heading
	alignment_heading_vector = [0, 0, 0]
	neighbour_vector = [0, 0, 0]
	# get the number of neighbouring agents in the neighbours list [find the length of the list]
	number_of_neighbours = len(neighbours)
	# IF we have 1 or more agents in the neighbours list
	if number_of_neighbours > 0:
		# FOR every neighbour in the neighbours list
		for neighbour in neighbours:
			# get the heading of the neighbour as a vector
			neighbour_vector = neighbour.get_agent_heading_vector()
			# add this heading to the alignment_heading_vector
			alignment_heading_vector = vectorMath.vector_add(alignment_heading_vector, neighbour_vector)
		# calculate the averaged alignment_heading_vector
		alignment_heading_vector[0] = alignment_heading_vector[0] / number_of_neighbours
		alignment_heading_vector[1] = alignment_heading_vector[1] / number_of_neighbours
		alignment_heading_vector[2] = alignment_heading_vector[2] / number_of_neighbours
		# normalise alignment vector
		alignment_heading_vector = vectorMath.vector_normalise(alignment_heading_vector)
	
	return alignment_heading_vector
Example #10
0
def do_flocking_behaviour(agent):
    # initialise our heading to 0
    agent_heading_vector = [0, 0, 0]
    #find all the neighbours of this agent
    neighbours = find_agents_within_distance(agent, agent_near_distance)
    #only get a new heading if there any agents close by

    if len(neighbours):
        agent_heading_vector = get_flocking_heading(agent, neighbours)
    #even if we have no neighbours we still want to move towards the goal
    goal_heading_vector = get_goal_heading(agent)
    global goal_weight
    goal_heading_vector = vectorMath.vector_scale(goal_heading_vector,
                                                  goal_weight)
    agent_heading_vector = vectorMath.vector_add(agent_heading_vector,
                                                 goal_heading_vector)
    #we now have the desired heading
    agent_heading_angle = vectorMath.get_heading_angle_from_vector(
        agent_heading_vector)
    ##print agent,agent_heading_angle

    #agent.turn_towards_heading(agent_heading_angle)# decided not to use this
    agent.set_agent_heading(agent_heading_angle)  #used this instead. better.