1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.Programmable {
|
---|
13 | [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
|
---|
14 | [StorableClass]
|
---|
15 | public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer, IStochasticOperator {
|
---|
16 | public bool EnabledByDefault { get { return true; } }
|
---|
17 |
|
---|
18 | public ILookupParameter<IEncoding> EncodingParameter {
|
---|
19 | get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
23 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
27 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public ILookupParameter<IRandom> RandomParameter {
|
---|
31 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | private SingleObjectiveAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
38 | private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
39 | public SingleObjectiveAnalyzer() {
|
---|
40 | Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
|
---|
41 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
42 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
|
---|
43 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new SingleObjectiveAnalyzer(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IOperation Apply() {
|
---|
51 | var encoding = EncodingParameter.ActualValue;
|
---|
52 | var results = ResultsParameter.ActualValue;
|
---|
53 | var random = RandomParameter.ActualValue;
|
---|
54 |
|
---|
55 | IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
|
---|
56 | for (var i = 0; i < QualityParameter.Depth; i++)
|
---|
57 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
|
---|
58 |
|
---|
59 | var individuals = scopes.Select(encoding.GetIndividual).ToArray();
|
---|
60 | AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random);
|
---|
61 | return base.Apply();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|