Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/18/16 15:03:52 (8 years ago)
Author:
abeham
Message:

#2560: added loading of characteristic calculators into OKB problem view

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs

    r13534 r13540  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Globalization;
    2425using System.Linq;
    2526using System.Windows.Forms;
     
    2829using HeuristicLab.Data;
    2930using HeuristicLab.MainForm;
     31using HeuristicLab.Optimization;
     32using HeuristicLab.PluginInfrastructure;
    3033
    3134namespace HeuristicLab.Clients.OKB.RunCreation {
     
    4144    public OKBProblemView() {
    4245      InitializeComponent();
     46      refreshButton.Text = string.Empty;
     47      refreshButton.Image = VSImageLibrary.Refresh;
     48      cloneProblemButton.Text = string.Empty;
     49      cloneProblemButton.Image = VSImageLibrary.Clone;
    4350      downloadCharacteristicsButton.Text = string.Empty;
    4451      downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
     
    7279        parameterCollectionView.Content = Content.Parameters;
    7380      }
     81      UpdateCharacteristicCalculators();
    7482    }
    7583
     
    92100    }
    93101
     102    private void UpdateCharacteristicCalculators() {
     103      calculatorListView.Items.Clear();
     104      calculatorListView.Groups.Clear();
     105      if (Content == null || Content.ProblemId == -1) return;
     106      var problem = Content.CloneProblem();
     107      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().Where(x => x.CanCalculate(problem)).ToList();
     108      try {
     109        calculatorListView.BeginUpdate();
     110        foreach (var calc in calculators) {
     111          var group = calculatorListView.Groups.Add(calc.Name, calc.Name);
     112          group.Tag = calc;
     113          foreach (var c in calc.Characteristics) {
     114            var item = calculatorListView.Items.Add(c, c);
     115            item.Group = group;
     116          }
     117        }
     118      } finally { calculatorListView.EndUpdate(); }
     119    }
     120
    94121    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
    95122      if (InvokeRequired) {
     
    161188      } catch (Exception ex) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex); }
    162189    }
     190    private void calculateButton_Click(object sender, EventArgs e) {
     191      var problem = Content.CloneProblem();
     192      var characteristics = calculatorListView.CheckedItems.OfType<ListViewItem>().GroupBy(x => x.Group);
     193      var results = new Dictionary<string, double>();
     194      foreach (var c in characteristics) {
     195        var calc = (ICharacteristicCalculator)c.Key.Tag;
     196        foreach (var result in calc.Calculate(problem, c.Select(x => x.Text).ToArray()))
     197          results[result.Key] = result.Value;
     198      }
     199      var matrix = characteristicsMatrixView.Content as StringMatrix;
     200      if (matrix == null) matrix = new StringMatrix(results.Count, 3);
     201      for (var i = 0; i < matrix.Rows; i++) {
     202        double r;
     203        if (results.TryGetValue(matrix[i, 0], out r)) {
     204          matrix[i, 1] = r.ToString(CultureInfo.CurrentCulture.NumberFormat);
     205          matrix[i, 2] = "DoubleValue";
     206          results.Remove(matrix[i, 0]);
     207        }
     208      }
     209      if (results.Count == 0) return;
     210      var resultsList = results.ToList();
     211      var counter = resultsList.Count - 1;
     212      for (var i = 0; i < matrix.Rows; i++) {
     213        if (string.IsNullOrEmpty(matrix[i, 0])) {
     214          matrix[i, 0] = resultsList[counter].Key;
     215          matrix[i, 1] = resultsList[counter].Value.ToString(CultureInfo.CurrentCulture.NumberFormat);
     216          matrix[i, 2] = "DoubleValue";
     217          resultsList.RemoveAt(counter);
     218          counter--;
     219          if (counter < 0) return;
     220        }
     221      }
     222      if (counter >= 0) {
     223        ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
     224        for (var i = matrix.Rows - 1; counter >= 0; i--) {
     225          matrix[i, 0] = resultsList[0].Key;
     226          matrix[i, 1] = resultsList[0].Value.ToString(CultureInfo.CurrentCulture.NumberFormat);
     227          matrix[i, 2] = "DoubleValue";
     228          counter--;
     229        }
     230      }
     231    }
    163232    #endregion
    164233
Note: See TracChangeset for help on using the changeset viewer.