#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.Drawing;
using System.Linq;
using System.Windows.Forms;
using Accord.Statistics.Testing;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
[View("Parameter Influence View")]
[Content(typeof(RunCollection), false)]
public sealed partial class ParameterInfluenceView : ItemView {
public ParameterInfluenceView() {
InitializeComponent();
}
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();
parameterComboBox.Items.Clear();
if (Content != null) {
UpdateResultComboBox();
UpdateParameterComboBox();
}
}
#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
//RebuildInfluenceDataTable();
}
private void UpdateResultComboBox() {
resultComboBox.Items.Clear();
var results = (from run in Content
from result in run.Results
select result.Key).Distinct().ToArray();
resultComboBox.Items.AddRange(results);
if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
}
private void UpdateParameterComboBox() {
parameterComboBox.Items.Clear();
var parameters = (from run in Content
from param in run.Parameters
select param.Key).Distinct().ToArray();
parameterComboBox.Items.AddRange(parameters);
if (parameterComboBox.Items.Count > 0) parameterComboBox.SelectedItem = parameterComboBox.Items[0];
}
private List GetColumnNames() {
string resultName = (string)resultComboBox.SelectedItem;
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
var parameters = (from run in runs
from p in run.Parameters
select p.Key).Distinct();
parameters = parameters.Where(x => x != "Seed" && x != "Algorithm Name");
List ret = new List();
foreach (string para in parameters) {
var r = runs.Where(x => x.Parameters.ContainsKey(para));
var vals = r.First().Parameters[para];
if (vals.GetType().GetMember("Value").Count() > 0) {
var cnt = r.Select(x => ((dynamic)x.Parameters[para]).Value).Distinct();
if (cnt.Count() > 1)
ret.Add(para);
}
}
return ret;
}
private int CalculateParameterVariations() {
string parameterName = (string)parameterComboBox.SelectedItem;
var runs = Content.Where(x => x.Parameters.ContainsKey(parameterName) && x.Visible);
var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
var vals = r.First().Parameters[parameterName];
if (vals.GetType().GetMember("Value").Count() > 0) {
var cnt = r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct();
return cnt.Count();
}
return 0;
}
private List GetRowNames() {
string parameterName = (string)parameterComboBox.SelectedItem;
var runs = Content.Where(x => x.Parameters.ContainsKey(parameterName) && x.Visible);
List ret = new List();
var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
var vals = r.First().Parameters[parameterName];
if (vals.GetType().GetMember("Value").Count() > 0) {
var cnt = r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct();
ret.AddRange(cnt.Select(x => (string)x.ToString()));
}
return ret;
}
private bool ContainsParameterValue(IRun run, string parameterName, string valueName) {
if (!run.Parameters.ContainsKey(parameterName))
return false;
var para = run.Parameters[parameterName];
if (para.GetType().GetMember("Value").Count() > 0) {
var val = ((dynamic)para).Value;
if (val.ToString() == valueName) {
return true;
}
}
return false;
}
private List GetParameterVariatons(IEnumerable runs, string parameterName) {
List vals = new List();
var ps = runs.Select(x => (dynamic)x.Parameters[parameterName]);
foreach (var p in ps) {
if (p.GetType().GetMember("Value").Length > 0) {
var val = ((dynamic)p).Value;
if (!vals.Contains(val)) {
vals.Add(val);
}
}
}
return vals;
}
private void RebuildInfluenceDataTable() {
string resultName = (string)resultComboBox.SelectedItem;
string parameterName = (string)parameterComboBox.SelectedItem;
var columnNames = GetColumnNames().ToArray();
var tmpRowNames = GetRowNames();
tmpRowNames.Add("Overall");
var rowNames = tmpRowNames.ToArray();
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
StringMatrix dt = new StringMatrix(CalculateParameterVariations() + 1, columnNames.Count());
dt.RowNames = rowNames;
dt.ColumnNames = columnNames;
int i = 0, j = 0;
foreach (var rowName in rowNames) {
j = 0;
foreach (var columnName in columnNames) {
IEnumerable curRuns = null;
if (i + 1 == rowNames.Count()) {
//calculate Overall
curRuns = runs.Where(x => x.Parameters.ContainsKey(columnName));
} else {
curRuns = runs.Where(x => x.Parameters.ContainsKey(columnName) && ContainsParameterValue(x, parameterName, rowName));
}
var parameterVals = GetParameterVariatons(curRuns, columnName);
List results = new List();
foreach (var cpVal in parameterVals) {
var pvRuns = curRuns.Where(x => ((dynamic)x.Parameters[columnName]).Value == cpVal);
var ress = pvRuns.Select(x => (double)((dynamic)x.Results[resultName]).Value).ToArray();
results.Add(ress);
}
if (results.Count() > 1) {
OneWayAnova owAnova = new OneWayAnova(results.ToArray());
dt[i, j] = owAnova.Table.First().Significance.PValue.ToString();
} else {
dt[i, j] = string.Empty;
}
j++;
}
i++;
}
stringConvertibleMatrixView.Content = dt;
}
private void calculateInfluence_Click(object sender, EventArgs e) {
RebuildInfluenceDataTable();
}
private void colorButton_Click(object sender, EventArgs e) {
string resultName = (string)resultComboBox.SelectedItem;
string rowName = (string)parameterComboBox.SelectedItem;
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
Dictionary values = new Dictionary();
if (stringConvertibleMatrixView.DataGridView.CurrentCell != null) {
int curIndex = stringConvertibleMatrixView.DataGridView.CurrentCell.ColumnIndex;
for (int i = 0; i < stringConvertibleMatrixView.Content.Rows; i++) {
values[i] = double.Parse(stringConvertibleMatrixView.Content.GetValue(i, curIndex));
}
var orderedValues = values.OrderBy(x => x.Value);
for (int i = 0; i < orderedValues.Count(); i++) {
var row = stringConvertibleMatrixView.DataGridView.Rows[orderedValues.ElementAt(i).Key];
int r = (int)Math.Round((double)i / (double)orderedValues.Count() * 255.0);
int g = (int)Math.Round(((double)orderedValues.Count() - (double)i) / (double)orderedValues.Count() * 255.0);
Color color = Color.FromArgb(r, g, 0);
row.DefaultCellStyle.ForeColor = color;
}
}
}
}
}