Statistics

Total Posts: 34
This Year: 0
This Month: 0
This Week: 0
Comments: 160


RSS 2.0

Recent Posts


On this page....

The Running Mate - The geometry calculations
The Running Mate - Code available at Codeplex
The Running Mate – What is it and why?
The Running Mate - An Introduction

Archives

 Full Archives By Category
 2007 Calendar View

Categories


Admin

Sign In

Acknowledgments

DasBlog Theme Design by: Tom Watts
E-mail: Send mail to the author(s)
Theme Image by: dreamLogic

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

 Sunday, November 02, 2008

This blog post is part of a series that start here.

I have already descrived what the Running Mate is and why I would like to build such a thing. Now, it’s time to get my hands a bit dirty with the mathematical theory that’s needed for this. I would not say that it is an advanced mathematics, but it is definitly a fun applicability since it is really easy to see and understand the purpose and need for the mathematic formulas.

So here’s my 10 km run again. Let us focus on small area of this run: the square that the red arrow points at:

If we enlarge this square area, it would look something like this:

As you can see, I’ve painted some blue stars on the map and high-lighted the road with a black line. The blue stars signify the GPS points that my running mate is picking up with the help of the satellite(s). I don’t know how often these coordinates actually come, but naturally, I will not be able to get the exact track of my run. By drawing a line between the coordinates we will se how the running path looks like according to the PGS system. Here is another image high lighting the coordinates with their straight lines between them in an X-Y graph instead:

 

Calculating the distance between these stars in this graph is simply a matter of utilizing the Pythagorean Theorem. I honestly think that no matter how rusty your math knowledge becomes, this formula will forever stick in your head:

z2 = x2+y2,

z is evidently being the sought distance between two of the stars in my graph. So in order to withdraw the distance between the second and the third coordinate, the formula becomes:

z2 = (x3-x2)2+(y3-y2)2

Calculating all distances between all coordinates on the map gives us the total distance which is exactly what shapelink.com did for me when I utilised their service to draw my run on a map. Basically, I gave them the coordinates by clicking on the map and they drew the lines in between these coordinates and calculated the total distance of the run.

But we are still not at where I want to be: getting the current time comparison of each spot to a previous run. And this is where the fun begins.

The difficult part here would be to compare different coordinates from different races with each other. Naturally, the coordinates will not be on the exact same place as the previous coordinate. This image will illustrate this:

Here I have high-lighted a second race coordinate with red stars. As you can see I did not run the exact same spots as I did the last time I ran the race. The deviation shown is most likely larger than what would be in reality considering that the track I run on is quite narrow. But nevertheless, I obviously have to take this into account when comparing the coordinates.

Naturally each coordinate also has an associated timestamp. As I see it, finding the coordinates that are closest to each other in order to calculate the difference between the two timestamps can be done in at least two different ways. Let us start with the easiest one.

Algorithm: Finding the closest coordinate

So, as this image shows, we have to calculate the distance (d1, d2, and d3) for each red coordinate to all blue coordinate and pick the one that is the closest to the red coordinate. The needed calculation is basically just a matter of using the Pythagorean Theorem again a number of times, so it is a piece of cake really.  

In the example in this image, the d3 distance is obviosly the shortest one so this coorrelating blue coordinate is selected as the one to compare against the red coordinate. The difference in timestamp between these two coordinates would give you the desired result for the Running Mate application. If we don't need  a more accuracy result, this is where we would stop.

However, of course we want more accuracy, so let us move on to the more interesting algorithm.

Algorithm: Interpolating the time

As you might have gathered, the previous algorithm is not particularly accurate when the number of coordinates is low for a run. If the number of coordinates approaches infinity (as mathematicians do love to state) the result would indeed be good. But if merely a few coordinates would have been used for the whole track, naturally this first algorithm just doesn’t cut it. This is when time interpolation is needed.

In this image we are focusing on two of the blue coordinates, B1 and B2, that lies closest to one of the red coordinate, R1. Basically, we want to find the V (as in Virtual) coordinate in order to find the time interpolation between the B1 and B2 coordinate. All we have to do is to calculate the ratio of the distances between the coordinate V and the B1 and B2 coordinates. This ratio will give us the needed number in order to calculate a new blue time stamp at which the virtual V coordinate is located.

So in theory, this algorithm seem to boil down to the following steps:

  1. Find the two blue stars that are closest to the red star.
  2. Find the coordinates of the virtual coordinate V.  
  3. Calculate the ratio of the distances between coordinate V and the two closest blue coordinate.
  4. Calculate the virtual timestamp of coordinate V using the ratio found in step 3.
  5. Compare the timestamp for the red coordinate with the new virtual timestamp for the coordinate V.

The only difficulty here really lies in the second step of this algorithm, i.e. finding coordinate V. So let us concentrate on the information we do know, i.e. the known variables. Consider the following image:

Here, d1 is the distance between B1 and R1, d2 similarly between R1 and B2, and finally d3 being the distance between B1 and B2. Ok, these distances are not known, but we do know that if we have the coordinates, we can easily calculate the distances using … yes you guessed it: the Pythagorean Theorem. So we can consider these distances as known.

Ok, next would be to calculate the angle α at the B1 coordinate. Knowing all distances d1, d2, and d3 in this triangle we can utilize the Law of cosines:

d22  = d12 + d32  - 2d1d3cos(α)

The only unknown variable here is the α angle which a bit of algebra magic solves for us.

So when we have α, we can calculate the distance dv, i.e. the distance between B1 and V coordinates with the following basic trigonometry formula:

Cos(α) = dv  / d1.

Again, a bit of algebra gives us dv and you know what? We are actually home safe now with this knowledge. Basically we now know the ratio in distance V between B1 and B2. Now all we have to do is to apply that ratio to the timestamp of B1 to get V coordinates virtual timestamp.

I have not yet had the time to implement the second “interpolating the time” algorithm in my Running Mate code demo as available at Codeplex, but when I have I will delete this sentence and add a comment about it.

Sunday, November 02, 2008 1:54:50 PM (GMT Standard Time, UTC+00:00)
 Friday, October 31, 2008

This blog post is part of a series of blog post that starts here.

Well, to be honest, it has been available all since this Monday before the Swenugpresentation. But I just added some missing files too so now it actually compiles too... Sorry about this if someone chose to download the code right after the presentation.

As perviously stated in this blog post series, I initiated the creation of the Running Mate mostly because I needed a demo application for a performance talk. It served its purpose well I think, and I think I can make use of this applications in upcoming presentations. However, being a demo application, it is far from complete in functionality:

  • There is no PDA based version of this code built yet. I.e. the device that tracks and sends the GPS ticks as you run to the server. Obviously without this, there is no purpose of the Running Mate. So I hope that I or someone else will have the time to build this eventually.
  • There is no real Graphical User Interface built yet. I built a test load client for simulating the load and I also built a windows client application for inserting the test data into the database which both are available. But the real GUI is not built yet. I have some ideas for this and hopefully someone will help out with this GUI eventually since GUI is not my cop of tea...

As for all the source code it is available here: http://www.codeplex.com/runningmate. It is published under the GNU GPL v2 license model.  You can quite easily use Subversion (TuroiseSvn client) URL at https://runningmate.svn.codeplex.com/svn or the TFS Server URL at https://tfs05.codeplex.com.

I am usnig SVN myself and unfortunately, the throughput is very low. I suspect Microsoft is actually to blame here since Codeplex is a very popular open source repository these days. Microsoft don't seem to have scaled codeplex to meet the demand. If it does not improve or if I see it is a problem I might move to another availble code repository. We'll see.

The current file folder structure in the repository looke like this:

TheRunningMate_filestructure.JPG

Although this most likely will change in the future, the idea is that a working, non-demo version of the code will be available in parallell with the DemoVersion folder. Eventually..

In the VisualStudio folder, all the C# 3.0 code is placed. I am using Visual Studio.NET 2008. Currently, I am running MySql as the data persistance provider, but I might make more options available too. After all, MySql requires a MySql server to be installed in some way or another (there are quick ways to install it) so it is a bit too much of a hassle to get the application up and running if you are not using MySql. The DB folder has everything you need here though for the application script wise, but please let me know if something is missing.

Hmm, that's it for know I think. I'll back on the topic of design and architecture in an upcoming blog post.  

Friday, October 31, 2008 4:37:03 PM (GMT Standard Time, UTC+00:00)
 Sunday, October 26, 2008

     This blog post is part of a series that starts here.

I have long distance running as a small interest of mine. It is far from a passion, but I try to get out in the forest and run a distance of approximately 10 km every weekend. I usually run in a park called Hisingsparken here in Göteborg which is a beautiful and peaceful forest to run in. With the courtesy of shapelink.com , here is a map of the 10 km lap that I usually run.

hisingsparken.jpg

Well, beautiful and peaceful isn’t all, is it? The current condition and health situation surely makes running a lot more enjoyable. And in order to get in fit which makes running a great sensation, you have to run faster and push yourself a bit.  So this blog post is basically about finding this motivation with something called the Running Mate.

In order to motivate myself to run a bit faster, I clock the time it takes to run the distance. But unfortunately I seem to be stuck at about 55 minutes for 10 km run which is a bit too long time I think... I know. There are various good ways to improve the time: run more often, vary with shorter distances etc. If lowering the time would have been of really strong importance to me, these ways of doing would probably suffice for me. But as it, my level of interest makes me look in the direction of technical tools to help me out…                          

My problem when I run is probably motivation and focus at the running process itself. I let my mind wonder and I think of everything except keeping the correct pace. Running is great for problem solving by the way. Anyway, the clock does not give me this feedback since I have no idea of what time I am supposed to have when I am looking at the clock during the run. Naturally, this can be solved by having a number of part times at specific distances. And I do, but I don’t really want to keep track of too many of these part times since it disturbs my thoughts on other things. I would like to be able to know instantly, at the time of interest, what my pace is compared with a previous run at that exact spot of the run.

So the answer is obvious: The running mate application. It has been possible for quite some time now to buy a GPS equipped watch with this type of applications. The main idea is that it should be possible to run against yourself, i.e. being able to compare your current running time against a previous time you have had in a previous run at each and every spot of the run.

So I could have gone ahead and bought this GPS device. But something has stopped me so far. Of course it would be cool and exciting to fabricate this tool myself with the help of a mobile phone and a GPS device. Not that I know if I will ever get around constructing the whole thing, but I intend to build something in code and blog about it. I do this partly because it is a fun thing to do, but also since I think this application could be the basis for future examples both on the blog as for presentation I do.

So next blog post, coming up soon I hope, will be about the mathematical geometry needed complete such an application. Fun stuff I think.

Sunday, October 26, 2008 9:39:54 PM (GMT Standard Time, UTC+00:00)

For quite some time now, I’ve been thinking of the design of a so-called Running Mate application. I decided that it was time to implement it and the trigger that set me of was to have a fun example to use at a performance tuning presentation. So beneath is what I have in mind as blog topics although it can change somewhat as I go ahead:

  1. The Running Mate – What is it and why?
    This is a personal touched blog post with the background story as to why on earth I think the Running Mate application is a nice thing. Off course, this blog post also lets you know what the application is actually doing
  2. The Running Mate – Code available at Codeplex
    Source code available and some words about what is provided and what is not provided in the repository as well as some file structure information.
  3. The Running Mate – The geometry calculations
    In order to implement this application some interesting geometric calculation is needed. This blog describes these calculations.
  4. The Running Mate – The Super Hack implementation
    Sometimes I start off by doing a rather quick and dirty implementation of the application and then refactor my way into a more nice design. I didn't actually do this for this one, but I needed super hack implememntation for my presentation. So this is breif presentation of a quick and dirty implementation.
  5. The Running Mate – The Domain Driven Design approach
    In this blog post I transform the application into a design that is closer to a Domain Driven Design.
  6. The Running Mate – The O/R Mapper Repository
    The repositories built in the previous blog post uses a classical database access layer wrapped in repositories with inline (ad hoc) SQL. Here’s an alternative approach with the MindScape LightSpeed O/R mapper.
  7. The Running Mate – The loosely coupled Domain Layer
    Most O/R mappers integrate into the Domain Model rather tightly and make it a bit more difficult to switch from one O/R mapper to another one. This is a bit strange since O/R mappers themselves make the choice of underlying database very flexible. In this blog post I describe how you can decouple a domain model from the application so that you can have several different versions of domain layers that the application can utilize. The loosely coupled domain model is not always that useful in reality, but it is a nice experiment.   

I may change the topic names slightly as I go ahead and write about them, but I hope you can live with this. There are actually many more blog posts that I can write based on this application. For instance I would like to write some more about the LightSpeed O/R mapper and how you can wrap the basic CRUD stuff to make these methods really easy to access with a simple API. Also, there is a lot of stuff on performance tuning this application that I would like to write about eventually. But I better not promise too many blog topics in advance since you never know what lies round the corner.

So. Basically, I am back in the blogosphere for another round.

Sunday, October 26, 2008 9:12:12 PM (GMT Standard Time, UTC+00:00)