Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11598


Ignore:
Timestamp:
11/27/14 20:02:35 (10 years ago)
Author:
mkommend
Message:

#2174: Added first version of refactored individuals.

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
Files:
1 added
1 deleted
17 edited

Legend:

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

    r11593 r11598  
    3636  public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> {
    3737    #region Encoding Parameters
     38    [Storable]
    3839    private IFixedValueParameter<IntValue> lengthParameter;
    3940    public IFixedValueParameter<IntValue> LengthParameter {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11588 r11598  
    7676    protected Encoding(string name) : base(name) { }
    7777
     78    public virtual  Individual CreateIndividual(IScope scope) {
     79      return new Individual(this, scope);
     80    }
    7881
    7982    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs

    r11484 r11598  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
     26using HeuristicLab.Core;
    2527using HeuristicLab.Encodings.BinaryVectorEncoding;
    2628using HeuristicLab.Encodings.IntegerVectorEncoding;
    2729using HeuristicLab.Encodings.PermutationEncoding;
    2830using HeuristicLab.Encodings.RealVectorEncoding;
     31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2932
    3033namespace HeuristicLab.Problems.Programmable {
    31   public sealed class Individual : IDeepCloneable {
    32     private readonly Dictionary<string, BinaryVector> binaryVectors;
    33     private readonly Dictionary<string, IntegerVector> integerVectors;
    34     private readonly Dictionary<string, RealVector> realVectors;
    35     private readonly Dictionary<string, Permutation> permutationVectors;
     34  [StorableClass]
     35  public class Individual : Item {
     36    [Storable]
     37    public IEncoding Encoding { get; private set; }
     38    [Storable]
     39    protected IScope Scope { get; private set; }
    3640
    37     public Individual(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null,
    38       IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null,
    39       IEnumerable<KeyValuePair<string, RealVector>> realVectors = null,
    40       IEnumerable<KeyValuePair<string, Permutation>> permutations = null) {
    41       // prevent unnecessary allocation of dictionaries for unused vectors
    42       if (binaryVectors != null) this.binaryVectors = binaryVectors.ToDictionary(x => x.Key, x => x.Value);
    43       if (integerVectors != null) this.integerVectors = integerVectors.ToDictionary(x => x.Key, x => x.Value);
    44       if (realVectors != null) this.realVectors = realVectors.ToDictionary(x => x.Key, x => x.Value);
    45       if (permutations != null) this.permutationVectors = permutations.ToDictionary(x => x.Key, x => x.Value);
    46     }
    47     private Individual(Individual original, Cloner cloner) {
    48       cloner.RegisterClonedObject(original, this);
    49       if (original.binaryVectors != null) {
    50         binaryVectors = new Dictionary<string, BinaryVector>(original.binaryVectors.Comparer);
    51         foreach (var param in original.binaryVectors)
    52           binaryVectors[param.Key] = cloner.Clone(param.Value);
    53       }
    54       if (original.integerVectors != null) {
    55         integerVectors = new Dictionary<string, IntegerVector>(original.integerVectors.Comparer);
    56         foreach (var param in original.integerVectors)
    57           integerVectors[param.Key] = cloner.Clone(param.Value);
    58       }
    59       if (original.realVectors != null) {
    60         realVectors = new Dictionary<string, RealVector>(original.realVectors.Comparer);
    61         foreach (var param in original.realVectors)
    62           realVectors[param.Key] = cloner.Clone(param.Value);
    63       }
    64       if (original.permutationVectors != null) {
    65         permutationVectors = new Dictionary<string, Permutation>(original.permutationVectors.Comparer);
    66         foreach (var param in original.permutationVectors)
    67           permutationVectors[param.Key] = cloner.Clone(param.Value);
    68       }
     41    public Individual(IEncoding encoding, IScope scope) {
     42      Encoding = encoding;
     43      Scope = scope;
    6944    }
    7045
    71     public object Clone() {
    72       return Clone(new Cloner());
     46    [StorableConstructor]
     47    protected Individual(bool deserializing) : base(deserializing) { }
     48
     49    public override IDeepCloneable Clone(Cloner cloner) { return new Individual(this, cloner); }
     50    protected Individual(Individual original, Cloner cloner)
     51      : base(original, cloner) {
     52      Encoding = cloner.Clone(original.Encoding);
     53      Scope = cloner.Clone(original.Scope);
    7354    }
    7455
    75     public IDeepCloneable Clone(Cloner cloner) {
    76       return new Individual(this, cloner);
     56    public virtual IItem this[string name] {
     57      get { return ExtractScopeValue(name, Encoding.Name, Scope); }
     58      set { SetScopeValue(name, Encoding.Name, Scope, value); }
    7759    }
    7860
    79     public BinaryVector BinaryVector(string name) {
    80       return binaryVectors[name];
     61    internal virtual Individual Copy(IScope scope) {
     62      var individual = Encoding.CreateIndividual(scope);
     63      individual[Encoding.Name] = this[Encoding.Name];
     64      return individual;
    8165    }
    8266
    83     public bool BinaryValue(string name, int index) {
    84       return binaryVectors[name][index];
     67    protected static IItem ExtractScopeValue(string name, string encodingName, IScope scope) {
     68      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     69      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format("Encoding part {0} cannot be found in the provided scope.", name));
     70      var value = scope.Variables[name].Value;
     71      if (value == null) throw new ArgumentException(string.Format("Encoding part {0} is null.", name));
     72      return value;
    8573    }
    8674
    87     public IEnumerable<string> BinaryNames {
    88       get { return binaryVectors != null ? binaryVectors.Keys : Enumerable.Empty<string>(); }
    89     }
     75    protected static void SetScopeValue(string name, string encodingName, IScope scope, IItem value) {
     76      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     77      if (value == null) throw new ArgumentNullException("value");
    9078
    91     public IntegerVector IntegerVector(string name) {
    92       return integerVectors[name];
    93     }
    94 
    95     public int IntegerValue(string name, int index) {
    96       return integerVectors[name][index];
    97     }
    98 
    99     public IEnumerable<string> IntegerNames {
    100       get { return integerVectors != null ? integerVectors.Keys : Enumerable.Empty<string>(); }
    101     }
    102 
    103     public RealVector RealVector(string name) {
    104       return realVectors[name];
    105     }
    106 
    107     public double RealValue(string name, int index) {
    108       return realVectors[name][index];
    109     }
    110 
    111     public IEnumerable<string> RealNames {
    112       get { return realVectors != null ? realVectors.Keys : Enumerable.Empty<string>(); }
    113     }
    114 
    115     public Permutation Permutation(string name) {
    116       return permutationVectors[name];
    117     }
    118 
    119     public IEnumerable<string> PermutationNames {
    120       get { return permutationVectors != null ? permutationVectors.Keys : Enumerable.Empty<string>(); }
     79      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
     80      else scope.Variables[name].Value = value;
     81      var variable = scope.Variables[name];
    12182    }
    12283  }
     84
     85  #region extension methods
     86  public static class IndividualExtensionMethods {
     87    public static BinaryVector BinaryVector(this Individual individual, string name) {
     88      return (BinaryVector)individual[name];
     89    }
     90    public static IntegerVector IntegerVector(this Individual individual, string name) {
     91      return (IntegerVector)individual[name];
     92    }
     93    public static Permutation Permutation(this Individual individual, string name) {
     94      return (Permutation)individual[name];
     95    }
     96    public static RealVector RealVector(this Individual individual, string name) {
     97      return (RealVector)individual[name];
     98    }
     99  }
     100  #endregion
    123101}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs

    r11593 r11598  
    3737  public class IntegerEncoding : Encoding<IIntegerVectorCreator> {
    3838    #region Encoding Parameters
     39    [Storable]
    3940    private IFixedValueParameter<IntValue> lengthParameter;
    4041    public IFixedValueParameter<IntValue> LengthParameter {
     
    5253    }
    5354
     55    [Storable]
    5456    private IValueParameter<IntMatrix> boundsParameter;
    5557    public IValueParameter<IntMatrix> BoundsParameter {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs

    r11588 r11598  
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828using HeuristicLab.PluginInfrastructure;
     29using HeuristicLab.Problems.Programmable.Encodings;
    2930using HeuristicLab.Problems.Programmable.Interfaces;
    3031
     
    3233  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
    3334  [StorableClass]
    34   public class MultiEncoding : Encoding<MultiEncodingCreator> {
     35  public sealed class MultiEncoding : Encoding<MultiEncodingCreator> {
    3536
    3637    private readonly List<IEncoding> encodings;
     
    4344
    4445    [StorableConstructor]
    45     protected MultiEncoding(bool deserializing)
     46    private MultiEncoding(bool deserializing)
    4647      : base(deserializing) {
    4748      encodings = new List<IEncoding>();
     
    4950
    5051    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
    51     protected MultiEncoding(MultiEncoding original, Cloner cloner)
     52    private MultiEncoding(MultiEncoding original, Cloner cloner)
    5253      : base(original, cloner) {
    5354      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
     
    6061      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
    6162        encodingOperators.Add(@operator);
     63    }
     64
     65    public override Individual CreateIndividual(IScope scope) {
     66      return new MultiEncodingIndividual(this, scope);
    6267    }
    6368
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs

    r11593 r11598  
    3636  public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
    3737    #region encoding parameters
     38    [Storable]
    3839    private IFixedValueParameter<IntValue> lengthParameter;
    3940    public IFixedValueParameter<IntValue> LengthParameter {
     
    5152    }
    5253
     54    [Storable]
    5355    private IFixedValueParameter<PermutationType> permutationTypeParameter;
    5456    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11593 r11598  
    3737  public sealed class RealEncoding : Encoding<IRealVectorCreator> {
    3838    #region Encoding Parameters
     39    [Storable]
    3940    private IFixedValueParameter<IntValue> lengthParameter;
    4041    public IFixedValueParameter<IntValue> LengthParameter {
     
    5152      }
    5253    }
    53 
     54    [Storable]
    5455    private IValueParameter<DoubleMatrix> boundsParameter;
    5556    public IValueParameter<DoubleMatrix> BoundsParameter {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11587 r11598  
    127127  </ItemGroup>
    128128  <ItemGroup>
     129    <Compile Include="Encodings\MultiEncodingIndividual.cs" />
    129130    <Compile Include="Encodings\PermutationEncoding.cs" />
    130131    <Compile Include="Encodings\RealEncoding.cs" />
     
    134135    <Compile Include="Encodings\Encoding.cs" />
    135136    <Compile Include="Encodings\Individual.cs" />
    136     <Compile Include="Helper.cs" />
    137137    <Compile Include="Interfaces\IEncoding.cs" />
    138138    <Compile Include="Interfaces\IMultiEncodingOperator.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

    r11588 r11598  
    3232    //event EventHandler ParameterConfigurationChanged;
    3333
     34    Individual CreateIndividual(IScope scope);
    3435    void ConfigureOperator(IOperator @operator);
    3536    void ConfigureOperators(IEnumerable<IOperator> operators);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs

    r11559 r11598  
    5050      var definition = ProblemDefinitionParameter.ActualValue;
    5151      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    52       var config = EncodingParameter.ActualValue;
     52      var encoding = EncodingParameter.ActualValue;
    5353      var results = ResultsParameter.ActualValue;
    5454
     
    5757        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    5858
    59       var vectors = scopes.Select(scope => Helper.Extract(scope, config)).ToArray();
    60       definition.Analyze(vectors, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results);
     59      var individuals = scopes.Select(encoding.CreateIndividual).ToArray();
     60      definition.Analyze(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results);
    6161      return base.Apply();
    6262    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveEvaluator.cs

    r11559 r11598  
    6868      var definition = ProblemDefinitionParameter.ActualValue;
    6969      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    70       var config = EncodingParameter.ActualValue;
    71       var vector = Helper.Extract(ExecutionContext.Scope, config);
    72       QualitiesParameter.ActualValue = new DoubleArray(definition.Evaluate(random, vector));
     70      var encoding = EncodingParameter.ActualValue;
     71      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     72      QualitiesParameter.ActualValue = new DoubleArray(definition.Evaluate(random, individual));
    7373      return base.Apply();
    7474    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs

    r11559 r11598  
    5050      var definition = ProblemDefinitionParameter.ActualValue;
    5151      if (definition == null) throw new InvalidOperationException("Problem definition is null");
    52       var config = EncodingParameter.ActualValue;
     52      var encoding = EncodingParameter.ActualValue;
    5353      var results = ResultsParameter.ActualValue;
    5454
     
    5757        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    5858
    59       var vectors = scopes.Select(scope => Helper.Extract(scope, config)).ToArray();
    60       definition.Analyze(vectors, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results);
     59      var individuals = scopes.Select(encoding.CreateIndividual).ToArray();
     60      definition.Analyze(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results);
    6161      return base.Apply();
    6262    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs

    r11559 r11598  
    6868      var definition = ProblemDefinitionParameter.ActualValue;
    6969      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    70       var config = EncodingParameter.ActualValue;
    71       var vector = Helper.Extract(ExecutionContext.Scope, config);
    72       QualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, vector));
     70      var encoding = EncodingParameter.ActualValue;
     71      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     72      QualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual));
    7373      return base.Apply();
    7474    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs

    r11559 r11598  
    7575      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
    7676      var sampleSize = SampleSizeParameter.ActualValue.Value;
    77       var vector = Helper.Extract(ExecutionContext.Scope, encoding);
    78       var quality = QualityParameter.ActualValue == null ? definition.Evaluate(random, vector) : QualityParameter.ActualValue.Value;
     77      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     78      var quality = QualityParameter.ActualValue == null ? definition.Evaluate(random, individual) : QualityParameter.ActualValue.Value;
    7979
    8080      var count = 0;
     
    8282        Individual best = null;
    8383        var bestQuality = quality;
    84         foreach (var neighbor in definition.GetNeighbors(random, vector).Take(sampleSize)) {
     84        foreach (var neighbor in definition.GetNeighbors(random, individual).Take(sampleSize)) {
    8585          var q = definition.Evaluate(random, neighbor);
    8686          count++;
     
    9090        }
    9191        if (best == null) break;
    92         vector = best;
     92        individual = best;
    9393        quality = bestQuality;
    9494      }
    9595
    9696      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
    97       Helper.Write(ExecutionContext.Scope, vector);
     97      individual.Copy(ExecutionContext.Scope);
    9898      return base.Apply();
    9999    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs

    r11559 r11598  
    7373      var definition = ProblemDefinitionParameter.ActualValue;
    7474      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    75       var config = EncodingParameter.ActualValue;
    76       var vector = Helper.Extract(ExecutionContext.Scope, config);
    77       MoveQualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, vector));
     75      var encoding = EncodingParameter.ActualValue;
     76      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     77      MoveQualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual));
    7878      return base.InstrumentedApply();
    7979    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs

    r11559 r11598  
    7373      var sampleSize = SampleSizeParameter.ActualValue.Value;
    7474      var encoding = EncodingParameter.ActualValue;
    75       var vector = Helper.Extract(ExecutionContext.Scope, encoding);
    76       var nbhood = definition.GetNeighbors(random, vector).Take(sampleSize).ToList();
     75      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     76      var nbhood = definition.GetNeighbors(random, individual).Take(sampleSize).ToList();
    7777
    7878      var moveScopes = new Scope[nbhood.Count];
    7979      for (int i = 0; i < moveScopes.Length; i++) {
    8080        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
    81         Helper.Write(moveScopes[i], nbhood[i]);
     81        nbhood[i].Copy(moveScopes[i]);
    8282      }
    8383      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs

    r11559 r11598  
    6262
    6363      var encoding = EncodingParameter.ActualValue;
    64       Helper.Write(ExecutionContext.Scope.Parent, Helper.Extract(ExecutionContext.Scope, encoding));
     64      var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     65      individual.Copy(ExecutionContext.Scope.Parent);
    6566
    6667      if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset for help on using the changeset viewer.