-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathForm1.cs
More file actions
655 lines (541 loc) · 23.1 KB
/
Form1.cs
File metadata and controls
655 lines (541 loc) · 23.1 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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Clusterization_algorithms
{
public partial class Form1 : Form
{
private GraphicsController _graphicsController;
private Graphics _graphics;
private Forel _forel;
private K_means _k_means;
private RouteBuilder _routeBuilder;
private EnergyCalculator _energyCalculator;
private Dictionary<Point, int> _allPoints;
private Dictionary<Point, int> _allPointsClustered;
private List<Point> _clusterCenters = new List<Point> { };
private int _forelRadius;
private ClusterizationType _clusterizationType;
#region Default values
private int _defaultPointsCount = 100; // count nodes generated on map
private int _defaultClusterRadius = 100; // round forel cluster radius
private int _defaultSeedCount = 2; // count of centroids in k-means
private int _fppwrDefaultNetRows = 6;
private int _fppwrDefaultNetCols = 6;
#endregion
#region 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
#endregion
public Form1()
{
InitializeComponent();
_graphics = pictBoxArea.CreateGraphics();
_graphicsController = new GraphicsController(_graphics);
_forel = new Forel(_graphicsController);
_k_means = new K_means(_graphicsController);
_routeBuilder = new RouteBuilder();
}
private void btnGenPoints_Click(object sender, EventArgs e)
{
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(_defaultPointsCount, pictBoxArea.Width, pictBoxArea.Height);
}
}
_allPointsClustered = _allPoints;
_routeBuilder.RouteList = new List<Point> { };
_energyCalculator = new EnergyCalculator(_graphicsController, _allPoints);
ClearFields();
}
public void ClearFields()
{
_graphics.Clear(Color.White);
textBoxInfo.Clear();
_graphicsController.DrawPointDictionary(_allPoints);
PrintToTextBoxInfo(_allPoints);
_clusterCenters.Clear();
}
private void btnKMeans_Click(object sender, EventArgs e)
{
_clusterizationType = ClusterizationType.K_means;
Labels_ClearText();
ClearFields();
EnableRouteCalculating();
DisallowSelectClusterAndCalculateEnergy();
SeedGenerator seedGenerator = new SeedGenerator();
seedGenerator.SetPoints(_allPoints);
seedGenerator._seedCount = _defaultSeedCount;
if (Int32.TryParse(textBoxSetSeedsCount.Text, out int seedCount))
{
seedGenerator._seedCount = seedCount;
}
List<Point> seeds = seedGenerator.GetSeeds();
_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 = _defaultClusterRadius;
if (Int32.TryParse(textBoxSetRadius.Text, out int r))
{
_forel.Radius = r;
}
_forelRadius = _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.SetPoints(Calculator.DictionaryToList(_allPoints));
}
else
{
_routeBuilder.SetPoints(_clusterCenters);
}
_routeBuilder.CalculateRouteByConvexHullInsertion();
_graphicsController.DrawRoute(_routeBuilder.RouteList, Color.Yellow);
labelRoute.Text = "Route _length: " + Math.Round(Calculator.CalcRouteLength(_routeBuilder.RouteList), 3);
PrintToTextBoxInfo(_allPointsClustered);
}
private void btnSpiralRoute_Click(object sender, EventArgs e)
{
AllowSelectClusterAndCalculateEnergy();
btnConvexHull.Enabled = false;
if (_clusterCenters.Count > 0)
{
_routeBuilder.SetPoints(_clusterCenters);
}
else
{
_routeBuilder.SetPoints(Calculator.DictionaryToList(_allPoints));
}
_routeBuilder.JarvisMarch(true);
_graphicsController.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));
}
_graphicsController.DrawRoute(route, Color.Blue);
labelRoute.Text = "Route _length: " + Math.Round(Calculator.CalcRouteLength(route), 3);
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 - _forelRadius, center.Y - _forelRadius);
center = new Point(_forelRadius, _forelRadius);
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 / (_forelRadius * 2.0); // proportion between cluster zone size and field size
//>
//< Calculate new points position, radius with zooming
int newRadius = (int)Math.Round(_forelRadius * 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);
_graphicsController.DrawPoint(newCenter, Brushes.Red);
_graphicsController.DrawCircle(newCenter, newRadius, Color.Black, 0);
_graphicsController.DrawPointList(cluster);
_graphicsController.DrawRoute(routeFragment, Color.Orange);
//>
}
private void DrawAllSavedObjects()
{
_graphics.Clear(Color.White);
textBoxInfo.Clear();
PrintToTextBoxInfo(_allPointsClustered);
if (_allPoints.Count != 0)
{
_graphicsController.DrawPointDictionary(_allPoints);
}
if (_clusterCenters.Count != 0)
{
for (int i = 0; i < _clusterCenters.Count; i++)
{
_graphicsController.DrawPoint(_clusterCenters[i], Brushes.Red, 6);
if (_clusterizationType == ClusterizationType.Forel)
{
_graphicsController.DrawCircle(_clusterCenters[i], _forelRadius, Color.Black, 0);
}
}
}
if (_routeBuilder.RouteList.Count != 0)
{
_graphicsController.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;
DataTransmittionType connectionType = DataTransmittionType.DT_to_Center; // default connection
switch (comboBoxConnectionType.SelectedIndex)
{
case 1:
connectionType = DataTransmittionType.DT_to_Route;
break;
case 2:
connectionType = DataTransmittionType.PP_to_Center;
break;
case 3:
connectionType = DataTransmittionType.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 label = new Label
{
Location = new Point(X + i - 10, Y - 15),
Text = $"{number}",
AutoSize = true
};
panel.Controls.Add(label);
}
}
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));
}
_graphicsController.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 = _fppwrDefaultNetCols;
int y_count = _fppwrDefaultNetRows;
_graphicsController.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);
}
_graphicsController.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 = " ";
}
#region User input control
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;
}
#endregion
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");
}
}
}