Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/SampleSizeChecker.cs @ 9330

Last change on this file since 9330 was 9330, checked in by ascheibe, 11 years ago

#1886 added a view for sample size determination

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
33  [View("Sample Size Checker View")]
34  [Content(typeof(RunCollection), false)]
35  public sealed partial class SampleSizeChecker : ItemView {
36
37    private double[] data;
38
39    public SampleSizeChecker() {
40      InitializeComponent();
41
42      double prec = 0.01;
43      double conf = 0.95;
44      precisionTextBox.Text = prec.ToString();
45      confIntervalTextBox.Text = conf.ToString();
46    }
47
48    public new RunCollection Content {
49      get { return (RunCollection)base.Content; }
50      set { base.Content = value; }
51    }
52
53    public override bool ReadOnly {
54      get { return true; }
55      set { /*not needed because results are always readonly */}
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      resultComboBox.Items.Clear();
61
62      if (Content != null) {
63        UpdateResultComboBox();
64      }
65    }
66
67    #region events
68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
71      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
72      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
73
74      RegisterRunEvents(Content);
75    }
76
77    private void RegisterRunEvents(IEnumerable<IRun> runs) {
78      foreach (IRun run in runs)
79        run.Changed += new EventHandler(run_Changed);
80    }
81
82    protected override void DeregisterContentEvents() {
83      base.DeregisterContentEvents();
84      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
85      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
86      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
87
88      DeregisterRunEvents(Content);
89    }
90
91    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
92      foreach (IRun run in runs)
93        run.Changed -= new EventHandler(run_Changed);
94    }
95
96    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
97      DeregisterRunEvents(e.OldItems);
98      RegisterRunEvents(e.Items);
99      //RebuildInfluenceDataTable();
100    }
101
102    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
103      DeregisterRunEvents(e.Items);
104      //RebuildInfluenceDataTable();
105    }
106
107    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
108      RegisterRunEvents(e.Items);
109      //RebuildInfluenceDataTable();
110    }
111
112    private void run_Changed(object sender, EventArgs e) {
113      if (InvokeRequired)
114        this.Invoke(new EventHandler(run_Changed), sender, e);
115      else {
116        IRun run = (IRun)sender;
117        UpdateRun(run);
118      }
119    }
120    #endregion
121
122    private void UpdateRun(IRun run) {
123      //TODO: hacky di hack... this is baaaadddd
124      RebuildDataTable();
125    }
126
127    private void UpdateResultComboBox() {
128      resultComboBox.Items.Clear();
129      var results = (from run in Content
130                     where run.Visible
131                     from result in run.Results
132                     where result.Value is IntValue || result.Value is DoubleValue
133                     select result.Key).Distinct().ToArray();
134
135      resultComboBox.Items.AddRange(results);
136      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
137    }
138
139    private void RebuildDataTable() {
140      string resultName = (string)resultComboBox.SelectedItem;
141
142      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
143      var vals = runs.Select(x => (double)((dynamic)x.Results[resultName]).Value);
144      data = vals.ToArray();
145
146      DoubleMatrix dt = new DoubleMatrix(runs.Count(), 1);
147      dt.ColumnNames = new string[] { resultName };
148
149      int i = 0;
150      foreach (double val in vals) {
151        dt[i++, 0] = val;
152      }
153
154      stringConvertibleMatrixView.Content = dt;
155    }
156
157    private void estimateSampleSizeButton_Click(object sender, EventArgs e) {
158      double precision = double.Parse(precisionTextBox.Text);
159      double confInterval = double.Parse(confIntervalTextBox.Text);
160
161      int nrSamplesReq = SampleSizeDetermination.DetermineSampleSizeByEstimatingMean(data, precision, confInterval);
162      int curNrSamples = data.Count();
163      int diff = curNrSamples - nrSamplesReq;
164
165      IntMatrix m = new IntMatrix(1, 3);
166      m[0, 0] = curNrSamples;
167      m[0, 1] = nrSamplesReq;
168      m[0, 2] = diff;
169      m.ColumnNames = new string[] { "Nr. of current samples", "Minimum nr. of required samples", "Difference" };
170
171      MainFormManager.MainForm.ShowContent(m);
172    }
173
174    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
175      RebuildDataTable();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.