Free cookie consent management tool by TermsFeed Policy Generator

Changes between Initial Version and Version 1 of Documentation/DevelopmentCenter/IntegrateHeuristicLab


Ignore:
Timestamp:
06/15/12 19:41:56 (12 years ago)
Author:
ascheibe
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/DevelopmentCenter/IntegrateHeuristicLab

    v1 v1  
     1= How to integrate HeuristicLab into an application =
     2This HowTo will show you how you can write a console application that reads in a file with an user-defined algorithm, parameterizes the algorithm and the problem and then executes the algorithm and displays the results. This is useful if you want to write an application that you can start from another program or you can use the code in your own .NET application if you need the functionality that HeuristicLab offers.
     3
     4Attached to this page you can find a zip file containing the sample HeuristicLab file, the needed assemblies as well as a Visual Studio project with the source code.
     5
     6We will start out by creating a new Visual Studio C# console application. Because we want to read in a user-defined algorithm based on a GA containing a Traveling Salesman Problem, we first need to reference the following assemblies:
     7
     8[[Image(assemblies.png)]]
     9
     10We will now write all the code for loading and executing the algorithm in the Program.cs generated by Visual Studio.
     11The following snippet shows the Main method of the program:
     12
     13{{{#!csharp
     14 static void Main(string[] args) {
     15      string samplePath = @"..\..\User-defined-algorithm.hl";
     16      ContentManager.Initialize(new PersistenceContentManager());
     17      ContentManager.LoadAsync(samplePath, LoadingCompleted);
     18      Console.ReadLine();
     19 }
     20}}}
     21
     22In the first line the path to the HeuristicLab file containing the algorithm and the problem is specified. In the next step the Content and Persistence Manager is initialized so that HeuristicLab files can be loaded in the next step. We use the ContentManager to load the file and on completion, the LoadingCompleted method is called:
     23
     24{{{#!csharp
     25private static void LoadingCompleted(IStorableContent content, Exception error) {
     26      if (error != null) throw error;
     27      Console.WriteLine("Loading completed!");
     28      Console.WriteLine("Content loaded: " + content.ToString());
     29
     30      if (content is UserDefinedAlgorithm) {
     31        UserDefinedAlgorithm algorithm = content as UserDefinedAlgorithm;
     32        TravelingSalesmanProblem problem = algorithm.Problem as TravelingSalesmanProblem;
     33        if (problem == null) {
     34          Console.WriteLine("Unsupported problem type detected!");
     35          return;
     36        }
     37
     38        ParameterizeProblem(problem);
     39        ParameterizeAlgorithm(algorithm);
     40
     41        RegisterAlgorithmEventHandlers(algorithm);
     42        algorithm.Start();
     43      } else {
     44        Console.WriteLine("Unknown content in file: " + content.ToString());
     45      }
     46    }
     47}}}
     48
     49The LoadingCompleted method gets an object of type IStorableContent. This is the object which the ContentManager loaded from the HeuristicLab file. In this simple example we only support user-defined algorithms and the TSP. Therefore we cast the algorithm to a user-defined algorithm and the problem to the TSP. In the next step we can then set the parameters of the problem and the algorithm. After parameterizing the algorithm we have to register event handlers for the algorithm so that we receive status updates during execution. The Parameterize* methods set parameters of the algorithm and the problem:
     50
     51{{{#!csharp
     52private static void ParameterizeAlgorithm(UserDefinedAlgorithm algorithm) {
     53      //set all parameters of the algorithm, e.g.
     54      ((IntValue)algorithm.Parameters["MaximumGenerations"].ActualValue).Value = 5000;
     55}
     56
     57private static void ParameterizeProblem(TravelingSalesmanProblem problem) {
     58      //set all parameters of the problem, e.g.
     59      problem.Maximization.Value = false;
     60}
     61}}}
     62
     63Algorithm parameters can be accessed through the parameters collection. In the code above the maximum generations is set from 1000 generations to 5000 generations.
     64The problem has various properties that can be set, like in the above code we define that the problem is a minimization problem. Another example would be to set the problem data using the coordinates or distance matrix properties.
     65
     66RegisterAlgorithmEventHandlers registers for some events so that the application can print the execution time and state changes of the algorithm during execution.
     67
     68{{{#!csharp
     69private static void RegisterAlgorithmEventHandlers(EngineAlgorithm alg) {
     70      alg.ExecutionStateChanged += new EventHandler(alg_ExecutionStateChanged);
     71      alg.ExecutionTimeChanged += new EventHandler(alg_ExecutionTimeChanged);
     72      alg.Stopped += new EventHandler(alg_Stopped);
     73}
     74
     75    private static void DeregisterAlgorithmEventHandlers(EngineAlgorithm alg) {
     76      alg.ExecutionStateChanged -= new EventHandler(alg_ExecutionStateChanged);
     77      alg.ExecutionTimeChanged -= new EventHandler(alg_ExecutionTimeChanged);
     78      alg.Stopped -= new EventHandler(alg_Stopped);
     79}
     80}}}
     81
     82The stopped event is called when the algorithm has finished it's execution. In the event handler the results of the algorithm are queried and printed to the console.
     83
     84{{{#!csharp
     85    static void alg_Stopped(object sender, EventArgs e) {
     86      EngineAlgorithm alg = sender as EngineAlgorithm;
     87      Console.WriteLine("Run is finished! Results are: ");
     88
     89      foreach (var s in alg.Results) {
     90        Console.WriteLine(s.Name + ": " + s.Value);
     91      }
     92      DeregisterAlgorithmEventHandlers(alg);
     93    }
     94
     95    static void alg_ExecutionTimeChanged(object sender, EventArgs e) {
     96      EngineAlgorithm alg = sender as EngineAlgorithm;
     97      Console.WriteLine("Execution time changed: " + alg.ExecutionTime);
     98    }
     99
     100    static void alg_ExecutionStateChanged(object sender, EventArgs e) {
     101      EngineAlgorithm alg = sender as EngineAlgorithm;
     102      Console.WriteLine("Execution state changed: " + alg.ExecutionState);
     103    }
     104}}}
     105
     106And that's all! You have now a HeuristicLab console client for a specific algorithm and problem. If you want to use different algorithms and problems, you have to reference the required assemblies and can then parameterize and execute these algorithms.