[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using HeuristicLab.DataImporter.Data;
|
---|
| 29 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 30 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 31 | using HeuristicLab.DataImporter.Command.View;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.DataImporter.Command {
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [ViewableCommandInfoAttribute("Scaling between Min/Max", 1, ColumnGroupState.DoubleColumnSelected, "Change Values",
|
---|
| 37 | Position = 4, OptionsView = typeof(MinMaxCommandView))]
|
---|
| 38 | public class ScalingBetweenMinAndMaxCommand : ColumnGroupCommandWithAffectedColumnsBase {
|
---|
| 39 | private List<double> oldMinValues;
|
---|
| 40 | private List<double> oldMaxValues;
|
---|
| 41 |
|
---|
| 42 | private ScalingBetweenMinAndMaxCommand()
|
---|
| 43 | : base(null, string.Empty, null) {
|
---|
| 44 | oldMinValues = new List<double>();
|
---|
| 45 | oldMaxValues = new List<double>();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public ScalingBetweenMinAndMaxCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
|
---|
| 49 | : base(dataSet, columnGroupName, affectedColumns) {
|
---|
| 50 | oldMinValues = new List<double>();
|
---|
| 51 | oldMaxValues = new List<double>();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override string Description {
|
---|
| 55 | get { return "Scale Values between minimum and maximum"; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [Storable]
|
---|
| 59 | private double minValue;
|
---|
| 60 | public double MinValue {
|
---|
| 61 | get { return this.minValue; }
|
---|
| 62 | set { this.minValue = value; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | [Storable]
|
---|
| 66 | private double maxValue;
|
---|
| 67 | public double MaxValue {
|
---|
| 68 | get { return maxValue; }
|
---|
| 69 | set { this.maxValue = value; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public override void Execute() {
|
---|
| 73 | base.Execute();
|
---|
| 74 | if (maxValue < minValue)
|
---|
| 75 | throw new CommandExecutionException("Minimum must be smaller or equal than Maximum.", this);
|
---|
| 76 | DoubleColumn column;
|
---|
| 77 | int j = 0;
|
---|
| 78 | for (int i = 0; i < AffectedColumns.Length; i++) {
|
---|
[6227] | 79 | if (ColumnGroup.GetColumn(AffectedColumns[i]) is DoubleColumn) {
|
---|
[6133] | 80 | column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
|
---|
| 81 | if (column.ContainsNotNullValues) {
|
---|
| 82 | oldMinValues.Add((double)column.Minimum);
|
---|
| 83 | oldMaxValues.Add((double)column.Maximum);
|
---|
| 84 | ScaleLinear(column, oldMinValues[j], oldMaxValues[j], minValue, maxValue);
|
---|
| 85 | j++;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | ColumnGroup.FireChanged();
|
---|
| 90 | ColumnGroup = null;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public override void UndoExecute() {
|
---|
| 94 | base.UndoExecute();
|
---|
| 95 | DoubleColumn column;
|
---|
| 96 | int j = 0;
|
---|
| 97 | for (int i = 0; i < AffectedColumns.Length; i++) {
|
---|
| 98 | if (ColumnGroup.GetColumn(i) is DoubleColumn) {
|
---|
| 99 | column = (DoubleColumn)ColumnGroup.GetColumn(AffectedColumns[i]);
|
---|
| 100 | if (column.ContainsNotNullValues) {
|
---|
| 101 | oldMinValues.Add((double)column.Minimum);
|
---|
| 102 | oldMaxValues.Add((double)column.Maximum);
|
---|
| 103 | ScaleLinear(column, minValue, maxValue, oldMinValues[j], oldMaxValues[j]);
|
---|
| 104 | j++;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | oldMinValues.Clear();
|
---|
| 109 | oldMaxValues.Clear();
|
---|
| 110 | ColumnGroup.FireChanged();
|
---|
| 111 | ColumnGroup = null;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private void ScaleLinear(DoubleColumn column, double oldMin, double oldMax, double newMin, double newMax) {
|
---|
| 115 | double newRange = newMax - newMin;
|
---|
| 116 | double? actValue;
|
---|
| 117 | double oldRange = oldMax - oldMin;
|
---|
| 118 | if (oldRange == 0)
|
---|
| 119 | oldRange = 1;
|
---|
| 120 | for (int i = 0; i < column.TotalValuesCount; i++) {
|
---|
| 121 | actValue = (double?)column.GetValue(i);
|
---|
| 122 | if (actValue != null)
|
---|
| 123 | column.ChangeValue(i, newMin + newRange * (((double)actValue) - oldMin) / oldRange);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|