#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.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.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("DD721D8A-A4DE-44C1-8D83-7888E8433B5F")]
public abstract class EvolvmentModelsOfModelsAlgorithmBase : FixedDataAnalysisAlgorithm {
#region data members
[Storable]
protected IList solutions;
[Storable]
protected List population;
[Storable]
protected List offspringPopulation;
[Storable]
protected List jointPopulation;
[Storable]
protected int evaluatedSolutions;
[Storable]
protected ExecutionContext executionContext;
[Storable]
protected IScope globalScope;
[Storable]
protected ExecutionState previousExecutionState;
#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";
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 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 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 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(100)));
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();
ProblemChanged += EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged;
}
private void EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged(object sender, EventArgs e) {
if (Problem != null) {
Problem.SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter();
//Problem.SymbolicExpressionTreeGrammar = new EMMGrammar();
}
}
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);
}
}
[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 = 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
}
}