# This is Colin Ross's Golfer Module. My name is Colin5.py.
import numpy
import random; random.seed()
import cgolf
import scipy.optimize
class Golfer(object):
def __init__(self, id, x=250., z=10., friends=[], foes=[], Vmin=0., Vmax=85.,
rpm_min=2000., rpm_max=10000., *args, **kwargs):
"""Initialization code for this golfer.
id: Name of this golfer.
x,z: Position assigned to this golfer.
friends: List of (name,x,z) tuples for friends.
foes: List of (name,x,z) tuples for friends.
Vmin, Vmax: The min and max launch speeds (m/s) allowed.
rpm_min, rpm_max: The min and max rotation rates (rpms) allowed.
args,kwargs: Extra positional and keyword arguments.
"""
self.Vmin = Vmin
self.Vmax = Vmax
self.rpm_min = rpm_min
self.rpm_max = rpm_max
self.x=x
self.z=z
self.foes=foes #Allows me to reference foes later.
self.id=id #Allows me to reference id later.
self.N_shots=0 #Defines a first shot. So me first shot is at N_shot=0.
def swing(self,friend_ids=[],foe_ids=[],data=[],*args,**kwargs):
"""Returns launch parameters.
friend_ids: Names of active friends.
foe_ids: Names of active foes.
data: A list of recently landed projectiles. The data for each
projectile are contained in dictionaries with the following
keys:
id - the player's name who launched this projectile
N - the shot number for this projectile
xend - the final resting position of the projectile
carry - the absolute carry (m) of the projectile
roll - the absolute roll (m) of the projectile
args,kwargs: Extra positional and keyword arguments.
The launch parameters should be returned as (V0,theta0,rpm0),
or None to pass up this opportunity.
"""
# This is a function I made to calculate the total distance the ball travels after a swing. It is the addition of the carry and the roll
# distances. Note: This is the distance traveled by the ball if there is no wind or roll variability present.
def get_tot_dist(V0,theta0,rpm0):
x,y=cgolf.get_trajectory(V0,theta0,rpm0,self.x,self.z) #This gives x,y values to put into carry dist function.
xf=cgolf.get_carry_distance(x,y) #This gives the final x value of my projectile after carry.
xr,yr=cgolf.get_roll(x,y,alpha=1.0) #This gives the x,y values to feed into roll function.
x_roll=cgolf.get_roll_distance(xr,yr) #This gives the final x value of my projectile after roll.
total_distance=xf+x_roll #This is the total distance carry+roll.
return total_distance
#This returns the distance betwen where my projectile has landed and where my foe is located ( in horizontal).
def error_traj(V0):
#name,x,z=target_foe
return numpy.abs(self.x+get_tot_dist(V0,theta0,rpm0) - x)
#This gives the true error in the trajectory after wind is accounted for.(The dist between my foe and where the projectile actually lands.)
def true_error_traj(V0):
return numpy.abs(self.x+x-x_true)
target_foes=[] #This sets up a list to store my active target foes in.
for foe in self.foes: #This searches through the list of foes.
print foe[0],foe_ids
if foe[0] in foe_ids: #This matches the foe id with a foe id out of the active list.
target_foes.append(foe) #This appends the value into a list of target foes.
xs=[] #Here I create a list to store the x values of my target foe.
for target_foe in target_foes: #Here I search through my target foes to retreive there x values.
xs.append(target_foe[1]) #Here I append those values into a list (xs).
xs=numpy.array(xs) #This ensures I have an array so I can subract off self.x to get the distance between me and my foe.
print xs
dxs=xs-self.x #This finds the distance.
#This compares the absolute values of the difference between me (my x coord) and my foe (his x coord). Function compares dxs.
def cmp(a,b):
if numpy.abs(a)numpy.abs(b):
return 1
dxs = list(dxs) #This turns the dxs from an array to a list so I can use the comparison function.
print dxs
dxs.sort(cmp=cmp) #This sorts those distances from smallest to largest.
x=self.x+dxs[0] #This takes the first distance out of the list which will be the smallest and therefore gives
#the x coord of the closest foe. Added self.x back to get the total x coord of the foe.
print x
flag=False #Sets up an initial False condition, which says if I am not in the recently
# returned data execute something else. Comes into play below.
if self.N_shots==0: #This block of code just lets me fire a projectile off, which will be aimed
if self.x