Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace Dissertatsiya
- {
- public partial class Form1 : Form
- {
- int N;
- int MaxTime;
- int LambdaVxod;
- double ErrorMyu;
- int Start;
- int End;
- int[,] arr = new int[100, 100];
- int[] Myu = new int[100 * 100];
- double[] Lambdas = new double[100 * 100];
- List<List<int>> AllPaths = new List<List<int>>();
- List<Tuple<int, int>> Neighbours = new List<Tuple<int, int>>();
- Random rnd = new Random();
- public static List<T> Splice<T>(List<T> source, int count)
- {
- var items = source.GetRange(0, count);
- source.RemoveRange(0, count);
- return items;
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void buttonEnterN_Click(object sender, EventArgs e)
- {
- if (textBoxValueN.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!int.TryParse(textBoxValueN.Text, out N))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- textBoxValueN.Enabled = false;
- buttonEnterN.Enabled = false;
- labelMaxTime.Visible = true;
- textBoxValueMaxTime.Visible = true;
- buttonEnterMaxTime.Visible = true;
- }
- private void buttonEnterMaxTime_Click(object sender, EventArgs e)
- {
- if (textBoxValueMaxTime.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!int.TryParse(textBoxValueMaxTime.Text, out MaxTime))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- textBoxValueMaxTime.Enabled = false;
- buttonEnterMaxTime.Enabled = false;
- labelLambdaVxod.Visible = true;
- textBoxValueLambdaVxod.Visible = true;
- buttonEnterLambdaVxod.Visible = true;
- }
- private void buttonEnterLambdaVxod_Click(object sender, EventArgs e)
- {
- if (textBoxValueLambdaVxod.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!int.TryParse(textBoxValueLambdaVxod.Text, out LambdaVxod))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- textBoxValueLambdaVxod.Enabled = false;
- buttonEnterLambdaVxod.Enabled = false;
- labelErrorMyu.Visible = true;
- textBoxValueErrorMyu.Visible = true;
- buttonEnterErrorMyu.Visible = true;
- }
- private void buttonEnterErrorMyu_Click(object sender, EventArgs e)
- {
- if (textBoxValueErrorMyu.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!double.TryParse(textBoxValueErrorMyu.Text, out ErrorMyu))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- textBoxValueErrorMyu.Enabled = false;
- buttonEnterErrorMyu.Enabled = false;
- labelStart.Visible = true;
- labelEnd.Visible = true;
- textBoxValueStart.Visible = true;
- textBoxValueEnd.Visible = true;
- buttonEnterPoints.Visible = true;
- }
- private void buttonEnterPoints_Click(object sender, EventArgs e)
- {
- if (textBoxValueStart.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!int.TryParse(textBoxValueStart.Text, out Start))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- if (textBoxValueEnd.Text == "")
- {
- MessageBox.Show("Введите значение");
- return;
- }
- if (!int.TryParse(textBoxValueEnd.Text, out End))
- {
- MessageBox.Show("Значение должно быть целое число!");
- return;
- }
- Start--;
- End--;
- textBoxValueStart.Enabled = false;
- textBoxValueEnd.Enabled = false;
- buttonEnterPoints.Enabled = false;
- MessageBox.Show(N + "" + MaxTime + "" + LambdaVxod + "" + ErrorMyu + "" + Start + "" + End);
- for (int i = 0; i < N; i++)
- {
- dataGridViewGraph.Columns.Add("X" + i, (i + 1) + "");
- }
- for (int i = 0; i < N; i++)
- {
- dataGridViewGraph.Rows.Add();
- dataGridViewGraph.Rows[i].Cells[i].Value = 0;
- }
- labelGraph.Visible = true;
- dataGridViewGraph.Visible = true;
- buttonEnterGraph.Visible = true;
- }
- private void buttonEnterGraph_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- if (!int.TryParse(dataGridViewGraph.Rows[j].Cells[i].Value.ToString(), out arr[i, j]))
- {
- MessageBox.Show("Значении должны быть целое число!");
- return;
- }
- }
- }
- dataGridViewGraph.Enabled = false;
- buttonEnterGraph.Enabled = false;
- labelMyu.Visible = true;
- dataGridViewMyu.Visible = true;
- buttonEnterMyu.Visible = true;
- bool[] isVisited = new bool[N];
- List<int> pathList = new List<int>();
- // add source to path[]
- pathList.Add(Start);
- findAllPathsUtil(Start, End, isVisited, pathList);
- dataGridViewMyu.Columns.Add("Ребро", "Ребро");
- dataGridViewMyu.Columns.Add("Значение", "Значение");
- for (int i = 0; i < N; i++)
- {
- for (int j = i; j < N; j++)
- {
- if (arr[i, j] == 1)
- {
- dataGridViewMyu.Rows.Add((i + 1) + "-" + (j + 1));
- Neighbours.Add(new Tuple<int, int>(i, j));
- }
- }
- }
- //foreach (List<int> path in AllPaths) {
- //}
- }
- private void findAllPathsUtil(int u, int d, bool[] isVisited, List<int> localPathList)
- {
- // Mark the current node
- isVisited[u] = true;
- if (u.Equals(d))
- {
- AllPaths.Add(localPathList);
- Console.WriteLine(string.Join(" ", localPathList));
- // if match found then no need
- // to traverse more till depth
- isVisited[u] = false;
- return;
- }
- // Recur for all the vertices
- // adjacent to current vertex
- for (int i = 0; i < N; i++)
- {
- if (arr[u, i] == 1 && !isVisited[i])
- {
- // store current node
- // in path[]
- localPathList.Add(i);
- findAllPathsUtil(i, d, isVisited,
- localPathList);
- // remove current node
- // in path[]
- localPathList.Remove(i);
- }
- }
- // Mark the current node
- isVisited[u] = false;
- }
- private void buttonEnterMyu_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < N; i++)
- {
- if (!int.TryParse(dataGridViewGraph.Rows[i].Cells[1].Value.ToString(), out Myu[i]))
- {
- MessageBox.Show("Значении должны быть целое число!");
- return;
- }
- }
- dataGridViewMyu.Enabled = false;
- buttonEnterMyu.Enabled = false;
- startCalculating();
- }
- private void startCalculating() {
- int pathsCount = AllPaths.Count;
- for (int i = 0; i < pathsCount; i++) {
- Lambdas[i] = LambdaVxod * 1.0 / pathsCount;
- }
- int steps = 1;
- while (true) {
- calculate();
- if (steps == 100) {
- break;
- }
- steps++;
- }
- }
- private int weightedRand() {
- List<int> list = new List<int>();
- for (int i = 0; i < Lambdas.Length; i++) {
- for (int j = 0; j < Lambdas[i] * 1000; j++) {
- list.Add(i);
- }
- }
- int index = rnd.Next(list.Count);
- return list[index];
- }
- private void calculate() {
- int pathsCount = AllPaths.Count;
- List<int> generations = new List<int>();
- List<int> choseenPaths = new List<int>();
- for (int i = 0; i < LambdaVxod; i++) {
- generations.Add(rnd.Next(1, MaxTime));
- choseenPaths.Add(weightedRand());
- }
- List<List<int>> currentItems = new List<List<int>>();
- List<List<double>> currentTimes = new List<List<double>>();
- List<double> waitingTimes = new List<double>();
- for (int i = 0; i < Neighbours.Count; i++) {
- currentItems.Add(new List<int>());
- currentTimes.Add(new List<double>());
- waitingTimes.Add(0);
- }
- currentItems[Start].Add(choseenPaths[0]);
- currentTimes[Start].Add(0);
- choseenPaths = Splice<int>(choseenPaths, 1);
- generations = Splice<int>(generations, 1);
- while (true) {
- double minTime = double.MaxValue;
- for (int i = 0; i < Neighbours.Count; i++) {
- //if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
- // continue;
- for (int j = 0; j < currentTimes[i].Count; i++) {
- double estemated = 1000.0 / Myu[i] - currentTimes[i][j];
- if (minTime > estemated)
- minTime = estemated;
- }
- }
- if (generations.Count > 0 && minTime > generations[0])
- {
- minTime = generations[0];
- currentItems[Start].Add(choseenPaths[0]);
- currentTimes[Start].Add(0);
- choseenPaths = Splice<int>(choseenPaths, 1);
- generations = Splice<int>(generations, 1);
- for (var i = 0; i < Neighbours.Count; i++)
- {
- if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
- continue;
- waitingTimes[i] += currentItems[i].Count * minTime;
- if (currentItems[i].Count > 0)
- currentTimes[i][0] += minTime;
- }
- continue;
- }
- var ok = true;
- for (var i = 0; i < Neighbours.Count && ok; i++)
- {
- //if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
- // continue;
- for (var j = 0; j < currentTimes[i].Count && ok; j++)
- {
- var estemated = 1000.0 / Myu[i] - currentTimes[i][j];
- if (Math.Abs(minTime - estemated) < 0.000001)
- {
- ok = false;
- for (var i1 = 0; i1 < Neighbours.Count; i1++)
- {
- if (Neighbours[i].Item1 == End || Neighbours[i].Item2 == End)
- continue;
- waitingTimes[i] += currentItems[i].Count * minTime;
- if (currentItems[i].Count > 0)
- currentTimes[i][0] += minTime;
- }
- var path1 = currentItems[i][j];
- var next_item = -1;
- for (var i1 = 1; i1 < AllPaths[path1].Count; i1++)
- {
- if ((AllPaths[path1][i1 - 1] == Neighbours[i].Item1 && AllPaths[path1][i1] == Neighbours[i].Item2)
- || (AllPaths[path1][i1 - 1] == Neighbours[i].Item2 && AllPaths[path1][i1] == Neighbours[i].Item1))
- for (var i2 = 0; i2 < Neighbours.Count; i2++)
- if ((AllPaths[path1][i1] == Neighbours[i2].Item1 && AllPaths[path1][i1 + 1] == Neighbours[i2].Item2)
- || (AllPaths[path1][i1] == Neighbours[i2].Item2 && AllPaths[path1][i1 + 1] == Neighbours[i2].Item1))
- next_item = i2;
- }
- currentItems[next_item].Add(currentItems[i][0]);
- currentTimes[next_item].Add(0);
- currentItems[i]
- = Splice<int>(currentItems[i], 1);
- currentTimes[i] = Splice<double>(currentTimes[i], 1);
- }
- }
- }
- if (ok)
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement