[15273] | 1 | using System.Collections.Generic;
|
---|
| 2 |
|
---|
[15771] | 3 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[15275] | 4 | using System;
|
---|
| 5 | using System.Linq;
|
---|
[15273] | 6 |
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
[15275] | 9 | using HeuristicLab.Data;
|
---|
[15273] | 10 | using HeuristicLab.Optimization;
|
---|
[15275] | 11 | using HeuristicLab.Parameters;
|
---|
[15273] | 12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15275] | 13 | using HeuristicLab.PluginInfrastructure;
|
---|
[15273] | 14 |
|
---|
| 15 | [Item("PlushEncoding", "Describes an linear push (Plush) encoding.")]
|
---|
| 16 | [StorableClass]
|
---|
| 17 | public class PlushEncoding : Encoding<IPlushCreator> {
|
---|
[15275] | 18 |
|
---|
| 19 | public PlushEncoding() : this("Plush") { }
|
---|
| 20 |
|
---|
| 21 | public PlushEncoding(string name) : base(name) {
|
---|
[15341] | 22 | InitParameters();
|
---|
[15275] | 23 |
|
---|
| 24 | SolutionCreator = new PlushCreator();
|
---|
| 25 | RegisterParameterEvents();
|
---|
| 26 | DiscoverOperators();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[15341] | 29 | [StorableConstructor]
|
---|
[15273] | 30 | public PlushEncoding(bool deserializing)
|
---|
| 31 | : base(deserializing) {
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[15275] | 34 | public PlushEncoding(PlushEncoding original, Cloner cloner)
|
---|
[15273] | 35 | : base(original, cloner) {
|
---|
[15275] | 36 | minLengthParameter = cloner.Clone(original.minLengthParameter);
|
---|
| 37 | maxLengthParameter = cloner.Clone(original.maxLengthParameter);
|
---|
[15341] | 38 | maxCloseParameter = cloner.Clone(original.maxCloseParameter);
|
---|
| 39 | closeBiasLevelParameter = cloner.Clone(original.closeBiasLevelParameter);
|
---|
| 40 | instructionsParameter = cloner.Clone(original.instructionsParameter);
|
---|
| 41 | ercOptionsParameter = cloner.Clone(original.ercOptionsParameter);
|
---|
| 42 | inInstructionProbabilityParameter = cloner.Clone(original.inInstructionProbabilityParameter);
|
---|
| 43 |
|
---|
| 44 | RegisterParameterEvents();
|
---|
[15273] | 45 | }
|
---|
| 46 |
|
---|
[15275] | 47 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 48 | private void AfterDeserialization() {
|
---|
[15341] | 49 | InitParameters();
|
---|
[15275] | 50 | RegisterParameterEvents();
|
---|
| 51 | DiscoverOperators();
|
---|
[15273] | 52 | }
|
---|
| 53 |
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new PlushEncoding(this, cloner);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[15341] | 58 | private void InitParameters() {
|
---|
| 59 | if (!Parameters.ContainsKey(Name + ".MinLength")) {
|
---|
| 60 | minLengthParameter = new FixedValueParameter<IntValue>(Name + ".MinLength", new IntValue(0));
|
---|
| 61 | Parameters.Add(minLengthParameter);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | if (!Parameters.ContainsKey(Name + ".MaxLength")) {
|
---|
| 65 | maxLengthParameter = new FixedValueParameter<IntValue>(Name + ".MaxLength", new IntValue(100));
|
---|
| 66 | Parameters.Add(maxLengthParameter);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | if (!Parameters.ContainsKey(Name + ".MaxClose")) {
|
---|
| 70 | maxCloseParameter = new FixedValueParameter<IntValue>(Name + ".MaxClose", new IntValue(3));
|
---|
| 71 | Parameters.Add(maxCloseParameter);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (!Parameters.ContainsKey(Name + ".CloseBiasLevel")) {
|
---|
| 75 | closeBiasLevelParameter = new FixedValueParameter<DoubleValue>(Name + ".CloseBiasLevel", new DoubleValue(3d));
|
---|
| 76 | Parameters.Add(closeBiasLevelParameter);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (!Parameters.ContainsKey(Name + ".Instructions")) {
|
---|
| 80 | instructionsParameter = new ValueParameter<IExpressionsConfiguration>(Name + ".Instructions");
|
---|
| 81 | Parameters.Add(instructionsParameter);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (!Parameters.ContainsKey(Name + ".ErcOptions")) {
|
---|
| 85 | ercOptionsParameter = new ValueParameter<ErcOptions>(Name + ".ErcOptions");
|
---|
| 86 | Parameters.Add(ercOptionsParameter);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (!Parameters.ContainsKey(Name + ".InInstructionProbability")) {
|
---|
| 90 | inInstructionProbabilityParameter = new FixedValueParameter<PercentValue>(Name + ".InInstructionProbability", new PercentValue(0.1));
|
---|
| 91 | Parameters.Add(inInstructionProbabilityParameter);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[15275] | 95 | #region events
|
---|
| 96 |
|
---|
| 97 | private void OnMinLengthParameterChanged() {
|
---|
| 98 | RegisterMinLengthParameterEvents();
|
---|
| 99 | ConfigureOperators(Operators);
|
---|
| 100 | }
|
---|
| 101 | private void OnMaxLengthParameterChanged() {
|
---|
| 102 | RegisterMaxLengthParameterEvents();
|
---|
| 103 | ConfigureOperators(Operators);
|
---|
| 104 | }
|
---|
| 105 | private void OnMaxCloseParameterChanged() {
|
---|
| 106 | RegisterMaxCloseParameterEvents();
|
---|
| 107 | ConfigureOperators(Operators);
|
---|
| 108 | }
|
---|
| 109 | private void OnCloseBiasLevelParameterChanged() {
|
---|
| 110 | RegisterCloseBiasLevelParameterEvents();
|
---|
| 111 | ConfigureOperators(Operators);
|
---|
| 112 | }
|
---|
| 113 | private void OnErcOptionsParameterChanged() {
|
---|
| 114 | RegisterErcOptionsParameterEvents();
|
---|
| 115 | ConfigureOperators(Operators);
|
---|
| 116 | }
|
---|
| 117 | private void OnInstructionsParameterChanged() {
|
---|
| 118 | RegisterInstructionsParameterEvents();
|
---|
| 119 | ConfigureOperators(Operators);
|
---|
| 120 | }
|
---|
| 121 | private void OnInInstructionProbabilityParameterChanged() {
|
---|
| 122 | RegisterInInstructionProbabilityParameterEvents();
|
---|
| 123 | ConfigureOperators(Operators);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void RegisterParameterEvents() {
|
---|
| 127 | RegisterMinLengthParameterEvents();
|
---|
| 128 | RegisterMaxLengthParameterEvents();
|
---|
| 129 | RegisterMaxCloseParameterEvents();
|
---|
| 130 | RegisterCloseBiasLevelParameterEvents();
|
---|
| 131 | RegisterErcOptionsParameterEvents();
|
---|
| 132 | RegisterInstructionsParameterEvents();
|
---|
| 133 | RegisterInInstructionProbabilityParameterEvents();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | private void RegisterMinLengthParameterEvents() {
|
---|
| 137 | MinLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 138 | MinLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private void RegisterMaxLengthParameterEvents() {
|
---|
| 142 | MaxLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 143 | MaxLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void RegisterMaxCloseParameterEvents() {
|
---|
| 147 | MaxCloseParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 148 | MaxCloseParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private void RegisterCloseBiasLevelParameterEvents() {
|
---|
| 152 | CloseBiasLevelParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 153 | CloseBiasLevelParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | private void RegisterInInstructionProbabilityParameterEvents() {
|
---|
| 157 | InInstructionProbabilityParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 158 | InInstructionProbabilityParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | private void RegisterErcOptionsParameterEvents() {
|
---|
| 162 | ErcOptionsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | private void RegisterInstructionsParameterEvents() {
|
---|
| 166 | InstructionsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | #endregion
|
---|
| 170 |
|
---|
| 171 | #region Encoding Parameters
|
---|
| 172 | [Storable]
|
---|
[15334] | 173 | private IValueParameter<IntValue> minLengthParameter;
|
---|
[15771] | 174 | public IValueParameter<IntValue> MinLengthParameter {
|
---|
[15275] | 175 | get { return minLengthParameter; }
|
---|
[15771] | 176 | set {
|
---|
[15275] | 177 | if (value == null) throw new ArgumentNullException("Min length parameter must not be null.");
|
---|
| 178 | if (value.Value == null) throw new ArgumentNullException("Min length parameter value must not be null.");
|
---|
| 179 | if (minLengthParameter == value) return;
|
---|
| 180 |
|
---|
| 181 | if (minLengthParameter != null) Parameters.Remove(minLengthParameter);
|
---|
| 182 | minLengthParameter = value;
|
---|
| 183 | Parameters.Add(minLengthParameter);
|
---|
| 184 | OnMinLengthParameterChanged();
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | [Storable]
|
---|
[15334] | 189 | private IValueParameter<IntValue> maxLengthParameter;
|
---|
[15771] | 190 | public IValueParameter<IntValue> MaxLengthParameter {
|
---|
[15275] | 191 | get { return maxLengthParameter; }
|
---|
[15771] | 192 | set {
|
---|
[15275] | 193 | if (value == null) throw new ArgumentNullException("Max length parameter must not be null.");
|
---|
| 194 | if (value.Value == null) throw new ArgumentNullException("Max length parameter value must not be null.");
|
---|
| 195 | if (maxLengthParameter == value) return;
|
---|
| 196 |
|
---|
| 197 | if (maxLengthParameter != null) Parameters.Remove(maxLengthParameter);
|
---|
| 198 | maxLengthParameter = value;
|
---|
| 199 | Parameters.Add(maxLengthParameter);
|
---|
| 200 | OnMaxLengthParameterChanged();
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | [Storable]
|
---|
[15334] | 205 | private IValueParameter<IntValue> maxCloseParameter;
|
---|
[15771] | 206 | public IValueParameter<IntValue> MaxCloseParameter {
|
---|
[15275] | 207 | get { return maxCloseParameter; }
|
---|
[15771] | 208 | set {
|
---|
[15275] | 209 | if (value == null) throw new ArgumentNullException("Max close parameter must not be null.");
|
---|
| 210 | if (value.Value == null) throw new ArgumentNullException("Max close parameter value must not be null.");
|
---|
| 211 | if (maxCloseParameter == value) return;
|
---|
| 212 |
|
---|
| 213 | if (maxCloseParameter != null) Parameters.Remove(maxCloseParameter);
|
---|
| 214 | maxCloseParameter = value;
|
---|
| 215 | Parameters.Add(maxCloseParameter);
|
---|
| 216 | OnMaxCloseParameterChanged();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | [Storable]
|
---|
[15334] | 221 | private IValueParameter<DoubleValue> closeBiasLevelParameter;
|
---|
[15771] | 222 | public IValueParameter<DoubleValue> CloseBiasLevelParameter {
|
---|
[15275] | 223 | get { return closeBiasLevelParameter; }
|
---|
[15771] | 224 | set {
|
---|
[15275] | 225 | if (value == null) throw new ArgumentNullException("Close bias level parameter must not be null.");
|
---|
| 226 | if (value.Value == null) throw new ArgumentNullException("Close bias level parameter value must not be null.");
|
---|
| 227 | if (closeBiasLevelParameter == value) return;
|
---|
| 228 |
|
---|
| 229 | if (closeBiasLevelParameter != null) Parameters.Remove(closeBiasLevelParameter);
|
---|
| 230 | closeBiasLevelParameter = value;
|
---|
| 231 | Parameters.Add(closeBiasLevelParameter);
|
---|
| 232 | OnCloseBiasLevelParameterChanged();
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | [Storable]
|
---|
[15334] | 237 | private IValueParameter<PercentValue> inInstructionProbabilityParameter;
|
---|
[15771] | 238 | public IValueParameter<PercentValue> InInstructionProbabilityParameter {
|
---|
[15275] | 239 | get { return inInstructionProbabilityParameter; }
|
---|
[15771] | 240 | set {
|
---|
[15275] | 241 | if (value == null) throw new ArgumentNullException("In instruciton probability parameter must not be null.");
|
---|
| 242 | if (value.Value == null) throw new ArgumentNullException("In instruciton probability parameter value must not be null.");
|
---|
| 243 | if (inInstructionProbabilityParameter == value) return;
|
---|
| 244 |
|
---|
| 245 | if (inInstructionProbabilityParameter != null) Parameters.Remove(inInstructionProbabilityParameter);
|
---|
| 246 | inInstructionProbabilityParameter = value;
|
---|
| 247 | Parameters.Add(inInstructionProbabilityParameter);
|
---|
| 248 | OnInInstructionProbabilityParameterChanged();
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | [Storable]
|
---|
| 253 | private IValueParameter<IExpressionsConfiguration> instructionsParameter;
|
---|
[15771] | 254 | public IValueParameter<IExpressionsConfiguration> InstructionsParameter {
|
---|
[15275] | 255 | get { return instructionsParameter; }
|
---|
[15771] | 256 | set {
|
---|
[15275] | 257 | if (value == null) throw new ArgumentNullException("Instructions paramter must not be null");
|
---|
| 258 | if (instructionsParameter == value) return;
|
---|
| 259 |
|
---|
| 260 | if (instructionsParameter != null) Parameters.Remove(instructionsParameter);
|
---|
| 261 | instructionsParameter = value;
|
---|
| 262 | Parameters.Add(instructionsParameter);
|
---|
| 263 | OnInstructionsParameterChanged();
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | [Storable]
|
---|
| 268 | private IValueParameter<ErcOptions> ercOptionsParameter;
|
---|
[15771] | 269 | public IValueParameter<ErcOptions> ErcOptionsParameter {
|
---|
[15275] | 270 | get { return ercOptionsParameter; }
|
---|
[15771] | 271 | set {
|
---|
[15275] | 272 | if (value == null) throw new ArgumentNullException("ErcOptions paramter must not be null");
|
---|
| 273 | if (ercOptionsParameter == value) return;
|
---|
| 274 |
|
---|
| 275 | if (ercOptionsParameter != null) Parameters.Remove(ercOptionsParameter);
|
---|
| 276 | ercOptionsParameter = value;
|
---|
| 277 | Parameters.Add(ercOptionsParameter);
|
---|
| 278 | OnErcOptionsParameterChanged();
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[15771] | 282 | public IExpressionsConfiguration Instructions {
|
---|
[15275] | 283 | get { return InstructionsParameter.Value; }
|
---|
| 284 | set { InstructionsParameter.Value = value; }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[15771] | 287 | public ErcOptions ErcOptions {
|
---|
[15275] | 288 | get { return ErcOptionsParameter.Value; }
|
---|
| 289 | set { ErcOptionsParameter.Value = value; }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[15771] | 292 | public int MinLength {
|
---|
[15275] | 293 | get { return MinLengthParameter.Value.Value; }
|
---|
| 294 | set { MinLengthParameter.Value.Value = value; }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[15771] | 297 | public int MaxLength {
|
---|
[15275] | 298 | get { return MaxLengthParameter.Value.Value; }
|
---|
| 299 | set { MaxLengthParameter.Value.Value = value; }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[15771] | 302 | public int MaxClose {
|
---|
[15275] | 303 | get { return MaxCloseParameter.Value.Value; }
|
---|
| 304 | set { MaxCloseParameter.Value.Value = value; }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[15771] | 307 | public double InInstructionProbability {
|
---|
[15275] | 308 | get { return InInstructionProbabilityParameter.Value.Value; }
|
---|
| 309 | set { InInstructionProbabilityParameter.Value.Value = value; }
|
---|
| 310 | }
|
---|
| 311 | #endregion
|
---|
| 312 |
|
---|
| 313 | #region Operator Discovery
|
---|
[15334] | 314 | private static readonly IEnumerable<Type> EncodingSpecificOperatorTypes;
|
---|
[15275] | 315 | static PlushEncoding() {
|
---|
[15334] | 316 | EncodingSpecificOperatorTypes = new List<Type>() {
|
---|
[15275] | 317 | typeof (IPlushOperator),
|
---|
| 318 | typeof (IPlushCreator),
|
---|
| 319 | typeof (IPlushCrossover),
|
---|
| 320 | typeof (IPlushManipulator),
|
---|
| 321 | };
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | private void DiscoverOperators() {
|
---|
| 325 | var assembly = typeof(IPlushOperator).Assembly;
|
---|
[15334] | 326 | var discoveredTypes = ApplicationManager.Manager.GetTypes(EncodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
[15275] | 327 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
| 328 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
| 329 |
|
---|
| 330 | ConfigureOperators(newOperators);
|
---|
| 331 | foreach (var @operator in newOperators)
|
---|
| 332 | AddOperator(@operator);
|
---|
| 333 | }
|
---|
| 334 | #endregion
|
---|
| 335 |
|
---|
[15273] | 336 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[15275] | 337 | ConfigureCreators(operators.OfType<IPlushCreator>());
|
---|
| 338 | ConfigureCrossovers(operators.OfType<IPlushCrossover>());
|
---|
| 339 | ConfigureManipulators(operators.OfType<IPlushManipulator>());
|
---|
[15273] | 340 | }
|
---|
| 341 |
|
---|
[15275] | 342 | private void ConfigureCreators(IEnumerable<IPlushCreator> creators) {
|
---|
| 343 | foreach (var creator in creators) {
|
---|
| 344 | creator.PlushVectorParameter.ActualName = Name;
|
---|
| 345 | creator.MinLengthParameter.ActualName = MinLengthParameter.Name;
|
---|
| 346 | creator.MaxLengthParameter.ActualName = MaxLengthParameter.Name;
|
---|
[15334] | 347 | creator.MaxCloseParameter.ActualName = MaxCloseParameter.Name;
|
---|
[15289] | 348 | creator.CloseBiasLevelParameter.ActualName = CloseBiasLevelParameter.Name;
|
---|
[15275] | 349 | creator.ErcOptionsParameter.ActualName = ErcOptionsParameter.Name;
|
---|
| 350 | creator.InstructionsParameter.ActualName = InstructionsParameter.Name;
|
---|
| 351 | creator.InInstructionProbabilityParameter.ActualName = InInstructionProbabilityParameter.Name;
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | private void ConfigureCrossovers(IEnumerable<IPlushCrossover> crossovers) {
|
---|
| 356 | foreach (var crossover in crossovers) {
|
---|
| 357 | crossover.ChildParameter.ActualName = Name;
|
---|
| 358 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | private void ConfigureManipulators(IEnumerable<IPlushManipulator> manipulators) {
|
---|
| 363 | foreach (var manipulator in manipulators) {
|
---|
| 364 | manipulator.PlushVectorParameter.ActualName = Name;
|
---|
| 365 | manipulator.PlushVectorParameter.Hidden = true;
|
---|
| 366 |
|
---|
| 367 | manipulator.ErcOptionsParameter.ActualName = ErcOptionsParameter.Name;
|
---|
| 368 | manipulator.InstructionsParameter.ActualName = InstructionsParameter.Name;
|
---|
| 369 | manipulator.InInstructionProbabilityParameter.ActualName = InInstructionProbabilityParameter.Name;
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
[15273] | 372 | }
|
---|
[15275] | 373 |
|
---|
| 374 | public static class IndividualExtensionMethods {
|
---|
| 375 | public static PlushVector PlushVector(this Individual individual) {
|
---|
| 376 | var encoding = individual.GetEncoding<PlushEncoding>();
|
---|
| 377 | return individual.PlushVector(encoding.Name);
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | public static PlushVector PlushVector(this Individual individual, string name) {
|
---|
| 381 | return (PlushVector)individual[name];
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
[15273] | 384 | }
|
---|