#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Optimization.Operators {
///
/// An operator which creates new solutions. Evaluation of the new solutions is executed in parallel, if an engine is used which supports parallelization.
///
[Item("SolutionsCreator", "An operator which creates new solutions. Evaluation of the new solutions is executed in parallel, if an engine is used which supports parallelization.")]
[StorableClass]
public sealed class SolutionsCreator : SingleSuccessorOperator {
public ValueLookupParameter NumberOfSolutionsParameter {
get { return (ValueLookupParameter)Parameters["NumberOfSolutions"]; }
}
public ValueLookupParameter SolutionCreatorParameter {
get { return (ValueLookupParameter)Parameters["SolutionCreator"]; }
}
public ValueLookupParameter EvaluatorParameter {
get { return (ValueLookupParameter)Parameters["Evaluator"]; }
}
public ValueLookupParameter ParallelParameter {
get { return (ValueLookupParameter)Parameters["Parallel"]; }
}
private ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
public IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
public IntValue NumberOfSolutions {
get { return NumberOfSolutionsParameter.Value; }
set { NumberOfSolutionsParameter.Value = value; }
}
[StorableConstructor]
private SolutionsCreator(bool deserializing) : base(deserializing) { }
private SolutionsCreator(SolutionsCreator original, Cloner cloner) : base(original, cloner) { }
public SolutionsCreator()
: base() {
Parameters.Add(new ValueLookupParameter("NumberOfSolutions", "The number of solutions that should be created."));
Parameters.Add(new ValueLookupParameter("SolutionCreator", "The operator which is used to create new solutions."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The operator which is used to evaluate new solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
Parameters.Add(new ValueLookupParameter("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey("Parallel")) Parameters.Add(new ValueLookupParameter("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true))); // backwards compatibility
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SolutionsCreator(this, cloner);
}
public override IOperation Apply() {
int count = NumberOfSolutionsParameter.ActualValue.Value;
IOperator creator = SolutionCreatorParameter.ActualValue;
IOperator evaluator = EvaluatorParameter.ActualValue;
bool parallel = ParallelParameter.ActualValue.Value;
int current = CurrentScope.SubScopes.Count;
for (int i = 0; i < count; i++)
CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
OperationCollection creation = new OperationCollection();
OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
for (int i = 0; i < count; i++) {
if (creator != null) creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
if (evaluator != null) evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
}
OperationCollection next = new OperationCollection();
next.Add(creation);
next.Add(evaluation);
next.Add(base.Apply());
return next;
}
}
}