Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. namespace Dissertatsiya
  6. {
  7. public partial class Form1 : Form
  8. {
  9. int N;
  10. int MaxTime;
  11. int LambdaVxod;
  12. double ErrorMyu;
  13. int Start;
  14. int End;
  15. int[,] arr = new int[100, 100];
  16. int[] Myu = new int[100 * 100];
  17. double[] Lambdas = new double[100 * 100];
  18. List<List<int>> AllPaths = new List<List<int>>();
  19. List<Tuple<int, int>> Neighbours = new List<Tuple<int, int>>();
  20.  
  21. Random rnd = new Random();
  22.  
  23. public static List<T> Splice<T>(List<T> source, int count)
  24. {
  25. var items = source.GetRange(0, count);
  26. source.RemoveRange(0, count);
  27. return items;
  28. }
  29.  
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. private void buttonEnterN_Click(object sender, EventArgs e)
  36. {
  37. if (textBoxValueN.Text == "")
  38. {
  39. MessageBox.Show("Введите значение");
  40. return;
  41. }
  42. if (!int.TryParse(textBoxValueN.Text, out N))
  43. {
  44. MessageBox.Show("Значение должно быть целое число!");
  45. return;
  46. }
  47. textBoxValueN.Enabled = false;
  48. buttonEnterN.Enabled = false;
  49.  
  50. labelMaxTime.Visible = true;
  51. textBoxValueMaxTime.Visible = true;
  52. buttonEnterMaxTime.Visible = true;
  53. }
  54.  
  55. private void buttonEnterMaxTime_Click(object sender, EventArgs e)
  56. {
  57. if (textBoxValueMaxTime.Text == "")
  58. {
  59. MessageBox.Show("Введите значение");
  60. return;
  61. }
  62. if (!int.TryParse(textBoxValueMaxTime.Text, out MaxTime))
  63. {
  64. MessageBox.Show("Значение должно быть целое число!");
  65. return;
  66. }
  67. textBoxValueMaxTime.Enabled = false;
  68. buttonEnterMaxTime.Enabled = false;
  69.  
  70. labelLambdaVxod.Visible = true;
  71. textBoxValueLambdaVxod.Visible = true;
  72. buttonEnterLambdaVxod.Visible = true;
  73. }
  74.  
  75. private void buttonEnterLambdaVxod_Click(object sender, EventArgs e)
  76. {
  77. if (textBoxValueLambdaVxod.Text == "")
  78. {
  79. MessageBox.Show("Введите значение");
  80. return;
  81. }
  82. if (!int.TryParse(textBoxValueLambdaVxod.Text, out LambdaVxod))
  83. {
  84. MessageBox.Show("Значение должно быть целое число!");
  85. return;
  86. }
  87. textBoxValueLambdaVxod.Enabled = false;
  88. buttonEnterLambdaVxod.Enabled = false;
  89.  
  90. labelErrorMyu.Visible = true;
  91. textBoxValueErrorMyu.Visible = true;
  92. buttonEnterErrorMyu.Visible = true;
  93. }
  94.  
  95. private void buttonEnterErrorMyu_Click(object sender, EventArgs e)
  96. {
  97. if (textBoxValueErrorMyu.Text == "")
  98. {
  99. MessageBox.Show("Введите значение");
  100. return;
  101. }
  102. if (!double.TryParse(textBoxValueErrorMyu.Text, out ErrorMyu))
  103. {
  104. MessageBox.Show("Значение должно быть целое число!");
  105. return;
  106. }
  107. textBoxValueErrorMyu.Enabled = false;
  108. buttonEnterErrorMyu.Enabled = false;
  109.  
  110. labelStart.Visible = true;
  111. labelEnd.Visible = true;
  112. textBoxValueStart.Visible = true;
  113. textBoxValueEnd.Visible = true;
  114. buttonEnterPoints.Visible = true;
  115. }
  116.  
  117. private void buttonEnterPoints_Click(object sender, EventArgs e)
  118. {
  119. if (textBoxValueStart.Text == "")
  120. {
  121. MessageBox.Show("Введите значение");
  122. return;
  123. }
  124. if (!int.TryParse(textBoxValueStart.Text, out Start))
  125. {
  126. MessageBox.Show("Значение должно быть целое число!");
  127. return;
  128. }
  129.  
  130. if (textBoxValueEnd.Text == "")
  131. {
  132. MessageBox.Show("Введите значение");
  133. return;
  134. }
  135. if (!int.TryParse(textBoxValueEnd.Text, out End))
  136. {
  137. MessageBox.Show("Значение должно быть целое число!");
  138. return;
  139. }
  140. Start--;
  141. End--;
  142. textBoxValueStart.Enabled = false;
  143. textBoxValueEnd.Enabled = false;
  144. buttonEnterPoints.Enabled = false;
  145.  
  146. MessageBox.Show(N + "" + MaxTime + "" + LambdaVxod + "" + ErrorMyu + "" + Start + "" + End);
  147.  
  148. for (int i = 0; i < N; i++)
  149. {
  150. dataGridViewGraph.Columns.Add("X" + i, (i + 1) + "");
  151. }
  152. for (int i = 0; i < N; i++)
  153. {
  154. dataGridViewGraph.Rows.Add();
  155. dataGridViewGraph.Rows[i].Cells[i].Value = 0;
  156. }
  157. labelGraph.Visible = true;
  158. dataGridViewGraph.Visible = true;
  159. buttonEnterGraph.Visible = true;
  160. }
  161.  
  162. private void buttonEnterGraph_Click(object sender, EventArgs e)
  163. {
  164. for (int i = 0; i < N; i++)
  165. {
  166. for (int j = 0; j < N; j++)
  167. {
  168. if (!int.TryParse(dataGridViewGraph.Rows[j].Cells[i].Value.ToString(), out arr[i, j]))
  169. {
  170. MessageBox.Show("Значении должны быть целое число!");
  171. return;
  172. }
  173. }
  174. }
  175. dataGridViewGraph.Enabled = false;
  176. buttonEnterGraph.Enabled = false;
  177.  
  178. labelMyu.Visible = true;
  179. dataGridViewMyu.Visible = true;
  180. buttonEnterMyu.Visible = true;
  181.  
  182. bool[] isVisited = new bool[N];
  183. List<int> pathList = new List<int>();
  184.  
  185. // add source to path[]
  186. pathList.Add(Start);
  187.  
  188. findAllPathsUtil(Start, End, isVisited, pathList);
  189.  
  190. dataGridViewMyu.Columns.Add("Ребро", "Ребро");
  191. dataGridViewMyu.Columns.Add("Значение", "Значение");
  192.  
  193. for (int i = 0; i < N; i++)
  194. {
  195. for (int j = i; j < N; j++)
  196. {
  197. if (arr[i, j] == 1)
  198. {
  199. dataGridViewMyu.Rows.Add((i + 1) + "-" + (j + 1));
  200. Neighbours.Add(new Tuple<int, int>(i, j));
  201. }
  202. }
  203. }
  204.  
  205. //foreach (List<int> path in AllPaths) {
  206.  
  207. //}
  208. }
  209.  
  210. private void findAllPathsUtil(int u, int d, bool[] isVisited, List<int> localPathList)
  211. {
  212. // Mark the current node
  213. isVisited[u] = true;
  214. if (u.Equals(d))
  215. {
  216. AllPaths.Add(localPathList);
  217. Console.WriteLine(string.Join(" ", localPathList));
  218. // if match found then no need
  219. // to traverse more till depth
  220. isVisited[u] = false;
  221. return;
  222. }
  223. // Recur for all the vertices
  224. // adjacent to current vertex
  225. for (int i = 0; i < N; i++)
  226. {
  227. if (arr[u, i] == 1 && !isVisited[i])
  228. {
  229. // store current node
  230. // in path[]
  231. localPathList.Add(i);
  232. findAllPathsUtil(i, d, isVisited,
  233. localPathList);
  234. // remove current node
  235. // in path[]
  236. localPathList.Remove(i);
  237. }
  238. }
  239. // Mark the current node
  240. isVisited[u] = false;
  241. }
  242.  
  243. private void buttonEnterMyu_Click(object sender, EventArgs e)
  244. {
  245. for (int i = 0; i < N; i++)
  246. {
  247. if (!int.TryParse(dataGridViewGraph.Rows[i].Cells[1].Value.ToString(), out Myu[i]))
  248. {
  249. MessageBox.Show("Значении должны быть целое число!");
  250. return;
  251. }
  252. }
  253.  
  254. dataGridViewMyu.Enabled = false;
  255. buttonEnterMyu.Enabled = false;
  256.  
  257. startCalculating();
  258. }
  259.  
  260. private void startCalculating() {
  261. int pathsCount = AllPaths.Count;
  262. for (int i = 0; i < pathsCount; i++) {
  263. Lambdas[i] = LambdaVxod * 1.0 / pathsCount;
  264. }
  265. int steps = 1;
  266. while (true) {
  267. calculate();
  268. if (steps == 100) {
  269. break;
  270. }
  271. steps++;
  272. }
  273. }
  274.  
  275. private int weightedRand() {
  276. List<int> list = new List<int>();
  277. for (int i = 0; i < Lambdas.Length; i++) {
  278. for (int j = 0; j < Lambdas[i] * 1000; j++) {
  279. list.Add(i);
  280. }
  281. }
  282. int index = rnd.Next(list.Count);
  283. return list[index];
  284. }
  285.  
  286. private void calculate() {
  287. int pathsCount = AllPaths.Count;
  288. List<int> generations = new List<int>();
  289. List<int> choseenPaths = new List<int>();
  290. for (int i = 0; i < LambdaVxod; i++) {
  291. generations.Add(rnd.Next(1, MaxTime));
  292. choseenPaths.Add(weightedRand());
  293. }
  294. List<List<int>> currentItems = new List<List<int>>();
  295. List<List<double>> currentTimes = new List<List<double>>();
  296. List<double> waitingTimes = new List<double>();
  297. for (int i = 0; i < Neighbours.Count; i++) {
  298. currentItems.Add(new List<int>());
  299. currentTimes.Add(new List<double>());
  300. waitingTimes.Add(0);
  301. }
  302. currentItems[Start].Add(choseenPaths[0]);
  303. currentTimes[Start].Add(0);
  304. choseenPaths = Splice<int>(choseenPaths, 1);
  305. generations = Splice<int>(generations, 1);
  306.  
  307. while (true) {
  308. double minTime = double.MaxValue;
  309. for (int i = 0; i < Neighbours.Count; i++) {
  310. //if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
  311. // continue;
  312. for (int j = 0; j < currentTimes[i].Count; i++) {
  313. double estemated = 1000.0 / Myu[i] - currentTimes[i][j];
  314. if (minTime > estemated)
  315. minTime = estemated;
  316. }
  317. }
  318.  
  319. if (generations.Count > 0 && minTime > generations[0])
  320. {
  321. minTime = generations[0];
  322. currentItems[Start].Add(choseenPaths[0]);
  323. currentTimes[Start].Add(0);
  324. choseenPaths = Splice<int>(choseenPaths, 1);
  325. generations = Splice<int>(generations, 1);
  326.  
  327. for (var i = 0; i < Neighbours.Count; i++)
  328. {
  329. if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
  330. continue;
  331. waitingTimes[i] += currentItems[i].Count * minTime;
  332. if (currentItems[i].Count > 0)
  333. currentTimes[i][0] += minTime;
  334. }
  335. continue;
  336. }
  337.  
  338. var ok = true;
  339. for (var i = 0; i < Neighbours.Count && ok; i++)
  340. {
  341. //if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
  342. // continue;
  343. for (var j = 0; j < currentTimes[i].Count && ok; j++)
  344. {
  345. var estemated = 1000.0 / Myu[i] - currentTimes[i][j];
  346. if (Math.Abs(minTime - estemated) < 0.000001)
  347. {
  348. ok = false;
  349. for (var i1 = 0; i1 < Neighbours.Count; i1++)
  350. {
  351. if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
  352. continue;
  353. waitingTimes[i] += currentItems[i].Count * minTime;
  354. if (currentItems[i].Count > 0)
  355. currentTimes[i][0] += minTime;
  356. }
  357. var path1 = currentItems[i][j];
  358. var next_item = -1;
  359. for (var i1 = 1; i1 < AllPaths[path1].Count; i1++)
  360. {
  361. if ((AllPaths[path1][i1 - 1] == Neighbours[i].Item1 && AllPaths[path1][i1] == Neighbours[i].Item2)
  362. || (AllPaths[path1][i1 - 1] == Neighbours[i].Item2 && AllPaths[path1][i1] == Neighbours[i].Item1))
  363. for (var i2 = 0; i2 < Neighbours.Count; i2++)
  364. if ((AllPaths[path1][i1] == Neighbours[i2].Item1 && AllPaths[path1][i1 + 1] == Neighbours[i2].Item2)
  365. || (AllPaths[path1][i1] == Neighbours[i2].Item2 && AllPaths[path1][i1 + 1] == Neighbours[i2].Item1))
  366. next_item = i2;
  367. }
  368. currentItems[next_item].Add(currentItems[i][0]);
  369. currentTimes[next_item].Add(0);
  370. currentItems[i]
  371. = Splice<int>(currentItems[i], 1);
  372. currentTimes[i] = Splice<double>(currentTimes[i], 1);
  373. }
  374. }
  375. }
  376. if (ok)
  377. break;
  378. }
  379. }
  380. }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement