#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
namespace HeuristicLab.Analysis.Statistics {
[View("Sample Size Checker View")]
[Content(typeof(RunCollection), false)]
public sealed partial class SampleSizeChecker : ItemView {
private double[] data;
public SampleSizeChecker() {
InitializeComponent();
double prec = 0.01;
double conf = 0.95;
precisionTextBox.Text = prec.ToString();
confIntervalTextBox.Text = conf.ToString();
}
public new RunCollection Content {
get { return (RunCollection)base.Content; }
set { base.Content = value; }
}
public override bool ReadOnly {
get { return true; }
set { /*not needed because results are always readonly */}
}
protected override void OnContentChanged() {
base.OnContentChanged();
resultComboBox.Items.Clear();
if (Content != null) {
UpdateResultComboBox();
}
}
#region events
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded);
Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved);
Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset);
RegisterRunEvents(Content);
}
private void RegisterRunEvents(IEnumerable runs) {
foreach (IRun run in runs)
run.Changed += new EventHandler(run_Changed);
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded);
Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved);
Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset);
DeregisterRunEvents(Content);
}
private void DeregisterRunEvents(IEnumerable runs) {
foreach (IRun run in runs)
run.Changed -= new EventHandler(run_Changed);
}
private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
DeregisterRunEvents(e.OldItems);
RegisterRunEvents(e.Items);
//RebuildInfluenceDataTable();
}
private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
DeregisterRunEvents(e.Items);
//RebuildInfluenceDataTable();
}
private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
RegisterRunEvents(e.Items);
//RebuildInfluenceDataTable();
}
private void run_Changed(object sender, EventArgs e) {
if (InvokeRequired)
this.Invoke(new EventHandler(run_Changed), sender, e);
else {
IRun run = (IRun)sender;
UpdateRun(run);
}
}
#endregion
private void UpdateRun(IRun run) {
//TODO: hacky di hack... this is baaaadddd
RebuildDataTable();
}
private void UpdateResultComboBox() {
resultComboBox.Items.Clear();
var results = (from run in Content
where run.Visible
from result in run.Results
where result.Value is IntValue || result.Value is DoubleValue
select result.Key).Distinct().ToArray();
resultComboBox.Items.AddRange(results);
if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
}
private void RebuildDataTable() {
string resultName = (string)resultComboBox.SelectedItem;
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
var vals = runs.Select(x => (double)((dynamic)x.Results[resultName]).Value);
data = vals.ToArray();
DoubleMatrix dt = new DoubleMatrix(runs.Count(), 1);
dt.ColumnNames = new string[] { resultName };
int i = 0;
foreach (double val in vals) {
dt[i++, 0] = val;
}
stringConvertibleMatrixView.Content = dt;
}
private void estimateSampleSizeButton_Click(object sender, EventArgs e) {
double precision = double.Parse(precisionTextBox.Text);
double confInterval = double.Parse(confIntervalTextBox.Text);
int nrSamplesReq = SampleSizeDetermination.DetermineSampleSizeByEstimatingMean(data, precision, confInterval);
int curNrSamples = data.Count();
int diff = curNrSamples - nrSamplesReq;
IntMatrix m = new IntMatrix(1, 3);
m[0, 0] = curNrSamples;
m[0, 1] = nrSamplesReq;
m[0, 2] = diff;
m.ColumnNames = new string[] { "Nr. of current samples", "Minimum nr. of required samples", "Difference" };
MainFormManager.MainForm.ShowContent(m);
}
private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
RebuildDataTable();
}
}
}