Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/12/14 23:48:56 (9 years ago)
Author:
mkommend
Message:

#2174: Extracted wiring of real vector operators from the single objective programmable problem to the real encoding.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11543 r11546  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Linq;
     
    3132using HeuristicLab.Parameters;
    3233using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     34using HeuristicLab.PluginInfrastructure;
    3335
    3436namespace HeuristicLab.Problems.Programmable {
     
    4244      get { return lengthParameter; }
    4345      set {
    44         if(value == null) throw new ArgumentNullException("Length parameter must not be null.");
     46        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
    4547        if (lengthParameter == value) return;
    4648        lengthParameter = value;
     
    7476    }
    7577
    76     public ISolutionCreator SolutionCreator {
     78    public IRealVectorCreator DefaultSolutionCreator {
    7779      get {
    7880        var creator = new UniformRandomRealVectorCreator();
     
    8385      }
    8486    }
    85      
     87
    8688    [StorableConstructor]
    8789    private RealEncoding(bool deserializing) : base(deserializing) { }
     90    [StorableHook(HookType.AfterDeserialization)]
     91    private void AfterDeserialization() {
     92      RegisterParameterEvents();
     93    }
     94
     95    public override IDeepCloneable Clone(Cloner cloner) { return new RealEncoding(this, cloner); }
    8896    private RealEncoding(RealEncoding original, Cloner cloner)
    8997      : base(original, cloner) {
    9098      lengthParameter = cloner.Clone(original.lengthParameter);
    9199      boundsParameter = cloner.Clone(original.boundsParameter);
    92     }
     100      RegisterParameterEvents();
     101    }
     102
    93103    public RealEncoding(string name, int length, double min, double max)
    94104      : base(name) {
     
    101111      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
    102112      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
    103     }
     113      RegisterParameterEvents();
     114    }
     115
    104116    public RealEncoding(string name, int length, IList<double> min, IList<double> max)
    105117      : base(name) {
     
    115127      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
    116128      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
    117     } 
    118 
    119     public override IDeepCloneable Clone(Cloner cloner) {
    120       return new RealEncoding(this, cloner);
     129      RegisterParameterEvents();
    121130    }
    122131
    123132    private void OnLengthParameterChanged() {
    124        //TODO rewire parameters
    125     }
    126 
     133      RegisterLengthParameterEvents();
     134    }
    127135    private void OnBoundsParameterChanged() {
    128       //TODO rewire parameters     
    129     }
    130 
    131 
     136      RegisterBoundsParameterEvents();
     137    }
     138    private void RegisterParameterEvents()
     139    {
     140      RegisterLengthParameterEvents();
     141      RegisterBoundsParameterEvents();
     142    }
     143    private void RegisterLengthParameterEvents() {
     144      //TODO fire correct event
     145      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
     146      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
     147    }
     148    private void RegisterBoundsParameterEvents() {
     149      //TODO fire correct event
     150      BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
     151      boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
     152    }
     153
     154    public void DiscoverOperators() {
     155      var types = new List<Type>()
     156      {
     157        typeof (IRealVectorCreator),
     158        typeof (IRealVectorCrossover),
     159        typeof (IRealVectorManipulator),
     160        typeof (IRealVectorStdDevStrategyParameterOperator),
     161        typeof (IRealVectorParticleCreator),
     162        typeof (IRealVectorParticleUpdater),
     163        typeof (IRealVectorParticleUpdater),
     164      };
     165      var discoveredTypes = ApplicationManager.Manager.GetTypes(types, true, false, false);
     166      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)).ToList();
     167    }
     168
     169    public void ConfigureOperators(IEnumerable<IOperator> operators) {
     170      ConfigureCreators(operators.OfType<IRealVectorCreator>());
     171      ConfigureCrossovers(operators.OfType<IRealVectorCrossover>());
     172      ConfigureManipulators(operators.OfType<IRealVectorManipulator>());
     173      ConfigureStdDevStrategyParameterOperators(operators.OfType<IRealVectorStdDevStrategyParameterOperator>());
     174      ConfigureSwarmUpdaters(operators.OfType<IRealVectorSwarmUpdater>());
     175      ConfigureParticleCreators(operators.OfType<IRealVectorParticleCreator>());
     176      ConfigureParticleUpdaters(operators.OfType<IRealVectorParticleUpdater>());
     177      ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>());
     178    }
     179
     180    private void ConfigureCreators(IEnumerable<IRealVectorCreator> creators) {
     181      foreach (var creator in creators) {
     182        creator.RealVectorParameter.ActualName = Name;
     183        creator.LengthParameter.ActualName = LengthParameter.Name;
     184        creator.BoundsParameter.ActualName = BoundsParameter.Name;
     185      }
     186    }
     187    private void ConfigureCrossovers(IEnumerable<IRealVectorCrossover> crossovers) {
     188      foreach (var crossover in crossovers) {
     189        crossover.ChildParameter.ActualName = Name;
     190        crossover.ParentsParameter.ActualName = Name;
     191        crossover.BoundsParameter.ActualName = BoundsParameter.Name;
     192      }
     193    }
     194    private void ConfigureManipulators(IEnumerable<IRealVectorManipulator> manipulators) {
     195      foreach (var manipulator in manipulators) {
     196        manipulator.RealVectorParameter.ActualName = Name;
     197        manipulator.BoundsParameter.ActualName = BoundsParameter.Name;
     198        manipulator.BoundsParameter.Hidden = true;
     199        var sm = manipulator as ISelfAdaptiveManipulator;
     200        if (sm != null) {
     201          var p = sm.StrategyParameterParameter as ILookupParameter;
     202          if (p != null) {
     203            p.ActualName = Name + "Strategy";
     204            p.Hidden = true;
     205          }
     206        }
     207      }
     208    }
     209    private void ConfigureStdDevStrategyParameterOperators(IEnumerable<IRealVectorStdDevStrategyParameterOperator> strategyOperators) {
     210      var bounds = (DoubleMatrix)Bounds.Clone();
     211      for (var i = 0; i < bounds.Rows; i++) {
     212        bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]);
     213        bounds[i, 0] = 0;
     214      }
     215      foreach (var s in strategyOperators) {
     216        var c = s as IRealVectorStdDevStrategyParameterCreator;
     217        if (c != null) {
     218          c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
     219          c.LengthParameter.ActualName = LengthParameter.Name;
     220          c.StrategyParameterParameter.ActualName = Name + "Strategy";
     221        }
     222        var m = s as IRealVectorStdDevStrategyParameterManipulator;
     223        if (m != null) {
     224          m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
     225          m.StrategyParameterParameter.ActualName = Name + "Strategy";
     226        }
     227        var mm = s as StdDevStrategyVectorManipulator;
     228        if (mm != null) {
     229          mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length));
     230          mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length)));
     231        }
     232        var x = s as IRealVectorStdDevStrategyParameterCrossover;
     233        if (x != null) {
     234          x.ParentsParameter.ActualName = Name + "Strategy";
     235          x.StrategyParameterParameter.ActualName = Name + "Strategy";
     236        }
     237      }
     238    }
     239    private void ConfigureSwarmUpdaters(IEnumerable<IRealVectorSwarmUpdater> swarmUpdaters) {
     240      foreach (var su in swarmUpdaters) {
     241        su.RealVectorParameter.ActualName = Name;
     242      }
     243    }
     244    private void ConfigureParticleCreators(IEnumerable<IRealVectorParticleCreator> particleCreators) {
     245      foreach (var particleCreator in particleCreators) {
     246        particleCreator.RealVectorParameter.ActualName = Name;
     247        particleCreator.BoundsParameter.ActualName = BoundsParameter.Name;
     248        particleCreator.ProblemSizeParameter.ActualName = LengthParameter.Name;
     249      }
     250    }
     251    private void ConfigureParticleUpdaters(IEnumerable<IRealVectorParticleUpdater> particleUpdaters) {
     252      foreach (var particleUpdater in particleUpdaters) {
     253        particleUpdater.RealVectorParameter.ActualName = Name;
     254        particleUpdater.BoundsParameter.ActualName = BoundsParameter.Name;
     255      }
     256    }
     257    private void ConfigureShakingOperators(IEnumerable<IRealVectorMultiNeighborhoodShakingOperator> shakingOperators) {
     258      foreach (var shakingOperator in shakingOperators) {
     259        shakingOperator.RealVectorParameter.ActualName = Name;
     260      }
     261    }
    132262  }
    133 }                           
     263}
Note: See TracChangeset for help on using the changeset viewer.