Monday, September 14, 2009

Simulated Annealing - Poker Solitaire and C#

I recently revisited the Simulated Annealing heuristic algorithm using C#. This algorithm is traditionally the choice for solving the Traveling Salesman problem, but I wanted to tackle another large combinatorial problem, Poker Solitaire.

For a description of the problem, visit Dr. Dobbs original article at:

http://www.ddj.com/184408203?pgno=16

How do you calculate a result with a solution set of 25 factorial (15,511,210,043,330,983,907,819,520)?

The Visual Studio 2008 solution (see below) provides a console application that repeatable executes a single annealing process, each time trying to optimize a 5x5 matrix of playing cards. The algorithm attempts to lay the cards out to optimize 12 poker hands (5 rows, 5 columns, 2 diagonals).

You can execute the console application using the syntax below, passing it the name of a text file that represents 25 cards for the 5x5 matrix.

C:> SimulatedSolitaire.Console.exe TestData\BestCards.txt

Download the Visual Studio 2008 solution by clicking here.

Wednesday, September 2, 2009

Is "simple" better?

Just thought I'd pass along a lesson that I continue to learn.

A couple of years ago I had the need to know my ever-changing home IP address. Granted there are some free tools like Dynamic DNS and others that do this, but I had some special requirements that the IP had to be placed on another server for special reasons.

When I wrote the code, I created a service that ran on my home server. I used a C# interface so that I could "one day" move other types of data from my home PC to a server. I also had to have Visual Studio create the service installer and manually install the service. I created my Visual Studio solution with nicely decoupoled projects (assemblies). I was a genius.

Recently I had to change some stuff in that piece of code. The more I thought about it (and tried to remember all the code parts), I realized had I just taken a pure TDD (test-driven development) approach I could have done it much simpler. I then sat down and wrote a simple console app main() with about 4 lines of code to call a web page with a magical query parameter that did everything I needed. A quick job creation in Windows Task Manager and things were now running nicely.

Much less code and more importantly, much easier to maintain and make sense of. I know we all hear often (and I preach it as well) that we are to develop "extensible" code, but the reality of it is that probably 90% of the code we write will never even need to be extended. Given that, a TDD attribute is warranted in many cases (if not all of them).

I'm still learning that "simple is better" a lot of the time.

Wednesday, August 26, 2009

Prevent XSS attacks

I found this link very helpful in understanding and providing a good set of rules for protecting against Cross Site Scripting (XSS) attacks.

http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet

Tuesday, May 26, 2009

jQuery 1.3 Cheat Sheet

Looking for a handy cheat sheet of jQuery objects and methods? Here is a pretty good two page guide in PDF format.

jQuery 1.3 cheat sheet (2 pages, 8.5" x 11")

Sunday, May 24, 2009

How to install MVC 1.0 on a web server

Eventually you are probably going to want to install MVC on a web server. Assuming you do not have Visual Studio on that server, the command to install the MVC installation package is (all on a single command line):

msiexec /i AspNetMVC1.msi /q /l*v mvc.log MVC_SERVER_INSTALL="YES"

You will need to execute this command in an elevated command prompt window.

Wednesday, May 13, 2009

Learn jquery

Tired of your websites looking years old? Want a bit of that "Web 2.0" look-n-feel?

I highly recommend web developers learn jquery. The overhead of the included JavaScript (15K) and the learning curve are both so minimal that there shouldn't be an excuse for not using it.

There are not a lot of books out there on jquery yet (at least not around May 2009), but I would recommend this one.

Learning jQuery 1.3.

It's a pretty good book. The index is bad, but the coverage of jquery in the book should be well worth the read.

Thursday, April 16, 2009

Web developers should know CSS

I speak from experience when I say that web developers should understand the basics of cascading style sheets (CSS). Granted, when I first looked at CSS and tried using it for page layouts a few years ago, I couldn't understand why anyone would use CSS for layouts when we all used tables instead*.

Well, years later I'm convinced that tables are for presenting tabular data, not page layouts.

Developers don't have to be CSS gurus, but they should understand what a "div" really is and how a "float" style attribute works. They need to understand the "boxing model" and how IE handles it differently than do other browsers. CSS is much more than just applying different colors and text attributes.

If you want to take your web development skills up a notch, I recommend the following book I purchased a few years back. It is short, concise, and easy to read. Plus, as far as books go, it's pretty cheap.

CSS Mastery from Amazon

Update: A major advantage to knowing CSS is that it will make the adoption of tools such as jquery much easier!

*About the only time I favor a table layout is when I have to bottom-align tabular data and don't want to set a fixed height for content.

Tuesday, March 17, 2009

Windows Explorer - Setting default size and location

Want to set the default size and window location for Windows Explorer on Vista or later?

I've got a new monitor that is larger than my old one. When I would open Windows Explorer the first thing I would always do is resize it to larger size.

I found out you can set the default Explorer size and windows location by simply holding the SHIFT key down when closing the window. Presto!

Sunday, March 8, 2009

Catch (Exception e): Right or wrong?

I've long heard that you shouldn't throw a type Exception in C#. I try to always throw a type derived from Exception.

In addition, I've also read many brief snippets over the years that says you shouldn't write C# code like:

catch(Exception e)
{
...
}

I recently read the best explanation of this in Microsoft's MSDN Magazine (Feb. 2009). The article is entitled Handling Corrupted State Exceptions and would highly recommend those programming in C# to read the full article. It also provides details about how exception handling will change a bit in .NET 4.0.

A read well worth it!

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

Tuesday, February 17, 2009

ASP.NET CodeBehind vs. CodeFile

I use Visual Studio 2008. Once and a while I get a .NET error that causes me to modify the @Page directive from CodeBehind to CodeFile, or visa-versa.

Folks tend to give all types of lengthy explanations on when to use one vs. the other. Here is a quick explanation...

.NET Web Sites ---> Use CodeFile
.NET Web Apps ---> Use CodeBehind

Done. Any questions?

Tuesday, February 3, 2009

Remove / Uninstall old devices (network card)

I've run into this twice during the past month so I figured I had better post something about it.

Once and a while I remove something like a network or graphics card from a computer. If I do this while the computer is off, then the next time the computer is turned on, the old device is still there, sometimes hidden. Attempts to install a replacement device may either fail because of "not enough resources" or something else like a PCI slot conflict.

First, before uninstalling a device, use the device manager and perform an Uninstall. If you do that, the information below won't be needed. But if you are like me and remove device while computers are off (normally old ones sitting in closets), you may need to do the following...

Start the computer and open a console window (may want to run with Admin privs for Vista or later operating systems).

Set the following environment variable:

set devmgr_show_nonpresent_devices=1

Now open the device manager via the console window (must be done via the console window since this is where the new environment variable exists).

devmgmt.msc

On the View menu, enable "Show Hidden Devices" then find the old/removed device and uninstall it.

For best results, reboot!

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...