Changeset 11587 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
- Timestamp:
- 11/26/14 13:24:20 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11559 r11587 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.PermutationEncoding;27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.PluginInfrastructure; 29 using HeuristicLab.Problems.Programmable.Interfaces; 28 30 29 31 namespace HeuristicLab.Problems.Programmable { … … 31 33 [StorableClass] 32 34 public class MultiEncoding : Encoding<MultiEncodingCreator> { 35 36 private readonly List<IEncoding> encodings; 37 33 38 [Storable] 34 public KeyedItemCollection<string, IEncoding> Encodings { get; protected set; } 39 public IEnumerable<IEncoding> Encodings { 40 get { return encodings; } 41 private set { encodings.AddRange(value); } 42 } 43 44 public override IEnumerable<IValueParameter> Parameters { 45 get { return Encodings.SelectMany(e => e.Parameters); } 46 } 35 47 36 48 [StorableConstructor] 37 protected MultiEncoding(bool deserializing) : base(deserializing) { } 49 protected MultiEncoding(bool deserializing) 50 : base(deserializing) { 51 encodings = new List<IEncoding>(); 52 } 53 54 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); } 38 55 protected MultiEncoding(MultiEncoding original, Cloner cloner) 39 56 : base(original, cloner) { 40 Encodings = cloner.Clone(original.Encodings);57 encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone)); 41 58 } 42 59 public MultiEncoding() 43 60 : base("MultiEncoding") { 44 Encodings = new NamedItemCollection<IEncoding>(); 61 encodings = new List<IEncoding>(); 62 SolutionCreator = new MultiEncodingCreator(); 63 64 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) 65 encodingOperators.Add(@operator); 45 66 } 46 67 47 public override IDeepCloneable Clone(Cloner cloner) { 48 return new MultiEncoding(this, cloner); 49 } 68 public MultiEncoding Add(IEncoding encoding) { 69 if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported."); 70 if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name"); 71 encodings.Add(encoding); 50 72 51 public MultiEncoding AddBinaryVector(string variableName, int length) {52 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");53 Encodings.Add(new BinaryEncoding(variableName, length));73 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) { 74 @operator.AddEncoding(encoding); 75 } 54 76 return this; 55 77 } 56 78 57 public MultiEncoding AddIntegerVector(string variableName, int length, int min, int max, int? step = null) { 58 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName"); 59 Encodings.Add(new IntegerEncoding(variableName, length, min, max, step)); 60 return this; 79 public bool Remove(IEncoding encoding) { 80 var success = encodings.Remove(encoding); 81 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) { 82 @operator.RemoveEncoding(encoding); 83 } 84 return success; 61 85 } 62 86 63 public MultiEncoding AddIntegerVector(string variableName, int length, IList<int> min, IList<int> max, IList<int> step = null) { 64 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName"); 65 Encodings.Add(new IntegerEncoding(variableName, length, min, max, step)); 66 return this; 67 } 87 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 68 88 69 public MultiEncoding AddRealVector(string variableName, int length, double min, double max) {70 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");71 Encodings.Add(new RealEncoding(variableName, length, min, max));72 return this;73 }74 75 public MultiEncoding AddRealVector(string variableName, int length, IList<double> min, IList<double> max) {76 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");77 Encodings.Add(new RealEncoding(variableName, length, min, max));78 return this;79 }80 81 public MultiEncoding AddPermutation(string variableName, int length, PermutationTypes type) {82 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");83 Encodings.Add(new PermutationEncoding(variableName, length, type));84 return this;85 89 } 86 90 }
Note: See TracChangeset
for help on using the changeset viewer.