Пример #1
0
        void DrawNMIteration(NMIteration it)
        {
            ReadGraphParams();
            if (it.arrXRes.Length != 2)
            {
                throw new Exception();
            }
            psItGraph.Remove(lpItLast, false);
            psItGraph.Remove(ppItLast, false);
            double[] arrX = new double[] {
                it.matrX[0][0], it.matrX[1][0], it.matrX[2][0], it.matrX[0][0]
            };
            double[] arrY = new double[] {
                it.matrX[0][1], it.matrX[1][1], it.matrX[2][1], it.matrX[0][1]
            };
            lpItLast = new LinePlot();
            lpItLast.OrdinateData = arrY;
            lpItLast.AbscissaData = arrX;
            lpItLast.Pen          = new Pen(Color.Green, 2f);
            psItGraph.Add(lpItLast);

            ppItLast = new PointPlot(new Marker(Marker.MarkerType.Circle, 4));
            ppItLast.OrdinateData = new double[] { it.arrXRes[1] };
            ppItLast.AbscissaData = new double[] { it.arrXRes[0] };
            psItGraph.Add(ppItLast);

            psItGraph.XAxis1.WorldMin = xMin;
            psItGraph.XAxis1.WorldMax = xMax;
            psItGraph.YAxis1.WorldMin = yMin;
            psItGraph.YAxis1.WorldMax = yMax;
            psItGraph.XAxis1.Label    = (string)dgvX.Rows[0].Cells["Имя"].Value;
            psItGraph.YAxis1.Label    = (string)dgvX.Rows[1].Cells["Имя"].Value;
            psItGraph.Refresh();
        }
Пример #2
0
        void ReadNMParams(out NMInitialParams ip, out NMIteration it)
        {
            string[] arrName = new string[dgvX.RowCount];
            for (int i = 0; i < dgvX.RowCount; i++)
            {
                arrName[i] = (string)dgvX.Rows[i].Cells["Имя"].Value;
            }
            ip = new NMInitialParams(double.Parse(tbAlpha.Text),
                                     double.Parse(tbBeta.Text), double.Parse(tbGamma.Text),
                                     double.Parse(tbEpsilon.Text), arrName, arrName,
                                     tbExpression.Text);

            int n = (int)nudN.Value;

            double[][] matr = new double[n + 1][];
            for (int i = 0; i < n + 1; i++)
            {
                matr[i] = new double[n];
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    matr[j][i] = (double)dgvArrXNM.Rows[i].Cells[j].Value;
                }
            }
            it = new NMIteration(matr);
        }
Пример #3
0
 void lbIterNM_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         NMIteration it = (NMIteration)lbIterNM.SelectedItem;
         wb.DocumentText = it.ToHtml(ipNM, d);
         DrawNMIteration(it);
     }
     catch { }
 }
Пример #4
0
        void nMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                NMIteration it;
                ReadNMParams(out ipNM, out it);

                NMOptimizer opt = new NMOptimizer();
                opt.Initialize(ipNM);
                listNM = new List <NMIteration>();
                it.CalcFuncAndResult(ipNM);
                listNM.Add(it);
                it = new NMIteration(it.matrX);
                it.CalcFuncAndResult(ipNM);
                int iterNum = 0;
                do
                {
                    listNM.Add(it);
                    it = (NMIteration)opt.DoIteration(it);
                    iterNum++;
                }while (it != null && iterNum < maxIt);
                lbIterNM.Items.Clear();
                lbIterNM.Items.AddRange(listNM.ToArray());
                psGraph.Clear();
                psItGraph.Clear();
                if (listNM[0].arrXRes.Length != 2)
                {
                    return;
                }
                LinePlot[] arrLp = GetPotentials(ipNM);
                foreach (LinePlot lp in arrLp)
                {
                    psGraph.Add(lp);
                    psItGraph.Add(lp);
                }
                try
                {
                    DrawNMTrajectory();
                }
                catch { }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка: " + ex.Message, "Расчеты прерваны",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
            return;

            NMOptimizer     opt = new NMOptimizer();
            NMInitialParams ip  = new NMInitialParams(1, 0.5, 2, 0.001,
                                                      new string[] { "x", "y" },
                                                      new string[] { "x", "y" },
                                                      "x ^ 2 + y ^ 2");

            opt.Initialize(ip);
            List <NMIteration> listIter = new List <NMIteration>();
            NMIteration        it       = new NMIteration(new double[][] { new double[] { 1, 2 }, new double[] { 5, 6 }, new double[] { 7, 10 } });

            it.CalcFuncAndResult(ip);
            do
            {
                listIter.Add(it);
                it = (NMIteration)opt.DoIteration(it);
            }while (it != null);

            HJOptimizer     opt2 = new HJOptimizer();
            HJInitialParams ip2  = new HJInitialParams(0.01, 0.1,
                                                       new string[] { "x", "y" }, new string[] { "x", "y" },
                                                       "x * x + 2 * x + 1 + y * y + 2 * y + 1");

            opt2.Initialize(ip2);
            List <HJIteration> listIter2 = new List <HJIteration>();
            HJIteration        it2       = new HJIteration(new double[] { 20, 20 }, new double[] { 0.1, 0.1 }, null, 1);

            it2.CalcResult(ip2);
            do
            {
                listIter2.Add(it2);
                it2 = (HJIteration)opt2.DoIteration(it2);
            }while (it2 != null);
        }
        public IIteration DoIteration(IIteration prevIter)
        {
            it = (NMIteration)prevIter;
            for (int i = 0; i < it.matrX.Length; i++)
            {
                if (it.matrX[i].Length + 1 != it.matrX.Length)
                {
                    throw new Exception();
                }
            }
            int n = it.matrX[0].Length;

            it.arrF = new double[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                it.arrF[i] = ip.GetFuncValue(it.matrX[i]);
            }
            it.fg = it.fh = float.MinValue;
            it.fl = float.MaxValue;
            for (int i = 0; i < n + 1; i++)
            {
                if (it.arrF[i] > it.fh)
                {
                    it.fh = it.arrF[i];
                    it.h  = i;
                }
                if (it.arrF[i] > it.fg && it.arrF[i] < it.fh)
                {
                    it.fg = it.arrF[i];
                    it.g  = i;
                }
                if (it.arrF[i] < it.fl)
                {
                    it.fl = it.arrF[i];
                    it.l  = i;
                }
            }

            // центр
            it.arrX0 = new double[n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    if (j == it.h)
                    {
                        continue;
                    }
                    it.arrX0[i] += it.matrX[j][i];
                }
                it.arrX0[i] /= n;
            }
            it.f0 = ip.GetFuncValue(it.arrX0);

            // отражение
            it.arrXr = new double[n];
            for (int i = 0; i < n; i++)
            {
                it.arrXr[i] = (1 + ip.alpha) * it.arrX0[i] - ip.alpha * it.matrX[it.h][i];
            }
            it.fr = ip.GetFuncValue(it.arrXr);

            if (it.fr < it.fl)
            {
                // растяжение
                it.arrXe = new double[n];
                for (int i = 0; i < n; i++)
                {
                    it.arrXe[i] = ip.gamma * it.arrXr[i] + (1 - ip.gamma) * it.arrX0[i];
                }
                it.fe = ip.GetFuncValue(it.arrXe);

                if (it.fe < it.fl)
                {
                    it.matrX[it.h] = (double[])it.arrXe.Clone();
                    it.CalcResult(ip);
                    if (!CheckIsDone())
                    {
                        return(new NMIteration(it.matrX));
                    }
                    else
                    {
                        return(null);
                    }
                }

                it.matrX[it.h] = (double[])it.arrXr.Clone();
                it.CalcResult(ip);
                if (!CheckIsDone())
                {
                    return(new NMIteration(it.matrX));
                }
                else
                {
                    return(null);
                }
            }

            if (it.fr <= it.fg)
            {
                it.matrX[it.h] = (double[])it.arrXr.Clone();
                it.CalcResult(ip);
                if (!CheckIsDone())
                {
                    return(new NMIteration(it.matrX));
                }
                else
                {
                    return(null);
                }
            }

            if (it.fr <= it.fh)
            {
                it.matrX[it.h] = (double[])it.arrXr.Clone();
            }

            // сжатие
            it.arrXc = new double[n];
            for (int i = 0; i < it.arrXc.Length; i++)
            {
                it.arrXc[i] = ip.beta * it.matrX[it.h][i] + (1 - ip.beta) * it.arrX0[i];
            }
            it.fc = ip.GetFuncValue(it.arrXc);

            if (it.fc > it.fh)
            {
                for (int i = 0; i < n + 1; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        it.matrX[i][j] = 0.5 * (it.matrX[i][j] + it.matrX[it.l][j]);
                    }
                }
                it.CalcResult(ip);
                if (!CheckIsDone())
                {
                    return(new NMIteration(it.matrX));
                }
                else
                {
                    return(null);
                }
            }

            it.matrX[it.h] = (double[])it.arrXc.Clone();
            it.CalcResult(ip);
            if (!CheckIsDone())
            {
                return(new NMIteration(it.matrX));
            }
            else
            {
                return(null);
            }
        }