#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HEAL.Attic; using HeuristicLab.Algorithms.DataAnalysis; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HeuristicLab.Random; using System; using System.Collections.Generic; using System.IO; using System.Linq; using CancellationToken = System.Threading.CancellationToken; using ExecutionContext = HeuristicLab.Core.ExecutionContext; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [Item("MOEADAlgorithmBase", "Base class for all MOEA/D algorithm variants.")] [StorableType("A56A396B-965A-4ADE-8A2B-AE3A45F9C239")] public abstract class EvolvmentModelsOfModelsAlgorithmBase : FixedDataAnalysisAlgorithm { #region data members [Storable] protected IList Solutions; [Storable] protected List Population; [Storable] protected int EvaluatedSolutions; [Storable] protected ExecutionContext executionContext; [Storable] protected IScope globalScope; [Storable] protected ExecutionState previousExecutionState; [Storable] protected IEnumerable trees; [Storable] protected ExecutionState currentExecutionState; #endregion #region parameters private const string SeedParameterName = "Seed"; private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; private const string PopulationSizeParameterName = "PopulationSize"; private const string SelectorParameterName = "Selector"; private const string GroupSizeParameterName = "GroupSize"; private const string CrossoverProbabilityParameterName = "CrossoverProbability"; private const string CrossoverParameterName = "Crossover"; private const string MutationProbabilityParameterName = "MutationProbability"; private const string MutatorParameterName = "Mutator"; private const string MaximumEvaluatedSolutionsParameterName = "MaximumEvaluatedSolutions"; private const string RandomParameterName = "Random"; private const string AnalyzerParameterName = "Analyzer"; private const string InputFileParameterName = "InputFile"; private const string ClusterNumbersParameterName = "ClusterNumbers"; private const string ClusterNumbersShowParameterName = "ClusterNumbersShow"; private const string AlgorithmImplementationTypeParameterName = "AlgorithmImplementationType"; private const string MapParameterName = "Map"; private const string NegbourTypeParameterName = "NegbourType"; private const string NegbourNumberParameterName = "NegbourNumber"; public IValueParameter AnalyzerParameter { get { return (ValueParameter)Parameters[AnalyzerParameterName]; } } public IFixedValueParameter SeedParameter { get { return (IFixedValueParameter)Parameters[SeedParameterName]; } } public IValueParameter ClusterNumbersParameter { get { return (IValueParameter)Parameters[ClusterNumbersParameterName]; } } public IValueParameter ClusterNumbersShowParameter { get { return (IValueParameter)Parameters[ClusterNumbersShowParameterName]; } } public IConstrainedValueParameter AlgorithmImplementationTypeParameter { get { return (IConstrainedValueParameter)Parameters[AlgorithmImplementationTypeParameterName]; } } public IConstrainedValueParameter> MapParameter { get { return (IConstrainedValueParameter>)Parameters[MapParameterName]; } } public IFixedValueParameter NegbourTypeParameter { get { return (IFixedValueParameter)Parameters[NegbourTypeParameterName]; } } public IFixedValueParameter NegbourNumberParameter { get { return (IFixedValueParameter)Parameters[NegbourNumberParameterName]; } } public IFixedValueParameter InputFileParameter { get { return (IFixedValueParameter)Parameters[InputFileParameterName]; } } public IFixedValueParameter SetSeedRandomlyParameter { get { return (IFixedValueParameter)Parameters[SetSeedRandomlyParameterName]; } } private IValueParameter PopulationSizeParameter { get { return (IValueParameter)Parameters[PopulationSizeParameterName]; } } public IValueParameter CrossoverProbabilityParameter { get { return (IValueParameter)Parameters[CrossoverProbabilityParameterName]; } } public IValueParameter GroupSizeParameter { get { return (IValueParameter)Parameters[GroupSizeParameterName]; } } public IConstrainedValueParameter CrossoverParameter { get { return (IConstrainedValueParameter)Parameters[CrossoverParameterName]; } } public IConstrainedValueParameter SelectorParameter { get { return (IConstrainedValueParameter)Parameters[SelectorParameterName]; } } public IValueParameter MutationProbabilityParameter { get { return (IValueParameter)Parameters[MutationProbabilityParameterName]; } } public IConstrainedValueParameter MutatorParameter { get { return (IConstrainedValueParameter)Parameters[MutatorParameterName]; } } public IValueParameter MaximumEvaluatedSolutionsParameter { get { return (IValueParameter)Parameters[MaximumEvaluatedSolutionsParameterName]; } } public IValueParameter RandomParameter { get { return (IValueParameter)Parameters[RandomParameterName]; } } #endregion #region parameter properties public ValueParameter ElitesParameter { get { return (ValueParameter)Parameters["Elites"]; } } public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } } public IntValue ClusterNumbers { get { return ClusterNumbersParameter.Value; } set { ClusterNumbersParameter.Value = value; } } public IntValue ClusterNumbersShow { get { return ClusterNumbersShowParameter.Value; } set { ClusterNumbersShowParameter.Value = value; } } public StringValue AlgorithmImplemetationType { get { return AlgorithmImplementationTypeParameter.Value; } set { AlgorithmImplementationTypeParameter.Value.Value = value.Value; } } public EMMMapBase Map { get { return MapParameter.Value; } set { MapParameter.Value = value; } } public StringValue NegbourType { get { return NegbourTypeParameter.Value; } set { NegbourTypeParameter.Value.Value = value.Value; } } public IntValue NegbourNumber { get { return NegbourNumberParameter.Value; } set { NegbourNumberParameter.Value.Value = value.Value; } } public StringValue InputFile { get { return InputFileParameter.Value; } set { InputFileParameter.Value.Value = value.Value; } } public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } } public IntValue PopulationSize { get { return PopulationSizeParameter.Value; } set { PopulationSizeParameter.Value = value; } } public PercentValue CrossoverProbability { get { return CrossoverProbabilityParameter.Value; } set { CrossoverProbabilityParameter.Value = value; } } public IntValue GroupSize { get { return GroupSizeParameter.Value; } set { GroupSizeParameter.Value = value; } } public ICrossover Crossover { get { return CrossoverParameter.Value; } set { CrossoverParameter.Value = value; } } public ISelector Selector { get { return SelectorParameter.Value; } set { SelectorParameter.Value = value; } } public PercentValue MutationProbability { get { return MutationProbabilityParameter.Value; } set { MutationProbabilityParameter.Value = value; } } public IManipulator Mutator { get { return MutatorParameter.Value; } set { MutatorParameter.Value = value; } } public MultiAnalyzer Analyzer { get { return AnalyzerParameter.Value; } set { AnalyzerParameter.Value = value; } } public IntValue MaximumEvaluatedSolutions { get { return MaximumEvaluatedSolutionsParameter.Value; } set { MaximumEvaluatedSolutionsParameter.Value = value; } } public IntValue Elites { get { return ElitesParameter.Value; } } #endregion #region constructors public EvolvmentModelsOfModelsAlgorithmBase() { Parameters.Add(new FixedValueParameter(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); Parameters.Add(new FixedValueParameter(InputFileParameterName, "The file with set of models that will be.", new StringValue("input.txt"))); Parameters.Add(new ConstrainedValueParameter(AlgorithmImplementationTypeParameterName, "The Type of possible algorith, implemetation, choose one: OnlyMap, Full, Read.")); Parameters.Add(new ConstrainedValueParameter>(MapParameterName, "The type of map crearion algorithm. Use one from: IslandMap, NetworkMap.")); Parameters.Add(new FixedValueParameter(NegbourNumberParameterName, "The parametr for FullMap type of map crearion algorithm. Use one from: 10, 20.", new IntValue(10))); Parameters.Add(new FixedValueParameter(NegbourTypeParameterName, "The parametr for FullMap type of map crearion algorithm. Use one from: Percent, Number.", new StringValue("Number"))); Parameters.Add(new FixedValueParameter(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); Parameters.Add(new ValueParameter(PopulationSizeParameterName, "The size of the population of Solutions.", new IntValue(100))); Parameters.Add(new ConstrainedValueParameter(SelectorParameterName, "The operator used to sellect parents.")); Parameters.Add(new ValueParameter(CrossoverProbabilityParameterName, "The probability that the crossover operator is applied.", new PercentValue(0.9))); Parameters.Add(new ValueParameter(GroupSizeParameterName, "The GoupSize that the Selector operator is applied.", new IntValue(3))); Parameters.Add(new ConstrainedValueParameter(CrossoverParameterName, "The operator used to cross Solutions.")); Parameters.Add(new ValueParameter(MutationProbabilityParameterName, "The probability that the mutation operator is applied on a solution.", new PercentValue(0.25))); Parameters.Add(new ConstrainedValueParameter(MutatorParameterName, "The operator used to mutate Solutions.")); Parameters.Add(new ValueParameter("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer())); Parameters.Add(new ValueParameter(MaximumEvaluatedSolutionsParameterName, "The maximum number of evaluated Solutions (approximately).", new IntValue(100_000))); Parameters.Add(new ValueParameter(RandomParameterName, new MersenneTwister())); Parameters.Add(new ValueParameter("Elites", "The numer of elite Solutions which are kept in each generation.", new IntValue(1))); Parameters.Add(new ValueParameter(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(10))); Parameters.Add(new ValueParameter(ClusterNumbersShowParameterName, "The number of clusters for model Map.", new IntValue(10))); foreach (ISelector selector in ApplicationManager.Manager.GetInstances().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name)) SelectorParameter.ValidValues.Add(selector); ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector")); if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector; ParameterizeSelectors(); //begin hack ... InputFile.ValueChanged += InputFile_ValueChanged; InfixExpressionParser parser = new InfixExpressionParser(); trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse).ToArray(); // end hack ProblemChanged += EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged; MapParameterUpdate(); } // also hack vvvvvvvvv private void InputFile_ValueChanged(object sender, EventArgs e) { InfixExpressionParser parser = new InfixExpressionParser(); trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse); } // remove again ^^^^^^^^^ private void EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged(object sender, EventArgs e) { if (Problem != null) { Problem.SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter(); //Problem.SymbolicExpressionTreeGrammar = new EMMGrammar(); } } protected void MapParameterUpdate() { int neghboorNumber = 10; switch (NegbourType.Value) { case "Percent": neghboorNumber = Convert.ToInt32((Convert.ToDouble(ClusterNumbersParameter.Value.Value)) * (Convert.ToDouble(NegbourNumber.Value)) / 100.0); break; case "Number": neghboorNumber = NegbourNumber.Value; break; default: neghboorNumber = NegbourNumber.Value; break; } var mapTypes = new EMMMapBase[] { new EMMIslandMap(), new EMMNetworkMap(neghboorNumber), new EMMDisatanceMap(), new EMMRankMap(), new EMMSucsessMap (), new EMMZeroMap () }; foreach (var t in mapTypes) { MapParameter.ValidValues.Add(t); } var algorithmType = new StringValue[] { new StringValue("Full"), new StringValue("Read"), new StringValue ("OnlyMap") }; foreach (var t in algorithmType) { AlgorithmImplementationTypeParameter.ValidValues.Add(t); } } protected EvolvmentModelsOfModelsAlgorithmBase(EvolvmentModelsOfModelsAlgorithmBase original, Cloner cloner) : base(original, cloner) { EvaluatedSolutions = original.EvaluatedSolutions; previousExecutionState = original.previousExecutionState; if (original.Solutions != null) { Solutions = original.Solutions.Select(cloner.Clone).ToArray(); } if (original.Population != null) { Population = original.Population.Select(cloner.Clone).ToList(); } //if (original.offspringPopulation != null) { // offspringPopulation = original.offspringPopulation.Select(cloner.Clone).ToList(); //} //if (original.jointPopulation != null) { // jointPopulation = original.jointPopulation.Select(x => cloner.Clone(x)).ToList(); //} if (original.executionContext != null) { executionContext = cloner.Clone(original.executionContext); } if (original.globalScope != null) { globalScope = cloner.Clone(original.globalScope); } // hack trees = original.trees.Select(x => cloner.Clone(x)).ToArray(); } [StorableConstructor] protected EvolvmentModelsOfModelsAlgorithmBase(StorableConstructorFlag _) : base(_) { } #endregion public override void Prepare() { base.Prepare(); } protected override void Initialize(CancellationToken cancellationToken) { base.Initialize(cancellationToken); } public override bool SupportsPause => true; // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation public IList GetResult(IRandom random) { return Population; } #region operator wiring and events private void ParameterizeStochasticOperator(IOperator op) { IStochasticOperator stochasticOp = op as IStochasticOperator; if (stochasticOp != null) { stochasticOp.RandomParameter.ActualName = "Random"; stochasticOp.RandomParameter.Hidden = true; } } private void ParameterizeSelectors() { foreach (ISelector selector in SelectorParameter.ValidValues) { selector.CopySelected = new BoolValue(true); selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); selector.NumberOfSelectedSubScopesParameter.Hidden = true; ParameterizeStochasticOperator(selector); } if (Problem != null) { foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType()) { selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; selector.MaximizationParameter.Hidden = true; selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; selector.QualityParameter.Hidden = true; } } } protected void ExecuteOperation(ExecutionContext executionContext, CancellationToken cancellationToken, IOperation operation) { Stack executionStack = new Stack(); executionStack.Push(operation); while (executionStack.Count > 0) { cancellationToken.ThrowIfCancellationRequested(); IOperation next = executionStack.Pop(); if (next is OperationCollection) { OperationCollection coll = (OperationCollection)next; for (int i = coll.Count - 1; i >= 0; i--) if (coll[i] != null) executionStack.Push(coll[i]); } else if (next is IAtomicOperation) { IAtomicOperation op = (IAtomicOperation)next; next = op.Operator.Execute((IExecutionContext)op, cancellationToken); if (next != null) executionStack.Push(next); } } } private void UpdateAnalyzers() { Analyzer.Operators.Clear(); if (Problem != null) { foreach (IAnalyzer analyzer in Problem.Operators.OfType()) { foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType()) param.Depth = 1; Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault); } } } private void UpdateCrossovers() { ICrossover oldCrossover = CrossoverParameter.Value; CrossoverParameter.ValidValues.Clear(); ICrossover defaultCrossover = Problem.Operators.OfType().FirstOrDefault(); foreach (ICrossover crossover in Problem.Operators.OfType().OrderBy(x => x.Name)) CrossoverParameter.ValidValues.Add(crossover); if (oldCrossover != null) { ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType()); if (crossover != null) CrossoverParameter.Value = crossover; else oldCrossover = null; } if (oldCrossover == null && defaultCrossover != null) CrossoverParameter.Value = defaultCrossover; } private void UpdateMutators() { IManipulator oldMutator = MutatorParameter.Value; MutatorParameter.ValidValues.Clear(); IManipulator defaultMutator = Problem.Operators.OfType().FirstOrDefault(); foreach (IManipulator mutator in Problem.Operators.OfType().OrderBy(x => x.Name)) MutatorParameter.ValidValues.Add(mutator); if (oldMutator != null) { IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType()); if (mutator != null) MutatorParameter.Value = mutator; else oldMutator = null; } if (oldMutator == null && defaultMutator != null) MutatorParameter.Value = defaultMutator; } private void UpdateSelectors() { ISelector oldSelector = SelectorParameter.Value; SelectorParameter.ValidValues.Clear(); ISelector defaultSelector = Problem.Operators.OfType().FirstOrDefault(); foreach (ISelector selector in Problem.Operators.OfType().OrderBy(x => x.Name)) SelectorParameter.ValidValues.Add(selector); if (oldSelector != null) { ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType()); if (selector != null) SelectorParameter.Value = selector; else oldSelector = null; } if (oldSelector == null && defaultSelector != null) SelectorParameter.Value = defaultSelector; } protected override void OnProblemChanged() { UpdateCrossovers(); UpdateMutators(); UpdateAnalyzers(); base.OnProblemChanged(); } protected override void OnExecutionStateChanged() { previousExecutionState = currentExecutionState; currentExecutionState = ExecutionState; base.OnExecutionStateChanged(); } protected override void OnStopped() { if (Solutions != null) { Solutions.Clear(); } if (Population != null) { Population.Clear(); } //if (offspringPopulation != null) { // offspringPopulation.Clear(); //} //if (jointPopulation != null) { // jointPopulation.Clear(); //} executionContext.Scope.SubScopes.Clear(); base.OnStopped(); } #endregion } }