Changeset 17557 for branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3
- Timestamp:
- 05/25/20 18:52:56 (4 years ago)
- 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 64 64 <Private>False</Private> 65 65 </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> 66 71 <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 67 72 <SpecificVersion>False</SpecificVersion> … … 106 111 <Compile Include="Plugin.cs" /> 107 112 <Compile Include="Properties\AssemblyInfo.cs" /> 113 <Compile Include="Solution.cs" /> 108 114 </ItemGroup> 109 115 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3.cs
r17551 r17557 1 1 using System; 2 using System.Linq; 2 3 using System.Threading; 3 4 using HEAL.Attic; 4 using HeuristicLab.Common; // required for parameters collection5 using HeuristicLab.Core; // required for parameters collection6 using HeuristicLab.Data; // IntValue, ...7 using HeuristicLab.Encodings. BinaryVectorEncoding;8 using HeuristicLab.Optimization; // BasicAlgorithm5 using HeuristicLab.Common; 6 using HeuristicLab.Core; 7 using HeuristicLab.Data; 8 using HeuristicLab.Encodings.RealVectorEncoding; 9 using HeuristicLab.Optimization; 9 10 using HeuristicLab.Parameters; 10 using HeuristicLab.Problems.Binary;11 using HeuristicLab.Random; // MersenneTwister12 11 13 12 namespace HeuristicLab.Algorithms.NSGA3 14 13 { 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")] 22 23 public class NSGA3 : BasicAlgorithm 23 24 { 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 28 26 29 public new BinaryProblem Problem { get { return (BinaryProblem)base.Problem; } }27 #region ProblemProperties 30 28 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 36 30 { 37 get { return (IFixedValueParameter<IntValue>)Parameters["MaxIterations"]; }31 get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); } 38 32 } 39 33 40 public int MaxIterations34 public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem 41 35 { 42 get { return MaxIterationsParameter.Value.Value; }43 set { MaxIterationsParameter.Value.Value= value; }36 get { return (MultiObjectiveBasicProblem<RealVectorEncoding>)base.Problem; } 37 set { base.Problem = value; } 44 38 } 45 39 46 #endregion parameters40 #endregion ProblemProperties 47 41 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 50 69 { 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]; } 53 71 } 54 72 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. 58 137 [StorableConstructor] 59 138 public NSGA3(StorableConstructorFlag _) : base(_) { } 60 139 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 62 142 public NSGA3(NSGA3 original, Cloner cloner) : base(original, cloner) 63 143 { 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(); 66 147 } 67 148 … … 73 154 protected override void Run(CancellationToken cancellationToken) 74 155 { 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(); 114 157 } 115 158 } -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Plugin.cs
r17551 r17557 3 3 namespace HeuristicLab.Algorithms.NSGA3 4 4 { 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")] 6 6 [PluginFile("HeuristicLab.Algorithms.NSGA3-3.3.dll", PluginFileType.Assembly)] // each plugin represents a collection of files. The minimum is one file; the assembly. 7 7 … … 11 11 // which uses plugin dependency resolution will not work correctly. For instance if plugin dependencies are 12 12 // not correct then your plugin cannot be used on HeuristicLab.Hive 13 //14 13 [PluginDependency("HeuristicLab.Collections", "3.3")] 15 14 [PluginDependency("HeuristicLab.Common", "3.3")] … … 17 16 [PluginDependency("HeuristicLab.Data", "3.3")] 18 17 [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")] 18 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 19 19 [PluginDependency("HeuristicLab.Optimization", "3.3")] 20 20 [PluginDependency("HeuristicLab.Parameters", "3.3")]
Note: See TracChangeset
for help on using the changeset viewer.