Posts

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]++; ...

Applying the 2-opt algorithm to travelling salesman problems in C# / WPF

Image
For the Java equivalent see this link: https://www.technical-recipes.com/2017/applying-the-2-opt-algorithm-to-traveling-salesman-problems-in-java/ For the C++ equivalent see this link: https://www.technical-recipes.com/2012/applying-c-implementations-of-2-opt-to-travelling-salesman-problems/ This post demonstrates how to apply the 2-opt algorithm to a number of standard test problems in C# while displaying the results in a WPF style window, while using the MVVM design pattern. See this link for an overview of the two opt algorithm. http://en.wikipedia.org/wiki/2-opt But essentially the 2-opt link swapping heuristic can be summarised by the following steps: The actual 2-opt heuristic can be summarised by the following pseudocode steps, repeating for all feasible combinations of I and k: [code language="text"] 1. take route[1] to route[i-1] and add them in order to new_route 2. take route[i] to route[k] and add them in reverse order to new_route 3. take r...

Using the Supervisor Controller Pattern to access View controls in MVVM

Image
As discussed on a StackOverflow post , this is considered bad practice, so only use this when necessary. Step 1: Create a new WPF project Step 2: Add event handling code A number of classes are needed for doing command bindings in MVVM. Create the following classes and add to your Visual Studio project: RelayCommand.cs [code language="csharp"] using System; using System.Windows.Input; namespace SupervisorControllingPattern { public class RelayCommand<T> : ICommand { private readonly Predicate<T> _canExecute; private readonly Action<T> _execute; public RelayCommand(Action<T> execute) : this(execute, null) { _execute = execute; } public RelayCommand(Action<T> execute, Predicate<T> canExecute) { if (execute == null) throw new ArgumentNullException(nameof(execute)); _execute = execute; _canExecute = canExecute; ...

A simple example of using dependency injection using Unity in WPF

Image
Step 1: Create a new Visual Studio project Step 2: Add the necessary Unity reference Select Tools > NuGet Package Manager. Type in the following command: [code language="txt"] Install-Package Unity -Version 4.0.1 [/code] Unity website for NuGet can be found here: https://www.nuget.org/packages/Unity/ Step 3: Remove the startup URL In App.xaml get rid of the StartupUri value: Step 4: Add the Model class / interface IModel.cs [code language="csharp"] namespace DependencyInjection { public interface IModel { } } [/code] Model.cs [code language="csharp"] namespace DependencyInjection { public class Model : IModel { public Model() { Text = "Hello"; } public string Text { get; set; } } } [/code] Step 5: Add the ViewModel class To deal with the error that we receive in the XAML when a ViewModel has "No parameterless constructor defined for this object" ...

Using fullpage.js to implement HTML page scrolling

Image
Some instructions on how to use fullpage.js to scroll between sections in your web page. I am not a web developer, so this post will contain a minimum of javascripting, css styling and so on. I just wanted to see how fullpage.js can be incorporated so that individual web pages can be scrolled horizontally in an animated way. Step 1: Download fullpage.js Fullpage.js javascript can be downloaded from here: https://alvarotrigo.com/fullPage/ This will download a file called fullPage.js-master.zip. Once the zip file has been downloaded, extract it somewhere. Step 2: Create folder structures for your web page(s) I have created a folder called 'FullPage' containing the subfolders 'html' and 'resources' in which the HTML file(s) and style sheets/javascripts will be contained respectively: Step 3: Install the necessary styling and resources. In the 'resources' folder create a further two subfolders 'css' and 'javascript'...

Applying the 2-opt algorithm to travelling salesman problems in Java

Image
This post tackles the problem of applying the 2-opt algorithm to travelling salesman problems in Java. The results of applying the 2-opt heuristic and applying it to a number standard traveling salesman test problems. are shown For a more in-depth description of the 2-opt heuristic, please refer to the following Wiki page: http://en.wikipedia.org/wiki/2-opt The actual 2-opt heuristic can be summarised by the following pseudocode steps, repeating for all feasible combinations of I and k: [code language="xml"] 1. take route[1] to route[i-1] and add them in order to new_route 2. take route[i] to route[k] and add them in reverse order to new_route 3. take route[k+1] to end and add them in order to new_route 4. return the new_route; [/code] A nearest neighbour search algorithm is included in the Java implementation. A comparison is made of the kind of results we get from the 2-opt algorithms, with and without improving the initial tour using the nearest n...

Creating installations using Inno Setup in Visual Studio

Image
Step 1: Run the Inno Setup installer When using Inno Setup within Visual Studio project as a post-build exercise to create an installer package, you will need the contents of the Inno Setup folder as this will contain the command line executables, dll files etc. If you have not already done this, you can get it from here: http://www.jrsoftware.org/ Step 2: Create a new Visual Studio project Right click on the solution and select to add a new Installer project (class library) 3. Add 'Inno Setup' contents to your Installer project I like to keep everything completely self-contained within Visual Studio projects, so whether you prefer to reference the Inno content that has already been installed to the Program Files folder is up to you Right-click and select Add > New folder. In Windows Explorer copy the existing Inno Setup folder contents to the one you created in your Visual Studio project. Once the contents are copied over I then right-click the...