Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13649


Ignore:
Timestamp:
03/03/16 15:54:48 (9 years ago)
Author:
abeham
Message:

#2457: fixed bugs in suggestions

Location:
branches/PerformanceComparison
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemView.Designer.cs

    r13561 r13649  
    211211      this.suggestedInstancesComboBox.Size = new System.Drawing.Size(322, 21);
    212212      this.suggestedInstancesComboBox.TabIndex = 0;
     213      this.suggestedInstancesComboBox.SelectedIndexChanged += new System.EventHandler(this.SuggestedInstancesComboBoxOnSelectedIndexChanged);
    213214      //
    214215      // runsTabPage
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemView.cs

    r13569 r13649  
    2020#endregion
    2121
     22using HeuristicLab.Common.Resources;
     23using HeuristicLab.Core.Views;
     24using HeuristicLab.MainForm;
     25using HeuristicLab.Optimization;
     26using HeuristicLab.Optimization.Views;
    2227using System;
    2328using System.ComponentModel;
     
    2631using System.Windows.Forms;
    2732using System.Windows.Forms.DataVisualization.Charting;
    28 using HeuristicLab.Common.Resources;
    29 using HeuristicLab.Core.Views;
    30 using HeuristicLab.MainForm;
    31 using HeuristicLab.Optimization;
    32 using HeuristicLab.Optimization.Views;
    3333
    3434namespace HeuristicLab.OptimizationExpertSystem.Views {
     
    104104          kbViewHost.Content = Content.KnowledgeBase;
    105105          problemInstancesView.Content = Content.ProblemInstances;
     106          algorithmViewHost.Content = Content.CurrentResult;
    106107        }
    107108      } finally { SuppressEvents = false; }
     
    161162            break;
    162163          case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break;
     164          case "CurrentResult": algorithmViewHost.Content = Content.CurrentResult; break;
    163165        }
    164166      } finally { SuppressEvents = false; }
     
    231233
    232234    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
    233       var selectedInstance = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
    234       var clone = (IAlgorithm)selectedInstance.Clone();
    235       clone.Prepare(true);
    236       clone.Start();
    237       algorithmViewHost.Content = clone.Results;
     235      if (suggestedInstancesComboBox.SelectedIndex >= 0)
     236        Content.StartAlgorithmAsync(suggestedInstancesComboBox.SelectedIndex);
    238237    }
    239238
     
    268267      }
    269268    }
     269
     270    private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
     271      SetEnabledStateOfControls();
     272    }
    270273  }
    271274}
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemView.resx

    r13551 r13649  
    121121    <value>17, 17</value>
    122122  </metadata>
    123   <metadata name="errorProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    124     <value>17, 17</value>
    125   </metadata>
    126123  <metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    127124    <value>140, 17</value>
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/ExpertSystem.cs

    r13561 r13649  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    25 using System.Drawing;
    26 using System.IO;
    27 using System.Linq;
    28 using System.Threading.Tasks;
    2922using HeuristicLab.Analysis;
    3023using HeuristicLab.Collections;
     
    3730using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3831using HeuristicLab.Persistence.Default.Xml;
     32using System;
     33using System.Collections.Generic;
     34using System.ComponentModel;
     35using System.Drawing;
     36using System.IO;
     37using System.Linq;
     38using System.Threading;
     39using System.Threading.Tasks;
    3940using RunCreationClient = HeuristicLab.Clients.OKB.RunCreation.RunCreationClient;
    4041using SingleObjectiveOKBProblem = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBProblem;
     
    108109        problemInstances = value;
    109110        OnPropertyChanged("ProblemInstances");
     111      }
     112    }
     113
     114    [Storable]
     115    private ResultCollection currentResult;
     116    public ResultCollection CurrentResult {
     117      get { return currentResult; }
     118      set {
     119        if (currentResult == value) return;
     120        currentResult = value;
     121        OnPropertyChanged("CurrentResult");
    110122      }
    111123    }
     
    327339      "QualityPerEvaluations", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality"
    328340    };
     341
     342    public async void StartAlgorithmAsync(int index) {
     343      var selectedInstance = suggestedInstances[index];
     344      var algorithm = (IAlgorithm)selectedInstance.Clone();
     345      algorithm.Prepare(true);
     346      IParameter stopParam;
     347      var monitorStop = true;
     348      if (algorithm.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) {
     349        var maxEvalParam = stopParam as IValueParameter<Data.IntValue>;
     350        if (maxEvalParam != null) {
     351          maxEvalParam.Value.Value = MaximumEvaluations;
     352          monitorStop = false;
     353        }
     354      }
     355      algorithm.ExecutionStateChanged += AlgorithmOnExecutionStateChanged;
     356      algorithm.ExceptionOccurred += AlgorithmOnExceptionOccurred;
     357      if (monitorStop) algorithm.ExecutionTimeChanged += AlgorithmOnExecutionTimeChanged;
     358
     359      algorithm.Start();
     360    }
     361
     362    public void StartAlgorithm(int index) {
     363      var selectedInstance = suggestedInstances[index];
     364      var algorithm = (IAlgorithm)selectedInstance.Clone();
     365      algorithm.Prepare(true);
     366      IParameter stopParam;
     367      var monitorStop = true;
     368      if (algorithm.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) {
     369        var maxEvalParam = stopParam as IValueParameter<Data.IntValue>;
     370        if (maxEvalParam != null) {
     371          maxEvalParam.Value.Value = MaximumEvaluations;
     372          monitorStop = false;
     373        }
     374      }
     375      algorithm.ExecutionStateChanged += AlgorithmOnExecutionStateChanged;
     376      algorithm.ExceptionOccurred += AlgorithmOnExceptionOccurred;
     377      if (monitorStop) algorithm.ExecutionTimeChanged += AlgorithmOnExecutionTimeChanged;
     378
     379      using (algWh = new AutoResetEvent(false)) {
     380        algorithm.Start();
     381        algWh.WaitOne();
     382      }
     383      algWh = null;
     384    }
     385
     386    private AutoResetEvent algWh;
     387
     388    private void AlgorithmOnExecutionStateChanged(object sender, EventArgs eventArgs) {
     389      var alg = sender as IAlgorithm;
     390      if (alg == null) return;
     391      if (alg.ExecutionState == ExecutionState.Started) {
     392        CurrentResult = alg.Results;
     393      } else if (alg.ExecutionState == ExecutionState.Stopped) {
     394        alg.ExecutionStateChanged -= AlgorithmOnExecutionStateChanged;
     395        alg.ExceptionOccurred -= AlgorithmOnExceptionOccurred;
     396        alg.ExecutionTimeChanged -= AlgorithmOnExecutionTimeChanged;
     397        if (algWh != null) algWh.Set();
     398      }
     399    }
     400
     401    private void AlgorithmOnExceptionOccurred(object sender, EventArgs<Exception> eventArgs) {
     402      var alg = sender as IAlgorithm;
     403      if (alg == null) return;
     404      alg.ExecutionStateChanged -= AlgorithmOnExecutionStateChanged;
     405      alg.ExceptionOccurred -= AlgorithmOnExceptionOccurred;
     406      alg.ExecutionTimeChanged -= AlgorithmOnExecutionTimeChanged;
     407      if (algWh != null) algWh.Set();
     408    }
     409
     410    private void AlgorithmOnExecutionTimeChanged(object sender, EventArgs eventArgs) {
     411      var alg = sender as IAlgorithm;
     412      if (alg == null) return;
     413      IResult evalSolResult;
     414      if (!alg.Results.TryGetValue("EvaluatedSolutions", out evalSolResult) || !(evalSolResult.Value is Data.IntValue)) return;
     415      var evalSols = ((Data.IntValue)evalSolResult.Value).Value;
     416      if (evalSols >= MaximumEvaluations) alg.Stop();
     417    }
    329418
    330419    public async void UpdateKnowledgeBaseAsync(IProgress progress) {
     
    372461                var probRun = new Run() { Name = prob.Name };
    373462                prob.CollectParameterValues(probRun.Parameters);
     463                probRun.Parameters["Problem Name"] = new StringValue(prob.Name);
     464                probRun.Parameters["Problem Type"] = new StringValue(prob.GetType().Name);
    374465                progress.Status += Environment.NewLine + "Downloading characteristics...";
    375466                foreach (var v in RunCreationClient.GetCharacteristicValues(problInst.Id)) {
     
    412503        var count = queryClient.GetNumberOfRuns(problemClassFilter);
    413504        if (count == 0) return;
    414 
     505       
    415506        var runIds = queryClient.GetRunIds(problemClassFilter).ToList();
    416507        var conversions = new List<Task>();
     
    445536
    446537          var probInstanceName = ((StringValue)probNameParam).Value;
    447           var bkQuality = ((DoubleValue)instance.Parameters["BestKnownQuality"]).Value;
    448538          var maximization = ((BoolValue)instance.Parameters["Maximization"]).Value;
     539         
     540          IItem bkParam;
     541          if (!instance.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue)) {
     542            Dictionary<string, List<IRun>> algRuns;
     543            if (!algInstRunDict.TryGetValue(probInstanceName, out algRuns)) continue;
     544            var list = algRuns.SelectMany(x => x.Value)
     545                              .Where(x => x.Results.ContainsKey("QualityPerEvaluations"))
     546                              .Select(x => ((IndexedDataTable<double>)x.Results["QualityPerEvaluations"]).Rows.First().Values.Last().Item2);
     547            bkParam = new DoubleValue(maximization ? list.Max() : list.Min());
     548            instance.Parameters["BestKnownQuality"] = bkParam;
     549          } else bkParam = instance.Parameters["BestKnownQuality"];
     550
     551          var bkQuality = ((DoubleValue)bkParam).Value;
    449552
    450553          if (!algInstRunDict.ContainsKey(probInstanceName)) continue;
    451554          foreach (var kvp in algInstRunDict[probInstanceName]) {
    452             var algInstanceName = kvp.Key;
    453555            // TODO: Things needs to be configurable here (table name, targets)
    454556            foreach (var target in new[] { 1, 1.01, 1.05, 1.1, 1.2, 1.5 }) {
    455557              var result = ExpectedRuntimeHelper.CalculateErt(kvp.Value, "QualityPerEvaluations", bkQuality * target, maximization);
    456               var resultName = algInstanceName + "@" + ((target - 1) * 100) + "%";
     558              var resultName = kvp.Key + "@" + ((target - 1) * 100) + "%";
    457559              IItem item;
    458560              if (instance.Results.TryGetValue(resultName, out item)) {
     
    469571      if (Problem == null) return;
    470572      var instances = new SortedList<double, IAlgorithm>();
    471       foreach (var relevantRuns in knowledgeBase.GroupBy(x => x.Algorithm)) {
     573      foreach (var relevantRuns in knowledgeBase.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())) {
     574        var algorithm = algorithmId2AlgorithmInstanceMapping.GetByFirst(relevantRuns.Key);
    472575        var avgQuality = 0.0;
    473576        var counter = 0;
    474         foreach (var run in relevantRuns) {
    475           var performanceGraph = ((IndexedDataTable<double>)run.Results["QualityPerEvaluations"]);
    476           try {
    477             avgQuality += performanceGraph.Rows.First().Values.TakeWhile(x => x.Item1 < MaximumEvaluations).Last().Item2;
    478             counter++;
    479           } catch { continue; }
     577        foreach (var problemRuns in relevantRuns.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)) {
     578          var probInstance = ProblemInstances.SingleOrDefault(x => x.Name == problemRuns.Key);
     579          if (probInstance == null) continue;
     580          var bkQuality = ((DoubleValue)probInstance.Parameters["BestKnownQuality"]).Value;
     581          foreach (var run in problemRuns) {
     582            var performanceGraph = ((IndexedDataTable<double>)run.Results["QualityPerEvaluations"]);
     583            try {
     584              avgQuality += performanceGraph.Rows.First().Values.TakeWhile(x => x.Item1 < MaximumEvaluations).Last().Item2 / bkQuality;
     585              counter++;
     586            } catch {
     587              continue;
     588            }
     589          }
    480590        }
    481591        avgQuality /= counter;
    482         instances.Add(avgQuality, relevantRuns.Key);
     592        instances.Add(avgQuality, algorithm);
    483593      }
    484594
    485595      suggestedInstances.Clear();
    486       var instanceLadder = instances.Select(x => (IAlgorithm)x.Value.Clone());
    487       suggestedInstances.AddRange(Maximization ? instanceLadder.Reverse() : instanceLadder);
     596      var instanceLadder = instances.Select(x => (IAlgorithm)x.Value.Clone()).ToList();
     597      if (Maximization) instanceLadder.Reverse();
     598      suggestedInstances.AddRange(instanceLadder);
    488599    }
    489600
Note: See TracChangeset for help on using the changeset viewer.