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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Analysis;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Common;
|
---|
32 | using HeuristicLab.Common.Resources;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.OptimizationExpertSystem {
|
---|
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.")]
|
---|
37 | [StorableClass]
|
---|
38 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)]
|
---|
39 | public sealed class ExpertSystem : NamedItem, IStorableContent, INotifyPropertyChanged {
|
---|
40 |
|
---|
41 | public string Filename { get; set; }
|
---|
42 |
|
---|
43 | public static new Image StaticItemImage {
|
---|
44 | get { return VSImageLibrary.Library; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [Storable]
|
---|
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");
|
---|
55 | UpdateSuggestions();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | [Storable]
|
---|
60 | private RunCollection runs;
|
---|
61 | public RunCollection Runs {
|
---|
62 | get { return runs; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [Storable]
|
---|
66 | private ItemList<IAlgorithm> algorithmInstances;
|
---|
67 | public ItemList<IAlgorithm> AlgorithmInstances {
|
---|
68 | get { return algorithmInstances; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [Storable]
|
---|
72 | private ISingleObjectiveHeuristicOptimizationProblem problem;
|
---|
73 | public ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
74 | get { return problem; }
|
---|
75 | set {
|
---|
76 | if (problem == value) return;
|
---|
77 | problem = value;
|
---|
78 | OnPropertyChanged("Problem");
|
---|
79 | UpdateSuggestions();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [Storable]
|
---|
84 | private ItemList<IAlgorithm> suggestedInstances;
|
---|
85 | private ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;
|
---|
86 | public ReadOnlyItemList<IAlgorithm> SuggestedInstances {
|
---|
87 | get { return readOnlySuggestedInstances; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private bool Maximization {
|
---|
91 | get { return Problem != null && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | [StorableConstructor]
|
---|
95 | private ExpertSystem(bool deserializing) : base(deserializing) { }
|
---|
96 | private ExpertSystem(ExpertSystem original, Cloner cloner)
|
---|
97 | : base(original, cloner) {
|
---|
98 | runs = cloner.Clone(original.runs);
|
---|
99 | algorithmInstances = cloner.Clone(original.algorithmInstances);
|
---|
100 | problem = cloner.Clone(original.problem);
|
---|
101 | suggestedInstances = cloner.Clone(original.suggestedInstances);
|
---|
102 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
103 | RegisterEventHandlers();
|
---|
104 | }
|
---|
105 | public ExpertSystem() {
|
---|
106 | Name = ItemName;
|
---|
107 | Description = ItemDescription;
|
---|
108 | runs = new RunCollection();
|
---|
109 | algorithmInstances = new ItemList<IAlgorithm>();
|
---|
110 | suggestedInstances = new ItemList<IAlgorithm>();
|
---|
111 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
112 | RegisterEventHandlers();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
116 | return new ExpertSystem(this, cloner);
|
---|
117 | }
|
---|
118 |
|
---|
119 | [StorableHook(HookType.AfterDeserialization)]
|
---|
120 | private void AfterDeserialization() {
|
---|
121 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
122 | RegisterEventHandlers();
|
---|
123 | }
|
---|
124 |
|
---|
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;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void InformationChanged(object sender, EventArgs e) {
|
---|
137 | var runCollection = sender as RunCollection;
|
---|
138 | if (runCollection != null && runCollection.UpdateOfRunsInProgress) return;
|
---|
139 | UpdateSuggestions();
|
---|
140 | }
|
---|
141 |
|
---|
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 | }
|
---|
159 |
|
---|
160 | suggestedInstances.Clear();
|
---|
161 | var instanceLadder = instances.Select(x => x.Value);
|
---|
162 | suggestedInstances.AddRange(Maximization ? instanceLadder.Reverse() : instanceLadder);
|
---|
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 | }
|
---|