private void doCalculateToolStripMenuItem_Click(object sender, EventArgs e) { if (ReadData() == false) { return; } if (cbMax.Checked == false) { for (int i = 0; i < varCount; i++) { C.Elements[0, i] *= -1; } } lbIters.Items.Clear(); webBrowser1.DocumentText = "<P>Вычисления не завершены</P>"; ExMatrix[] PsEx = new ExMatrix[varCount + 1]; for (int j = 0; j < varCount; j++) { ExMatrix matr = new ExMatrix(eqCount + 1, 1); for (int i = 0; i < eqCount; i++) { matr.Elements[i, 0] = Ps[j].Elements[i, 0]; } matr.Elements[eqCount, 0] = D.Elements[0, j]; PsEx[j] = matr; } PsEx[varCount] = new ExMatrix(eqCount + 1, 1); for (int i = 0; i < eqCount; i++) { PsEx[varCount].Elements[i, 0] = -b.Elements[i, 0]; } PsEx[varCount].Elements[eqCount, 0] = 0; ExMatrix bEx = new ExMatrix(eqCount + 1, 1); bEx.Elements[eqCount, 0] = 1; ExMatrix CEx = new ExMatrix(1, varCount + 1); for (int j = 0; j < varCount; j++) { CEx.Elements[0, j] = C.Elements[0, j]; } CEx.Elements[0, varCount] = 0; MsMethod m = new MsMethod(PsEx, bEx, CEx, 0); SelectBasIndForm sForm = new SelectBasIndForm(varCount + 1, eqCount + 1, m); if (sForm.ShowDialog() == DialogResult.Cancel) { return; } int[] basInd = sForm.GetBasInd(); try { m.SetBasis(basInd); while (m.DoIteration()) { ; } } catch (Exception ex) { MessageBox.Show("Вычисления прерваны: " + ex.Message, "Прерывание вычислений", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } states = m.states; lbIters.SuspendLayout(); for (int i = 0; i < states.Count; i++) { IterationState s = states[i] as IterationState; int iterNum = i + 1; string str = iterNum.ToString(); if (s.isDecisionLegal) { str += " (F = " + s.GetFuncValue().ToString() + ", допустимое решение)"; } else { str += " (F = " + s.GetFuncValue().ToString() + ", недопустимое решение)"; } lbIters.Items.Add(str); } lbIters.ResumeLayout(); webBrowser1.DocumentText = (m.states[0] as IterationState).GetReport(); tabControl1.SelectedIndex = 1; if (cbMax.Checked == false) { MessageBox.Show("Поскольку требуется минимизация, " + "целевая функция была умножена на -1, и далее " + "произведена максимизация", "Целевая функция умножена на -1", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void SetBasis(int[] basInd) { this.basIndInit = basInd; ExMatrix matr = new ExMatrix(eqCount, varCount + 1); for (int j = 0; j < varCount; j++) { for (int i = 0; i < eqCount; i++) { matr.Elements[i, j] = P[j].Elements[i, 0]; } } for (int i = 0; i < eqCount; i++) { matr.Elements[i, varCount] = b.Elements[i, 0]; } for (int eqNum = 0; eqNum < eqCount; eqNum++) { double elem = matr.Elements[eqNum, basInd[eqNum]]; if (elem == 0) { int eqIndex = eqNum + 1, varIndex = basInd[eqNum] + 1; throw new ArgumentException("Нулевой коэффициент при базисной переменной номер " + varIndex.ToString() + " в уравнении номер " + eqIndex.ToString() + ". Выберете другой начальный базис"); } for (int j = 0; j < matr.N; j++) { matr.Elements[eqNum, j] /= elem; } for (int i = 0; i < matr.M; i++) { if (i == eqNum) { continue; } elem = matr.Elements[i, basInd[eqNum]]; for (int j = 0; j < matr.N; j++) { matr.Elements[i, j] -= matr.Elements[eqNum, j] * elem; } } } for (int i = 0; i < eqCount; i++) { for (int j = 0; j < varCount; j++) { P[j].Elements[i, 0] = matr.Elements[i, j]; } } for (int i = 0; i < eqCount; i++) { b.Elements[i, 0] = matr.Elements[i, varCount]; } for (int eqNum = 0; eqNum < eqCount; eqNum++) { double elem = C.Elements[0, basIndInit[eqNum]]; for (int j = 0; j < varCount; j++) { C.Elements[0, j] -= P[j].Elements[eqNum, 0] * elem; } cCoeff -= -(b.Elements[eqNum, 0]) * elem; } }
public double GetFuncValue() { ExMatrix value = Cb * Xb; return(Math.Round(value.Elements[0, 0] + cCoeff, 5)); }
public bool DoIteration() { IterationState s; if (iterNum > 0) { IterationState prevState = states[states.Count - 1] as IterationState; s = new IterationState(prevState); } else { s = new IterationState(varCount, eqCount, P, C, b, cCoeff, basIndInit); } states.Add(s); s.Cb = new ExMatrix(1, eqCount); for (int i = 0; i < eqCount; i++) { s.Cb.Elements[0, i] = C.Elements[0, s.basInd[i]]; } s.gamma = s.Cb * s.Binv; ArrayList zcPMatrices = new ArrayList(); ExMatrix zcC = new ExMatrix(1, varCount - eqCount); s.zcInd = new int[varCount - eqCount]; int k = 0; for (int i = 0; i < varCount; i++) { if (s.IsBaseVar(i)) { continue; } s.zcInd[k] = i; zcPMatrices.Add(P[i]); zcC.Elements[0, k] = C.Elements[0, i]; k++; } ExMatrix zcP = new ExMatrix(zcPMatrices.ToArray(typeof(ExMatrix)) as ExMatrix[]); s.zc = s.gamma * zcP - zcC; s.Xb = s.Binv * b; s.isDecisionLegal = true; for (int i = 0; i < s.Xb.M; i++) { if (s.Xb.Elements[i, 0] < 0) { s.isDecisionLegal = false; break; } } bool res; if (s.isDecisionLegal) { res = SetSwapVarsLegalDecision(s); } else { res = SetSwapVarsIllegalDecision(s); } if (res == false) { return(false); } s.xi = new ExMatrix(eqCount, 1); for (int i = 0; i < s.xi.M; i++) { if (i == s.eqIndex) { s.xi.Elements[i, 0] = 1 / s.alphar; } else { s.xi.Elements[i, 0] = -s.alpha.Elements[i, 0] / s.alphar; } } ExMatrix matr = new ExMatrix(eqCount); for (int i = 0; i < matr.M; i++) { matr.Elements[i, s.eqIndex] = s.xi.Elements[i, 0]; } s.BinvNext = matr * s.Binv; iterNum++; return(true); }