#region License Information /* HeuristicLab * Copyright (C) 2002-2013 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.Collections.Generic; 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.LCS { /// /// An operator which creates new solutions. Evaluation of the new solutions is executed in parallel, if an engine is used which supports parallelization. /// [Item("NicheSolutionCreator", "")] [StorableClass] public sealed class NicheSolutionCreator : SingleSuccessorOperator { private const string INITIALNICHEPARAMETERNAME = "InitialNiche"; 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"]; } } public IValueLookupParameter NichingParameter { get { return (IValueLookupParameter)Parameters["Niching"]; } } public ILookupParameter GAssistNichesProblemDataParameter { get { return (ILookupParameter)Parameters["GAssistNichesProblemData"]; } } public ILookupParameter DefaultClassParameter { get { return (ILookupParameter)Parameters["DefaultClass"]; } } 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 NicheSolutionCreator(bool deserializing) : base(deserializing) { } private NicheSolutionCreator(NicheSolutionCreator original, Cloner cloner) : base(original, cloner) { } public NicheSolutionCreator() : 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.")); Parameters.Add(new ValueLookupParameter("Niching", "")); Parameters.Add(new LookupParameter("GAssistNichesProblemData", "")); Parameters.Add(new LookupParameter("DefaultClass")); } [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 NicheSolutionCreator(this, cloner); } public override IOperation Apply() { int count = NumberOfSolutionsParameter.ActualValue.Value; var creator = SolutionCreatorParameter.ActualValue; var 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 niche = new OperationCollection(); OperationCollection creation = new OperationCollection(); OperationCollection evaluation = new OperationCollection() { Parallel = parallel }; var vcList = new List(); if (DefaultClassParameter.ActualValue != null) { //default rule has been set beforehand var vc = new VariableCreator(); vc.CollectedValues.Add(new ValueParameter(INITIALNICHEPARAMETERNAME, DefaultClassParameter.ActualValue)); vcList.Add(vc); } else { //default rule is set to auto foreach (var curNiche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) { var vc = new VariableCreator(); vc.CollectedValues.Add(new ValueParameter(INITIALNICHEPARAMETERNAME, curNiche)); vcList.Add(vc); } } creator.GAssistNicheParameter.ActualName = INITIALNICHEPARAMETERNAME; for (int i = 0; i < count; i++) { //only if niching is disabled and no default class has been set, then no niche has to be created in the scope if (NichingParameter.ActualValue.Value || DefaultClassParameter.ActualValue != null) { niche.Add(ExecutionContext.CreateOperation(vcList[i % vcList.Count], CurrentScope.SubScopes[current + 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(niche); next.Add(creation); next.Add(evaluation); next.Add(base.Apply()); return next; } } }