Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.DataAnalysis/RowShuffler.cs @ 13365

Last change on this file since 13365 was 457, checked in by gkronber, 16 years ago

improved shuffle rows feature (#233)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
26using System.Windows.Forms;
27using System.IO;
28using System.Globalization;
29
30namespace HeuristicLab.DataAnalysis {
31  class RowShuffler : Form, IDatasetManipulator {
32    private Label helpLabel;
33    private Button nextButton;
34    private DataGridView dataGridView;
35    private Dataset dataset;
36    public RowShuffler()
37      : base() {
38      InitializeComponent();
39    }
40
41
42    #region IDatasetManipulator Members
43
44    public string Action {
45      get { return "Shuffle rows..."; }
46    }
47
48    public void Execute(Dataset dataset) {
49      this.dataset = dataset;
50      dataGridView.ColumnCount = dataset.Columns;
51      dataGridView.RowCount = dataset.Rows;
52      for(int i = 0; i < dataset.Rows; i++) {
53        for(int j = 0; j < dataset.Columns; j++) {
54          dataGridView.Rows[i].Cells[j].Value = dataset.GetValue(i, j);
55          dataGridView.Rows[i].HeaderCell.Value = i.ToString();
56        }
57      }
58      for(int i = 0; i < dataset.Columns; i++) {
59        dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
60        dataGridView.Columns[i].Name = dataset.VariableNames[i];
61      }
62      dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
63
64      ShowDialog();
65    }
66
67    private void ExchangeRows(Dataset dataset, int i, int j) {
68      for(int k = 0; k < dataset.Columns; k++) {
69        double temp = dataset.GetValue(i, k);
70        dataset.SetValue(i, k, dataset.GetValue(j, k));
71        dataset.SetValue(j, k, temp);
72      }
73    }
74
75    #endregion
76
77    private void InitializeComponent() {
78      System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
79      this.dataGridView = new System.Windows.Forms.DataGridView();
80      this.helpLabel = new System.Windows.Forms.Label();
81      this.nextButton = new System.Windows.Forms.Button();
82      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
83      this.SuspendLayout();
84      //
85      // dataGridView
86      //
87      this.dataGridView.AllowUserToAddRows = false;
88      this.dataGridView.AllowUserToDeleteRows = false;
89      this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
90                  | System.Windows.Forms.AnchorStyles.Left)
91                  | System.Windows.Forms.AnchorStyles.Right)));
92      this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
93      this.dataGridView.Location = new System.Drawing.Point(12, 25);
94      this.dataGridView.Name = "dataGridView";
95      this.dataGridView.ReadOnly = true;
96      dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
97      dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
98      dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
99      dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
100      dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
101      dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
102      dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
103      this.dataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle1;
104      this.dataGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
105      this.dataGridView.Size = new System.Drawing.Size(607, 444);
106      this.dataGridView.TabIndex = 0;
107      this.dataGridView.SelectionChanged += new System.EventHandler(this.dataGridView_SelectionChanged);
108      //
109      // helpLabel
110      //
111      this.helpLabel.AutoSize = true;
112      this.helpLabel.Location = new System.Drawing.Point(12, 9);
113      this.helpLabel.Name = "helpLabel";
114      this.helpLabel.Size = new System.Drawing.Size(183, 13);
115      this.helpLabel.TabIndex = 1;
116      this.helpLabel.Text = "Please select the rows to be shuffled.";
117      //
118      // nextButton
119      //
120      this.nextButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
121      this.nextButton.Location = new System.Drawing.Point(544, 475);
122      this.nextButton.Name = "nextButton";
123      this.nextButton.Size = new System.Drawing.Size(75, 23);
124      this.nextButton.TabIndex = 2;
125      this.nextButton.Text = "Next...";
126      this.nextButton.UseVisualStyleBackColor = true;
127      this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
128      //
129      // RowShuffler
130      //
131      this.ClientSize = new System.Drawing.Size(631, 510);
132      this.Controls.Add(this.nextButton);
133      this.Controls.Add(this.helpLabel);
134      this.Controls.Add(this.dataGridView);
135      this.Name = "RowShuffler";
136      this.Text = "Row shuffler";
137      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
138      this.ResumeLayout(false);
139      this.PerformLayout();
140
141    }
142
143    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
144      if(dataGridView.SelectedRows.Count > 1) nextButton.Enabled = true;
145      else nextButton.Enabled = false;
146    }
147
148    private void nextButton_Click(object sender, EventArgs e) {
149      Random random = new Random();
150      for(int i = 0; i < dataGridView.SelectedRows.Count - 1; i++) {
151        int j = random.Next(i, dataGridView.SelectedRows.Count);
152        int col0 = dataGridView.SelectedRows[i].Index;
153        int col1 = dataGridView.SelectedRows[j].Index;
154        ExchangeRows(dataset, i, j);
155      }
156
157      Close();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.