Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/ExpertSystem.cs @ 12860

Last change on this file since 12860 was 12860, checked in by abeham, 9 years ago

#2457: worked on expert system

File size: 6.3 KB
RevLine 
[12842]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.ComponentModel;
24using System.Collections.Generic;
25using System.Drawing;
[12860]26using System.Linq;
27using HeuristicLab.Analysis;
[12842]28using HeuristicLab.Core;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Common;
32using HeuristicLab.Common.Resources;
[12860]33using HeuristicLab.Data;
[12842]34
35namespace HeuristicLab.OptimizationExpertSystem {
[12860]36  [Item("Expert-System", "Currently in experimental phase, an expert system that makes algorithm suggestions based on fitness landscape analysis features and an optimization knowledge base.")]
[12842]37  [StorableClass]
[12860]38  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)]
39  public sealed class ExpertSystem : NamedItem, IStorableContent, INotifyPropertyChanged {
[12842]40
41    public string Filename { get; set; }
42
43    public static new Image StaticItemImage {
44      get { return VSImageLibrary.Library; }
45    }
46
47    [Storable]
[12847]48    private int maximumEvaluations;
49    public int MaximumEvaluations {
50      get { return maximumEvaluations; }
51      set {
52        if (maximumEvaluations == value) return;
53        maximumEvaluations = value;
54        OnPropertyChanged("MaximumEvaluations");
[12860]55        UpdateSuggestions();
[12847]56      }
57    }
58
59    [Storable]
[12842]60    private RunCollection runs;
61    public RunCollection Runs {
62      get { return runs; }
63    }
64
65    [Storable]
[12860]66    private ItemList<IAlgorithm> algorithmInstances;
67    public ItemList<IAlgorithm> AlgorithmInstances {
68      get { return algorithmInstances; }
[12842]69    }
70
71    [Storable]
[12860]72    private ISingleObjectiveHeuristicOptimizationProblem problem;
73    public ISingleObjectiveHeuristicOptimizationProblem Problem {
[12842]74      get { return problem; }
75      set {
76        if (problem == value) return;
77        problem = value;
78        OnPropertyChanged("Problem");
[12860]79        UpdateSuggestions();
[12842]80      }
81    }
82
[12847]83    [Storable]
84    private ItemList<IAlgorithm> suggestedInstances;
85    private ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;
86    public ReadOnlyItemList<IAlgorithm> SuggestedInstances {
87      get { return readOnlySuggestedInstances; }
88    }
89
[12860]90    private bool Maximization {
91      get { return Problem != null && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
[12842]92    }
93
94    [StorableConstructor]
[12860]95    private ExpertSystem(bool deserializing) : base(deserializing) { }
96    private ExpertSystem(ExpertSystem original, Cloner cloner)
[12842]97      : base(original, cloner) {
98      runs = cloner.Clone(original.runs);
[12860]99      algorithmInstances = cloner.Clone(original.algorithmInstances);
[12842]100      problem = cloner.Clone(original.problem);
[12847]101      suggestedInstances = cloner.Clone(original.suggestedInstances);
102      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
[12860]103      RegisterEventHandlers();
[12842]104    }
[12860]105    public ExpertSystem() {
106      Name = ItemName;
107      Description = ItemDescription;
108      runs = new RunCollection();
109      algorithmInstances = new ItemList<IAlgorithm>();
[12847]110      suggestedInstances = new ItemList<IAlgorithm>();
111      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
[12860]112      RegisterEventHandlers();
[12842]113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
[12860]116      return new ExpertSystem(this, cloner);
[12842]117    }
118
[12847]119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserialization() {
121      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
[12860]122      RegisterEventHandlers();
[12847]123    }
124
[12860]125    private void RegisterEventHandlers() {
126      runs.CollectionReset += InformationChanged;
127      runs.ItemsAdded += InformationChanged;
128      runs.ItemsRemoved += InformationChanged;
129      runs.Reset += InformationChanged;
130      runs.UpdateOfRunsInProgressChanged += InformationChanged;
131      algorithmInstances.CollectionReset += InformationChanged;
132      algorithmInstances.ItemsAdded += InformationChanged;
133      algorithmInstances.ItemsRemoved += InformationChanged;
[12842]134    }
135
[12860]136    private void InformationChanged(object sender, EventArgs e) {
137      var runCollection = sender as RunCollection;
138      if (runCollection != null && runCollection.UpdateOfRunsInProgress) return;
139      UpdateSuggestions();
[12842]140    }
141
[12860]142    private void UpdateSuggestions() {
143      if (Problem == null) return;
144      var instances = new SortedList<double, IAlgorithm>();
145      foreach (var instance in algorithmInstances) {
146        var relevantRuns = instance.Runs.Where(x => ((StringValue)x.Parameters["Problem Type"]).Value == Problem.GetType().Name);
147        var avgQuality = 0.0;
148        var counter = 0;
149        foreach (var run in relevantRuns) {
150          var performanceGraph = ((IndexedDataTable<double>)run.Results["QualityPerEvaluations"]);
151          try {
152            avgQuality += performanceGraph.Rows.First().Values.TakeWhile(x => x.Item1 < MaximumEvaluations).Last().Item2;
153            counter++;
154          } catch { continue; }
155        }
156        avgQuality /= counter;
157        instances.Add(avgQuality, instance);
158      }
[12842]159
[12860]160      suggestedInstances.Clear();
161      var instanceLadder = instances.Select(x => x.Value);
162      suggestedInstances.AddRange(Maximization ? instanceLadder.Reverse() : instanceLadder);
[12842]163    }
164
165    public event PropertyChangedEventHandler PropertyChanged;
166    private void OnPropertyChanged(string propertyName) {
167      var handler = PropertyChanged;
168      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.