public IIteration DoIteration(IIteration prevIter)
        {
            it = (HJIteration)prevIter;
            double f = ip.GetFuncValue(it.arrX);

            while (it.arrE == null)
            {
                f = ip.GetFuncValue(it.arrX);
                double[] arrENext, arrXNext;
                double   fNext = Research(it.arrX, out arrENext, out arrXNext);
                if (Math.Abs(fNext - f) < ip.fEpsilon)
                {
                    for (int i = 0; i < it.arrXDelta.Length; i++)
                    {
                        it.arrXDelta[i] /= 2;
                    }
                    for (int i = 0; i < it.arrXDelta.Length; i++)
                    {
                        if (it.arrXDelta[i] < ip.xEpsilon)
                        {
                            it.CalcResult(ip);
                            return(null);
                        }
                    }
                }
                else
                {
                    it.arrE = arrENext;
                }
            }
            double[] arrXSmp = new double[it.arrX.Length];
            for (int i = 0; i < arrXSmp.Length; i++)
            {
                arrXSmp[i] = it.arrX[i] + it.arrE[i] * it.mult;
            }
            double fSmp = ip.GetFuncValue(arrXSmp);

            if (Math.Abs(fSmp - f) < ip.fEpsilon || fSmp > f)
            {
                it.fRes = f;
                return(new HJIteration(it.arrX, it.arrXDelta, null, 1));
            }
            double[]    arrXRes, arrERes;
            double      fRes = Research(arrXSmp, out arrERes, out arrXRes);
            HJIteration itNext;

            if (Math.Abs(fRes - f) < ip.fEpsilon)
            {
                it.fRes = f;
                itNext  = new HJIteration(it.arrX, it.arrXDelta, null, 1);
            }
            else
            {
                it.fRes = fSmp;
                itNext  = new HJIteration(arrXSmp, it.arrXDelta, it.arrE, it.mult + 1);
            }
            return(itNext);
        }
Exemplo n.º 2
0
        void hJToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                HJIteration it;
                ReadHJParams(out ipHJ, out it);

                HJOptimizer opt = new HJOptimizer();
                opt.Initialize(ipHJ);
                listHJ = new List <HJIteration>();
                it.CalcResult(ipHJ);
                listHJ.Add(it);
                it = new HJIteration(it.arrX, it.arrXDelta);
                it.CalcResult(ipHJ);
                int iterNum = 0;
                do
                {
                    listHJ.Add(it);
                    it = (HJIteration)opt.DoIteration(it);
                    iterNum++;
                }while (it != null && iterNum < maxIt);
                lbIterHJ.Items.Clear();
                lbIterHJ.Items.AddRange(listHJ.ToArray());
                psGraph.Clear();
                psItGraph.Clear();
                if (listHJ[0].arrX.Length != 2)
                {
                    return;
                }
                LinePlot[] arrLp = GetPotentials(ipHJ);
                foreach (LinePlot lp in arrLp)
                {
                    psGraph.Add(lp);
                    psItGraph.Add(lp);
                }
                try
                {
                    DrawHJTrajectory();
                }
                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);
        }