#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.Common;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.VariableInteractionNetworks {
public partial class CreateTargetVariationExperimentDialog : Form {
public Experiment Experiment { get; private set; }
private int repetitions;
private int combinationGroupSize;
public CreateTargetVariationExperimentDialog() : this(null) { }
public CreateTargetVariationExperimentDialog(IAlgorithm alg) {
InitializeComponent();
repetitions = (int)repetitionsNumericUpDown.Value;
combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
var problem = alg.Problem as IDataAnalysisProblem;
var inputCount = problem.ProblemData.AllowedInputVariables.Count();
if (combinationGroupSize > inputCount)
combinationGroupSize = inputCount;
warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
Experiment = null;
okButton.Enabled = alg != null;
}
public Experiment CreateVariableCombinations(IAlgorithm algorithm) {
var experiment = new Experiment("Target Variation Experiment");
if (algorithm == null)
throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
var problem = (IDataAnalysisProblem)algorithm.Problem;
var problemData = problem.ProblemData;
var variables = problemData.AllowedInputVariables.ToList();
for (int i = 0; i < variables.Count; i++) {
var target = variables[i];
var inputs = variables.Where(x => x != target).ToList();
if (combinationGroupSize > inputs.Count)
combinationGroupSize = inputs.Count;
var combinations = inputs.Combinations(combinationGroupSize);
foreach (var combination in combinations) {
var h = new HashSet(combination);
var alg = (IAlgorithm)algorithm.Clone();
alg.Name += " {Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
problem = (IDataAnalysisProblem)alg.Problem;
problemData = problem.ProblemData;
foreach (var item in problemData.InputVariables) {
problemData.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
}
SetTargetName(problemData, target);
var batchrun = new BatchRun(string.Format("Batchrun {0}", i + 1)) {
Repetitions = repetitions,
Optimizer = alg
};
experiment.Optimizers.Add(batchrun);
}
}
return experiment;
}
private static string GetTargetName(IDataAnalysisProblemData problemData) {
var regressionProblemData = problemData as IRegressionProblemData;
if (regressionProblemData != null)
return regressionProblemData.TargetVariable;
var classificationProblemData = problemData as IClassificationProblemData;
if (classificationProblemData != null)
return classificationProblemData.TargetVariable;
throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
}
private static void SetTargetName(IDataAnalysisProblemData problemData, string targetName) {
var regressionProblemData = problemData as IRegressionProblemData;
if (regressionProblemData != null) {
regressionProblemData.TargetVariable = targetName;
return;
}
var classificationProblemData = problemData as IClassificationProblemData;
if (classificationProblemData != null) {
classificationProblemData.TargetVariable = targetName;
return;
}
throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
}
#region events
private void okButton_Click(object sender, EventArgs e) {
IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) {
Experiment = CreateVariableCombinations((IAlgorithm)activeView.Content);
DialogResult = DialogResult.OK;
Close();
}
}
private void combinationGroupSizeNumericUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
if (activeView == null) return;
var algorithm = (IAlgorithm)activeView.Content;
var problem = algorithm.Problem as IDataAnalysisProblem;
if (problem == null) return;
var inputCount = problem.ProblemData.AllowedInputVariables.Count();
if (combinationGroupSizeNumericUpDown.Value > inputCount)
combinationGroupSizeNumericUpDown.Value = inputCount;
}
private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
if (repetitionsNumericUpDown.Text == string.Empty)
repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
repetitions = (int)repetitionsNumericUpDown.Value;
}
private void combinationGroupSizeNumericUpDown_Validated(object sender, EventArgs e) {
if (combinationGroupSizeNumericUpDown.Text == string.Empty)
combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString();
combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
if (activeView == null) return;
var algorithm = (IAlgorithm)activeView.Content;
var problem = (IDataAnalysisProblem)algorithm.Problem;
var inputCount = problem.ProblemData.AllowedInputVariables.Count();
warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
}
#endregion
}
}