Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/13/15 16:05:08 (9 years ago)
Author:
abeham
Message:

#2457: worked on expert system

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/ExpertSystem.cs

    r12847 r12860  
    2424using System.Collections.Generic;
    2525using System.Drawing;
    26 
     26using System.Linq;
     27using HeuristicLab.Analysis;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Optimization;
     
    3031using HeuristicLab.Common;
    3132using HeuristicLab.Common.Resources;
     33using HeuristicLab.Data;
    3234
    3335namespace 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.")]
     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.")]
    3537  [StorableClass]
    36   [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 118)]
    37   public sealed class ExpertSystemOptimizer : ParameterizedNamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
     38  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)]
     39  public sealed class ExpertSystem : NamedItem, IStorableContent, INotifyPropertyChanged {
    3840
    3941    public string Filename { get; set; }
     
    5153        maximumEvaluations = value;
    5254        OnPropertyChanged("MaximumEvaluations");
     55        UpdateSuggestions();
    5356      }
    5457    }
     
    5861    public RunCollection Runs {
    5962      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       }
    6663    }
    6764
    6865    [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       }
     66    private ItemList<IAlgorithm> algorithmInstances;
     67    public ItemList<IAlgorithm> AlgorithmInstances {
     68      get { return algorithmInstances; }
    8069    }
    8170
    8271    [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 {
     72    private ISingleObjectiveHeuristicOptimizationProblem problem;
     73    public ISingleObjectiveHeuristicOptimizationProblem Problem {
    10974      get { return problem; }
    11075      set {
     
    11277        problem = value;
    11378        OnPropertyChanged("Problem");
     79        UpdateSuggestions();
    11480      }
    11581    }
     
    12288    }
    12389
    124     public IEnumerable<IOptimizer> NestedOptimizers {
    125       get { return SuggestedInstances; }
     90    private bool Maximization {
     91      get { return Problem != null && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
    12692    }
    12793
    12894    [StorableConstructor]
    129     private ExpertSystemOptimizer(bool deserializing) : base(deserializing) { }
    130     private ExpertSystemOptimizer(ExpertSystemOptimizer original, Cloner cloner)
     95    private ExpertSystem(bool deserializing) : base(deserializing) { }
     96    private ExpertSystem(ExpertSystem original, Cloner cloner)
    13197      : base(original, cloner) {
    13298      runs = cloner.Clone(original.runs);
    133       executionState = original.executionState;
    134       executionTime = original.executionTime;
    135       knowledgeBase = cloner.Clone(original.knowledgeBase);
     99      algorithmInstances = cloner.Clone(original.algorithmInstances);
    136100      problem = cloner.Clone(original.problem);
    137101      suggestedInstances = cloner.Clone(original.suggestedInstances);
    138102      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
     103      RegisterEventHandlers();
    139104    }
    140     public ExpertSystemOptimizer() {
    141       Runs = new RunCollection();
    142       KnowledgeBase = new RunCollection();
     105    public ExpertSystem() {
     106      Name = ItemName;
     107      Description = ItemDescription;
     108      runs = new RunCollection();
     109      algorithmInstances = new ItemList<IAlgorithm>();
    143110      suggestedInstances = new ItemList<IAlgorithm>();
    144111      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
    145       ExecutionState = ExecutionState.Stopped;
    146       ExecutionTime = TimeSpan.Zero;
     112      RegisterEventHandlers();
    147113    }
    148114
    149115    public override IDeepCloneable Clone(Cloner cloner) {
    150       return new ExpertSystemOptimizer(this, cloner);
     116      return new ExpertSystem(this, cloner);
    151117    }
    152118
     
    154120    private void AfterDeserialization() {
    155121      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
     122      RegisterEventHandlers();
    156123    }
    157124
    158     public void Prepare() {
    159       Prepare(false);
     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;
    160134    }
    161135
    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();
     136    private void InformationChanged(object sender, EventArgs e) {
     137      var runCollection = sender as RunCollection;
     138      if (runCollection != null && runCollection.UpdateOfRunsInProgress) return;
     139      UpdateSuggestions();
    169140    }
    170141
    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     }
     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      }
    177159
    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);
     160      suggestedInstances.Clear();
     161      var instanceLadder = instances.Select(x => x.Value);
     162      suggestedInstances.AddRange(Maximization ? instanceLadder.Reverse() : instanceLadder);
    233163    }
    234164
Note: See TracChangeset for help on using the changeset viewer.