Ejemplo n.º 1
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);
        }
        public void CalcResult(NMInitialParams ip)
        {
            double fMax  = float.MinValue;
            int    index = -1;

            for (int i = 0; i < arrF.Length; i++)
            {
                if (arrF[i] > fMax)
                {
                    fMax  = arrF[i];
                    index = i;
                }
            }

            int n = arrF.Length - 1;

            arrXRes = new double[n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < matrX.Length; j++)
                {
                    if (j == index)
                    {
                        continue;
                    }
                    arrXRes[i] += matrX[j][i];
                }
                arrXRes[i] /= (matrX.Length - 1);
            }
            fRes = ip.GetFuncValue(arrXRes);
        }
 public void CalcFuncAndResult(NMInitialParams ip)
 {
     arrF = new double[matrX.Length];
     for (int i = 0; i < arrF.Length; i++)
     {
         arrF[i] = ip.GetFuncValue(matrX[i]);
     }
     CalcResult(ip);
 }
        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 void Initialize(IInitialParams initParams)
 {
     ip = (NMInitialParams)initParams;
 }