Changeset 14877
- Timestamp:
- 04/19/17 14:42:41 (8 years ago)
- Location:
- trunk/sources
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs
r14185 r14877 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Core; 24 26 … … 27 29 protected IEncoding Encoding { get; private set; } 28 30 protected IScope Scope { get; private set; } 29 30 31 public string Name { get { return Encoding.Name; } } 31 32 … … 35 36 } 36 37 37 public abstract IItem this[string name] { get; set; } 38 public IItem this[string name] { 39 get { return ExtractScopeValue(name, Scope); } 40 set { SetScopeValue(name, Scope, value); } 41 } 42 43 public IEnumerable<KeyValuePair<string, IItem>> Values { 44 get { return Scope.Variables.Select(v => new KeyValuePair<string, IItem>(v.Name, v.Value)); } 45 } 38 46 public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding; 39 47 40 public Individual Copy() { 41 return CopyToScope(new Scope()); 48 public abstract Individual Copy(); 49 internal void CopyToScope(IScope scope) { 50 foreach (var val in Values) 51 SetScopeValue(val.Key, scope, val.Value); 42 52 } 43 53 44 public abstract Individual CopyToScope(IScope scope); 45 46 protected static IItem ExtractScopeValue(string name, IScope scope) { 54 private static IItem ExtractScopeValue(string name, IScope scope) { 55 if (scope == null) throw new ArgumentNullException("scope"); 47 56 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 48 57 var value = scope.Variables[name].Value; … … 51 60 } 52 61 53 protected static void SetScopeValue(string name, IScope scope, IItem value) { 62 private static void SetScopeValue(string name, IScope scope, IItem value) { 63 if (scope == null) throw new ArgumentNullException("scope"); 54 64 if (value == null) throw new ArgumentNullException("value"); 65 55 66 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value)); 56 67 else scope.Variables[name].Value = value; -
trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiEncodingIndividual.cs
r14185 r14877 32 32 } 33 33 34 private readonly IEnumerable<Individual> individuals; 34 public MultiEncodingIndividual(MultiEncoding encoding, IScope scope) 35 : base(encoding, scope) { } 35 36 36 public MultiEncodingIndividual(MultiEncoding encoding, IScope scope) 37 : base(encoding, scope) { 38 individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray(); 37 private MultiEncodingIndividual(MultiEncodingIndividual copy) : base(copy.Encoding, new Scope()) { 38 copy.CopyToScope(Scope); 39 39 } 40 41 private MultiEncodingIndividual(MultiEncoding encoding, IScope scope, IEnumerable<Individual> individuals) 42 : base(encoding, scope) { 43 this.individuals = individuals; 44 } 45 46 47 public override IItem this[string name] { 48 get { 49 var individual = individuals.SingleOrDefault(i => i.Name == name); 50 if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name)); 51 return individual[name]; 52 } 53 set { 54 var individual = individuals.SingleOrDefault(i => i.Name == name); 55 if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name)); 56 individual[name] = value; 57 } 40 public override Individual Copy() { 41 return new MultiEncodingIndividual(this); 58 42 } 59 43 … … 62 46 try { 63 47 encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding); 64 } catch (InvalidOperationException) { 48 } 49 catch (InvalidOperationException) { 65 50 throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName())); 66 51 } … … 68 53 return encoding; 69 54 } 70 71 public override Individual CopyToScope(IScope scope) {72 var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray();73 return new MultiEncodingIndividual(Encoding, scope, copies);74 }75 55 } 76 56 } -
trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/SingleEncodingIndividual.cs
r14185 r14877 32 32 } 33 33 34 public override IItem this[string name] { 35 get { 36 if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name)); 37 return ExtractScopeValue(Name, Scope); 38 } 39 set { 40 if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name)); 41 SetScopeValue(Name, Scope, value); 42 } 34 private SingleEncodingIndividual(SingleEncodingIndividual copy) : base(copy.Encoding, new Scope()) { 35 copy.CopyToScope(Scope); 36 } 37 public override Individual Copy() { 38 return new SingleEncodingIndividual(this); 43 39 } 44 40 … … 48 44 return encoding; 49 45 } 50 51 public override Individual CopyToScope(IScope scope) {52 SetScopeValue(Name, scope, (IItem)this[Name].Clone());53 return new SingleEncodingIndividual(Encoding, scope);54 }55 46 } 56 47 -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
r14185 r14877 158 158 try { 159 159 return client.Evaluate(message, GetQualityMessageExtensions()); 160 } finally { 160 } 161 finally { 161 162 lock (clientLock) { 162 163 activeClients.Remove(client); … … 170 171 SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder(); 171 172 protobufBuilder.SolutionId = solutionId; 172 var scope = new Scope(); 173 individual.CopyToScope(scope); 174 foreach (var variable in scope.Variables) { 173 foreach (var variable in individual.Values) { 175 174 try { 176 MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder); 177 } catch (ArgumentException ex) { 175 MessageBuilder.AddToMessage(variable.Value, variable.Key, protobufBuilder); 176 } 177 catch (ArgumentException ex) { 178 178 throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex); 179 179 } -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs
r14185 r14877 155 155 try { 156 156 return client.Evaluate(message, GetQualityMessageExtensions()); 157 } finally { 157 } 158 finally { 158 159 lock (clientLock) { 159 160 activeClients.Remove(client); … … 167 168 SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder(); 168 169 protobufBuilder.SolutionId = solutionId; 169 var scope = new Scope(); 170 individual.CopyToScope(scope); 171 foreach (var variable in scope.Variables) { 170 foreach (var variable in individual.Values) { 172 171 try { 173 MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder); 174 } catch (ArgumentException ex) { 172 MessageBuilder.AddToMessage(variable.Value, variable.Key, protobufBuilder); 173 } 174 catch (ArgumentException ex) { 175 175 throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex); 176 176 }
Note: See TracChangeset
for help on using the changeset viewer.