#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ConditionActionEncoding {
[Item("InsertInPopulationOperator", "Description missing")]
[StorableClass]
public class InsertInPopulationOperator : SingleSuccessorOperator {
#region Parameter Properties
public ILookupParameter> InsertInPopulationParameter {
get { return (ILookupParameter>)Parameters["InsertInPopulation"]; }
}
public ILookupParameter> ClassifiersParameter {
get { return (ILookupParameter>)Parameters["Classifiers"]; }
}
public ILookupParameter> NumerositiesParameter {
get { return (ILookupParameter>)Parameters["Numerosity"]; }
}
public ILookupParameter> HasToBeDeletedParameter {
get { return (ILookupParameter>)Parameters["HasToBeDeleted"]; }
}
#endregion
[StorableConstructor]
protected InsertInPopulationOperator(bool deserializing) : base(deserializing) { }
protected InsertInPopulationOperator(InsertInPopulationOperator original, Cloner cloner)
: base(original, cloner) {
}
public InsertInPopulationOperator()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("InsertInPopulation"));
Parameters.Add(new ScopeTreeLookupParameter("Classifiers"));
Parameters.Add(new ScopeTreeLookupParameter("Numerosity"));
Parameters.Add(new ScopeTreeLookupParameter("HasToBeDeleted"));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new InsertInPopulationOperator(this, cloner);
}
public override IOperation Apply() {
var numerosities = NumerositiesParameter.ActualValue;
var classifiers = ClassifiersParameter.ActualValue;
var hasToBeDeleted = HasToBeDeletedParameter.ActualValue;
var insertInPopulation = InsertInPopulationParameter.ActualValue;
if (insertInPopulation.Length != numerosities.Length || insertInPopulation.Length != classifiers.Length
|| insertInPopulation.Length != hasToBeDeleted.Length) {
throw new ArgumentException("Different number of classifiers, numerosity, hasToBeDeleted and insertInPopulation values.");
}
IList insertIndices = new List();
for (int i = 0; i < insertInPopulation.Length; i++) {
if (insertInPopulation[i].Value) {
insertIndices.Add(i);
}
}
foreach (var indices in insertIndices) {
IClassifier cl = classifiers[indices];
for (int i = 0; i < classifiers.Length; i++) {
if (!hasToBeDeleted[i].Value && !insertInPopulation[i].Value && cl.Identical(classifiers[i])) {
numerosities[i].Value += numerosities[indices].Value;
hasToBeDeleted[indices].Value = true;
break;
}
}
insertInPopulation[indices].Value = false;
}
return base.Apply();
}
}
}