#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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 System; using System.Collections; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ALPS { [Item("LayerUniformSubScopesProcessor", "")] [StorableClass] public class LayerUniformSubScopesProcessor : SingleSuccessorOperator { private OperatorParameter OperatorParameter { get { return (OperatorParameter)Parameters["Operator"]; } } public ValueLookupParameter ParallelParameter { get { return (ValueLookupParameter)Parameters["Parallel"]; } } private ILookupParameter PopulationSizeParameter { get { return (ILookupParameter)Parameters["PopulationSize"]; } } public IOperator Operator { get { return OperatorParameter.Value; } set { OperatorParameter.Value = value; } } public BoolValue Parallel { get { return ParallelParameter.Value; } set { ParallelParameter.Value = value; } } [StorableConstructor] private LayerUniformSubScopesProcessor(bool deserializing) : base(deserializing) { } private LayerUniformSubScopesProcessor(LayerUniformSubScopesProcessor original, Cloner cloner) : base(original, cloner) { } public LayerUniformSubScopesProcessor() : base() { Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope.")); Parameters.Add(new ValueLookupParameter("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(false))); Parameters.Add(new LookupParameter("PopulationSize")); } public override IDeepCloneable Clone(Cloner cloner) { return new LayerUniformSubScopesProcessor(this, cloner); } public override IOperation Apply() { var next = new OperationCollection(Successor != null ? ExecutionContext.CreateOperation(Successor) : null); if (Operator != null) { var scopes = ExecutionContext.Scope.SubScopes; var inner = new OperationCollection() { Parallel = Parallel == null ? false : Parallel.Value }; for (int i = 0; i < scopes.Count; i++) { var layerParameters = new LayerIntermediateParameter { new ValueParameter(PopulationSizeParameter.ActualName, new IntValue(PopulationSizeParameter.ActualValue[Math.Min(i, PopulationSizeParameter.ActualValue.Length - 1)])) }; var immediateContext = new ExecutionContext(ExecutionContext.Parent, layerParameters, scopes[i]); inner.Add(immediateContext.CreateChildOperation(Operator, scopes[i])); } next.Insert(0, inner); } return next; } } [Item("LayerIntermediateParameter", "")] [StorableClass] public class LayerIntermediateParameter : ParameterizedNamedItem, IEnumerable { [StorableConstructor] private LayerIntermediateParameter(bool deserializing) : base(deserializing) { } private LayerIntermediateParameter(LayerIntermediateParameter original, Cloner cloner) : base(original, cloner) { } public LayerIntermediateParameter() : base("LayerIntermediateParameter") { } public override IDeepCloneable Clone(Cloner cloner) { return new LayerIntermediateParameter(this, cloner); } public void Add(IParameter parameter) { Parameters.Add(parameter); } public IEnumerator GetEnumerator() { return Parameters.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }