[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 |
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Common;
|
---|
| 31 | using HeuristicLab.Common.Resources;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.OptimizationExpertSystem {
|
---|
| 34 | [Item("Expert-System Optimizer", "Currently in experimental phase, an expert system that makes algorithm suggestions based on fitness landscape analysis features and an optimization knowledge base.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 118)]
|
---|
| 37 | public sealed class ExpertSystemOptimizer : ParameterizedNamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
|
---|
| 38 |
|
---|
| 39 | public string Filename { get; set; }
|
---|
| 40 |
|
---|
| 41 | public static new Image StaticItemImage {
|
---|
| 42 | get { return VSImageLibrary.Library; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [Storable]
|
---|
[12847] | 46 | private int maximumEvaluations;
|
---|
| 47 | public int MaximumEvaluations {
|
---|
| 48 | get { return maximumEvaluations; }
|
---|
| 49 | set {
|
---|
| 50 | if (maximumEvaluations == value) return;
|
---|
| 51 | maximumEvaluations = value;
|
---|
| 52 | OnPropertyChanged("MaximumEvaluations");
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [Storable]
|
---|
[12842] | 57 | private RunCollection runs;
|
---|
| 58 | public RunCollection Runs {
|
---|
| 59 | get { return runs; }
|
---|
| 60 | private set {
|
---|
| 61 | if (value == null) throw new ArgumentNullException("value");
|
---|
| 62 | if (runs == value) return;
|
---|
| 63 | runs = value;
|
---|
| 64 | OnPropertyChanged("Runs");
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | [Storable]
|
---|
| 69 | private ExecutionState executionState;
|
---|
| 70 | public ExecutionState ExecutionState {
|
---|
| 71 | get { return executionState; }
|
---|
| 72 | private set {
|
---|
| 73 | if (executionState != value) {
|
---|
| 74 | executionState = value;
|
---|
| 75 | OnPropertyChanged("ExecutionState");
|
---|
| 76 | OnExecutionStateChanged();
|
---|
| 77 | OnItemImageChanged();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | [Storable]
|
---|
| 83 | private TimeSpan executionTime;
|
---|
| 84 | public TimeSpan ExecutionTime {
|
---|
| 85 | get { return executionTime; }
|
---|
| 86 | private set {
|
---|
| 87 | if (executionTime == value) return;
|
---|
| 88 | executionTime = value;
|
---|
| 89 | OnPropertyChanged("ExecutionTime");
|
---|
| 90 | OnExecutionTimeChanged();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | [Storable]
|
---|
| 95 | private RunCollection knowledgeBase;
|
---|
| 96 | public RunCollection KnowledgeBase {
|
---|
| 97 | get { return knowledgeBase; }
|
---|
| 98 | private set {
|
---|
| 99 | if (value == null) throw new ArgumentNullException("value");
|
---|
| 100 | if (knowledgeBase == value) return;
|
---|
| 101 | knowledgeBase = value;
|
---|
| 102 | OnPropertyChanged("KnowledgeBase");
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | [Storable]
|
---|
| 107 | private IProblem problem;
|
---|
| 108 | public IProblem Problem {
|
---|
| 109 | get { return problem; }
|
---|
| 110 | set {
|
---|
| 111 | if (problem == value) return;
|
---|
| 112 | problem = value;
|
---|
| 113 | OnPropertyChanged("Problem");
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[12847] | 117 | [Storable]
|
---|
| 118 | private ItemList<IAlgorithm> suggestedInstances;
|
---|
| 119 | private ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;
|
---|
| 120 | public ReadOnlyItemList<IAlgorithm> SuggestedInstances {
|
---|
| 121 | get { return readOnlySuggestedInstances; }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[12842] | 124 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
[12847] | 125 | get { return SuggestedInstances; }
|
---|
[12842] | 126 | }
|
---|
| 127 |
|
---|
| 128 | [StorableConstructor]
|
---|
| 129 | private ExpertSystemOptimizer(bool deserializing) : base(deserializing) { }
|
---|
| 130 | private ExpertSystemOptimizer(ExpertSystemOptimizer original, Cloner cloner)
|
---|
| 131 | : base(original, cloner) {
|
---|
| 132 | runs = cloner.Clone(original.runs);
|
---|
| 133 | executionState = original.executionState;
|
---|
| 134 | executionTime = original.executionTime;
|
---|
| 135 | knowledgeBase = cloner.Clone(original.knowledgeBase);
|
---|
| 136 | problem = cloner.Clone(original.problem);
|
---|
[12847] | 137 | suggestedInstances = cloner.Clone(original.suggestedInstances);
|
---|
| 138 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
[12842] | 139 | }
|
---|
| 140 | public ExpertSystemOptimizer() {
|
---|
| 141 | Runs = new RunCollection();
|
---|
[12847] | 142 | KnowledgeBase = new RunCollection();
|
---|
| 143 | suggestedInstances = new ItemList<IAlgorithm>();
|
---|
| 144 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
[12842] | 145 | ExecutionState = ExecutionState.Stopped;
|
---|
| 146 | ExecutionTime = TimeSpan.Zero;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 150 | return new ExpertSystemOptimizer(this, cloner);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[12847] | 153 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 154 | private void AfterDeserialization() {
|
---|
| 155 | readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[12842] | 158 | public void Prepare() {
|
---|
| 159 | Prepare(false);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | public void Prepare(bool clearRuns) {
|
---|
| 163 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
| 164 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 165 | if (clearRuns) Runs.Clear();
|
---|
| 166 | ExecutionTime = TimeSpan.Zero;
|
---|
| 167 | ExecutionState = ExecutionState.Prepared;
|
---|
| 168 | OnPrepared();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | public void Start() {
|
---|
| 172 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
| 173 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 174 | ExecutionState = ExecutionState.Started;
|
---|
| 175 | OnStarted();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | public void Pause() {
|
---|
| 179 | if (ExecutionState != ExecutionState.Started)
|
---|
| 180 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 181 | ExecutionState = ExecutionState.Paused;
|
---|
| 182 | OnPaused();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | public void Stop() {
|
---|
| 187 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
| 188 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 189 | ExecutionState = ExecutionState.Stopped;
|
---|
| 190 | OnStopped();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | public event EventHandler Prepared;
|
---|
| 194 | private void OnPrepared() {
|
---|
| 195 | var handler = Prepared;
|
---|
| 196 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | public event EventHandler Started;
|
---|
| 200 | private void OnStarted() {
|
---|
| 201 | var handler = Started;
|
---|
| 202 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | public event EventHandler Paused;
|
---|
| 206 | private void OnPaused() {
|
---|
| 207 | var handler = Paused;
|
---|
| 208 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | public event EventHandler Stopped;
|
---|
| 212 | private void OnStopped() {
|
---|
| 213 | var handler = Stopped;
|
---|
| 214 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 218 | private void OnExceptionOccurred(Exception e) {
|
---|
| 219 | var handler = ExceptionOccurred;
|
---|
| 220 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | public event EventHandler ExecutionStateChanged;
|
---|
| 224 | private void OnExecutionStateChanged() {
|
---|
| 225 | var handler = ExecutionStateChanged;
|
---|
| 226 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | public event EventHandler ExecutionTimeChanged;
|
---|
| 230 | private void OnExecutionTimeChanged() {
|
---|
| 231 | var handler = ExecutionTimeChanged;
|
---|
| 232 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 236 | private void OnPropertyChanged(string propertyName) {
|
---|
| 237 | var handler = PropertyChanged;
|
---|
| 238 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|