private void ВидалитивиробникаToolStripMenuItem_Click(object sender, EventArgs e) { if (transport.m <= 1) { return; } TransportProblem t = new TransportProblem(transport.m - 1, transport.n); for (int i = 0; i < t.m; ++i) { t.a[i] = transport.a[i]; } for (int i = 0; i < t.n; ++i) { t.b[i] = transport.b[i]; } for (int i = 0; i < t.m; ++i) { for (int j = 0; j < t.n; ++j) { t.c[i, j] = t.c[i, j]; } } transport = t; ShowProblem(); textBox1.Text += "\r\n" + "Видалино виробника A" + (transport.m + 1) + "."; textBox1.Text += "\r\n" + "Розмір задачі " + transport.m + "x" + transport.n + "."; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); }
private void додатиСпоживачаBToolStripMenuItem_Click(object sender, EventArgs e) { TransportProblem t = new TransportProblem(transport.m, transport.n + 1); for (int i = 0; i < transport.m; ++i) { t.a[i] = transport.a[i]; } for (int i = 0; i < transport.n; ++i) { t.b[i] = transport.b[i]; } for (int i = 0; i < transport.m; ++i) { for (int j = 0; j < transport.n; ++j) { t.c[i, j] = transport.c[i, j]; } } transport = t; ShowProblem(); textBox1.Text += "\r\n" + "Додано споживача B" + transport.n + "."; textBox1.Text += "\r\n" + "Розмір задачі " + transport.m + "x" + transport.n + "."; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); }
public string ChekOpornNW() { string s = ChekOporn(); if (s != "") { return(s); } TransportProblem t = new TransportProblem(m, n); for (int i = 0; i < m; ++i) { t.a[i] = a[i]; } for (int i = 0; i < n; ++i) { t.b[i] = b[i]; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { t.c[i, j] = c[i, j]; } } t.getPlanNorthWest(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (t.x[i, j] > 0 && t.x[i, j] != x[i, j]) { return("План є опорний, але не побудований методом північно-західного кута"); } } } return(""); }
public string ChekOpornMinE() { string s = ChekOporn(); if (s != "") { return(s); } TransportProblem t = new TransportProblem(m, n); for (int i = 0; i < m; ++i) { t.a[i] = a[i]; } for (int i = 0; i < n; ++i) { t.b[i] = b[i]; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { t.c[i, j] = c[i, j]; } } int flow = 0; for (int i = 0; i < m; ++i) { flow += a[i]; } while (flow > 0) { int mi = 10000000; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { int p = Math.Min(t.a[i], t.b[j]); if (mi > t.c[i, j] && p != 0) { mi = t.c[i, j]; } } } bool cont = false; for (int i = 0; i < m && !cont; ++i) { for (int j = 0; j < n && !cont; ++j) { int p = Math.Min(t.a[i], t.b[j]); if (t.c[i, j] == mi && p != 0) { if (p == x[i, j]) { t.x[i, j] = p; t.a[i] -= p; t.b[j] -= p; cont = true; flow -= p; } } } } if (cont == false) { return("План є опорний, але не побудований методом мінімального едемента."); } } return(""); }
private void toolStripStatusLabelOk_Click(object sender, EventArgs e) { if (procces == "new problem") { новаЗадачаToolStripMenuItem_Click(sender, e); return; } if (procces == "input") { int m = transport.m; int n = transport.n; for (int i = 0; i < m; ++i) { transport.a[i] = (int)dataGridView1.Rows[i + 2].Cells[2].Value; } for (int i = 0; i < n; ++i) { transport.b[i] = (int)dataGridView1.Rows[1].Cells[i + 3].Value; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { transport.c[i, j] = (int)dataGridView1.Rows[i + 2].Cells[j + 3].Value; } } int sumA = 0; for (int i = 0; i < m; ++i) { sumA += transport.a[i]; } int sumB = 0; for (int i = 0; i < n; ++i) { sumB += transport.b[i]; } if (sumA == sumB) { textBox1.Text += "\r\n" + "Задача закрита"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { випадковаЗадачаToolStripMenuItem.Enabled = false; formDialogCalcOporn = new FormDialogCalcOporn(); formDialogCalcOporn.form1 = this; formDialogCalcOporn.Show(); this.Visible = false; } } else { textBox1.Text += "\r\n" + "Задача відкрита: ";// +"sum Ai = " + sumA + " , sum Bi = " + sumB; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } return; } if (procces == "oporn plan NW") { int m = transport.m; int n = transport.n; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString() == "-") { transport.x[i, j] = -1; } else { transport.x[i, j] = int.Parse(dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString()); } } } string s = transport.ChekOpornNW(); if (s == "") { textBox1.Text += "\r\n" + "Опорний план знайдено правильно методом північно-західного кута"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { procces = "Calc potencial"; ShowPotencials(); toolStripStatusLabelStatus.Text = "Введіть потенціали. Перевірити?"; } } else { textBox1.Text += "\r\n" + "Опорний план знайдено НЕ правильно методом північно-західного кута:"; textBox1.Text += "\r\n" + " " + s; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } return; } if (procces == "oporn plan MinE") { int m = transport.m; int n = transport.n; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString() == "-") { transport.x[i, j] = -1; } else { transport.x[i, j] = int.Parse(dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString()); } } } string s = transport.ChekOpornMinE(); if (s == "") { textBox1.Text += "\r\n" + "Опорний план знайдено правильно методом мінімального едемента"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { procces = "Calc potencial"; ShowPotencials(); toolStripStatusLabelStatus.Text = "Введіть потенціали. Перевірити?"; } } else { textBox1.Text += "\r\n" + "Опорний план знайдено НЕ правильно методом мінімального елемента:"; textBox1.Text += "\r\n" + " " + s; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } return; } if (procces == "Calc potencial") { int m = transport.m; int n = transport.n; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { transport.delta[i, j] = int.Parse(dataGridView1.Rows[i + dataGridView1.RowCount - m - 2].Cells[j + 3].Value.ToString()); } } for (int i = 0; i < m; ++i) { transport.alpha[i] = int.Parse(dataGridView1.Rows[i + dataGridView1.RowCount - m - 2].Cells[3 + n].Value.ToString()); } for (int i = 0; i < n; ++i) { transport.beta[i] = int.Parse(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[i + 3].Value.ToString()); } string s = transport.CheckPotencial(); if (s == "") { textBox1.Text += "\r\n" + "Потенціали знайдено правильно."; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { procces = "Exist Cycle"; toolStripStatusLabelStatus.Text = "Чи план є оптимальний? Введіть + чи -. Перевірити?"; textBoxInput.Visible = true; } } else { textBox1.Text += "\r\n" + "Потенціали знайдено НЕ правильно: "; textBox1.Text += "\r\n" + " " + s; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } return; } if (procces == "Exist Cycle") { if (textBoxInput.Text == "+" || textBoxInput.Text == "-") { string s = transport.NextCycle(); if (s == "finish" && textBoxInput.Text == "+") { textBox1.Text += "\r\n" + "Транспортна задача розв'язана"; textBox1.Text += "\r\n" + "Вартість перевезення - " + transport.GetCost(); textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { textBoxInput.Visible = false; toolStripStatusLabelStatus.Text = "Задача розв'язана"; procces = "done"; } return; } if (s != "finish" && textBoxInput.Text == "-") { textBox1.Text += "\r\n" + "Відповідь правильна"; if (goNext) { textBox1.Text += "\r\n" + "Перехід до пошуку циклу"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); procces = "Find Cycle"; toolStripStatusLabelStatus.Text = "Введіть в поле цикл, наприклад: A1B1-A1B2-A2B2-A2B1-A1B1.Перевірити?"; textBoxInput.Text = "A1B1 - A2B1 - A2B2 - A1B2"; } return; } textBox1.Text += "\r\n" + "Відповідь НЕ правильна"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); return; } else { textBox1.Text += "\r\n" + "Введення не коректне: потрібно ввести + або -"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } return; } if (procces == "Find Cycle") { string s1 = transport.FindCycle(true); string s2 = transport.FindCycle(false); string [] s = textBoxInput.Text.Split(); string ss = "";; foreach (string t in s) { ss += t; } string f1 = "", f = ""; for (int i = 0; s1[i] != '-' && s1[i] != ' ' && i < s1.Length; ++i) { f1 += s1[i]; } for (int i = 0; ss[i] != '-' && ss[i] != ' ' && i < ss.Length; ++i) { f += ss[i]; } if (f == f1) { textBox1.Text += "\r\n" + "Перший елемент циклу знайдено правильно: " + f; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } else { textBox1.Text += "\r\n" + "Перший елемент циклу знайдено НЕ правильно"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); return; } if (ss == s1 || ss == s2) { textBox1.Text += "\r\n" + "Цикл знайдено правильно: " + ss; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { textBoxInput.Visible = false; toolStripStatusLabelStatus.Text = "Зробіть перерозподіл по циклу. Перевірити?"; ShowRes(); procces = "Go Cycle"; } return; } else { textBox1.Text += "\r\n" + "Цикл знайдено НЕ правильно."; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); } } if (procces == "Go Cycle") { int m = transport.m; int n = transport.n; TransportProblem t = new TransportProblem(m, n); for (int i = 0; i < m; ++i) { t.a[i] = transport.a[i]; } for (int i = 0; i < n; ++i) { t.b[i] = transport.b[i]; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString() == "-") { t.x[i, j] = -1; } else { t.x[i, j] = int.Parse(dataGridView1.Rows[i + dataGridView1.RowCount - m].Cells[j + 3].Value.ToString()); } } } if (t.ChekOporn() != "") { textBox1.Text += "\r\n" + "Перерозподіл НЕ правильний"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); return; } transport.CalcCycle(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (transport.x[i, j] > 0 && transport.x[i, j] != t.x[i, j] || transport.x[i, j] <= 0 && t.x[i, j] > 0) { textBox1.Text += "\r\n" + "Перерозподіл НЕ правильний"; textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); return; } } } textBox1.Text += "\r\n" + "Перерозподіл правильний"; textBox1.Text += "\r\n" + "Вартість перевезення - " + transport.GetCost(); textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); textBox1.Refresh(); if (goNext) { for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { transport.x[i, j] = t.x[i, j]; } } procces = "Calc potencial"; ShowPotencials(); toolStripStatusLabelStatus.Text = "Введіть потенціали. Перевірити?"; } } }
private void Form1_Load(object sender, EventArgs e) { transport = new TransportProblem(); }