#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.Programmable.Interfaces;
namespace HeuristicLab.Problems.Programmable.Operators {
public abstract class MultiEncodingOperator : Operator, IMultiEncodingOperator where T : class,IOperator {
[StorableConstructor]
protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
protected MultiEncodingOperator(MultiEncodingOperator original, Cloner cloner) : base(original, cloner) { }
protected MultiEncodingOperator() : base() { }
public override IOperation Apply() {
var operations = Parameters.Select(p => p.ActualValue).OfType().Select(op => ExecutionContext.CreateOperation(op));
return new OperationCollection(operations);
}
public virtual void AddEncoding(IEncoding encoding) {
if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
var param = new ConstrainedValueParameter(encoding.Name, new ItemSet(encoding.Operators.OfType()));
param.Value = param.ValidValues.First();
Parameters.Add(param);
}
public virtual bool RemoveEncoding(IEncoding encoding) {
return Parameters.Remove(encoding.Name);
}
protected IConstrainedValueParameter GetParameter(IEncoding encoding) {
if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
return (IConstrainedValueParameter)Parameters[encoding.Name];
}
}
}