-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
289 lines (218 loc) · 8.89 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GeneticSalesman
{
public partial class Form1 : Form
{
private static TravelingSalesman salesman = null;
private double generationsPerClick = -1.0;
public Form1()
{
InitializeComponent();
}
private void DrawCities(List<(float x, float y)> coordinates, PaintEventArgs e, Brush brush = null)
{
if (brush is null)
brush = Brushes.Black;
foreach (var coordinate in coordinates)
DrawCircleAtPoint(e, coordinate.x, coordinate.y, 10f, brush);
}
private void DrawPath(List<(float x, float y)> coordinates, PaintEventArgs e, Pen pen = null)
{
(float x, float y) startPoint;
(float x, float y) endPoint;
startPoint = coordinates[0];
// Draw the main path
for( int index = 1; index < coordinates.Count; ++index)
{
endPoint = coordinates[index];
DrawLineBetweenPoints(e, startPoint.x, startPoint.y, endPoint.x, endPoint.y, pen);
startPoint = endPoint;
}
// Draw the final line
endPoint = coordinates[0];
DrawLineBetweenPoints(e, startPoint.x, startPoint.y, endPoint.x, endPoint.y, pen);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
//e.Graphics.DrawLine(pen, 20, 10, 300, 100);
}
(float x, float y) GetPixelFromCoordinate( float x, float y )
{
float padding_x = 100f; // left and right margin, in pixels
float padding_y = 100f; // top and bottom margin, in pixels
float max_x = 5f; // x coordinate max value
float max_y = 5f; // y coordinate max value
float pixels_per_unit_x = (pictureBox1.Width - padding_x * 2f) / max_x;
float pixels_per_unit_y = (pictureBox1.Height - padding_y * 2f) / max_y;
float x_pixel = x * pixels_per_unit_x + padding_x;
float y_pixel = pictureBox1.Height - (y * pixels_per_unit_y + padding_y);
return (x_pixel, y_pixel);
}
public void DrawLineBetweenPoints(PaintEventArgs e, float x1, float y1, float x2, float y2, Pen pen = null)
{
if (pen is null)
{
pen = Pens.Black;
//pen.Width = 5f;
}
var start = GetPixelFromCoordinate(x1, y1);
var end = GetPixelFromCoordinate(x2, y2);
float delta_x = end.x - start.x;
float delta_y = end.y - start.y;
e.Graphics.DrawLine(pen, start.x, start.y, end.x, end.y);
}
public void DrawCircleAtPoint(PaintEventArgs e, float x, float y, float radius = 10f, Brush brush = null)
{
if (brush is null)
brush = Brushes.Coral;
var median = GetPixelFromCoordinate(x, y);
float x_pixel = median.x;
float y_pixel = median.y;
e.Graphics.FillEllipse(brush, x_pixel - radius, y_pixel - radius, radius * 2f, radius * 2f);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// Draw grid
for( int x = 0; x <= 5; ++x)
{
for (int y = 0; y <= 5; ++y)
DrawCircleAtPoint(e, x, y, 3, Brushes.LightGray);
}
// Draw map and paths
if (!(salesman is null))
{
if( checkBoxShowCities.CheckState == CheckState.Checked )
DrawCities(salesman.Map.Coordinates, e);
if (checkBoxShowBestPath.CheckState == CheckState.Checked)
DrawPath(salesman.GetBestPathCoordinates(), e, new Pen(Brushes.LimeGreen, 5f));
if( checkBoxShowWorst.CheckState == CheckState.Checked)
DrawPath(salesman.GetWorstPathCoordinates(), e, new Pen(Brushes.Salmon, 2f));
}
}
private void buttonGo_Click(object sender, EventArgs e)
{
}
private void buttonEvolve_Click(object sender, EventArgs e)
{
if (salesman is null)
return;
// Update ETA
double generationsToDo;
if (double.TryParse(textBoxGenerationsPerClick.Text, out generationsToDo) && generationsPerClick > 0)
{
double seconds = generationsToDo / generationsPerClick;
TimeSpan span = TimeSpan.FromSeconds(seconds);
toolStripStatusLabelEta.Text = $"ETA: {DateTime.Now + span}";
//labelEvolveTime.Text = $"Estimated time to evolve: {span}";
}
else
{
toolStripStatusLabelEta.Text = $"ETA: ???";
//labelEvolveTime.Text = $"Estimated time to evolve: ???";
}
//salesman.MutationRate = int.Parse(textBoxMutationRate.Text);
toolStripExecutionStatus.Text = "RUNNING";
toolStripExecutionStatus.BackColor = Color.Red;
statusStrip1.Refresh();
Stopwatch watch = Stopwatch.StartNew();
salesman.Evolve(int.Parse(textBoxGenerationsPerClick.Text),
addRandomParents: checkBoxRandomParents.Checked,
addMutantClones: checkBoxClones.Checked,
randomizeMutationRate: checkBoxRandomMutation.Checked
);
watch.Stop();
toolStripExecutionStatus.Text = "READY";
toolStripExecutionStatus.BackColor = Color.Green;
statusStrip1.Refresh();
pictureBox1.Refresh();
textBoxMain.Text = salesman.ToString();
generationsPerClick = double.Parse(textBoxGenerationsPerClick.Text) / (watch.Elapsed.TotalSeconds);
UpdateTimeToEvolveLabel();
toolStripStatusLabel1.Text = $"Last evolution: {int.Parse(textBoxGenerationsPerClick.Text):n0} generations in {watch.Elapsed.TotalSeconds} seconds. {generationsPerClick:n0} generations per second";
toolStripStatusLabelEta.Text = "";
}
private void UpdateTimeToEvolveLabel()
{
double generationsToDo;
if( double.TryParse(textBoxGenerationsPerClick.Text, out generationsToDo) && generationsPerClick > 0 )
{
double seconds = generationsToDo / generationsPerClick;
TimeSpan span = TimeSpan.FromSeconds(seconds);
labelEvolveTime.Text = $"Estimated time to evolve: {span}";
}
else
{
labelEvolveTime.Text = $"Estimated time to evolve: ???";
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void checkBoxShowWorst_CheckStateChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void checkBoxShowWorst_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void checkBoxShowBestPath_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void checkBoxShowCities_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void buttonNewMap_Click(object sender, EventArgs e)
{
generationsPerClick = -1.0;
UpdateTimeToEvolveLabel();
int pathCount = int.Parse(textBoxPath.Text);
int cityCount = int.Parse(textBoxCities.Text);
salesman = new TravelingSalesman(cityCount, pathCount);
//salesman.Map.Coordinates = new List<(float x, float y)>()
//{
// (0f,0f),
// (0f,3f),
// (4f,3f),
// (4f,0f),
//
//};
pictureBox1.Refresh();
textBoxMain.Text = salesman.ToString();
}
private void textBoxGenerationsPerClick_TextChanged(object sender, EventArgs e)
{
UpdateTimeToEvolveLabel();
}
}
}