Changeset 11598
- Timestamp:
- 11/27/14 20:02:35 (10 years ago)
- 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 36 36 public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> { 37 37 #region Encoding Parameters 38 [Storable] 38 39 private IFixedValueParameter<IntValue> lengthParameter; 39 40 public IFixedValueParameter<IntValue> LengthParameter { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11588 r11598 76 76 protected Encoding(string name) : base(name) { } 77 77 78 public virtual Individual CreateIndividual(IScope scope) { 79 return new Individual(this, scope); 80 } 78 81 79 82 public abstract void ConfigureOperators(IEnumerable<IOperator> operators); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs
r11484 r11598 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 25 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 26 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 29 using HeuristicLab.Encodings.PermutationEncoding; 28 30 using HeuristicLab.Encodings.RealVectorEncoding; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 32 30 33 namespace 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; } 36 40 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; 69 44 } 70 45 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); 73 54 } 74 55 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); } 77 59 } 78 60 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; 81 65 } 82 66 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; 85 73 } 86 74 87 p ublic 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"); 90 78 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]; 121 82 } 122 83 } 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 123 101 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs
r11593 r11598 37 37 public class IntegerEncoding : Encoding<IIntegerVectorCreator> { 38 38 #region Encoding Parameters 39 [Storable] 39 40 private IFixedValueParameter<IntValue> lengthParameter; 40 41 public IFixedValueParameter<IntValue> LengthParameter { … … 52 53 } 53 54 55 [Storable] 54 56 private IValueParameter<IntMatrix> boundsParameter; 55 57 public IValueParameter<IntMatrix> BoundsParameter { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11588 r11598 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 28 using HeuristicLab.PluginInfrastructure; 29 using HeuristicLab.Problems.Programmable.Encodings; 29 30 using HeuristicLab.Problems.Programmable.Interfaces; 30 31 … … 32 33 [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")] 33 34 [StorableClass] 34 public class MultiEncoding : Encoding<MultiEncodingCreator> {35 public sealed class MultiEncoding : Encoding<MultiEncodingCreator> { 35 36 36 37 private readonly List<IEncoding> encodings; … … 43 44 44 45 [StorableConstructor] 45 pr otectedMultiEncoding(bool deserializing)46 private MultiEncoding(bool deserializing) 46 47 : base(deserializing) { 47 48 encodings = new List<IEncoding>(); … … 49 50 50 51 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); } 51 pr otectedMultiEncoding(MultiEncoding original, Cloner cloner)52 private MultiEncoding(MultiEncoding original, Cloner cloner) 52 53 : base(original, cloner) { 53 54 encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone)); … … 60 61 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) 61 62 encodingOperators.Add(@operator); 63 } 64 65 public override Individual CreateIndividual(IScope scope) { 66 return new MultiEncodingIndividual(this, scope); 62 67 } 63 68 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs
r11593 r11598 36 36 public sealed class PermutationEncoding : Encoding<IPermutationCreator> { 37 37 #region encoding parameters 38 [Storable] 38 39 private IFixedValueParameter<IntValue> lengthParameter; 39 40 public IFixedValueParameter<IntValue> LengthParameter { … … 51 52 } 52 53 54 [Storable] 53 55 private IFixedValueParameter<PermutationType> permutationTypeParameter; 54 56 public IFixedValueParameter<PermutationType> PermutationTypeParameter { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11593 r11598 37 37 public sealed class RealEncoding : Encoding<IRealVectorCreator> { 38 38 #region Encoding Parameters 39 [Storable] 39 40 private IFixedValueParameter<IntValue> lengthParameter; 40 41 public IFixedValueParameter<IntValue> LengthParameter { … … 51 52 } 52 53 } 53 54 [Storable] 54 55 private IValueParameter<DoubleMatrix> boundsParameter; 55 56 public IValueParameter<DoubleMatrix> BoundsParameter { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r11587 r11598 127 127 </ItemGroup> 128 128 <ItemGroup> 129 <Compile Include="Encodings\MultiEncodingIndividual.cs" /> 129 130 <Compile Include="Encodings\PermutationEncoding.cs" /> 130 131 <Compile Include="Encodings\RealEncoding.cs" /> … … 134 135 <Compile Include="Encodings\Encoding.cs" /> 135 136 <Compile Include="Encodings\Individual.cs" /> 136 <Compile Include="Helper.cs" />137 137 <Compile Include="Interfaces\IEncoding.cs" /> 138 138 <Compile Include="Interfaces\IMultiEncodingOperator.cs" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs
r11588 r11598 32 32 //event EventHandler ParameterConfigurationChanged; 33 33 34 Individual CreateIndividual(IScope scope); 34 35 void ConfigureOperator(IOperator @operator); 35 36 void ConfigureOperators(IEnumerable<IOperator> operators); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs
r11559 r11598 50 50 var definition = ProblemDefinitionParameter.ActualValue; 51 51 if (definition == null) throw new InvalidOperationException("Problem definition is null."); 52 var config = EncodingParameter.ActualValue;52 var encoding = EncodingParameter.ActualValue; 53 53 var results = ResultsParameter.ActualValue; 54 54 … … 57 57 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 58 58 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); 61 61 return base.Apply(); 62 62 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveEvaluator.cs
r11559 r11598 68 68 var definition = ProblemDefinitionParameter.ActualValue; 69 69 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)); 73 73 return base.Apply(); 74 74 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveAnalyzer.cs
r11559 r11598 50 50 var definition = ProblemDefinitionParameter.ActualValue; 51 51 if (definition == null) throw new InvalidOperationException("Problem definition is null"); 52 var config = EncodingParameter.ActualValue;52 var encoding = EncodingParameter.ActualValue; 53 53 var results = ResultsParameter.ActualValue; 54 54 … … 57 57 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 58 58 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); 61 61 return base.Apply(); 62 62 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs
r11559 r11598 68 68 var definition = ProblemDefinitionParameter.ActualValue; 69 69 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)); 73 73 return base.Apply(); 74 74 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs
r11559 r11598 75 75 var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; 76 76 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; 79 79 80 80 var count = 0; … … 82 82 Individual best = null; 83 83 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)) { 85 85 var q = definition.Evaluate(random, neighbor); 86 86 count++; … … 90 90 } 91 91 if (best == null) break; 92 vector= best;92 individual = best; 93 93 quality = bestQuality; 94 94 } 95 95 96 96 LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count); 97 Helper.Write(ExecutionContext.Scope, vector);97 individual.Copy(ExecutionContext.Scope); 98 98 return base.Apply(); 99 99 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs
r11559 r11598 73 73 var definition = ProblemDefinitionParameter.ActualValue; 74 74 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)); 78 78 return base.InstrumentedApply(); 79 79 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs
r11559 r11598 73 73 var sampleSize = SampleSizeParameter.ActualValue.Value; 74 74 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(); 77 77 78 78 var moveScopes = new Scope[nbhood.Count]; 79 79 for (int i = 0; i < moveScopes.Length; i++) { 80 80 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 81 Helper.Write(moveScopes[i], nbhood[i]);81 nbhood[i].Copy(moveScopes[i]); 82 82 } 83 83 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs
r11559 r11598 62 62 63 63 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); 65 66 66 67 if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset
for help on using the changeset viewer.