Adam Stirtan

ASP.Net Nested Repeater with Paging Tutorial

In developing web applications using ASP.Net one of the most hated components of programmers is nested repeaters. I covered this topic before another tutorial found here. What I'd like to show you now is how to extend this further by paging the data source such that groups of elements show on individual pages, similar to how Google's search results are displayed.

We'll start by showing you the desired result. Pictured here is a paged and nested repeater. The outer repeater is a snippet from a discussion board where users can post messages to others. For each of these it is possible to comment on them. This is where we get the nesting requirement, as the comments are specific to the outer repeater items.

At bottom is a button which allows for navigating to the next page. In this picture we are displaying the first page, if we weren't (and not on the last page) another button would be visible allowing the action "Previous" to be handled.

Now that we have the concept let's look at how we accomplish this behavior. It should be clear that we require two distinct data sources and two repeaters. One repeater inside the other.

I have created a method which is executed inside the Page_Load(..) method of the web page as follows:

protected void SetupPaging()
{
PagedDataSource objPds = new PagedDataSource();
DataTable dt = //Retrieve outer DataTable type

bjPds.DataSource = dt.DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 10;
objPds.CurrentPageIndex = CurrentPage;
btPrev.Visible = !objPds.IsFirstPage;
btNext.Visible = !objPds.IsLastPage;

DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(// Get inner repeater DataTable);
DataColumn dcPosts = ds.Tables[0].Columns["DiscussionPostID"];
DataColumn dcReplies = ds.Tables[1].Columns["DiscussionPostID"];
ds.Relations.Add(new DataRelation("replies", dcPosts, dcReplies));

rptDiscussion.DataSource = objPds;
rptDiscussion.DataBind();
}

So lets examine this piece by piece to see how this works. First we instantiate a PagedDataSource object, this type has properties for setting up how many items per page and allows for a DataPager object to control which page it is currently on. The main thing we focus on however is where we set its DataSource property. We set this to the outer table's DefaultView.

Moving on, we create a DataSet, this is going to be the aggregate of both outer and inner tables. To setup the nesting we associate the two tables with a relationship. This relationship assigns which columns in both table relate to each other. In my example image, the relationship was in the outer repeater (Primary Key) and its corresponding discussion post. Each comment was associated with this discussion post and as such we assign the column as "DiscussionPostID".

Finally we add this association to the DataSet object and bind the repeater to the PagedDataSource. Our repeater is now ready for action, the only thing left is to add some controls for getting the current page. This is a straight forward method which we add to the code behind file as follows:

public int CurrentPage
{
get
{
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0;
else
return (int)o;
}

set
{
this.ViewState["_CurrentPage"] = value;
}
}

That completes our tutorial for now, if you have any questions leave a comment and I'll get back to as best I can.

PSO for Evolving Game Playing Agents

Tomorrow I will be giving my final presentation on the progress of my computer science thesis. The title of my paper is 'Particle Swarm Optimization for Evolving Game Playing Agents' and focuses on implementing PSO for the game of Super Mario Bros. As part of this presentation I've prepared a new set of PSO slides which walks through some background information and the details on how it is applied to games.


My paper will attempt to demonstrate the viability for PSO game agents in a generalized way which can be applied to any game type. I've given high level examples of how this can work for the game of Checkers and a brief overview for how it would be applied to a 3D first person shooter such as Halo. While these components are not implemented, it was the goal of the project to rather develop a method for PSO which simply functioned for games.

The result, in my opinion is astounding. I've posted videos before of the Mario PSO agent in action. My only regret is that I think I was a little late to the Mario competition party and I won't be able to compete this year. It was rumored that during the next round they will be introducing water to the game. I'm confident in the algorithm to be adaptable, more so than static hard coded search strategies some other people tried. Perhaps I'll get my chance then.

Happy coding! Keep fit and have fun.

Cascade Correlation Seminar

I thought I would make available some presentation slides I have created recently. I will be giving a presentation in my course for artificial neural networks tomorrow on cascade correlation.

Download Slides

For those unacquainted with cascade correlation it is a dynamic neural network which as it is processing determines its own topology. Networks begin with only input and output layers, and as necessary hidden units are introduced to the network.

The process requires to back propagation of error signals and often results in very unique solutions to problems. Cascade correlation's learning process is many times quicker than traditional back propagation, but in smaller problems its accuracy suffers. However, due to its dynamical creation, it tends to scale to much larger problems more efficiently while maintaining its break neck speeds of classification.

It's something I would like to experiment with over the summer once things calm down a bit. There are a few unexplored areas of study involved which I think potential papers are lurking.
I've finished my XNA simulation for my machine learning course this previous semester and am posting my resulting source code for all those interested. The project was an open ended venture in to machine learning. I chose to implement evolutionary neural networks and perform a comparative analysis between two biologically inspired algorithms as the basis for the neural network's weight adjustments.

Download Source Code

The simulation involves no user interaction beyond the included Settings.ini file where parameters of the simulation can be adjusted. This includes learning rates, PSO and GA settings and how long the simulation runs per generation.

If someone is looking for a proper implementation of a neural network in C# one can surely be found here which could be used in any area of necessity, there is nothing specific to this project.

Using just about all fathomable parameters I have found with this experimentation that PSO finds more optimal weights for the neural networks. They are found faster and retainment of these solutions are better compared to GA. However it should be noted that adaptive mutation is not implemented; that approach would likely increase the chances for GA to consistently compete in later generations.

Evolutionary Tanks

Here is the latest video from my machine learning course. This is my term project where I use evolutionary algorithms for weight updates in an artificial neural network. It was written to simulate a game for a better visual understanding of the learning capabilities. More statistics are going to be included, but I thought I'd share with everyone and bask in the glory that the learning is in fact taking place.

Blue tanks update using a genetic algorithm and red tanks use particle swarm optimization. This is a snapshot of several successive generations starting from the beginning all the way to where over fitting has occurred.

Once I clean up the source code more I'll share it here as well.

Engagement Photos

Here's a quick gallery of some of recently taken engagement photos. They turned out really well, by that I mean of course I don't look like an idiot in all of them.

Particle Swarm Optimization Tutorial Slides

Tomorrow marks the day of a seminar I will be holding in my Machine Learning course at Brock University. I have made my power point presentation slides available to all those interested. It provides an overview of how the optimization technique is formulated from biological inspirations to algorithm implementation to applications and examples. It features short videos found on the Internet and a sample video of my Mario PSO agent in its latest form.

It is targeted to a person with no such familiarity in PSO with a background in computer science or at least a passion for artificial intelligence.