Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs @ 7317

Last change on this file since 7317 was 6189, checked in by epitzer, 14 years ago

Replace the ExternalEvaluator's Client property with a CheckedItemCollection of clients and synchronize access so every client is used by only one thread at a time. (#1526)

File size: 12.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
41    public string Filename { get; set; }
42
43    public override Image ItemImage {
44      get { return HeuristicLab.Common.Resources.VSImageLibrary.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<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
56      get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
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 ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
65      get { return MaximizationParameter; }
66    }
67    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
68      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
69    }
70    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
71      get { return SolutionCreatorParameter; }
72    }
73    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
74      get { return EvaluatorParameter; }
75    }
76    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
77      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
78    }
79    IParameter ISingleObjectiveHeuristicOptimizationProblem.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    public OptionalValueParameter<EvaluationCache> CacheParameter {
89      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
90    }
91    #endregion
92
93    #region Properties
94    public BoolValue Maximization {
95      get { return MaximizationParameter.Value; }
96      set { MaximizationParameter.Value = value; }
97    }
98    public ISolutionCreator SolutionCreator {
99      get { return SolutionCreatorParameter.Value; }
100      set { SolutionCreatorParameter.Value = value; }
101    }
102    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
103      get { return SolutionCreatorParameter.Value; }
104    }
105    public IExternalEvaluationProblemEvaluator Evaluator {
106      get { return EvaluatorParameter.Value; }
107      set { EvaluatorParameter.Value = value; }
108    }
109    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
110      get { return EvaluatorParameter.Value; }
111    }
112    IEvaluator IHeuristicOptimizationProblem.Evaluator {
113      get { return EvaluatorParameter.Value; }
114    }
115    public DoubleValue BestKnownQuality {
116      get { return BestKnownQualityParameter.Value; }
117      set { BestKnownQualityParameter.Value = value; }
118    }
119    public IEnumerable<IOperator> Operators {
120      get { return OperatorsParameter.Value; }
121    }
122    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
123      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
124    }
125    #endregion
126
127    [StorableConstructor]
128    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
129    [StorableHook(HookType.AfterDeserialization)]
130    private void AfterDeserializationHook() {
131      AttachEventHandlers();
132    }
133
134    private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
135      : base(original, cloner) {
136      AttachEventHandlers();
137    }
138    public override IDeepCloneable Clone(Cloner cloner) {
139      return new ExternalEvaluationProblem(this, cloner);
140    }
141    public ExternalEvaluationProblem()
142      : base() {
143      ExternalEvaluator evaluator = new ExternalEvaluator();
144      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
145
146      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
147      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
148      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
149      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
150      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
151      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
152      Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
153      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
154
155      InitializeOperators();
156      AttachEventHandlers();
157    }
158    [StorableHook(HookType.AfterDeserialization)]
159    private void AfterDeserialization() {
160      // BackwardsCompatibility3.3
161      #region Backwards compatible code, remove with 3.4
162      if (!Parameters.ContainsKey("Clients")) {
163        Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
164        if (Parameters.ContainsKey("Client")) {
165          var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
166          if (client != null)
167            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
168          Parameters.Remove("Client");
169        }
170      }
171      #endregion
172    }
173
174    #region Events
175    public event EventHandler SolutionCreatorChanged;
176    private void OnSolutionCreatorChanged() {
177      EventHandler handler = SolutionCreatorChanged;
178      if (handler != null) handler(this, EventArgs.Empty);
179    }
180    public event EventHandler EvaluatorChanged;
181    private void OnEvaluatorChanged() {
182      EventHandler handler = EvaluatorChanged;
183      if (handler != null) handler(this, EventArgs.Empty);
184    }
185    public event EventHandler OperatorsChanged;
186    private void OnOperatorsChanged() {
187      EventHandler handler = OperatorsChanged;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190    public event EventHandler Reset;
191    private void OnReset() {
192      EventHandler handler = Reset;
193      if (handler != null) handler(this, EventArgs.Empty);
194    }
195
196    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
197      OnSolutionCreatorChanged();
198    }
199    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
200      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
201      ParameterizeOperators();
202      OnEvaluatorChanged();
203    }
204    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
205      ParameterizeOperators();
206    }
207    private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
208      OnOperatorsChanged();
209    }
210    private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
211      OnOperatorsChanged();
212    }
213    private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
214      OnOperatorsChanged();
215    }
216    private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
217      OnOperatorsChanged();
218    }
219    #endregion
220
221    #region Helper
222    private void AttachEventHandlers() {
223      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
224      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
225      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
226      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
227      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
228      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
229      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
230    }
231    private void InitializeOperators() {
232      ItemList<IOperator> operators = OperatorsParameter.Value;
233      operators.Add(new BestScopeSolutionAnalyzer());
234      ParameterizeAnalyzers();
235    }
236    private void ParameterizeAnalyzers() {
237      BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
238      BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
239      BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
240      BestScopeSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
241      BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
242    }
243    private void ParameterizeEvaluator() {
244      Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
245    }
246    private void ParameterizeOperators() {
247      // This is a best effort approach to wiring
248      string qualityName = Evaluator.QualityParameter.ActualName;
249      foreach (IOperator op in OperatorsParameter.Value) {
250        foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
251          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
252        }
253        foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
254          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
255        }
256      }
257    }
258    #endregion
259  }
260}
Note: See TracBrowser for help on using the repository browser.