Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs @ 4390

Last change on this file since 4390 was 4118, checked in by abeham, 14 years ago

#1090

  • Fixed problem plugins reloading their operators on deserialization in following problems (forgot on them in the first commit)
    • SupportVectorRegressionProblem
    • SymbolicTimeSeriesPrognosisProblem
  • Fixed a bug in the FeatureSelectionProblem introduced in r4098
  • Fixed the issues mentioned in the code review of mkommend
File size: 10.7 KB
RevLine 
[3852]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[3861]22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
[4068]26using HeuristicLab.Analysis;
[3861]27using HeuristicLab.Collections;
[3862]28using HeuristicLab.Common;
[3852]29using HeuristicLab.Core;
[3861]30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
[4068]32using HeuristicLab.Optimization.Operators;
[3861]33using HeuristicLab.Parameters;
[3852]34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Problems.ExternalEvaluation {
37  [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
[3862]38  [Creatable("Problems")]
[3852]39  [StorableClass]
[3862]40  public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
[3859]41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
43    }
[3861]44    public new ParameterCollection Parameters {
45      get { return base.Parameters; }
46    }
47    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
48      get { return Parameters; }
49    }
[3859]50
[3852]51    #region Parameters
[3872]52    public IValueParameter<IEvaluationServiceClient> ClientParameter {
53      get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
[3859]54    }
[3872]55    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
[3859]56      get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
57    }
58    public ValueParameter<BoolValue> MaximizationParameter {
59      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
60    }
61    IParameter ISingleObjectiveProblem.MaximizationParameter {
62      get { return MaximizationParameter; }
63    }
[3861]64    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
65      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
[3859]66    }
67    IParameter IProblem.SolutionCreatorParameter {
68      get { return SolutionCreatorParameter; }
69    }
70    IParameter IProblem.EvaluatorParameter {
71      get { return EvaluatorParameter; }
72    }
73    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
74      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
75    }
76    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
77      get { return BestKnownQualityParameter; }
78    }
79    public OptionalValueParameter<IScope> BestKnownSolutionParameter {
80      get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
81    }
[3861]82    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
83      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
84    }
[3852]85    #endregion
86
[3860]87    #region Properties
88    public BoolValue Maximization {
89      get { return MaximizationParameter.Value; }
90      set { MaximizationParameter.Value = value; }
91    }
[3861]92    public ISolutionCreator SolutionCreator {
[3860]93      get { return SolutionCreatorParameter.Value; }
94      set { SolutionCreatorParameter.Value = value; }
95    }
96    ISolutionCreator IProblem.SolutionCreator {
97      get { return SolutionCreatorParameter.Value; }
98    }
[3861]99    public IExternalEvaluationProblemEvaluator Evaluator {
[3860]100      get { return EvaluatorParameter.Value; }
101      set { EvaluatorParameter.Value = value; }
102    }
103    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
104      get { return EvaluatorParameter.Value; }
105    }
106    IEvaluator IProblem.Evaluator {
107      get { return EvaluatorParameter.Value; }
108    }
109    public DoubleValue BestKnownQuality {
110      get { return BestKnownQualityParameter.Value; }
111      set { BestKnownQualityParameter.Value = value; }
112    }
113    public IEnumerable<IOperator> Operators {
[3861]114      get { return OperatorsParameter.Value; }
[3860]115    }
[3872]116    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
117      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
[3860]118    }
119    #endregion
120
[3861]121    [StorableConstructor]
[3864]122    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
[3852]123    public ExternalEvaluationProblem()
124      : base() {
[3859]125      ExternalEvaluator evaluator = new ExternalEvaluator();
[3872]126      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
[3859]127
[3895]128      Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
[3859]129      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
[3862]130      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
[3861]131      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
132      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
133      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
134      Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
135
136      InitializeOperators();
137      AttachEventHandlers();
[3852]138    }
[3861]139
[3862]140    public override IDeepCloneable Clone(Cloner cloner) {
141      ExternalEvaluationProblem clone = (ExternalEvaluationProblem)base.Clone(cloner);
142      clone.AttachEventHandlers();
143      return clone;
144    }
145
[3861]146    #region Events
147    public event EventHandler SolutionCreatorChanged;
148    private void OnSolutionCreatorChanged() {
149      EventHandler handler = SolutionCreatorChanged;
150      if (handler != null) handler(this, EventArgs.Empty);
151    }
152    public event EventHandler EvaluatorChanged;
153    private void OnEvaluatorChanged() {
154      EventHandler handler = EvaluatorChanged;
155      if (handler != null) handler(this, EventArgs.Empty);
156    }
157    public event EventHandler OperatorsChanged;
158    private void OnOperatorsChanged() {
159      EventHandler handler = OperatorsChanged;
160      if (handler != null) handler(this, EventArgs.Empty);
161    }
162    public event EventHandler Reset;
163    private void OnReset() {
164      EventHandler handler = Reset;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
167
168    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
169      OnSolutionCreatorChanged();
170    }
171    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
[3872]172      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
173      ParameterizeOperators();
[3861]174      OnEvaluatorChanged();
175    }
176    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
[3872]177      ParameterizeOperators();
[3861]178    }
179    private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
180      OnOperatorsChanged();
181    }
182    private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
183      OnOperatorsChanged();
184    }
185    private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
186      OnOperatorsChanged();
187    }
188    private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
189      OnOperatorsChanged();
190    }
191    #endregion
192
193    #region Helpers
194    [StorableHook(HookType.AfterDeserialization)]
[4118]195    private void AfterDeserializationHook() {
196      AttachEventHandlers();
197    }
198
[3861]199    private void AttachEventHandlers() {
200      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
201      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
202      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3862]203      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
[3861]204      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
205      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
206      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
207    }
[4098]208    private void InitializeOperators() {
209      ItemList<IOperator> operators = OperatorsParameter.Value;
210      operators.Add(new BestScopeSolutionAnalyzer());
211      ParameterizeAnalyzers();
212    }
[3861]213    private void ParameterizeAnalyzers() {
[3872]214      BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
215      BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
216      BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
217      BestScopeSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
218      BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
[3861]219    }
220    private void ParameterizeEvaluator() {
[3872]221      Evaluator.ClientParameter.ActualName = ClientParameter.Name;
[3861]222    }
[3872]223    private void ParameterizeOperators() {
224      // This is a best effort approach to wiring
225      string qualityName = Evaluator.QualityParameter.ActualName;
226      foreach (IOperator op in OperatorsParameter.Value) {
227        foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
228          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
229        }
230        foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
231          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
232        }
233      }
234    }
[3861]235    #endregion
[3852]236  }
237}
Note: See TracBrowser for help on using the repository browser.