Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/25/20 19:33:28 (4 years ago)
Author:
dleko
Message:

#2825: Initialize and analyze solutions of first generation.

File:
1 edited

Legend:

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

    r17558 r17559  
    1010using HeuristicLab.Optimization;
    1111using HeuristicLab.Parameters;
     12using HeuristicLab.Random;
    1213
    1314namespace HeuristicLab.Algorithms.NSGA3
     
    6667
    6768        private const string GeneratedReferencePointsResultName = "Generated Reference Points";
     69        private const string CurrentFrontResultName = "Pareto Front"; // Do not touch this
    6870
    6971        #endregion ParameterAndResultsNames
     
    132134            get { return (DoubleMatrix)Results[GeneratedReferencePointsResultName].Value; }
    133135            set { Results[GeneratedReferencePointsResultName].Value = value; }
     136        }
     137
     138        public DoubleMatrix ResultsSolutions
     139        {
     140            get { return (DoubleMatrix)Results[CurrentFrontResultName].Value; }
     141            set { Results[CurrentFrontResultName].Value = value; }
    134142        }
    135143
     
    173181            base.Initialize(cancellationToken);
    174182
     183            InitFields();
    175184            InitResults();
    176185            InitReferencePoints();
     186            Analyze();
    177187        }
    178188
     
    186196        #region Private Methods
    187197
     198        private void InitFields()
     199        {
     200            random = new MersenneTwister();
     201            InitSolutions();
     202        }
     203
    188204        private void InitResults()
    189205        {
    190206            Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", new DoubleMatrix()));
    191         }
    192 
    193         // Generates the structured reference points
     207            Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix()));
     208        }
     209
    194210        private void InitReferencePoints()
    195211        {
     
    200216        }
    201217
     218        private void InitSolutions()
     219        {
     220            int minBound = 0; // todo: find min inside Problem.Encoding
     221            int maxBound = 1; // todo: find max inside Problem.Encoding
     222
     223            // Initialise solutions
     224            solutions = new Solution[PopulationSize.Value];
     225            for (int i = 0; i < PopulationSize.Value; i++)
     226            {
     227                RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);
     228
     229                solutions[i] = new Solution(StorableConstructorFlag.Default)
     230                {
     231                    Chromosome = randomRealVector
     232                };
     233                solutions[i].Fitness = Evaluate(solutions[i].Chromosome);
     234            }
     235        }
     236
     237        /// <summary>
     238        /// Returns the fitness of the given <paramref name="chromosome" /> by applying the Evaluate
     239        /// method of the Problem.
     240        /// </summary>
     241        /// <param name="chromosome"></param>
     242        /// <returns></returns>
     243        private double[] Evaluate(RealVector chromosome)
     244        {
     245            return Problem.Evaluate(new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, chromosome) } }), random);
     246        }
     247
     248        private void Analyze()
     249        {
     250            ResultsSolutions = solutions.Select(s => s.Chromosome.ToArray()).ToMatrix();
     251            Problem.Analyze(
     252                solutions.Select(s => (Individual)new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, s.Chromosome) } })).ToArray(),
     253                solutions.Select(s => s.Fitness).ToArray(),
     254                Results,
     255                random
     256                );
     257        }
     258
    202259        #endregion Private Methods
    203260    }
Note: See TracChangeset for help on using the changeset viewer.