- Timestamp:
- 11/20/14 15:12:27 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryEncoding.cs
r11553 r11559 35 35 [Item("BinaryEncoding", "Describes a binary vector encoding.")] 36 36 [StorableClass] 37 public sealed class BinaryEncoding : Encoding {37 public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> { 38 38 #region Encoding Parameters 39 39 [Storable] … … 53 53 #endregion 54 54 55 private List<IOperator> encodingOperators = new List<IOperator>();56 [Storable]57 public override IEnumerable<IOperator> Operators {58 get { return encodingOperators; }59 protected set { encodingOperators = new List<IOperator>(value); }60 }61 62 public override ISolutionCreator DefaultSolutionCreator {63 get { return Operators.OfType<RandomBinaryVectorCreator>().First(); }64 }65 66 55 public int Length { 67 56 get { return LengthParameter.Value.Value; } … … 80 69 : base(original, cloner) { 81 70 lengthParameter = cloner.Clone(original.lengthParameter); 82 encodingOperators = original.Operators.Select(cloner.Clone).ToList();83 71 RegisterParameterEvents(); 84 72 } … … 123 111 #endregion 124 112 125 public void ConfigureOperators(IEnumerable<IOperator> operators) {113 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 126 114 ConfigureCreators(operators.OfType<IBinaryVectorCreator>()); 127 115 ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>()); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11553 r11559 31 31 [Item("Encoding", "Base class for describing different encodings.")] 32 32 [StorableClass] 33 public abstract class Encoding : NamedItem, IEncoding { 33 public abstract class Encoding<T> : NamedItem, IEncoding 34 where T : class,ISolutionCreator { 34 35 public override sealed bool CanChangeName { 35 36 get { return false; } … … 40 41 } 41 42 42 public virtual IEnumerable<IOperator> Operators { 43 get { return Enumerable.Empty<IOperator>(); } 44 protected set { } 43 protected List<IOperator> encodingOperators = new List<IOperator>(); 44 [Storable] 45 public IEnumerable<IOperator> Operators { 46 get { return encodingOperators; } 47 protected set { encodingOperators = new List<IOperator>(value); } 45 48 } 46 49 47 public virtual ISolutionCreator DefaultSolutionCreator { 50 51 ISolutionCreator IEncoding.DefaultSolutionCreator { get { return DefaultSolutionCreator; } } 52 public virtual T DefaultSolutionCreator { 48 53 get { return null; } 54 } 55 56 57 ISolutionCreator IEncoding.SolutionCreator { 58 get { return SolutionCreator; } 59 set { 60 if (!(value is T)) throw new ArgumentException("???"); 61 SolutionCreator = (T)value; 62 } 63 } 64 private T solutionCreator; 65 public T SolutionCreator { 66 get { 67 if (solutionCreator == null) return DefaultSolutionCreator; 68 return solutionCreator; 69 } 70 set { 71 if (value == null) throw new ArgumentNullException("SolutionCreator must not be null."); 72 if (solutionCreator == value) return; 73 solutionCreator = value; 74 OnSolutionCreatorChanged(); 75 } 49 76 } 50 77 51 78 [StorableConstructor] 52 79 protected Encoding(bool deserializing) : base(deserializing) { } 53 protected Encoding(Encoding original, Cloner cloner) 54 : base(original, cloner) { } 80 81 protected Encoding(Encoding<T> original, Cloner cloner) 82 : base(original, cloner) { 83 encodingOperators = original.Operators.Select(cloner.Clone).ToList(); 84 solutionCreator = cloner.Clone(original.solutionCreator); 85 } 55 86 protected Encoding(string name) : base(name) { } 87 88 89 90 public void ConfigureOperator(IOperator @operator) { 91 ConfigureOperators(new[] { @operator }); 92 } 93 public virtual void ConfigureOperators(IEnumerable<IOperator> operators) { 94 95 } 96 97 protected virtual void OnSolutionCreatorChanged() { 98 ConfigureOperator(SolutionCreator); 99 } 100 56 101 57 102 public event EventHandler ParameterConfigurationChanged; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs
r11484 r11559 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 … … 31 32 [Item("IntegerEncoding", "Describes an integer vector encoding.")] 32 33 [StorableClass] 33 public class IntegerEncoding : Encoding {34 public class IntegerEncoding : Encoding<IIntegerVectorCreator> { 34 35 [Storable] 35 36 private IntValue length; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11484 r11559 30 30 [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")] 31 31 [StorableClass] 32 public class MultiEncoding : Encoding {32 public class MultiEncoding : Encoding<MultiEncodingCreator> { 33 33 [Storable] 34 public KeyedItemCollection<string, Encoding> Encodings { get; protected set; }34 public KeyedItemCollection<string, IEncoding> Encodings { get; protected set; } 35 35 36 36 [StorableConstructor] … … 42 42 public MultiEncoding() 43 43 : base("MultiEncoding") { 44 Encodings = new NamedItemCollection< Encoding>();44 Encodings = new NamedItemCollection<IEncoding>(); 45 45 } 46 46 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs
r11484 r11559 29 29 [Item("PermutationEncoding", "Describes a permutation encoding.")] 30 30 [StorableClass] 31 public class PermutationEncoding : Encoding {31 public class PermutationEncoding : Encoding<IPermutationCreator> { 32 32 [Storable] 33 33 private IntValue length; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11553 r11559 21 21 22 22 using System; 23 using System.Collections;24 23 using System.Collections.Generic; 25 24 using System.Linq; 26 using System.Runtime.CompilerServices;27 using System.Text;28 25 using HeuristicLab.Common; 29 26 using HeuristicLab.Core; … … 38 35 [Item("RealEncoding", "Describes a real vector encoding.")] 39 36 [StorableClass] 40 public sealed class RealEncoding : Encoding {37 public sealed class RealEncoding : Encoding<IRealVectorCreator> { 41 38 #region Encoding Parameters 42 39 [Storable] … … 69 66 #endregion 70 67 71 private List<IOperator> encodingOperators = new List<IOperator>();72 [Storable]73 public override IEnumerable<IOperator> Operators {74 get { return encodingOperators; }75 protected set { encodingOperators = new List<IOperator>(value); }76 }77 78 public override ISolutionCreator DefaultSolutionCreator {79 get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); }80 }81 82 68 public int Length { 83 69 get { return LengthParameter.Value.Value; } … … 88 74 set { BoundsParameter.Value = value; } 89 75 } 90 91 92 76 93 77 [StorableConstructor] … … 104 88 lengthParameter = cloner.Clone(original.lengthParameter); 105 89 boundsParameter = cloner.Clone(original.boundsParameter); 106 encodingOperators = original.Operators.Select(cloner.Clone).ToList();107 90 RegisterParameterEvents(); 108 91 } 109 110 111 92 112 93 public RealEncoding(string name, int length, double min, double max) … … 149 130 ConfigureOperators(Operators); 150 131 } 132 151 133 private void RegisterParameterEvents() { 152 134 RegisterLengthParameterEvents(); … … 166 148 static RealEncoding() { 167 149 encodingSpecificOperatorTypes = new List<Type>() { 168 169 170 171 172 173 174 175 176 177 178 179 180 150 typeof (IRealVectorOperator), 151 typeof (IRealVectorCreator), 152 typeof (IRealVectorCrossover), 153 typeof (IRealVectorManipulator), 154 typeof (IRealVectorStdDevStrategyParameterOperator), 155 typeof (IRealVectorSwarmUpdater), 156 typeof (IRealVectorParticleCreator), 157 typeof (IRealVectorParticleUpdater), 158 typeof (IRealVectorMultiNeighborhoodShakingOperator), 159 typeof (IRealVectorBoundsChecker), 160 typeof (IRealVectorMoveOperator), 161 typeof (IRealVectorMoveGenerator) 162 }; 181 163 } 182 164 private void DiscoverOperators() { 183 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IRealVectorOperator)); 184 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 165 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 185 166 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 186 167 var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList(); … … 188 169 ConfigureOperators(newOperators); 189 170 encodingOperators.AddRange(newOperators); 171 172 foreach (var strategyVectorCreator in Operators.OfType<IRealVectorStdDevStrategyParameterCreator>()) 173 strategyVectorCreator.BoundsParameter.ValueChanged += strategyVectorCreator_BoundsParameter_ValueChanged; 190 174 } 191 175 #endregion 192 176 193 public void ConfigureOperators(IEnumerable<IOperator> operators) { 177 178 private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) { 179 var boundsParameter = (IValueLookupParameter<DoubleMatrix>)sender; 180 if (boundsParameter.Value == null) return; 181 foreach (var strategyVectorManipulator in Operators.OfType<IRealVectorStdDevStrategyParameterManipulator>()) 182 strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)boundsParameter.Value.Clone(); 183 } 184 185 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 194 186 ConfigureCreators(operators.OfType<IRealVectorCreator>()); 195 187 ConfigureCrossovers(operators.OfType<IRealVectorCrossover>()); … … 201 193 ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()); 202 194 ConfigureBoundsCheckers(operators.OfType<IRealVectorBoundsChecker>()); 195 ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>()); 203 196 ConfigureMoveOperators(operators.OfType<IRealVectorMoveOperator>()); 204 Configure MoveGenerators(operators.OfType<IRealVectorMoveGenerator>());197 ConfigureAdditiveMoveOperator(operators.OfType<IAdditiveRealVectorMoveOperator>()); 205 198 } 206 199 … … 236 229 } 237 230 private void ConfigureStdDevStrategyParameterOperators(IEnumerable<IRealVectorStdDevStrategyParameterOperator> strategyOperators) { 238 var bounds = (DoubleMatrix)Bounds.Clone(); 239 for (var i = 0; i < bounds.Rows; i++) { 240 bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]); 231 var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns); 232 for (var i = 0; i < Bounds.Rows; i++) { 241 233 bounds[i, 0] = 0; 234 bounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]); 242 235 } 243 236 foreach (var s in strategyOperators) { … … 303 296 moveGenerator.BoundsParameter.ActualName = BoundsParameter.Name; 304 297 } 298 299 private void ConfigureAdditiveMoveOperator(IEnumerable<IAdditiveRealVectorMoveOperator> additiveMoveOperators) { 300 foreach (var additiveMoveOperator in additiveMoveOperators) { 301 additiveMoveOperator.AdditiveMoveParameter.ActualName = Name + "_AdditiveMove"; 302 } 303 } 305 304 #endregion 306 305 }
Note: See TracChangeset
for help on using the changeset viewer.