Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12214 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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