Free cookie consent management tool by TermsFeed Policy Generator

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

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

#866

  • Improved exception handling and added default client
File size: 10.7 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.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Optimization.Operators;
34using HeuristicLab.Analysis;
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 {
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
43    }
44    public new ParameterCollection Parameters {
45      get { return base.Parameters; }
46    }
47    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
48      get { return Parameters; }
49    }
50
51    #region Parameters
52    public IValueParameter<IEvaluationServiceClient> ClientParameter {
53      get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
54    }
55    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
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    }
64    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
65      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
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    }
82    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
83      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
84    }
85    #endregion
86
87    #region Properties
88    public BoolValue Maximization {
89      get { return MaximizationParameter.Value; }
90      set { MaximizationParameter.Value = value; }
91    }
92    public ISolutionCreator SolutionCreator {
93      get { return SolutionCreatorParameter.Value; }
94      set { SolutionCreatorParameter.Value = value; }
95    }
96    ISolutionCreator IProblem.SolutionCreator {
97      get { return SolutionCreatorParameter.Value; }
98    }
99    public IExternalEvaluationProblemEvaluator Evaluator {
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 {
114      get { return OperatorsParameter.Value; }
115    }
116    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
117      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
118    }
119    #endregion
120
121    [StorableConstructor]
122    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
123    public ExternalEvaluationProblem()
124      : base() {
125      ExternalEvaluator evaluator = new ExternalEvaluator();
126      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
127
128      Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
129      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
130      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
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();
138    }
139
140    public override IDeepCloneable Clone(Cloner cloner) {
141      ExternalEvaluationProblem clone = (ExternalEvaluationProblem)base.Clone(cloner);
142      clone.AttachEventHandlers();
143      return clone;
144    }
145
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) {
172      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
173      ParameterizeOperators();
174      OnEvaluatorChanged();
175    }
176    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
177      ParameterizeOperators();
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    private void InitializeOperators() {
195      ItemList<IOperator> operators = OperatorsParameter.Value;
196      operators.Add(new BestScopeSolutionAnalyzer());
197      ParameterizeAnalyzers();
198    }
199    [StorableHook(HookType.AfterDeserialization)]
200    private void AttachEventHandlers() {
201      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
202      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
203      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
204      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
205      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
206      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
207      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
208    }
209    private void ParameterizeAnalyzers() {
210      BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
211      BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
212      BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
213      BestScopeSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
214      BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
215    }
216    private void ParameterizeEvaluator() {
217      Evaluator.ClientParameter.ActualName = ClientParameter.Name;
218    }
219    private void ParameterizeOperators() {
220      // This is a best effort approach to wiring
221      string qualityName = Evaluator.QualityParameter.ActualName;
222      foreach (IOperator op in OperatorsParameter.Value) {
223        foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
224          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
225        }
226        foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
227          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
228        }
229      }
230    }
231    #endregion
232  }
233}
Note: See TracBrowser for help on using the repository browser.