Posts

Showing posts from April, 2012

C++ Implementation of Hill-climbing and Simulated Annealing applied to Travelling Salesman Problems

Image
Introduction Following from a previous post , I have extended the ability of the program to implement an algorithm based on Simulated Annealing and hill-climbing and applied it to some standard test problems. Once you get to grips with the terminology and background of this algorithm, it's implementation is mercifully simple. The algorithm can be tweaked such that it can also be implemented as a greedy hill-climing heuristic.

C++ Implementation of 2-opt to the "Att48" Travelling Salesman Problem

Image
Introduction Some initial results from experimenting with the 2-opt heuristic and applying it to a standard traveling salesman test problem. C# / WPF equivalent implementation can be found here: https://www.technical-recipes.com/2017/applying-the-2-opt-algorithm-to-travelling-salesman-problems-in-c-wpf/ A summary of the 2-opt heuristic is given here: http://en.wikipedia.org/wiki/2-opt A nearest neighbour search algorithm is included in the 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 neighbour algorithm.

Number System Conversions in C++

A posting which I will probably update from time to time that summarizes the conversion functions I encounter in C++. Hope others will find this useful too. 32-bit IEEE 754 floating point value to binary string [code language="cpp"] std::string GetBinary32( float value ) { union { float input; // assumes sizeof(float) == sizeof(int) int output; } data; data.input = value; std::bitset<sizeof(float) * CHAR_BIT> bits(data.output); std::string mystring = bits.to_string<char, std::char_traits<char>, std::allocator<char> >(); return mystring; } [/code] 32-bit IEEE 754 binary string to floating point value [code language="cpp"] float GetFloat32( std::string Binary ) { int HexNumber = Binary2Hex( Binary ); bool negative = !!(HexNumber & 0x80000000); int exponent = (HexNumber & 0x7f800000) >> 23; int sign = negative ? -1...

A Genetic Algorithm Function Optimizer in C++

Image
Introduction An example of how a genetic algorithm can be applied to optimize standard mathematical functions, such as the Rosenbrock function. (Image obtained from the Wikipedia page.)