Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiObjectiveAnalyzer.cs @ 11424

Last change on this file since 11424 was 11400, checked in by abeham, 10 years ago

#2174: Added multi-objective programmable problem

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Problems.Programmable {
13  [Item("Multi-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
14  [StorableClass]
15  public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveProgrammableProblemAnalyzer {
16    public bool EnabledByDefault { get { return true; } }
17
18    public ILookupParameter<IMultiObjectiveProblemDefinitionHost> ProblemDefinitionParameter {
19      get { return (ILookupParameter<IMultiObjectiveProblemDefinitionHost>)Parameters["ProblemDefinition"]; }
20    }
21
22    public ILookupParameter<Configuration> ConfigurationParameter {
23      get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
24    }
25
26    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
27      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
28    }
29
30    public ILookupParameter<ResultCollection> ResultsParameter {
31      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
32    }
33
34    [StorableConstructor]
35    protected MultiObjectiveAnalyzer(bool deserializing) : base(deserializing) { }
36    protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }
37    public MultiObjectiveAnalyzer() {
38      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
39      Parameters.Add(new LookupParameter<IMultiObjectiveProblemDefinitionHost>("ProblemDefinition", "The host that holds the problem definition."));
40      Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
41      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
42      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new MultiObjectiveAnalyzer(this, cloner);
47    }
48
49    public override IOperation Apply() {
50      var host = ProblemDefinitionParameter.ActualValue;
51      if (host.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
52      var config = ConfigurationParameter.ActualValue;
53      var results = ResultsParameter.ActualValue;
54
55      IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
56      for (var i = 0; i < QualitiesParameter.Depth; i++)
57        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
58
59      var vectors = scopes.Select(scope => Helper.Extract(scope, config)).ToArray();
60      host.Instance.Analyze(vectors, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results);
61      return base.Apply();
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.