-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForm1.cs
606 lines (490 loc) · 22.1 KB
/
Form1.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Clusterization_algorithms
{
public partial class Form1 : Form
{
Graphic graphic;
Graphics graphics;
Forel forel;
K_means k_means;
RouteBuilder routeBuilder;
EnergyCalculator energyCalculator;
Dictionary<Point, int> allPoints;
Dictionary<Point, int> allPointsClustered;
List<Point> clusterCenters = new List<Point> { };
int radius; // in forel
ClusterizationType clusterizationType;
// *** DEFAULT VALUES ***
int def_points_count = 100; // count nodes generated on map
int def_cluster_radius = 100; // round forel cluster radius
int def_seed_count = 2; // count of centroids in k-means
// FPPWR net draw default values
int def_net_rows = 6;
int def_net_cols = 6;
// counters
int loop_count = 0; // times was data collected from map by station
double map_usedE = 0; // used energy in current loop
double map_last_usedE = 0; // used energy in last loop
public Form1()
{
InitializeComponent();
//pictBoxArea.Size = new System.Drawing.Size(600, 600); //800 x 738
graphics = pictBoxArea.CreateGraphics();
graphic = new Graphic(graphics);
forel = new Forel(graphic);
k_means = new K_means(graphic);
routeBuilder = new RouteBuilder();
}
private void btnGenPoints_Click(object sender, EventArgs e)
{
//Console.WriteLine("PictBoxArea: W(X)=" + pictBoxArea.Width + " H(Y)=" + pictBoxArea.Height);
DisableAll();
EnableClusterization();
Labels_ClearText();
buttonReloadMap.Enabled = true;
buttonEnergyModel.Enabled = true;
if (checkBoxAllowGeneratePoints.Checked == true)
{
if (Int32.TryParse(textBoxSetPointsCount.Text, out int pointsCount))
allPoints = Calculator.generatePoints(pointsCount, pictBoxArea.Width, pictBoxArea.Height);
else
allPoints = Calculator.generatePoints(def_points_count, pictBoxArea.Width, pictBoxArea.Height);
}
allPointsClustered = allPoints;
routeBuilder.RouteList = new List<Point> { };
energyCalculator = new EnergyCalculator(graphic, allPoints);
ClearFields();
}
public void ClearFields() {
graphics.Clear(Color.White);
textBoxInfo.Clear();
graphic.DrawPointDictionary(allPoints);
PrintToTextBoxInfo(allPoints);
clusterCenters.Clear();
}
private void btnKMeans_Click(object sender, EventArgs e)
{
clusterizationType = ClusterizationType.K_means;
Labels_ClearText();
ClearFields();
EnableRouteCalculating();
DisallowSelectClusterAndCalculateEnergy();
SeedGenerator seedG = new SeedGenerator(); //
seedG.SetPoints(allPoints);
seedG.seedCount = def_seed_count; // by default
if (Int32.TryParse(textBoxSetSeedsCount.Text, out int seedCount))
seedG.seedCount = seedCount;
List<Point> seeds = seedG.GetSeeds(); // Calculate and get seeds
k_means.setPoints(allPoints);
k_means.Seeds = seeds;
k_means.startK_means();
clusterCenters = k_means.Seeds;
PrintToTextBoxInfo(k_means.getPoints());
allPointsClustered = k_means.getPoints();
}
private void btnForel_Click(object sender, EventArgs e)
{
clusterizationType = ClusterizationType.Forel;
Labels_ClearText();
ClearFields();
EnableRouteCalculating();
DisallowSelectClusterAndCalculateEnergy();
forel.setPoints(allPoints);
forel.Radius = def_cluster_radius;
if (Int32.TryParse(textBoxSetRadius.Text, out int r))
forel.Radius = r;
radius = forel.Radius;
forel.startForel();
clusterCenters = forel.listOfClastersCenter;
PrintToTextBoxInfo(forel.getPoints());
allPointsClustered = forel.getPoints();
}
private void btnClustersRoute_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
if (clusterCenters.Count == 0)
routeBuilder.All_points(Calculator.DictionaryToList(allPoints));
else
routeBuilder.All_points(clusterCenters);
routeBuilder.CalculateRouteByConvexHullInsertion();
graphic.DrawRoute(routeBuilder.RouteList, Color.Yellow);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(routeBuilder.RouteList), 3);
//allPointsClustered = routeBuilder.SortClustersByRoute(allPointsClustered);
PrintToTextBoxInfo(allPointsClustered);
}
private void btnSpiralRoute_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
btnConvexHull.Enabled = false;
if (clusterCenters.Count > 0)
routeBuilder.All_points(clusterCenters);
else
routeBuilder.All_points(Calculator.DictionaryToList(allPoints));
routeBuilder.JarvisMarch(true);
graphic.DrawRoute(routeBuilder.Convex_hull, Color.Aqua);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(routeBuilder.Convex_hull), 3);
}
private void btnGenPoints_MouseMove(object sender, MouseEventArgs e)
{
btnGenPoints.BackColor = Color.LightGreen;
}
private void btnGenPoints_MouseLeave(object sender, EventArgs e)
{
btnGenPoints.BackColor = Color.DarkGray;
}
private void btnBruteForce_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
List<Point> route = new List<Point> { };
if (clusterCenters.Count > 0)
route = routeBuilder.CalculateRouteBruteForce(clusterCenters);
else
route = routeBuilder.CalculateRouteBruteForce(Calculator.DictionaryToList(allPoints));
graphic.DrawRoute(route, Color.Blue);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(route), 3);
//allPointsClustered = routeBuilder.SortClustersByRoute(allPointsClustered);
PrintToTextBoxInfo(allPointsClustered);
}
private void btn1ClusterOn_Click(object sender, EventArgs e)
{
DisableAll();
btnDeselectCluster.Enabled = true;
graphics.Clear(Color.White);
int clusterNum = 1;
if (Int32.TryParse(textBoxSetClusterNum.Text, out int num)) // get cluster number
clusterNum = num;
List<Point> cluster = Calculator.getClusterList(clusterNum, allPointsClustered);
PrintToTextBoxInfo(Calculator.getClusterDictionary(clusterNum, allPointsClustered));
Point center = Calculator.findCentroid(cluster); // find cluster center
List<Point> routeFragment = Calculator.getRouteFragment(center, routeBuilder.RouteList); // find part route go throught cluster
//< Move all points to the start coordinate
Point vector = new Point(center.X - radius, center.Y - radius);
center = new Point(radius, radius);
Calculator.MovePointList(cluster, vector);
Calculator.MovePointList(routeFragment, vector);
//>
//< Calculate zoom coefficient.
int size = pictBoxArea.Height;
if (pictBoxArea.Width < size)
size = pictBoxArea.Width;
double zoom = size / (radius * 2.0); // proportion between cluster zone size and field size
//>
//< Calculate new points position, radius with zooming
int newRadius = (int)Math.Round(radius * zoom);
Point newCenter = new Point((int)Math.Round(center.X * zoom), (int)Math.Round(center.Y * zoom));
Calculator.ZoomPointList(cluster, zoom);
Calculator.ZoomPointList(routeFragment, zoom);
graphic.DrawPoint(newCenter, Brushes.Red);
graphic.DrawCircle(newCenter, newRadius, Color.Black, 0);
graphic.DrawPointList(cluster);
graphic.DrawRoute(routeFragment, Color.Orange);
//>
}
private void DrawAllSavedObjects() {
graphics.Clear(Color.White);
textBoxInfo.Clear();
PrintToTextBoxInfo(allPointsClustered);
if (allPoints.Count != 0)
graphic.DrawPointDictionary(allPoints);
if (clusterCenters.Count != 0) {
for (int i = 0; i < clusterCenters.Count; i++) {
graphic.DrawPoint(clusterCenters[i], Brushes.Red, 6);
if(clusterizationType == ClusterizationType.Forel)
graphic.DrawCircle(clusterCenters[i], radius, Color.Black, 0);
}
}
if (routeBuilder.RouteList.Count != 0)
{
graphic.DrawRoute(routeBuilder.RouteList, Color.Orange, 0);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(routeBuilder.RouteList), 3);
}
}
private void btnCalcEnergy_Click(object sender, EventArgs e) {
DrawAllSavedObjects();
buttonStatistic.Enabled = true;
loop_count++;
map_last_usedE = map_usedE;
ConnectionType connectionType = ConnectionType.DT_to_Center; // default connection
switch (comboBoxConnectionType.SelectedIndex) {
case 1:
connectionType = ConnectionType.DT_to_Route;
break;
case 2:
connectionType = ConnectionType.PP_to_Center;
break;
case 3:
connectionType = ConnectionType.PP_to_Route;
break;
}
energyCalculator.CalculteAllNodesEnergy(connectionType, allPointsClustered, routeBuilder.RouteList, ReadStationHeighth());
PrintToTextBoxInfo(allPointsClustered);
labelCharge.Visible = true;
labelUsedEnergy.Visible = true;
map_usedE = energyCalculator.GetMapUsedEnergy();
labelCharge.Text = "Total charge: " + String.Format("{0:0.000}", energyCalculator.GetMapCurrentChargeInPercents()) + "%";
labelUsedEnergy.Text = "Used energy: " + String.Format("{0:0.000}", map_usedE) + " (J)";
if (labelInfo.Enabled) ReloadStatistic();
}
private int ReadStationHeighth() {
int station_height = 0;
if (int.TryParse(textBoxSetHeight.Text, out int height))
station_height = height;
return station_height;
}
private void PrintToTextBoxInfo(Dictionary<Point, int> dictionary) {
textBoxInfo.Text = Calculator.printNodesAndCharge(dictionary, energyCalculator.GetNodesChargeDictionary());
}
private void Form1_Shown(object sender, EventArgs e)
{
DrawScale();
}
private void DrawScale() // draw scale in panel related to picturebox
{
int step = 25;
// Set panel size to all window
panel.Size = new Size(this.Size.Width, this.Size.Height);
panel.Location = new Point(0, 0);
Graphics graphicsVector = panel.CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
Point zeroPosition = new Point(pictBoxArea.Location.X - 25, pictBoxArea.Location.Y - 25);
int X = zeroPosition.X;
int Y = zeroPosition.Y;
graphicsVector.DrawLine(pen, new Point(X, Y), new Point(pictBoxArea.Location.X + pictBoxArea.Width, Y));
graphicsVector.DrawLine(pen, new Point(X, Y), new Point(X, pictBoxArea.Location.Y + pictBoxArea.Height));
int distanceX = pictBoxArea.Width + 10;
int number = 0;
for (int i = step; i <= distanceX;)
{
graphicsVector.DrawLine(pen, new Point(X + i, Y - 3), new Point(X + i, Y + 3));
if (i % (2 * step) != 0)
{
if (number == 0)
{
Label lb = new Label
{
Location = new Point(X + i - 6, Y - 15),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(lb);
}
else
{
Label lb = new Label
{
Location = new Point(X + i - 10, Y - 15),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(lb);
}
}
i += 25;
number += 25;
}
number = 0;
int distanceY = pictBoxArea.Height + 25;
for (int i = 25; i < distanceY;)
{
graphicsVector.DrawLine(pen, new Point(X - 3, Y + i), new Point(X + 3, Y + i));
if (i % 50 != 0)
{
if (number == 0)
{
Label lb = new Label
{
Location = new Point(X - 15, Y + i - 7),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(lb);
}
else if (number == 50)
{
Label lb = new Label
{
Location = new Point(X - 23, Y + i - 7),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(lb);
}
else
{
Label lb = new Label
{
Location = new Point(X - 27, Y + i - 7),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(lb);
}
}
i += 25;
number += 25;
}
}
private void btnNearestNeighbour_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
btnConvexHull.Enabled = true;
List<Point> route = new List<Point> { };
if (clusterCenters.Count > 0)
route = routeBuilder.CalculateRouteByNearestNeighbour(clusterCenters);
else
route = routeBuilder.CalculateRouteByNearestNeighbour(Calculator.DictionaryToList(allPoints));
graphic.DrawRoute(route, Color.Salmon);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(route), 3);
PrintToTextBoxInfo(allPointsClustered);
}
private void btnFPPWR_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
btnConvexHull.Enabled = true;
List<Point> route = new List<Point> { };
//< draw net on pictBoxArea
// divide field on cells; cells count:
int x_count = def_net_cols;
int y_count = def_net_rows;
graphic.DrawNet(x_count, y_count, pictBoxArea.Width, pictBoxArea.Height);
//>
if (clusterCenters.Count > 0)
route = routeBuilder.CalculateRouteByFPPWR(clusterCenters, x_count, y_count, pictBoxArea.Width, pictBoxArea.Height);
else
route = routeBuilder.CalculateRouteByFPPWR(Calculator.DictionaryToList(allPoints), x_count, y_count, pictBoxArea.Width, pictBoxArea.Height);
graphic.DrawRoute(route, Color.Purple);
labelRoute.Text = "Route length: " + Math.Round(Calculator.calcRouteLength(route), 3);
PrintToTextBoxInfo(allPointsClustered);
}
private void btnHelp_Click(object sender, EventArgs e)
{
labelInfo.Text =
"black points - nodes\n" +
"big red points - mass centers of clusters\n" +
"red points - nodes with 0 % charge\n" +
"black circles - clusters\n" +
"gray circles - clusters iterations\n" +
"green line - transmittion model for close distance (< 87, 7m)\n" +
"red line - transmittion model for long distance (> 87, 7m) (with using amplifier)\n" +
"route colors -different for each route-building algorithm, allow to see difference";
CheckLabelInfo();
}
private void Labels_ClearText() {
labelRoute.Text = "";
labelCharge.Text = " ";
labelUsedEnergy.Text = " ";
}
// ***** Protect from user errors *****
private void EnableClusterization() {
btnKMeans.Enabled = true;
btnForel.Enabled = true;
}
private void DisableClusterization()
{
btnKMeans.Enabled = false;
btnForel.Enabled = false;
}
private void EnableRouteCalculating()
{
btnNearestNeighbour.Enabled = true;
btnConvexHull.Enabled = true;
btnBruteForce.Enabled = true;
btnSpiralRoute.Enabled = true;
btnFPPWR.Enabled = true;
}
private void DisableRouteCalculating()
{
btnNearestNeighbour.Enabled = false;
btnConvexHull.Enabled = false;
btnBruteForce.Enabled = false;
btnSpiralRoute.Enabled = false;
btnFPPWR.Enabled = false;
}
private void DisableAll() {
DisableClusterization();
DisableRouteCalculating();
btnSelectCluster.Enabled = false;
btnCalcEnergy.Enabled = false;
buttonStatistic.Enabled = false;
}
private void AllowSelectClusterAndCalculateEnergy() {
btnSelectCluster.Enabled = true;
btnCalcEnergy.Enabled = true;
buttonStatistic.Enabled = false;
}
private void DisallowSelectClusterAndCalculateEnergy()
{
btnSelectCluster.Enabled = false;
btnCalcEnergy.Enabled = false;
buttonStatistic.Enabled = false;
loop_count = 0;
map_usedE = 0;
map_last_usedE = 0;
}
// *******************************
private void btnDeselectCluster_Click(object sender, EventArgs e)
{
DrawAllSavedObjects();
btnDeselectCluster.Enabled = false;
btnSelectCluster.Enabled = true;
btnCalcEnergy.Enabled = true;
EnableClusterization();
EnableRouteCalculating();
}
private void buttonStatistic_Click(object sender, EventArgs e)
{
ReloadStatistic();
CheckLabelInfo();
}
private void ReloadStatistic() {
double map_capacity = energyCalculator.GetNodesCapacity(allPoints.Count);
double map_charge = energyCalculator.GetMapCurrentCharge();
double map_charge_percents = energyCalculator.GetMapCurrentChargeInPercents();
int nodes_available = getCountOfAvailableNodes();
labelInfo.Text =
"Total nodes charge: " + map_charge_percents + "% (" + map_charge + " / " + map_capacity + " J)\n" +
"Available nodes: " + nodes_available + " / " + allPointsClustered.Count + "\n" +
"Clusters count: " + forel.clusterNum + "\n" +
"Loops: " + loop_count + "\n" +
"Average E, per loop: " + String.Format("{0:0.000}", (map_capacity - map_charge) / loop_count) + " J\n" +
"Last loop used E: " + String.Format("{0:0.000}", map_usedE - map_last_usedE) + " J";
}
private void buttonReloadMap_Click(object sender, EventArgs e)
{
DrawAllSavedObjects();
}
private void CheckLabelInfo() {
if (labelInfo.Visible)
labelInfo.Visible = false;
else
labelInfo.Visible = true;
}
private int getCountOfAvailableNodes() {
int count = 0;
var nodes_charge = energyCalculator.GetNodesChargeDictionary();
foreach (var node in nodes_charge)
if (node.Value != 0) count++;
return count;
}
private void buttonEnergyModel_Click(object sender, EventArgs e)
{
CheckLabelInfo();
labelInfo.Text =
"Field size: " + pictBoxArea.Width + " x " + pictBoxArea.Height + " m" +
"\nAmplifier energy:" +
"\n Free space model " + "(<= " + energyCalculator.d0 + " m): " + energyCalculator.E_fs + " nJ" +
"\n Multipath fading model " + "(> " + energyCalculator.d0 + " m): " + energyCalculator.E_mp * 1000000 + " mJ" +
"\nInitial node energy: " + energyCalculator.node_E / 1000000000.000 + " J" +
"\nEnergy for signal transmittion / recieve: " + energyCalculator.E_elec + " nJ/bit" +
"\nPackage size (20 - 65535): " + energyCalculator.package + " bit" +
"\nThreshold for swapping amplification models: " + energyCalculator.d0 + " m";
}
private void btnLink_Click(object sender, EventArgs e)
{
//System.Diagnostics.Process.Start("http://www.facebook.com");
}
}
}