Changeset 11619
- Timestamp:
- 12/02/14 11:44:18 (10 years ago)
- 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 76 76 protected Encoding(string name) : base(name) { } 77 77 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); 80 80 } 81 81 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11598 r11619 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 28 using HeuristicLab.PluginInfrastructure; 29 using HeuristicLab.Problems.Programmable.Encodings;30 29 using HeuristicLab.Problems.Programmable.Interfaces; 31 30 … … 63 62 } 64 63 65 public override Individual CreateIndividual(IScope scope) {64 public override Individual GetIndividual(IScope scope) { 66 65 return new MultiEncodingIndividual(this, scope); 67 66 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r11598 r11619 127 127 </ItemGroup> 128 128 <ItemGroup> 129 <Compile Include=" Encodings\MultiEncodingIndividual.cs" />129 <Compile Include="Individuals\MultiEncodingIndividual.cs" /> 130 130 <Compile Include="Encodings\PermutationEncoding.cs" /> 131 131 <Compile Include="Encodings\RealEncoding.cs" /> … … 134 134 <Compile Include="Encodings\MultiEncoding.cs" /> 135 135 <Compile Include="Encodings\Encoding.cs" /> 136 <Compile Include="Encodings\Individual.cs" /> 136 <Compile Include="Individuals\Individual.cs" /> 137 <Compile Include="Individuals\SingleEncodingIndividual.cs" /> 137 138 <Compile Include="Interfaces\IEncoding.cs" /> 138 139 <Compile Include="Interfaces\IMultiEncodingOperator.cs" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs
r11614 r11619 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using HeuristicLab.Common;26 23 using HeuristicLab.Core; 27 24 using HeuristicLab.Encodings.BinaryVectorEncoding; … … 29 26 using HeuristicLab.Encodings.PermutationEncoding; 30 27 using HeuristicLab.Encodings.RealVectorEncoding; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;32 28 33 29 namespace HeuristicLab.Problems.Programmable { 34 [StorableClass] 35 public class Individual : Item { 36 [Storable] 30 public abstract class Individual { 37 31 public IEncoding Encoding { get; private set; } 38 [Storable]39 32 protected IScope Scope { get; private set; } 40 33 41 public Individual(IEncoding encoding, IScope scope) { 34 public string Name { get { return Encoding.Name; } } 35 36 protected Individual(IEncoding encoding, IScope scope) { 42 37 Encoding = encoding; 43 38 Scope = scope; 44 39 } 45 40 46 [StorableConstructor] 47 protected Individual(bool deserializing) : base(deserializing) { } 41 public abstract IItem this[string name] { get; set; } 48 42 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()); 54 45 } 46 internal abstract Individual Copy(IScope scope); 55 47 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)); 70 50 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)); 72 52 return value; 73 53 } 74 54 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) { 77 56 if (value == null) throw new ArgumentNullException("value"); 78 79 57 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value)); 80 58 else scope.Variables[name].Value = value; 81 var variable = scope.Variables[name];82 59 } 83 60 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/MultiEncodingIndividual.cs
r11599 r11619 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 using HeuristicLab.Common;27 25 using HeuristicLab.Core; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;29 26 30 namespace HeuristicLab.Problems.Programmable .Encodings{27 namespace HeuristicLab.Problems.Programmable { 31 28 public sealed class MultiEncodingIndividual : Individual { 32 29 public new MultiEncoding Encoding { … … 34 31 } 35 32 36 public MultiEncodingIndividual(IEncoding encoding, IScope scope) 33 private readonly IEnumerable<Individual> individuals; 34 public MultiEncodingIndividual(MultiEncoding encoding, IScope scope) 37 35 : 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(); 40 37 } 41 38 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 } 44 43 45 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingIndividual(this, cloner); }46 private MultiEncodingIndividual(Individual original, Cloner cloner) : base(original, cloner) { }47 44 48 45 public override IItem this[string name] { 49 46 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]; 53 50 } 54 51 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; 58 55 } 59 56 } 60 57 61 58 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); 66 61 } 67 62 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs
r11598 r11619 32 32 //event EventHandler ParameterConfigurationChanged; 33 33 34 Individual CreateIndividual(IScope scope);34 Individual GetIndividual(IScope scope); 35 35 void ConfigureOperator(IOperator @operator); 36 36 void ConfigureOperators(IEnumerable<IOperator> operators); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs
r11598 r11619 57 57 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 58 58 59 var individuals = scopes.Select(encoding. CreateIndividual).ToArray();59 var individuals = scopes.Select(encoding.GetIndividual).ToArray(); 60 60 definition.Analyze(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results); 61 61 return base.Apply(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveEvaluator.cs
r11598 r11619 69 69 if (definition == null) throw new InvalidOperationException("Problem definition is null."); 70 70 var encoding = EncodingParameter.ActualValue; 71 var individual = encoding. CreateIndividual(ExecutionContext.Scope);71 var individual = encoding.GetIndividual(ExecutionContext.Scope); 72 72 QualitiesParameter.ActualValue = new DoubleArray(definition.Evaluate(random, individual)); 73 73 return base.Apply(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs
r11598 r11619 57 57 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 58 58 59 var individuals = scopes.Select(encoding. CreateIndividual).ToArray();59 var individuals = scopes.Select(encoding.GetIndividual).ToArray(); 60 60 definition.Analyze(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results); 61 61 return base.Apply(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs
r11598 r11619 69 69 if (definition == null) throw new InvalidOperationException("Problem definition is null."); 70 70 var encoding = EncodingParameter.ActualValue; 71 var individual = encoding. CreateIndividual(ExecutionContext.Scope);71 var individual = encoding.GetIndividual(ExecutionContext.Scope); 72 72 QualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual)); 73 73 return base.Apply(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs
r11598 r11619 75 75 var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; 76 76 var sampleSize = SampleSizeParameter.ActualValue.Value; 77 var individual = encoding. CreateIndividual(ExecutionContext.Scope);77 var individual = encoding.GetIndividual(ExecutionContext.Scope); 78 78 var quality = QualityParameter.ActualValue == null ? definition.Evaluate(random, individual) : QualityParameter.ActualValue.Value; 79 79 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs
r11598 r11619 74 74 if (definition == null) throw new InvalidOperationException("Problem definition is null."); 75 75 var encoding = EncodingParameter.ActualValue; 76 var individual = encoding. CreateIndividual(ExecutionContext.Scope);76 var individual = encoding.GetIndividual(ExecutionContext.Scope); 77 77 MoveQualityParameter.ActualValue = new DoubleValue(definition.Evaluate(random, individual)); 78 78 return base.InstrumentedApply(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs
r11598 r11619 73 73 var sampleSize = SampleSizeParameter.ActualValue.Value; 74 74 var encoding = EncodingParameter.ActualValue; 75 var individual = encoding. CreateIndividual(ExecutionContext.Scope);75 var individual = encoding.GetIndividual(ExecutionContext.Scope); 76 76 var nbhood = definition.GetNeighbors(random, individual).Take(sampleSize).ToList(); 77 77 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs
r11598 r11619 62 62 63 63 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); 66 66 67 67 if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset
for help on using the changeset viewer.