Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11619


Ignore:
Timestamp:
12/02/14 11:44:18 (9 years ago)
Author:
mkommend
Message:

#2174: Refactored individuals.

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
Files:
2 added
12 edited
2 moved

Legend:

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

    r11598 r11619  
    7676    protected Encoding(string name) : base(name) { }
    7777
    78     public virtual  Individual CreateIndividual(IScope scope) {
    79       return new Individual(this, scope);
     78    public virtual Individual GetIndividual(IScope scope) {
     79      return new SingleEncodingIndividual(this, scope);
    8080    }
    8181
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs

    r11598 r11619  
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828using HeuristicLab.PluginInfrastructure;
    29 using HeuristicLab.Problems.Programmable.Encodings;
    3029using HeuristicLab.Problems.Programmable.Interfaces;
    3130
     
    6362    }
    6463
    65     public override Individual CreateIndividual(IScope scope) {
     64    public override Individual GetIndividual(IScope scope) {
    6665      return new MultiEncodingIndividual(this, scope);
    6766    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11598 r11619  
    127127  </ItemGroup>
    128128  <ItemGroup>
    129     <Compile Include="Encodings\MultiEncodingIndividual.cs" />
     129    <Compile Include="Individuals\MultiEncodingIndividual.cs" />
    130130    <Compile Include="Encodings\PermutationEncoding.cs" />
    131131    <Compile Include="Encodings\RealEncoding.cs" />
     
    134134    <Compile Include="Encodings\MultiEncoding.cs" />
    135135    <Compile Include="Encodings\Encoding.cs" />
    136     <Compile Include="Encodings\Individual.cs" />
     136    <Compile Include="Individuals\Individual.cs" />
     137    <Compile Include="Individuals\SingleEncodingIndividual.cs" />
    137138    <Compile Include="Interfaces\IEncoding.cs" />
    138139    <Compile Include="Interfaces\IMultiEncodingOperator.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs

    r11614 r11619  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using HeuristicLab.Common;
    2623using HeuristicLab.Core;
    2724using HeuristicLab.Encodings.BinaryVectorEncoding;
     
    2926using HeuristicLab.Encodings.PermutationEncoding;
    3027using HeuristicLab.Encodings.RealVectorEncoding;
    31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3228
    3329namespace HeuristicLab.Problems.Programmable {
    34   [StorableClass]
    35   public class Individual : Item {
    36     [Storable]
     30  public abstract class Individual {
    3731    public IEncoding Encoding { get; private set; }
    38     [Storable]
    3932    protected IScope Scope { get; private set; }
    4033
    41     public Individual(IEncoding encoding, IScope scope) {
     34    public string Name { get { return Encoding.Name; } }
     35
     36    protected Individual(IEncoding encoding, IScope scope) {
    4237      Encoding = encoding;
    4338      Scope = scope;
    4439    }
    4540
    46     [StorableConstructor]
    47     protected Individual(bool deserializing) : base(deserializing) { }
     41    public abstract IItem this[string name] { get; set; }
    4842
    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);
     43    public Individual Copy() {
     44      return Copy(new Scope());
    5445    }
     46    internal abstract Individual Copy(IScope scope);
    5547
    56     public virtual IItem this[string name] {
    57       get { return ExtractScopeValue(name, Encoding.Name, Scope); }
    58       set { SetScopeValue(name, Encoding.Name, Scope, value); }
    59     }
    60 
    61     internal virtual Individual Copy(IScope scope) {
    62       var individual = Encoding.CreateIndividual(scope);
    63       individual[Encoding.Name] = this[Encoding.Name];
    64       return individual;
    65     }
    66 
    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));
     48    protected static IItem ExtractScopeValue(string name, IScope scope) {
     49      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
    7050      var value = scope.Variables[name].Value;
    71       if (value == null) throw new ArgumentException(string.Format("Encoding part {0} is null.", name));
     51      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
    7252      return value;
    7353    }
    7454
    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));
     55    protected static void SetScopeValue(string name, IScope scope, IItem value) {
    7756      if (value == null) throw new ArgumentNullException("value");
    78 
    7957      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
    8058      else scope.Variables[name].Value = value;
    81       var variable = scope.Variables[name];
    8259    }
    8360  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/MultiEncodingIndividual.cs

    r11599 r11619  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    26 using HeuristicLab.Common;
    2725using HeuristicLab.Core;
    28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2926
    30 namespace HeuristicLab.Problems.Programmable.Encodings {
     27namespace HeuristicLab.Problems.Programmable {
    3128  public sealed class MultiEncodingIndividual : Individual {
    3229    public new MultiEncoding Encoding {
     
    3431    }
    3532
    36     public MultiEncodingIndividual(IEncoding encoding, IScope scope)
     33    private readonly IEnumerable<Individual> individuals;
     34    public MultiEncodingIndividual(MultiEncoding encoding, IScope scope)
    3735      : base(encoding, scope) {
    38       if (encoding.GetType() != typeof(MultiEncoding))
    39         throw new ArgumentException("Encoding must be of type MultiEncoding.k");
     36      individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray();
    4037    }
    4138
    42     [StorableConstructor]
    43     private MultiEncodingIndividual(bool deserializing) : base(deserializing) { }
     39    private MultiEncodingIndividual(MultiEncoding encoding, IScope scope, IEnumerable<Individual> individuals)
     40      : base(encoding, scope) {
     41      this.individuals = individuals;
     42    }
    4443
    45     public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingIndividual(this, cloner); }
    46     private MultiEncodingIndividual(Individual original, Cloner cloner) : base(original, cloner) { }
    4744
    4845    public override IItem this[string name] {
    4946      get {
    50         var encoding = Encoding.Encodings.FirstOrDefault(e => e.Name == name);
    51         if (encoding == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    52         return ExtractScopeValue(name, encoding.Name, Scope);
     47        var individual = individuals.FirstOrDefault(i => i.Name == name);
     48        if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     49        return individual[name];
    5350      }
    5451      set {
    55         var encoding = Encoding.Encodings.FirstOrDefault(e => e.Name == name);
    56         if (encoding == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    57         SetScopeValue(name, encoding.Name, Scope, value);
     52        var individual = individuals.FirstOrDefault(i => i.Name == name);
     53        if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     54        individual[name] = value;
    5855      }
    5956    }
    6057
    6158    internal override Individual Copy(IScope scope) {
    62       var individual = Encoding.CreateIndividual(scope);
    63       foreach (var encoding in Encoding.Encodings)
    64         individual[encoding.Name] = this[encoding.Name];
    65       return individual;
     59      var copies = individuals.Select(i => i.Copy(scope)).ToArray();
     60      return new MultiEncodingIndividual(Encoding, scope, copies);
    6661    }
    6762  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

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

    r11598 r11619  
    5757        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    5858
    59       var individuals = scopes.Select(encoding.CreateIndividual).ToArray();
     59      var individuals = scopes.Select(encoding.GetIndividual).ToArray();
    6060      definition.Analyze(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results);
    6161      return base.Apply();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveEvaluator.cs

    r11598 r11619  
    6969      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    7070      var encoding = EncodingParameter.ActualValue;
    71       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     71      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    7272      QualitiesParameter.ActualValue = new DoubleArray(definition.Evaluate(random, individual));
    7373      return base.Apply();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs

    r11598 r11619  
    5757        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    5858
    59       var individuals = scopes.Select(encoding.CreateIndividual).ToArray();
     59      var individuals = scopes.Select(encoding.GetIndividual).ToArray();
    6060      definition.Analyze(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results);
    6161      return base.Apply();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs

    r11598 r11619  
    6969      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    7070      var encoding = EncodingParameter.ActualValue;
    71       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     71      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    7272      QualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual));
    7373      return base.Apply();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs

    r11598 r11619  
    7575      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
    7676      var sampleSize = SampleSizeParameter.ActualValue.Value;
    77       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     77      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    7878      var quality = QualityParameter.ActualValue == null ? definition.Evaluate(random, individual) : QualityParameter.ActualValue.Value;
    7979
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs

    r11598 r11619  
    7474      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
    7575      var encoding = EncodingParameter.ActualValue;
    76       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     76      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    7777      MoveQualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual));
    7878      return base.InstrumentedApply();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs

    r11598 r11619  
    7373      var sampleSize = SampleSizeParameter.ActualValue.Value;
    7474      var encoding = EncodingParameter.ActualValue;
    75       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
     75      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    7676      var nbhood = definition.GetNeighbors(random, individual).Take(sampleSize).ToList();
    7777
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs

    r11598 r11619  
    6262
    6363      var encoding = EncodingParameter.ActualValue;
    64       var individual = encoding.CreateIndividual(ExecutionContext.Scope);
    65       individual.Copy(ExecutionContext.Scope.Parent);
     64      var individual = encoding.GetIndividual(ExecutionContext.Scope);
     65      individual.Copy(ExecutionContext.Scope.Parent.Parent);
    6666
    6767      if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset for help on using the changeset viewer.