Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise on refactoring #3

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a362e35
Defined lists for ranges of initial variables and renamed variables f…
thomasgalley Nov 8, 2014
91729a8
Few other changes made to variable names
thomasgalley Nov 8, 2014
65144a6
Changed len(xs) to variable number_of_boids
thomasgalley Nov 8, 2014
241ac3d
Created velocity change function
thomasgalley Nov 8, 2014
e3bf518
Created position_difference_test function
thomasgalley Nov 8, 2014
9470226
Added new function change_in_position
thomasgalley Nov 8, 2014
48db6f1
Created structured array for boids, program runs much slower
thomasgalley Nov 9, 2014
b11fe0d
Replaced loops over i with iterators
thomasgalley Nov 9, 2014
281cd63
Added configuration file for parameters
thomasgalley Nov 9, 2014
90079d9
Created a boid class
thomasgalley Nov 9, 2014
f3a14af
Added class for boids and a method
thomasgalley Nov 9, 2014
fc88e0f
Created new modules for functions in main code
thomasgalley Nov 9, 2014
7aba43b
Created new module and functions to simplify update function in main …
thomasgalley Nov 9, 2014
02fb7fb
Added more functions to module
thomasgalley Nov 9, 2014
06bc9db
Added parameters file
thomasgalley Nov 9, 2014
7cce2cf
Cleaned up class, added a random boid method
thomasgalley Nov 11, 2014
ebea787
Added boids object and method to create initial flock
thomasgalley Nov 11, 2014
815e5d4
Changed fly_to_centre function into a method on boids
thomasgalley Nov 11, 2014
15ea8b1
Changed fly_away_from_boids function to a method
thomasgalley Nov 11, 2014
dcff1b0
Wrote test for the initial_flock method
thomasgalley Nov 11, 2014
4780fa9
Changed final functions to methods and made suitable modifications to…
thomasgalley Nov 11, 2014
7b67889
Removed modules related to dictionary from directory
thomasgalley Nov 11, 2014
a080a2f
Made number_of_boids into a class attribute
thomasgalley Nov 11, 2014
1f1f93a
Changed some file names for clarity
thomasgalley Nov 11, 2014
696d6e3
Modified regression test to work with classes, added a boids method t…
thomasgalley Nov 11, 2014
625db72
Created test for the initial_data method
thomasgalley Nov 11, 2014
eb1c06c
Final changes
thomasgalley Nov 11, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Animate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
A deliberately bad implementation of [Boids](http://dl.acm.org/citation.cfm?doid=37401.37406)
for use as an exercise on refactoring.
"""

from matplotlib import pyplot as plt
from matplotlib import animation

import numpy

from parameters import*
import boids

flock=boids.boids(attraction_strength,boidproximitythreshold,matchspeed_distance,matchspeed_strength,number_of_boids)
flock.initial_flock()





figure=plt.figure()
axes=plt.axes(xlim=(-500,1500), ylim=(-500,1500))

scatter=axes.scatter([boid.xposition for boid in flock.boids],[boid.yposition for boid in flock.boids])

def animate(frame):
flock.update_boids()
scatter.set_offsets(zip([boid.xposition for boid in flock.boids],[boid.yposition for boid in flock.boids]))


anim = animation.FuncAnimation(figure, animate,
frames=200, interval=50)

if __name__ == "__main__":
plt.show()


37 changes: 37 additions & 0 deletions Animate.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
A deliberately bad implementation of [Boids](http://dl.acm.org/citation.cfm?doid=37401.37406)
for use as an exercise on refactoring.
"""

from matplotlib import pyplot as plt
from matplotlib import animation

import numpy

from parameters import*
import boids

flock=boids.boids(attraction_strength,boidproximitythreshold,matchspeed_distance,matchspeed_strength,number_of_boids)
flock.initial_flock()





figure=plt.figure()
axes=plt.axes(xlim=(-500,1500), ylim=(-500,1500))

scatter=axes.scatter([boid.position[0] for boid in flock.boids],[boid.position[1] for boid in flock.boids])

def animate(frame):
flock.update_boids()
scatter.set_offsets(zip([boid.position[0] for boid in flock.boids],[boid.position[1] for boid in flock.boids]))


anim = animation.FuncAnimation(figure, animate,
frames=200, interval=50)

if __name__ == "__main__":
plt.show()


12 changes: 12 additions & 0 deletions Boid_differences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def velocity_change(velocity1,parameter1,parameter2,change_magnitude):
velocitynew=velocity1+(parameter2-parameter1)*change_magnitude
return velocitynew


def position_difference_test(position_x1,position_x2,position_y1,position_y2,threshold):
if (position_x2-position_x1)**2 + (position_y2-position_y1)**2 < threshold:
return True

def change_in_position(position,velocity,time_increment=1):
new_position=position+velocity*time_increment
return new_position
Binary file added Boid_differences.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions Boid_differences.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def velocity_change(velocity,parameter1,parameter2,change_magnitude):
for i in [0,1]:
return velocity[i]+(parameter2[i]-parameter1[i])*change_magnitude

def position_difference_test(position1,position2,threshold):
if (position2[0]-position1[0])**2 + (position[1]-position1[1])**2 < threshold:
return True

def change_in_position(position,velocity,time_increment=1):
for i in [0,1]:
return position[i]+velocity[i]*time_increment
29 changes: 29 additions & 0 deletions Boid_flight.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from Boid_differences import velocity_change, position_difference_test,change_in_position

def fly_to_middle(boids,threshold,number_of_boids):
for boid in boids:
for j in range(number_of_boids):
boid['xvelocity']=velocity_change(boid['xvelocity'],boid['xposition'],boids['xposition'][j],threshold)
boid['yvelocity']=velocity_change(boid['yvelocity'],boid['yposition'],boids['yposition'][j],threshold)



def fly_away_from_boids(boids,number_of_boids,threshold1,threshold2):
for boid in boids:
for j in range(number_of_boids):
if position_difference_test(boid['xposition'],boids['xposition'][j],boid['yposition'],boids['yposition'][j],threshold1):
boid['xvelocity']=velocity_change(boid['xvelocity'],boids['xposition'][j],boid['xposition'],threshold2)
boid['yvelocity']=velocity_change(boid['yvelocity'],boids['yposition'][j],boid['yposition'],threshold2)

def match_speed(boids,number_of_boids,threshold1,threshold2):
for boid in boids:
for j in range(number_of_boids):
if position_difference_test(boid['xposition'],boids['xposition'][j],boid['yposition'],boids['yposition'][j],threshold1):
boid['xvelocity']=velocity_change(boid['xvelocity'],boid['xvelocity'],boids['xvelocity'][j],threshold2)
boid['yvelocity']=velocity_change(boid['yvelocity'],boid['yvelocity'],boids['yvelocity'][j],threshold2)


def move_boids(boids):
for boid in boids:
boid['xposition']=change_in_position(boid['xposition'],boid['xvelocity'])
boid['yposition']=change_in_position(boid['yposition'],boid['yvelocity'])
5 changes: 5 additions & 0 deletions Change_in_velocity.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def velocity_change(boids,change_magnitude):
for boid['xposition'] in boids['xposition']:
boids['xvelocity']=boids['xveloctiy']+(boid['xposition']-boids['xposition'])*change_magnitude
return boids['xvelocity']
12 changes: 12 additions & 0 deletions Update.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from Boid_flight import fly_to_middle, fly_away_from_boids, match_speed, move_boids

def update_boids(boids,number_of_boids,attractionstrength,boidproximitythreshold,matchspeed_distance, matchspeed_strength):


fly_to_middle(boids,attractionstrength/number_of_boids,number_of_boids)

fly_away_from_boids(boids,number_of_boids,boidproximitythreshold,1)

match_speed(boids,number_of_boids,matchspeed_distance,matchspeed_strength/number_of_boids)

move_boids(boids)
97 changes: 97 additions & 0 deletions boid_class.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import yaml
import random
config=yaml.load(open("config.yml"))
from Boid_differences import*
import numpy


number_of_boids=config['number_of_boids']
boids_starting_x_position_range= config['boids_starting_x_position_range']
boids_starting_y_position_range= config['boids_starting_y_position_range']
boids_starting_x_velocity_range=config['boids_starting_x_velocity_range']
boids_starting_y_velocity_range=config['boids_starting_y_velocity_range']

class boid(object):
def __init__(self,xposition,yposition,xvelocity,yvelocity):
self.xposition=xposition
self.yposition=yposition
self.xvelocity=xvelocity
self.yvelocity=yvelocity

def random_boid(self):
return boid(random.uniform(*boids_starting_x_position_range),random.uniform(*boids_starting_y_position_range),random.uniform(*boids_starting_x_velocity_range),random.uniform(*boids_starting_y_velocity_range))






class boids(object):

def __init__(self,attraction_strength,boidproximitythreshold,matchspeed_distance,matchspeed_strength,number_of_boids):
self.attraction_strength=attraction_strength
self.boidproximitythreshold=boidproximitythreshold
self.matchspeed_distance=matchspeed_distance
self.matchspeed_strength=matchspeed_strength
self.number_of_boids=number_of_boids
self.boids= [boid(0,0,0,0)]*number_of_boids

def initial_flock(self):

self.boids=[boid(0,0,0,0).random_boid() for i in range (self.number_of_boids)]

def fly_to_centre(self):
for boid in self.boids:
for member in self.boids:
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,self.attraction_strength/self.number_of_boids)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,self.attraction_strength/self.number_of_boids)

def fly_away_from_boids(self):
for boid in self.boids:
for member in self.boids:
if position_difference_test(boid.xposition,member.xposition,boid.yposition,member.yposition,self.boidproximitythreshold):
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,1)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,1)

def match_speed(self):
for boid in self.boids:
for member in self.boids:
if position_difference_test(boid.xposition,member.xposition,boid.yposition,member.yposition,self.matchspeed_distance):
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,self.matchspeed_strength/self.number_of_boids)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,self.matchspeed_strength/self.number_of_boids)


def move_boids(self):
for boid in self.boids:
boid.xposition=change_in_position(boid.xposition,boid.xvelocity)
boid.yposition=change_in_position(boid.yposition,boid.yvelocity)

def update_boids(self,number_of_boids):


self.fly_to_centre(number_of_boids)

self.fly_away_from_boids(number_of_boids)

self.match_speed(number_of_boids)

self.move_boids()



















135 changes: 79 additions & 56 deletions boids.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,81 @@
"""
A deliberately bad implementation of [Boids](http://dl.acm.org/citation.cfm?doid=37401.37406)
for use as an exercise on refactoring.
"""

from matplotlib import pyplot as plt
from matplotlib import animation
import yaml
import random

# Deliberately terrible code for teaching purposes

boids_x=[random.uniform(-450,50.0) for x in range(50)]
boids_y=[random.uniform(300.0,600.0) for x in range(50)]
boid_x_velocities=[random.uniform(0,10.0) for x in range(50)]
boid_y_velocities=[random.uniform(-20.0,20.0) for x in range(50)]
boids=(boids_x,boids_y,boid_x_velocities,boid_y_velocities)

def update_boids(boids):
xs,ys,xvs,yvs=boids
# Fly towards the middle
for i in range(len(xs)):
for j in range(len(xs)):
xvs[i]=xvs[i]+(xs[j]-xs[i])*0.01/len(xs)
for i in range(len(xs)):
for j in range(len(xs)):
yvs[i]=yvs[i]+(ys[j]-ys[i])*0.01/len(xs)
# Fly away from nearby boids
for i in range(len(xs)):
for j in range(len(xs)):
if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 100:
xvs[i]=xvs[i]+(xs[i]-xs[j])
yvs[i]=yvs[i]+(ys[i]-ys[j])
# Try to match speed with nearby boids
for i in range(len(xs)):
for j in range(len(xs)):
if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 10000:
xvs[i]=xvs[i]+(xvs[j]-xvs[i])*0.125/len(xs)
yvs[i]=yvs[i]+(yvs[j]-yvs[i])*0.125/len(xs)
# Move according to velocities
for i in range(len(xs)):
xs[i]=xs[i]+xvs[i]
ys[i]=ys[i]+yvs[i]


figure=plt.figure()
axes=plt.axes(xlim=(-500,1500), ylim=(-500,1500))
scatter=axes.scatter(boids[0],boids[1])

def animate(frame):
update_boids(boids)
scatter.set_offsets(zip(boids[0],boids[1]))


anim = animation.FuncAnimation(figure, animate,
frames=50, interval=50)

if __name__ == "__main__":
plt.show()
from Boid_differences import*
import numpy
from parameters import*

class boid(object):
def __init__(self,xposition,yposition,xvelocity,yvelocity):
self.xposition=xposition
self.yposition=yposition
self.xvelocity=xvelocity
self.yvelocity=yvelocity

def random_boid(self):
return boid(random.uniform(*boids_starting_x_position_range),random.uniform(*boids_starting_y_position_range),random.uniform(*boids_starting_x_velocity_range),random.uniform(*boids_starting_y_velocity_range))






class boids(object):

def __init__(self,attraction_strength,boidproximitythreshold,matchspeed_distance,matchspeed_strength,number_of_boids):
self.attraction_strength=attraction_strength
self.boidproximitythreshold=boidproximitythreshold
self.matchspeed_distance=matchspeed_distance
self.matchspeed_strength=matchspeed_strength
self.number_of_boids=number_of_boids
self.boids= [boid(0,0,0,0)]*number_of_boids

def initial_flock(self):

self.boids=[boid(0,0,0,0).random_boid() for i in range (self.number_of_boids)]

def fly_to_centre(self):
for boid in self.boids:
for member in self.boids:
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,self.attraction_strength/self.number_of_boids)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,self.attraction_strength/self.number_of_boids)

def fly_away_from_boids(self):
for boid in self.boids:
for member in self.boids:
if position_difference_test(boid.xposition,member.xposition,boid.yposition,member.yposition,self.boidproximitythreshold):
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,1)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,1)

def match_speed(self):
for boid in self.boids:
for member in self.boids:
if position_difference_test(boid.xposition,member.xposition,boid.yposition,member.yposition,self.matchspeed_distance):
boid.xvelocity=velocity_change(boid.xvelocity,boid.xposition,member.xposition,self.matchspeed_strength/self.number_of_boids)
boid.yvelocity=velocity_change(boid.yvelocity,boid.yposition,member.yposition,self.matchspeed_strength/self.number_of_boids)


def move_boids(self):
for boid in self.boids:
boid.xposition=change_in_position(boid.xposition,boid.xvelocity)
boid.yposition=change_in_position(boid.yposition,boid.yvelocity)

def update_boids(self):


self.fly_to_centre()

self.fly_away_from_boids()

self.match_speed()

self.move_boids()

def initial_data(self, data):

self.boids=[boid(data[0][i],data[1][i],data[2][i],data[3][i]) for i in range (self.number_of_boids)]





Binary file added boids.pyc
Binary file not shown.
Loading