Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs @ 4949

Last change on this file since 4949 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 10.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
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.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
41    public string Filename { get; set; }
42
43    public override Image ItemImage {
44      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
45    }
46
47    public new ParameterCollection Parameters {
48      get { return base.Parameters; }
49    }
50    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
51      get { return Parameters; }
52    }
53
54    #region Parameters
55    public IValueParameter<IEvaluationServiceClient> ClientParameter {
56      get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
57    }
58    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
59      get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
60    }
61    public ValueParameter<BoolValue> MaximizationParameter {
62      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
63    }
64    IParameter ISingleObjectiveProblem.MaximizationParameter {
65      get { return MaximizationParameter; }
66    }
67    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
68      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
69    }
70    IParameter IProblem.SolutionCreatorParameter {
71      get { return SolutionCreatorParameter; }
72    }
73    IParameter IProblem.EvaluatorParameter {
74      get { return EvaluatorParameter; }
75    }
76    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
77      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
78    }
79    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
80      get { return BestKnownQualityParameter; }
81    }
82    public OptionalValueParameter<IScope> BestKnownSolutionParameter {
83      get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
84    }
85    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
86      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
87    }
88    #endregion
89
90    #region Properties
91    public BoolValue Maximization {
92      get { return MaximizationParameter.Value; }
93      set { MaximizationParameter.Value = value; }
94    }
95    public ISolutionCreator SolutionCreator {
96      get { return SolutionCreatorParameter.Value; }
97      set { SolutionCreatorParameter.Value = value; }
98    }
99    ISolutionCreator IProblem.SolutionCreator {
100      get { return SolutionCreatorParameter.Value; }
101    }
102    public IExternalEvaluationProblemEvaluator Evaluator {
103      get { return EvaluatorParameter.Value; }
104      set { EvaluatorParameter.Value = value; }
105    }
106    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
107      get { return EvaluatorParameter.Value; }
108    }
109    IEvaluator IProblem.Evaluator {
110      get { return EvaluatorParameter.Value; }
111    }
112    public DoubleValue BestKnownQuality {
113      get { return BestKnownQualityParameter.Value; }
114      set { BestKnownQualityParameter.Value = value; }
115    }
116    public IEnumerable<IOperator> Operators {
117      get { return OperatorsParameter.Value; }
118    }
119    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
120      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
121    }
122    #endregion
123
124    [StorableConstructor]
125    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
126    [StorableHook(HookType.AfterDeserialization)]
127    private void AfterDeserializationHook() {
128      AttachEventHandlers();
129    }
130
131    private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
132      : base(original, cloner) {
133      AttachEventHandlers();
134    }
135    public override IDeepCloneable Clone(Cloner cloner) {
136      return new ExternalEvaluationProblem(this, cloner);
137    }
138    public ExternalEvaluationProblem()
139      : base() {
140      ExternalEvaluator evaluator = new ExternalEvaluator();
141      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
142
143      Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
144      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
145      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
146      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
147      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
148      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
149      Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
150
151      InitializeOperators();
152      AttachEventHandlers();
153    }
154
155    #region Events
156    public event EventHandler SolutionCreatorChanged;
157    private void OnSolutionCreatorChanged() {
158      EventHandler handler = SolutionCreatorChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161    public event EventHandler EvaluatorChanged;
162    private void OnEvaluatorChanged() {
163      EventHandler handler = EvaluatorChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166    public event EventHandler OperatorsChanged;
167    private void OnOperatorsChanged() {
168      EventHandler handler = OperatorsChanged;
169      if (handler != null) handler(this, EventArgs.Empty);
170    }
171    public event EventHandler Reset;
172    private void OnReset() {
173      EventHandler handler = Reset;
174      if (handler != null) handler(this, EventArgs.Empty);
175    }
176
177    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
178      OnSolutionCreatorChanged();
179    }
180    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
181      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
182      ParameterizeOperators();
183      OnEvaluatorChanged();
184    }
185    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
186      ParameterizeOperators();
187    }
188    private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
189      OnOperatorsChanged();
190    }
191    private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
192      OnOperatorsChanged();
193    }
194    private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
195      OnOperatorsChanged();
196    }
197    private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
198      OnOperatorsChanged();
199    }
200    #endregion
201
202    #region Helper
203    private void AttachEventHandlers() {
204      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
205      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
206      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
207      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
208      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
209      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
210      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
211    }
212    private void InitializeOperators() {
213      ItemList<IOperator> operators = OperatorsParameter.Value;
214      operators.Add(new BestScopeSolutionAnalyzer());
215      ParameterizeAnalyzers();
216    }
217    private void ParameterizeAnalyzers() {
218      BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
219      BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
220      BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
221      BestScopeSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
222      BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
223    }
224    private void ParameterizeEvaluator() {
225      Evaluator.ClientParameter.ActualName = ClientParameter.Name;
226    }
227    private void ParameterizeOperators() {
228      // This is a best effort approach to wiring
229      string qualityName = Evaluator.QualityParameter.ActualName;
230      foreach (IOperator op in OperatorsParameter.Value) {
231        foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
232          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
233        }
234        foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
235          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
236        }
237      }
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.