-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
351 lines (268 loc) · 11.4 KB
/
Copy pathForm1.cs
File metadata and controls
351 lines (268 loc) · 11.4 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using cwklib2020;
namespace Engine_Gen_Alg_Test
{
public partial class Form1 : Form
{
private static Random rnd = new Random();
private int Pop_Size;
public List<Individual> Current_Pop = new List<Individual>();
public List<Individual> New_Pop = new List<Individual>();
List<TextBox> Levels;
List<CheckBox> Devices;
public int Gen_Num { get; private set; }
public Individual Best_Run { get; private set; }
public int Mutation_Rate;
public float Crossover_Rate;
public int Tournament_Pressure;
public int Cross_site;
public int Elitism; //Percentage of unchanged in new generation
private BackgroundWorker New_Gen = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.WorkerReportsProgress = true;
}
private void Pop_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void pop_btn_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(Pop_tbx.Text)) { MessageBox.Show("Enter Population size"); }
if (String.IsNullOrEmpty(M_tbx.Text)) { MessageBox.Show("Enter Mutation Rate"); }
if (String.IsNullOrEmpty(C_tbx.Text)) { MessageBox.Show("Enter CrossOver Rate"); }
if (String.IsNullOrEmpty(T_tbx.Text)) { MessageBox.Show("Enter Tournament size"); }
if (String.IsNullOrEmpty(Cs_tbx.Text) && !Cs_cbx.Checked) { MessageBox.Show("Choose Crossove Site"); }
if (String.IsNullOrEmpty(Mg_tbx.Text) || String.IsNullOrEmpty(Cv_tbx.Text) ) { MessageBox.Show("Enter Terminating Criteria"); }
else
{
pop_btn.Enabled = false;
//reset main Parameters for Algorithm
Pop_Size = Convert.ToInt32(Pop_tbx.Text);
Current_Pop = Population.Init_Pop_Of_size(Pop_Size).ToList();
Gen_Num = 0;
this.backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int Flag=0; int temp=0;
for (int i = 0; i <= Convert.ToInt32(Mg_tbx.Text); i++)
{
//Sort and find best fitness
Current_Pop.Sort(new IndividualComparer()); Best_Run = Current_Pop[0];
temp = Best_Run.Fitness;
New_Generation();
// sort again and find best fitness
Current_Pop.Sort(new IndividualComparer());
//Check for Convergence
if (temp==Current_Pop[0].Fitness)
{
Flag++;
// if Reached Convergence, Stop Algorithm
if (Flag == Convert.ToInt32(Cv_tbx.Text))
{
MessageBox.Show("Algorithm Appears to have Reached Convergence");
backgroundWorker1.CancelAsync();
}
}
else if(temp != Current_Pop[0].Fitness) { Flag = 0; }
// Sleep for 10ms to
System.Threading.Thread.Sleep(10);
// Report the progress now
this.backgroundWorker1.ReportProgress(i);
// Cancel process if it was flagged to be stopped.
if (this.backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.textBox1.Text = Gen_Num.ToString();
this.textBox2.Text = Best_Run.Fitness.ToString();
Update_Config(Best_Run);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pop_btn.Enabled = true;
if (e.Error != null)
{
// An error occurred
MessageBox.Show("Error!");
}
else if (e.Cancelled)
{
MessageBox.Show("Algorithm Stopped");
}
else
{
// The process finished
MessageBox.Show("Algorithm Has Finished!");
}
}
private void Stop_btn_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
}
private void M_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void C_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void T_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void Cs_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void Mg_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
public void New_Generation()
{
Pop_Size = Convert.ToInt32(Pop_tbx.Text);
Mutation_Rate = Convert.ToInt32(M_tbx.Text);
Crossover_Rate = Convert.ToInt32(C_tbx.Text);
Tournament_Pressure = Convert.ToInt32(T_tbx.Text);
Elitism = Convert.ToInt32((1 - (Crossover_Rate / 100)) * Pop_Size);
if (Cs_cbx.Checked) { Cross_site = rnd.Next(0, 15); }//Chooses a random Crossover site
else { Cross_site = Convert.ToInt32(Cs_tbx.Text); }
for (int i = 0; i < Pop_Size; i++)
{
if (i < Elitism)
{
// Current_Pop.Sort(new IndividualComparer());
New_Pop.Add(Current_Pop[i]);
}
if (i >= Elitism && i < Pop_Size)
{
Tuple<Individual, Individual> Parents = Population.Tournament_Selection(Current_Pop, Tournament_Pressure);
Tuple<Individual, Individual> Children = Population.Crossover_and_Mutate(Parents.Item1, Parents.Item2, Cross_site, Mutation_Rate);
//To ensure no Duplicates Check Existence of Children in the New Generation and if it exists, //Generate New Child until it doesnt
while (New_Pop.Contains(Children.Item1) || New_Pop.Contains(Children.Item2))
{ Children = Population.Crossover_and_Mutate(Parents.Item1, Parents.Item2, Cross_site, Mutation_Rate); }
if (!New_Pop.Contains(Children.Item1)) { New_Pop.Add(Children.Item1); }
if (!New_Pop.Contains(Children.Item2)) { New_Pop.Add(Children.Item2); }
}
}
Current_Pop = New_Pop;
Gen_Num++;
}
private void Update_Config(Individual Best)
{
Levels = new List<TextBox> { L0_tbx, L1_tbx, L2_tbx, L3_tbx, L4_tbx, L5_tbx, L6_tbx, L7_tbx, L8_tbx };
Devices = new List<CheckBox> { D0_cbx, D1_cbx, D2_cbx, D3_cbx, D4_cbx };
//Update Texbbox according to index and Coressponding Levels
foreach (var Lvl in Levels.Select((x, i) => new { Value = x, Index = i }))
{
for (int i = 0; i < 5; i++)
{
if (Best.Byte_Sequence[Lvl.Index][0] == 1)
{ Levels[Lvl.Index].Text = "Very low"; }
else if (Best.Byte_Sequence[Lvl.Index][1] == 1)
{ Levels[Lvl.Index].Text = "low"; }
else if (Best.Byte_Sequence[Lvl.Index][2] == 1)
{ Levels[Lvl.Index].Text = "Medium"; }
else if (Best.Byte_Sequence[Lvl.Index][3] == 1)
{ Levels[Lvl.Index].Text = "High"; }
else if (Best.Byte_Sequence[Lvl.Index][4] == 1)
{ Levels[Lvl.Index].Text = "Very High"; }
}
}
//Update Texbbox according to index and Coressponding Levels
foreach (var Dev in Devices.Select((x, i) => new { Value = x, Index = i }))
{
int temp = Dev.Index + 9; // First Device index will be Elemement 9 in Byte Sequence
if (Best.Byte_Sequence[temp][0] == 1) { Devices[Dev.Index].Checked = true; }
}
// Update Timing Level Texbox
if (Best.Byte_Sequence[14][0] == 1) { T0_tbx.Text = "Low"; }
if (Best.Byte_Sequence[14][1] == 1) { T0_tbx.Text = "Medium"; }
if (Best.Byte_Sequence[14][2] == 1) { T0_tbx.Text = "High"; }
}
private void Cv_tbx_KeyPress(object sender, KeyPressEventArgs e)
{
//This ensures that only positive Numbers are allowed
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void Cs_cbx_CheckedChanged(object sender, EventArgs e)
{
//Disable texbox when Random selected
if (Cs_cbx.Checked)
{ Cs_tbx.Enabled = false; }
}
}
public partial class Individual
{
public string String_Sequence { get; set; }
public byte[][] Byte_Sequence { get; set; }
public int Fitness { get; set; }
}
internal partial class IndividualComparer : IComparer<Individual>
{
// To sort individuals by fitest first
public int Compare(Individual x, Individual y)
{
int result = y.Fitness.CompareTo(x.Fitness); // decending order
return result;
}
}
}