Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5716 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 10.9 KB
RevLine 
[3852]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3852]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]
[4419]40  public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
41    public string Filename { get; set; }
42
[3859]43    public override Image ItemImage {
[5287]44      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[3859]45    }
[4419]46
[3861]47    public new ParameterCollection Parameters {
48      get { return base.Parameters; }
49    }
50    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
51      get { return Parameters; }
52    }
[3859]53
[3852]54    #region Parameters
[3872]55    public IValueParameter<IEvaluationServiceClient> ClientParameter {
56      get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
[3859]57    }
[3872]58    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
[3859]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    }
[3861]67    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
68      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
[3859]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    }
[3861]85    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
86      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
87    }
[3852]88    #endregion
89
[3860]90    #region Properties
91    public BoolValue Maximization {
92      get { return MaximizationParameter.Value; }
93      set { MaximizationParameter.Value = value; }
94    }
[3861]95    public ISolutionCreator SolutionCreator {
[3860]96      get { return SolutionCreatorParameter.Value; }
97      set { SolutionCreatorParameter.Value = value; }
98    }
99    ISolutionCreator IProblem.SolutionCreator {
100      get { return SolutionCreatorParameter.Value; }
101    }
[3861]102    public IExternalEvaluationProblemEvaluator Evaluator {
[3860]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 {
[3861]117      get { return OperatorsParameter.Value; }
[3860]118    }
[3872]119    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
120      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
[3860]121    }
122    #endregion
123
[3861]124    [StorableConstructor]
[3864]125    private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
[4722]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    }
[3852]138    public ExternalEvaluationProblem()
139      : base() {
[3859]140      ExternalEvaluator evaluator = new ExternalEvaluator();
[3872]141      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
[3859]142
[3895]143      Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
[3859]144      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
[3862]145      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
[3861]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();
[3852]153    }
[3861]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) {
[3872]181      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
182      ParameterizeOperators();
[3861]183      OnEvaluatorChanged();
184    }
185    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
[3872]186      ParameterizeOperators();
[3861]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
[4722]202    #region Helper
[3861]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);
[3862]207      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
[3861]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    }
[4098]212    private void InitializeOperators() {
213      ItemList<IOperator> operators = OperatorsParameter.Value;
214      operators.Add(new BestScopeSolutionAnalyzer());
215      ParameterizeAnalyzers();
216    }
[3861]217    private void ParameterizeAnalyzers() {
[3872]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;
[3861]223    }
224    private void ParameterizeEvaluator() {
[3872]225      Evaluator.ClientParameter.ActualName = ClientParameter.Name;
[3861]226    }
[3872]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    }
[3861]239    #endregion
[3852]240  }
241}
Note: See TracBrowser for help on using the repository browser.