Esempio n. 1
0
 public IterationState(IterationState prevState)
 {
     this.varCount   = prevState.varCount;
     this.eqCount    = prevState.eqCount;
     this.P          = prevState.P;
     this.C          = prevState.C;
     this.b          = prevState.b;
     this.cCoeff     = prevState.cCoeff;
     this.basIndInit = prevState.basIndInit;
     this.basInd     = prevState.basInd.Clone() as int[];
     this.basInd[prevState.eqIndex] = prevState.basIndex;
     this.Binv     = prevState.BinvNext;
     this.basIndex = this.eqIndex = this.oldIndex = -1;
 }
Esempio n. 2
0
        bool SetSwapVarsIllegalDecision(IterationState s)
        {
            s.basIndex = -1;
            double min = double.MaxValue;

            for (int i = 0; i < eqCount; i++)
            {
                double b = s.Xb.Elements[i, 0];
                if (b >= 0)
                {
                    continue;
                }
                for (int j = 0; j < varCount; j++)
                {
                    if (s.IsBaseVar(j))
                    {
                        continue;
                    }
                    double a = s.GetVarCoeff(j, i);
                    if (a >= 0)
                    {
                        continue;
                    }
                    if (b / a < min)
                    {
                        s.eqIndex  = i;
                        s.basIndex = j;
                        min        = b / a;
                    }
                }
            }
            if (s.basIndex == -1)
            {
                throw new ArgumentException("Допустимое решение не существует");
            }

            s.oldIndex = s.basInd[s.eqIndex];

            s.alpha  = s.Binv * P[s.basIndex];
            s.alphar = s.alpha.Elements[s.eqIndex, 0];
            return(true);
        }
Esempio n. 3
0
        bool SetSwapVarsLegalDecision(IterationState s)
        {
            s.basIndex = -1;
            double epslon = 0.000001;
            double min    = float.MaxValue;

            for (int i = 0; i < s.zc.N; i++)
            {
                if (s.zc.Elements[0, i] < 0 - epslon &&
                    s.zc.Elements[0, i] < min)
                {
                    min        = s.zc.Elements[0, i];
                    s.basIndex = s.zcInd[i];
                }
            }
            if (s.basIndex == -1)
            {
                return(false);
            }

            s.alpha = s.Binv * P[s.basIndex];

            s.theta = double.MaxValue;
            for (int i = 0; i < eqCount; i++)
            {
                double tmp = s.Xb.Elements[i, 0] / s.alpha.Elements[i, 0];
                if (tmp > 0 && tmp < s.theta)
                {
                    s.oldIndex = s.basInd[i];
                    s.theta    = tmp;
                    s.eqIndex  = i;
                }
            }
            if (s.theta == double.MaxValue)
            {
                throw new ArgumentException("Допустимое решение не существует");
            }
            s.alphar = s.alpha.Elements[s.eqIndex, 0];
            return(true);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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;
                }
                cCoeff *= -1;
            }
            lbIters.Items.Clear();
            webBrowser1.DocumentText = "<P>Вычисления прерваны</P>";
            MsMethod         m     = new MsMethod(Ps, b, C, cCoeff);
            SelectBasIndForm sForm = new SelectBasIndForm(varCount, eqCount, m);

            if (sForm.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            int[] basInd = sForm.GetBasInd();
            m = new MsMethod(Ps, b, C, cCoeff);
            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);
            }
        }