Posts

Showing posts with the label Algorithms

Applying Ant Colony Optimisation to travelling salesman problems in C#

Image
Some results of applying a C# / WPF implementation of the ant colony optimisation algorithm to the travelling salesman problem (TSP). My initial observation is that it finds fairly reasonable solutions within a given number of iterations, but falls short of algorithms such as two-opt, Lin-Kernighan etc. The software is built around the Model-View-ViewModel (MVVM) architecture, thereby keeping the graphical display and data separate. For an explanation of the ant colony algorithm see the Wikipedia page . Edge selection Each ant iteratively finds a path from the source node, visiting every other node until it reaches the start node again. The intermediate solutions (node choices) are referred to as solution states. At each iteration, each ant moves from a state x to state y, corresponding to a more complete intermediate solution. Thus, each ant k computes a set of feasible expansions to its current state in each iteration, and moves to one of these in probability. ...

Obtaining combinations of k elements from n in C#

Image
I needed an algorithm that could return all combinations of k number of elements within a finite set of n elements. Furthermore I wanted the algorithm to work in a non-recursive (iterative) way in C#. Step forward StackOverflow, specifically this answer given by Juan Antonio Cano I've taken his code and just made one or two modifications, as suggested by ReSharper . Full code listing for the console app as shown: Program.cs [code language="csharp"] using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Techniques { public static class Program { private static bool NextCombination(IList<int> num, int n, int k) { bool finished; var changed = finished = false; if (k <= 0) return false; for (var i = k - 1; !finished && !changed; i--) { if (num[i] < n - 1 - (k - 1) + i) { num[i]++; ...