-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainForm.cs
213 lines (179 loc) · 6.37 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#region License
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using HopfieldNetworkClient.Properties;
using MathNet.Numerics.LinearAlgebra;
using NeuralNetworks.Models;
using NeuralNetworks.Utility;
namespace HopfieldNetworkClient
{
public partial class MainForm : Form
{
private HopfieldNetwork _hopfieldNetwork;
private double[] _clearedData = { -1.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
public MainForm()
{
InitializeComponent();
InitializeControls();
_hopfieldNetwork = new HopfieldNetwork();
}
private void buttonSelect_Click(object sender, EventArgs e)
{
ChooseFolder();
SetStatusStrip(Color.LightBlue, "");
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
AddItemsToComboBox(cmbBoxFiles, GetFileNamesFromFolder());
SetStatusStrip(Color.LightBlue, "");
}
private void buttonTeach_Click(object sender, EventArgs e)
{
if (cmbBoxFiles.SelectedItem == null)
{
SetStatusStrip(Color.Red, Resources.statusStripInfo_SelectDataError);
return;
}
try
{
var datafilePath = $"{folderBrowser.SelectedPath}\\{cmbBoxFiles.SelectedItem}";
var separator = ' ';
_hopfieldNetwork.TeachWithHebbsRule(datafilePath, separator);
}
catch (Exception ex)
{
SetStatusStrip(Color.Red, ex.Message);
return;
}
SetStatusStrip(Color.Green, Resources.statusStripInfo_TeachingDone);
btnTest.Enabled = true;
}
private void chBox_CheckedChanged(object sender, EventArgs e)
{
var chBox = sender as CheckBox;
if (chBox != null)
{
chBox.CheckState = chBox.Checked ? CheckState.Indeterminate : CheckState.Unchecked;
}
FillTextBoxByData(txtBoxPatternVector, GetVectorFromPattern());
}
private void buttonClear_Click(object sender, EventArgs e)
{
FillTextBoxByData(txtBoxPatternVector, _clearedData);
var checkBoxes = GetPatternCheckBoxes();
foreach (var checkBox in checkBoxes)
{
checkBox.CheckState = CheckState.Unchecked;
}
}
private void buttonTest_Click(object sender, EventArgs e)
{
var testingVector = Vector<double>.Build.DenseOfArray(GetVectorFromPattern());
NeuralVector outputVector;
try
{
outputVector = _hopfieldNetwork.Test(new NeuralVector(testingVector));
}
catch (Exception exception)
{
SetStatusStrip(Color.Red, exception.Message);
return;
}
FillTextBoxByData(txtBoxOutputVector, outputVector.Data);
FillPatternByOutput(outputVector);
SetStatusStrip(Color.LightBlue, "");
}
private void InitializeControls()
{
folderBrowser.SelectedPath = Application.StartupPath;
SetDataPathAndFileComboBox();
FillTextBoxByData(txtBoxPatternVector, _clearedData);
ActiveControl = btnSelect;
btnTest.Enabled = false;
}
private void ChooseFolder()
{
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
SetDataPathAndFileComboBox();
}
}
private void SetDataPathAndFileComboBox()
{
txtBoxFolder.Text = folderBrowser.SelectedPath;
AddItemsToComboBox(cmbBoxFiles, GetFileNamesFromFolder());
}
private void AddItemsToComboBox(ComboBox comboBox, object[] itemsToAdd)
{
comboBox.Items.Clear();
comboBox.Items.AddRange(itemsToAdd);
}
private object[] GetFileNamesFromFolder()
{
var dirInfo = new DirectoryInfo(txtBoxFolder.Text);
var filesInfo = dirInfo.GetFiles("*.txt");
var filesNames = new object[filesInfo.Length];
for (int fileIndex = 0; fileIndex < filesInfo.Length; fileIndex++)
{
filesNames[fileIndex] = filesInfo[fileIndex].Name;
}
return filesNames;
}
private void FillTextBoxByData(TextBox textBox, IEnumerable<double> data)
{
textBox.Text = string.Join(", ", data);
}
private void SetStatusStrip(Color statusColor, string statusText)
{
statusStrip.BackColor = statusColor;
statusStripInfo.Text = statusText;
}
private double[] GetVectorFromPattern()
{
var vector = new List<double>();
var checkBoxes = GetPatternCheckBoxes();
foreach (var checkBox in checkBoxes)
{
if (checkBox.CheckState == CheckState.Indeterminate)
{
vector.Add(1.0);
}
else
{
vector.Add(-1.0);
}
}
return vector.ToArray();
}
private List<CheckBox> GetPatternCheckBoxes()
{
return groupBoxPattern.Controls.OfType<CheckBox>().OrderBy(chBox => chBox.Name).ToList();
}
private void FillPatternByOutput(NeuralVector outputVector)
{
var checkBoxes = GetPatternCheckBoxes();
checkBoxes.ForEach(checkBox => checkBox.CheckedChanged -= chBox_CheckedChanged);
for (int index = 0; index < checkBoxes.Count; index++)
{
if (outputVector.Data[index] == 1.0)
{
checkBoxes[index].CheckState = CheckState.Indeterminate;
}
else
{
checkBoxes[index].CheckState = CheckState.Unchecked;
}
}
checkBoxes.ForEach(checkBox => checkBox.CheckedChanged += chBox_CheckedChanged);
}
}
}