Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17557


Ignore:
Timestamp:
05/25/20 18:52:56 (4 years ago)
Author:
dleko
Message:

#2825: Add parameters for algorithm.

Location:
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/HeuristicLab.Algorithms.NSGA3-3.3.csproj

    r17551 r17557  
    6464      <Private>False</Private>
    6565    </Reference>
     66    <Reference Include="HeuristicLab.Encodings.RealVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     67      <SpecificVersion>False</SpecificVersion>
     68      <HintPath>..\..\..\..\trunk\bin\HeuristicLab.Encodings.RealVectorEncoding-3.3.dll</HintPath>
     69      <Private>False</Private>
     70    </Reference>
    6671    <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    6772      <SpecificVersion>False</SpecificVersion>
     
    106111    <Compile Include="Plugin.cs" />
    107112    <Compile Include="Properties\AssemblyInfo.cs" />
     113    <Compile Include="Solution.cs" />
    108114  </ItemGroup>
    109115  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3.cs

    r17551 r17557  
    11using System;
     2using System.Linq;
    23using System.Threading;
    34using HEAL.Attic;
    4 using HeuristicLab.Common; // required for parameters collection
    5 using HeuristicLab.Core; // required for parameters collection
    6 using HeuristicLab.Data; // IntValue, ...
    7 using HeuristicLab.Encodings.BinaryVectorEncoding;
    8 using HeuristicLab.Optimization; // BasicAlgorithm
     5using HeuristicLab.Common;
     6using HeuristicLab.Core;
     7using HeuristicLab.Data;
     8using HeuristicLab.Encodings.RealVectorEncoding;
     9using HeuristicLab.Optimization;
    910using HeuristicLab.Parameters;
    10 using HeuristicLab.Problems.Binary;
    11 using HeuristicLab.Random; // MersenneTwister
    1211
    1312namespace HeuristicLab.Algorithms.NSGA3
    1413{
    15     // each HL item needs to have a name and a description (BasicAlgorithm is an Item)
    16     // The name and description of items is shown in the GUI
    17     [Item(Name = "NSGA-III", Description = "An demo algorithm.")]
    18 
    19     // If the algorithm should be shown in the "New..." dialog it must be creatable. Entries in the new dialog are grouped to categories and ordered by priorities
    20     [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)]
    21     [StorableType("689280F7-E371-44A2-98A5-FCEDF22CA343")] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
     14    /// <summary>
     15    /// The Reference Point Based Non-dominated Sorting Genetic Algorithm III was introduced in Deb
     16    /// et al. 2013. An Evolutionary Many-Objective Optimization Algorithm Using Reference Point
     17    /// Based Non-dominated Sorting Approach. IEEE Transactions on Evolutionary Computation, 18(4),
     18    /// pp. 577-601.
     19    /// </summary>
     20    [Item("NSGA-III", "The Reference Point Based Non-dominated Sorting Genetic Algorithm III was introduced in Deb et al. 2013. An Evolutionary Many-Objective Optimization Algorithm Using Reference Point Based Non-dominated Sorting Approach. IEEE Transactions on Evolutionary Computation, 18(4), pp. 577-601.")]
     21    [Creatable(Category = CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 136)]
     22    [StorableType("30DF878D-D655-4E76-B3AD-2292C9DD6C5F")]
    2223    public class NSGA3 : BasicAlgorithm
    2324    {
    24         // This algorithm only works for BinaryProblems.
    25         // Overriding the ProblemType property has the effect that only BinaryProblems can be set as problem
    26         // for the algorithm in the GUI
    27         public override Type ProblemType { get { return typeof(BinaryProblem); } }
     25        public override bool SupportsPause => false; // todo: make true
    2826
    29         public new BinaryProblem Problem { get { return (BinaryProblem)base.Problem; } }
     27        #region ProblemProperties
    3028
    31         #region parameters
    32 
    33         // If an algorithm has parameters then we usually also add properties to access these parameters.
    34         // This is not strictly required but considered good shape.
    35         private IFixedValueParameter<IntValue> MaxIterationsParameter
     29        public override Type ProblemType
    3630        {
    37             get { return (IFixedValueParameter<IntValue>)Parameters["MaxIterations"]; }
     31            get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); }
    3832        }
    3933
    40         public int MaxIterations
     34        public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem
    4135        {
    42             get { return MaxIterationsParameter.Value.Value; }
    43             set { MaxIterationsParameter.Value.Value = value; }
     36            get { return (MultiObjectiveBasicProblem<RealVectorEncoding>)base.Problem; }
     37            set { base.Problem = value; }
    4438        }
    4539
    46         #endregion parameters
     40        #endregion ProblemProperties
    4741
    48         // createable items must have a default ctor
    49         public NSGA3()
     42        #region Storable fields
     43
     44        [Storable]
     45        private IRandom random;
     46
     47        [Storable]
     48        private Solution[] solutions;
     49
     50        #endregion Storable fields
     51
     52        #region ParameterAndResultsNames
     53
     54        // Parameter Names
     55
     56        private const string SeedName = "Seed";
     57        private const string SetSeedRandomlyName = "SetSeedRandomly";
     58        private const string PopulationSizeName = "PopulationSize";
     59        private const string CrossoverProbabilityName = "CrossOverProbability";
     60        private const string MutationProbabilityName = "MutationProbability";
     61        private const string MaximumGenerationsName = "MaximumGenerations";
     62        private const string DominateOnEqualQualitiesName = "DominateOnEqualQualities";
     63
     64        #endregion ParameterAndResultsNames
     65
     66        #region ParameterProperties
     67
     68        private IFixedValueParameter<IntValue> SeedParameter
    5069        {
    51             // algorithm parameters are shown in the GUI
    52             Parameters.Add(new FixedValueParameter<IntValue>("MaxIterations", new IntValue(10000)));
     70            get { return (IFixedValueParameter<IntValue>)Parameters[SeedName]; }
    5371        }
    5472
    55         // Persistence uses this ctor to improve deserialization efficiency.
    56         // If we would use the default ctor instead this would completely initialize the object (e.g. creating parameters)
    57         // even though the data is later overwritten by the stored data.
     73        private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter
     74        {
     75            get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
     76        }
     77
     78        private IFixedValueParameter<IntValue> PopulationSizeParameter
     79        {
     80            get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
     81        }
     82
     83        private IFixedValueParameter<PercentValue> CrossoverProbabilityParameter
     84        {
     85            get { return (IFixedValueParameter<PercentValue>)Parameters[CrossoverProbabilityName]; }
     86        }
     87
     88        private IFixedValueParameter<PercentValue> MutationProbabilityParameter
     89        {
     90            get { return (IFixedValueParameter<PercentValue>)Parameters[MutationProbabilityName]; }
     91        }
     92
     93        private IFixedValueParameter<IntValue> MaximumGenerationsParameter
     94        {
     95            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
     96        }
     97
     98        private IFixedValueParameter<BoolValue> DominateOnEqualQualitiesParameter
     99        {
     100            get { return (IFixedValueParameter<BoolValue>)Parameters[DominateOnEqualQualitiesName]; }
     101        }
     102
     103        #endregion ParameterProperties
     104
     105        #region Properties
     106
     107        public IntValue Seed => SeedParameter.Value;
     108
     109        public BoolValue SetSeedRandomly => SetSeedRandomlyParameter.Value;
     110
     111        public IntValue PopulationSize => PopulationSizeParameter.Value;
     112
     113        public PercentValue CrossoverProbability => CrossoverProbabilityParameter.Value;
     114
     115        public PercentValue MutationProbability => MutationProbabilityParameter.Value;
     116
     117        public IntValue MaximumGenerations => MaximumGenerationsParameter.Value;
     118
     119        public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value;
     120
     121        #endregion Properties
     122
     123        public NSGA3() : base()
     124        {
     125            Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     126            Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
     127            Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "The size of the population of Individuals.", new IntValue(100)));
     128            Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityName, "The probability that the crossover operator is applied on two parents.", new PercentValue(0.9)));
     129            Parameters.Add(new FixedValueParameter<PercentValue>(MutationProbabilityName, "The probability that the mutation operator is applied on a Individual.", new PercentValue(0.05)));
     130            Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
     131            Parameters.Add(new FixedValueParameter<BoolValue>(DominateOnEqualQualitiesName, "Flag which determines wether Individuals with equal quality values should be treated as dominated.", new BoolValue(false)));
     132        }
     133
     134        // Persistence uses this ctor to improve deserialization efficiency. If we would use the
     135        // default ctor instead this would completely initialize the object (e.g. creating
     136        // parameters) even though the data is later overwritten by the stored data.
    58137        [StorableConstructor]
    59138        public NSGA3(StorableConstructorFlag _) : base(_) { }
    60139
    61         // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle cyclic object references)
     140        // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle
     141        // cyclic object references). Don't forget to call the cloning ctor of the base class
    62142        public NSGA3(NSGA3 original, Cloner cloner) : base(original, cloner)
    63143        {
    64             // Don't forget to call the cloning ctor of the base class
    65             // This class does not have fields, therefore we don't need to actually clone anything
     144            // todo: don't forget to clone storable fields
     145            random = cloner.Clone(original.random);
     146            solutions = original.solutions?.Select(cloner.Clone).ToArray();
    66147        }
    67148
     
    73154        protected override void Run(CancellationToken cancellationToken)
    74155        {
    75             int maxIters = MaxIterations;
    76             var problem = Problem;
    77             var rand = new MersenneTwister(1234);
    78 
    79             var bestQuality = problem.Maximization ? double.MinValue : double.MaxValue;
    80 
    81             var curItersItem = new IntValue();
    82             var bestQualityItem = new DoubleValue(bestQuality);
    83             var curItersResult = new Result("Iteration", curItersItem);
    84             var bestQualityResult = new Result("Best quality", bestQualityItem);
    85             Results.Add(curItersResult);
    86             Results.Add(bestQualityResult);
    87 
    88             for (int i = 0; i < maxIters; i++)
    89             {
    90                 curItersItem.Value = i;
    91 
    92                 // -----------------------------
    93                 // IMPLEMENT YOUR ALGORITHM HERE
    94                 // -----------------------------
    95 
    96                 // this is an example for random search
    97                 // for a more elaborate algorithm check the source code of "HeuristicLab.Algorithms.ParameterlessPopulationPyramid"
    98                 var cand = new BinaryVector(problem.Length, rand);
    99                 var quality = problem.Evaluate(cand, rand); // calling Evaluate like this is not possible for all problems...
    100                 if (problem.Maximization) bestQuality = Math.Max(bestQuality, quality);
    101                 else bestQuality = Math.Min(quality, bestQuality);
    102                 bestQualityItem.Value = bestQuality;
    103 
    104                 // check the cancellation token to see if the used clicked "Stop"
    105                 if (cancellationToken.IsCancellationRequested) break;
    106             }
    107 
    108             Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime)));
    109         }
    110 
    111         public override bool SupportsPause
    112         {
    113             get { return false; }
     156            throw new NotImplementedException();
    114157        }
    115158    }
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Plugin.cs

    r17551 r17557  
    33namespace HeuristicLab.Algorithms.NSGA3
    44{
    5     [Plugin("HeuristicLab.Algorithms.NSGA3", "3.3.16.17198")]
     5    [Plugin("HeuristicLab.Algorithms.NSGA3", "Provides the reference point based, non-dominated sorting genetic algorithm III (NSGA-III) as described in Deb et al. 2013. An Evolutionary Many-Objective Optimization Algorithm Using Reference Point Based Non-dominated Sorting Approach. IEEE Transactions on Evolutionary Computation, 18(4), pp. 577-601.", "3.3.16.17198")]
    66    [PluginFile("HeuristicLab.Algorithms.NSGA3-3.3.dll", PluginFileType.Assembly)] // each plugin represents a collection of files. The minimum is one file; the assembly.
    77
     
    1111    // which uses plugin dependency resolution will not work correctly. For instance if plugin dependencies are
    1212    // not correct then your plugin cannot be used on HeuristicLab.Hive
    13     //
    1413    [PluginDependency("HeuristicLab.Collections", "3.3")]
    1514    [PluginDependency("HeuristicLab.Common", "3.3")]
     
    1716    [PluginDependency("HeuristicLab.Data", "3.3")]
    1817    [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")]
     18    [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
    1919    [PluginDependency("HeuristicLab.Optimization", "3.3")]
    2020    [PluginDependency("HeuristicLab.Parameters", "3.3")]
Note: See TracChangeset for help on using the changeset viewer.