Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/31/16 15:11:46 (8 years ago)
Author:
gkronber
Message:

implemented random search for binary problems

Location:
branches/Templates/EmptyAlgorithm/EmptyAlgorithm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/Templates/EmptyAlgorithm/EmptyAlgorithm/EmptyAlgorithm.csproj

    r14363 r14364  
    4747      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
    4848    </Reference>
    49     <Reference Include="HeuristicLab.Encodings.IntegerVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     49    <Reference Include="HeuristicLab.Encodings.BinaryVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    5050      <SpecificVersion>False</SpecificVersion>
    51       <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Encodings.IntegerVectorEncoding-3.3.dll</HintPath>
     51      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Encodings.BinaryVectorEncoding-3.3.dll</HintPath>
    5252    </Reference>
    5353    <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     
    6565    <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    6666      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     67    </Reference>
     68    <Reference Include="HeuristicLab.Problems.Binary-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     69      <SpecificVersion>False</SpecificVersion>
     70      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Problems.Binary-3.3.dll</HintPath>
     71    </Reference>
     72    <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     73      <SpecificVersion>False</SpecificVersion>
     74      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    6775    </Reference>
    6876    <Reference Include="System" />
  • branches/Templates/EmptyAlgorithm/EmptyAlgorithm/MyAlgorithm.cs

    r14362 r14364  
    1 using System.Threading;
     1using System;
     2using System.Threading;
    23using HeuristicLab.Common; // required for parameters collection
    34using HeuristicLab.Core; // required for parameters collection
    45using HeuristicLab.Data; // IntValue, ...
     6using HeuristicLab.Encodings.BinaryVectorEncoding;
    57using HeuristicLab.Optimization; // BasicAlgorithm
    68using HeuristicLab.Parameters;
    79using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     10using HeuristicLab.Problems.Binary;
     11using HeuristicLab.Random; // MersenneTwister
    812
    913namespace EmptyAlgorithm {
     
    1721  [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
    1822  public class MyAlgorithm : BasicAlgorithm {
     23    // This algorithm only works for BinaryProblems.
     24    // Overriding the ProblemType property has the effect that only BinaryProblems can be set as problem
     25    // for the algorithm in the GUI
     26    public override Type ProblemType { get { return typeof(BinaryProblem); } }
     27    public new BinaryProblem Problem { get { return (BinaryProblem)base.Problem; } }
     28
    1929    #region parameters
    2030    // If an algorithm has parameters then we usually also add properties to access these parameters.
     
    5363    protected override void Run(CancellationToken cancellationToken) {
    5464      int maxIters = MaxIterations;
     65      var problem = Problem;
     66      var rand = new MersenneTwister(1234);
     67
     68      var bestQuality = problem.Maximization ? double.MinValue : double.MaxValue;
     69
     70      var curItersItem = new IntValue();
     71      var bestQualityItem = new DoubleValue(bestQuality);
     72      var curItersResult = new Result("Iteration", curItersItem);
     73      var bestQualityResult = new Result("Best quality", bestQualityItem);
     74      Results.Add(curItersResult);
     75      Results.Add(bestQualityResult);
     76
     77      for (int i = 0; i < maxIters; i++) {
     78        curItersItem.Value = i;
     79
     80        // -----------------------------
     81        // IMPLEMENT YOUR ALGORITHM HERE
     82        // -----------------------------
    5583
    5684
    57       var curIters = new IntValue();
    58       var curItersResult = new Result("Iteration", curIters);
    59       Results.Add(curItersResult);
    60 
    61       for (int i = 0; i < maxIters; i++) {
    62         curIters.Value = i;
     85        // this is an example for random search
     86        // for a more elaborate algorithm check the source code of "HeuristicLab.Algorithms.ParameterlessPopulationPyramid"
     87        var cand = new BinaryVector(problem.Length, rand);
     88        var quality = problem.Evaluate(cand, rand); // calling Evaluate like this is not possible for all problems...
     89        if (problem.Maximization) bestQuality = Math.Max(bestQuality, quality);
     90        else bestQuality = Math.Min(quality, bestQuality);
     91        bestQualityItem.Value = bestQuality;
    6392
    6493        // check the cancellation token to see if the used clicked "Stop"
  • branches/Templates/EmptyAlgorithm/EmptyAlgorithm/Plugin.cs

    r14362 r14364  
    1111  // not correct then your plugin cannot be used on HeuristicLab.Hive
    1212  //
    13   // [PluginDependency("HeuristicLab.Data", "3.3")]
    14   // [PluginDependency("HeuristicLab.Core", "3.3")]
     13  [PluginDependency("HeuristicLab.Collections", "3.3")]
     14  [PluginDependency("HeuristicLab.Common", "3.3")]
     15  [PluginDependency("HeuristicLab.Core", "3.3")]
     16  [PluginDependency("HeuristicLab.Data", "3.3")]
     17  [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")]
     18  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     19  [PluginDependency("HeuristicLab.Parameters", "3.3")]
     20  [PluginDependency("HeuristicLab.Persistence", "3.3")]
     21  [PluginDependency("HeuristicLab.Problems.Binary", "3.3")]
     22  [PluginDependency("HeuristicLab.Random", "3.3")]
    1523
    1624  // HL plugin infrastructure discovers plugins on startup by trying to load all .dll and .exe files and looking for
Note: See TracChangeset for help on using the changeset viewer.