Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17688


Ignore:
Timestamp:
07/19/20 20:08:01 (4 years ago)
Author:
dleko
Message:

#2825 Bugfix NSGA3 can now be cloned.

Location:
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3
Files:
2 edited

Legend:

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

    r17686 r17688  
    4646            get
    4747            {
    48                 if (!(Problem is MultiObjectiveTestFunctionProblem testFunctionProblem)) throw new NotSupportedException("Only test multi objective test function problems are supported");
     48                if (!(Problem is MultiObjectiveTestFunctionProblem testFunctionProblem)) throw new NotSupportedException("Only Multi Objective Test Function problems are supported");
    4949                return testFunctionProblem.Objectives;
    5050            }
     
    6060        [Storable]
    6161        private List<Solution> solutions; // maybe todo: rename to nextGeneration (see Run method)
     62
     63        [Storable]
     64        private List<ReferencePoint> referencePoints;
    6265
    6366        #endregion Storable fields
     
    149152        public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value;
    150153
    151         public List<List<Solution>> Fronts { get; private set; }
    152 
    153         public List<ReferencePoint> ReferencePoints { get; private set; }
    154 
    155154        // todo: create one property for the Generated Reference Points and one for the current
    156155        // generations reference points
     
    224223            // todo: don't forget to clone storable fields
    225224            random = cloner.Clone(original.random);
    226             solutions = new List<Solution>(original.solutions?.Select(cloner.Clone));
     225            solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToList() : null;
     226            referencePoints = original.referencePoints != null ? original.referencePoints.Select(r => {
     227                var refPoint = new ReferencePoint(random, r.NumberOfDimensions);
     228                r.Values.CopyTo(refPoint.Values, 0);
     229                return refPoint;
     230            }).ToList() : null;
    227231        }
    228232
     
    245249            InitResults();
    246250            InitFields();
    247             InitReferencePoints();
    248251            Analyze();
    249         }
    250 
    251         private void InitReferencePoints()
    252         {
    253             // Generate reference points and add them to results
    254             ReferencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives);
    255             ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(ReferencePoints);
    256         }
    257 
    258         private void InitFields()
    259         {
    260             random = new MersenneTwister();
    261             InitSolutions();
    262         }
    263 
    264         private void InitSolutions()
    265         {
    266             int minBound = 0;
    267             int maxBound = 1;
    268 
    269             // Initialise solutions
    270             solutions = new List<Solution>(PopulationSize.Value);
    271             for (int i = 0; i < PopulationSize.Value; i++)
    272             {
    273                 RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);
    274 
    275                 solutions.Add(new Solution(randomRealVector));
    276                 solutions[i].Fitness = Evaluate(solutions[i].Chromosome);
    277             }
    278252        }
    279253
     
    293267        }
    294268
     269        private void InitFields()
     270        {
     271            random = new MersenneTwister();
     272            InitSolutions();
     273            InitReferencePoints();
     274        }
     275        private void InitReferencePoints()
     276        {
     277            // Generate reference points and add them to results
     278            referencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives);
     279            ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(referencePoints);
     280        }
     281
     282
     283        private void InitSolutions()
     284        {
     285            int minBound = 0;
     286            int maxBound = 1;
     287
     288            // Initialise solutions
     289            solutions = new List<Solution>(PopulationSize.Value);
     290            for (int i = 0; i < PopulationSize.Value; i++)
     291            {
     292                RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);
     293
     294                solutions.Add(new Solution(randomRealVector));
     295                solutions[i].Fitness = Evaluate(solutions[i].Chromosome);
     296            }
     297        }
     298
    295299        #endregion Initialization
    296300
     
    301305            while (ResultsCurrentGeneration.Value < MaximumGenerations.Value)
    302306            {
    303                 // create copies of generated reference points (to preserve the original ones for
    304                 // the next generation) maybe todo: use cloner?
    305307
    306308                try
     
    309311                    List<Solution> rt = Utility.Concat(solutions, qt);
    310312
     313                    // create copies of generated reference points (to preserve the original ones for
     314                    // the next generation) maybe todo: use cloner?
    311315                    solutions = NSGA3Selection.SelectSolutionsForNextGeneration(rt, GetCopyOfReferencePoints(), Problem.Maximization, random);
    312316
     
    327331        private List<ReferencePoint> GetCopyOfReferencePoints()
    328332        {
    329             if (ReferencePoints == null) return null;
    330 
    331             List<ReferencePoint> referencePoints = new List<ReferencePoint>();
    332             foreach (var referencePoint in ReferencePoints)
    333                 referencePoints.Add(new ReferencePoint(referencePoint));
    334 
    335             return referencePoints;
     333            if (referencePoints == null) return null;
     334
     335            List<ReferencePoint> referencePointsCopy = new List<ReferencePoint>();
     336            foreach (var referencePoint in referencePoints)
     337                referencePointsCopy.Add(new ReferencePoint(referencePoint));
     338
     339            return referencePointsCopy;
    336340        }
    337341
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs

    r17669 r17688  
    22using System.Collections.Generic;
    33using System.Linq;
     4using HeuristicLab.Common;
    45using HeuristicLab.Core;
     6using HeuristicLab.Optimization;
    57
    68namespace HeuristicLab.Algorithms.NSGA3
    79{
    8     public class ReferencePoint
     10    public class ReferencePoint : IDeepCloneable
    911    {
     12
     13        #region Properties
     14
    1015        // The potentially associated solutions to this reference point and the distance to that solution
    1116        private readonly Dictionary<Solution, double> potentialAssociatedSolutions = new Dictionary<Solution, double>();
    1217
    13         #region Properties
    14 
    1518        private readonly IRandom random;
     19
    1620        public double[] Values { get; }
    1721        public int NumberOfDimensions => Values.Length;
     
    3337            Values = new double[other.NumberOfDimensions];
    3438            other.Values.CopyTo(Values, 0);
     39        }
     40
     41        public ReferencePoint(ReferencePoint other, Cloner cloner)
     42        {
     43            random = cloner.Clone(other.random);
     44            Values = new double[other.NumberOfDimensions];
     45            other.Values.CopyTo(Values, 0);
     46        }
     47
     48        public IDeepCloneable Clone(Cloner cloner)
     49        {
     50            return new ReferencePoint(this, cloner);
     51        }
     52
     53        public object Clone()
     54        {
     55            return new Cloner().Clone(this);
    3556        }
    3657
Note: See TracChangeset for help on using the changeset viewer.