Exemplo n.º 1
0
        private void btnSolveRK_Click(object sender, EventArgs e)
        {
            this.rtbResult1.Text = this.rtbResult2.Text = string.Empty;
            this.dgvTabRes.DataSource = null;
            this.n = int.Parse(this.txtN.Text);
            this.setCount = this.rbN1.Checked ? 1 : 2;
            //this.gamma = double.Parse(txtGamma.Text);

            ZedGraph.GraphPane gp = this.zgcMainChart2.GraphPane;
            gp.CurveList.Clear();
            this.curves.Clear();

            this.curves.Add(this.curveName, new PointPairList());
            gp.AddCurve(this.curveName, this.curves[curveName], Color.Black, SymbolType.None);

            List<double> gammaList = new List<double>();
            List<double> p = new List<double>();

            for (int i = 0; i < dgvGama.ColumnCount; i++)
            {
                gammaList.Add(Convert.ToDouble(dgvGama.Rows[0].Cells[i].Value));
            }

            List<TaskParameter> list = new List<TaskParameter>();
            for (int i = 0; i < dgvParameters.RowCount; i++)
            {
                TaskParameter tp = new TaskParameter(dgvParameters.ColumnCount);
                for (int j = 0; j < dgvParameters.ColumnCount; j++)
                {
                    double val = Convert.ToDouble(dgvParameters.Rows[i].Cells[j].Value);
                    tp.Param[j] = val;
                }
                list.Add(tp);
            }

            this.tps = new TaskParameters(list, gammaList);
            string mfName = this.setCount == 1 ? "MF1" : "MF2";
            this.funcDescr = new Functions_2_2(mfName, this.fnames[(sfList.SelectedItem as string).ToUpper()]);

            this.tw = new TaskWorker(tps, funcDescr.GetType(), funcDescr.MainFuncName, funcDescr.SecFuncName, funcDescr);

            this.startVector = this.setCount == 1 ? new Vector(1, double.Parse(this.txtY0.Text)) :
                new Vector(2, double.Parse(this.txtY00.Text), double.Parse(this.txtY01.Text));

            RKVectorForm rk = new RKVectorForm(tw, curveName, double.Parse(this.txtT0.Text), double.Parse(this.txtT1.Text), startVector);
            this.res = rk.SolveWithConstH(n, RKMetodType.RK4_1);

            if (this.rbN1.Checked)
            {
                res.ForEach(r => this.curves[curveName].Add(r.X, r.Y[0]));
            }
            else
            {
                res.ForEach(r => this.curves[curveName].Add(r.Y[0], r.Y[1]));
            }
            this.zgcMainChart2.AxisChange();
            this.zgcMainChart2.Refresh();

            if (this.setCount == 1)
            {
                this.tblResList = new List<ResPointViewType1>();
                for (int i = 0; i < res.Count - 1; i++)
                    (this.tblResList as List<ResPointViewType1>).Add(new ResPointViewType1(res[i].X, res[i].Y[0], -1, -1));
                this.dgvTabRes.DataSource = null;
                this.dgvTabRes.DataSource = tblResList;
            }
            else
            {
                this.tblResList = new List<ResPointViewType2>();
                for (int i = 0; i < res.Count - 2; i++)
                    (this.tblResList as List<ResPointViewType2>).Add(new ResPointViewType2(res[i].X, res[i].Y[0], res[i].Y[1], -1, -1));
                this.dgvTabRes.DataSource = null;
                this.dgvTabRes.DataSource = tblResList;
            }
            this.dgvTabRes.RefreshDataSource();

            this.randomStartParameters = this.GetRandomStartParam();
        }
Exemplo n.º 2
0
        private void btnSolveRAlg2_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnSolveRAlg2.Enabled = false;
                int tick = Environment.TickCount;
                this.inf2 = null;
                //List<List<double>> sp = this.GetRandomStartParam();
                List<List<double>> sp = this.randomStartParameters;

                RAlgSolver ra = new RAlgSolver(this.tw, this.res, sp, this.startVector);
                ra.FUNCT = new RAlgSolver.FUNCTDelegate(ra.FUNCT4);
                ra.R_Algorithm();

                for (int i = 0; i < res.Count - this.setCount; i++)
                {
                    if (this.setCount == 1)
                    {
                        (this.tblResList as List<ResPointViewType1>)[i].M2 = ra.itab[i];
                    }
                    else
                    {
                        (this.tblResList as List<ResPointViewType2>)[i].M2 = ra.itab[i];
                    }
                }
                this.dgvTabRes.RefreshDataSource();

                Dictionary<double, int> tt = this.GetTT(this.res, ra.itab);

                StringBuilder sb = new StringBuilder();

                //-- draw res ---
                if (!this.curves.ContainsKey(this.curveName2))
                {
                    this.curves.Add(this.curveName2, new PointPairList());
                    LineItem curve = new LineItem(this.curveName2, this.curves[curveName2], Color.Blue, SymbolType.None);
                    curve.Line.Style = System.Drawing.Drawing2D.DashStyle.DashDot;
                    curve.Line.Width = 2;
                    this.zgcMainChart2.GraphPane.CurveList.Add(curve);
                }
                else
                {
                    this.curves[this.curveName2].Clear();
                }

                int k = 0;
                List<TaskParameter> listDraw = new List<TaskParameter>();
                for (int i = 0; i < tps.Count; i++)
                {
                    TaskParameter tpDraw = new TaskParameter(tps[i].Param.Length);
                    string line = string.Format("{0}) ", i);
                    for (int j = 0; j < tps[0].Param.Length; j++)
                    {
                        tpDraw.Param[j] = ra.x[k];
                        line += string.Format("a{0}={1:f6} ", j, ra.x[k]);
                        k++;
                    }
                    sb.AppendLine(line);
                    listDraw.Add(tpDraw);
                }
                sb.AppendLine("-----");
                sb.Append(string.Format("f={0}", ra.f));
                rtbResult2.Text = sb.ToString();

                TaskParameters tps1 = new TaskParameters(listDraw, tt);
                TaskWorker tw1 = new TaskWorker(tps1, this.funcDescr.GetType(), this.funcDescr.MainFuncName, this.funcDescr.SecFuncName, this.funcDescr);
                RKVectorForm rk1 = new RKVectorForm(tw1, curveName2, double.Parse(this.txtT0.Text), double.Parse(this.txtT1.Text), this.startVector);
                RKResults res1 = rk1.SolveWithConstH(n, RKMetodType.RK2_1);

                //if (this.setCount == 1)
                //{
                //    res1.ForEach(r => this.curves[this.curveName2].Add(r.X, r.Y[0]));
                //}
                //else
                //{
                //    res1.ForEach(r => this.curves[this.curveName2].Add(r.Y[0], r.Y[1]));
                //}
                int nn = this.setCount == 1 ? 1 : 2;
                for (int i = 0; i < res1.Count - nn; i++)
                {
                    if (nn == 1)
                    {
                        this.curves[curveName2].Add(res1[i].X, res1[i].Y[0]);
                    }
                    else
                    {
                        this.curves[curveName2].Add(res1[i].Y[0], res1[i].Y[1]);
                    }
                }
                this.zgcMainChart2.AxisChange();
                this.zgcMainChart2.Refresh();

                this.ra2 = ra;
                double t = ((Environment.TickCount - tick) / (double)1000);

                this.rtbResult2.Text += string.Format("\r\n-----\r\ntime = {0} sec", t);
                this.inf2 = string.Format("Result: f = {0:f6} time = {1} sec", ra.f, t);
            }
            finally
            {
                this.btnSolveRAlg2.Enabled = true;
            }
        }
Exemplo n.º 3
0
        private RKResults DrawRes(RKResults r, RAlgSolver ra, string cn, string cnsuff, Color c, OutputHelper output,
                                  int eb, out double functional)
        {
            if (!isReal)
            {
                for (int i = 0; i < r.Count - setCount; i++)
                {
                    if (eb == 1)
                    {
                        if (setCount == 1)
                        {
                            (tblResList as List<ResPointViewType1>)[i + i*step].M1 = ra.itab[i];
                        }
                        else
                        {
                            (tblResList as List<ResPointViewType2>)[i + i*step].M1 = ra.itab[i];
                        }
                    }
                    else
                    {
                        if (setCount == 1)
                        {
                            (tblResList as List<ResPointViewType1>)[i + i*step].M2 = ra.itab[i];
                        }
                        else
                        {
                            (tblResList as List<ResPointViewType2>)[i + i*step].M2 = ra.itab[i];
                        }
                    }
                }
                grcTabRes.RefreshDataSource();
            }

            var sb = new StringBuilder();

            Dictionary<double, int> tt = GetTT(r, ra.itab);

            //-- draw res ---
            if (!curves.ContainsKey(cn))
            {
                curves.Add(cn, new PointPairList());
                var curve = new LineItem(cn, curves[cn], c, SymbolType.None);
                curve.Line.Style = DashStyle.Dash;
                curve.Line.Width = 2;
                //this.zgcMainChart2.MasterPane[1].CurveList.Add(curve);

                if (setCount == 2)
                {
                    curves.Add(cn + cnsuff, new PointPairList());
                    var curve1 = new LineItem(cn + cnsuff, curves[cn + cnsuff], c, SymbolType.None);
                    curve.Line.Style = DashStyle.Dash;
                    curve.Line.Width = 2;
                    zgcMainChart2.MasterPane[0].CurveList.Add(curve1);

                    zgcMainChart2.MasterPane[1].CurveList.Add(curve);
                }
                else
                {
                    zgcMainChart2.MasterPane[0].CurveList.Add(curve);
                }
            }
            else
            {
                curves[cn].Clear();
                if (setCount == 2)
                {
                    curves[cn + cnsuff].Clear();
                }
            }

            int k = 0;
            var listDraw = new List<TaskParameter>();
            for (int i = 0; i < tps.Count; i++)
            {
                var tpDraw = new TaskParameter(tps[i].Param.Length);
                string line = string.Format("{0}) ", i);
                for (int j = 0; j < tps[0].Param.Length; j++)
                {
                    tpDraw.Param[j] = ra.x[k];
                    line += string.Format("a{0}={1:f6} ", j, ra.x[k]);
                    k++;
                }
                sb.AppendLine(line);
                listDraw.Add(tpDraw);
            }
            sb.AppendLine("-----");

            var tps1 = new TaskParameters(listDraw, tt);
            var tw1 = new TaskWorker(tps1, funcDescr.GetType(), funcDescr.MainFuncName, funcDescr.SecFuncName, funcDescr);

            double t0, t1;
            if (!isReal)
            {
                t0 = double.Parse(txtT0.Text);
                t1 = double.Parse(txtT1.Text);
            }
            else
            {
                t0 = 0;
                t1 = ASignal.Count*0.01;
            }
            var rk1 = new RKVectorForm(tw1, cn, t0, t1, startVector);
            RKResults res1 = rk1.SolveWithConstH(res.Count - 1, RKMetodType.RK2_1);

            functional = GetDiff(res, res1);
            sb.Append(string.Format("f={0:f6}", functional));
            //rtb.Text = sb.ToString();
            //this.SetControlFeathe(rtb, "text=", sb.ToString());
            output.WriteLine(sb.ToString());

            int nn = setCount == 1 ? 1 : 2;
            for (int i = 0; i < res1.Count - nn; i++)
            {
                if (nn == 1)
                {
                    curves[cn].Add(res1[i].X, res1[i].Y[0]);
                }
                else
                {
                    curves[cn].Add(res1[i].Y[0], res1[i].Y[1]);
                    curves[cn + cnsuff].Add(res1[i].X, res1[i].Y[0]);
                }
            }
            SetControlProperty(zgcMainChart2, "chart", null);
            //this.zgcMainChart2.AxisChange();
            //this.zgcMainChart2.Refresh();
            return res1;
        }
Exemplo n.º 4
0
        private object view_ViewAction(ViewEventType type, ViewEventArgs args)
        {
            switch (type)
            {
                case ViewEventType.Exit:
                    {
                        alive = false;
                        break;
                    }
                case ViewEventType.StartSolving1:
                    {
                        string fname = args.Parameters[0].ToString();
                        string curveName = args.Parameters[1].ToString();
                        var fe = new FunctionExecuter(typeof (Functions), fname);
                        var r = new Random();
                        double y0 = z1min + r.NextDouble()*(z1max - z1min);
                        var rk = new RKVectorForm(fe, curveName, t0, t1, new Vector(1, y0));
                        rk.OnResultGenerated += rk_OnResultGenerated;
                        rk.SolveWithConstH(rkN, RKMetodType.RK4_1);
                        break;
                    }
                case ViewEventType.StartSolving2:
                    {
                        string fname = args.Parameters[0].ToString();
                        var curveNames = args.Parameters[1] as string[,];
                        var fe = new FunctionExecuter(typeof (Functions), fname);

                        double h1 = (z1max - z1min)/(randN);
                        double h2 = (z2max - z2min)/(randN);
                        var results = new Dictionary<string, RKResults>();
                        for (int i = 0; i < randN; i++)
                        {
                            for (int j = 0; j < randN; j++)
                            {
                                double z00 = z1min + i*h1;
                                double z01 = z2min + j*h2;
                                var rk = new RKVectorForm(fe, curveNames[i, j], t0, t1, new Vector(2, z00, z01));
                                rk.OnSolvingDone += rk_OnSolvingDone;
                                RKResults res = rk.SolveWithConstH(rkN, RKMetodType.RK4_1);
                                results.Add(curveNames[i, j], res);
                            }
                        }
                        break;
                    }
                case ViewEventType.SolvePodhod2Type1:
                    {
                        string fname = args.Parameters[0].ToString();
                        string curveName = args.Parameters[1].ToString();
                        var fe = new FunctionExecuter(typeof (Functions), fname);

                        var rk = new RKVectorForm(fe, curveName, t0, t1, new Vector(1, y0));

                        rk.OnResultGenerated += rk_OnResultGenerated;
                        rk.OnSolvingDone += rk_OnSolvingDoneType1;
                        rk.SolveWithConstH(rkN, RKMetodType.RK4_1);
                        break;
                    }
                case ViewEventType.SolovePodhod2Type2:
                    {
                        string fname = args.Parameters[0].ToString();
                        string curveName = args.Parameters[1].ToString();
                        var fe = new FunctionExecuter(typeof (Functions), fname);

                        var rk = new RKVectorForm(fe, curveName, t0, t1, new Vector(2, y00, y01));

                        rk.OnResultGenerated += rk_OnResGenForType2;
                        rk.OnSolvingDone += rk_OnSolvingDoneType2;
                        rk.SolveWithConstH(rkN, RKMetodType.RK4_1);
                        break;
                    }
                case ViewEventType.SolovePodhod2Type2Mass:
                    {
                        string fname = args.Parameters[0].ToString();
                        var curveNames = args.Parameters[1] as string[,];
                        var fe = new FunctionExecuter(typeof (Functions), fname);

                        double h1 = (z1max - z1min)/(randN);
                        double h2 = (z2max - z2min)/(randN);
                        var results = new Dictionary<string, RKResults>();
                        for (int i = 0; i < randN; i++)
                        {
                            for (int j = 0; j < randN; j++)
                            {
                                double z00 = z1min + i*h1;
                                double z01 = z2min + j*h2;
                                var rk = new RKVectorForm(fe, curveNames[i, j], t0, t1, new Vector(2, z00, z01));
                                //rk.OnResultGenerated += new RKResultGeneratedDelegate(rk_OnResGenForType2);
                                //rk.OnSolvingDone += new RKSolvingDoneDelegate(rk_OnSolvingDoneType2);
                                RKResults res = rk.SolveWithConstH(rkN, RKMetodType.RK4_1);
                                results.Add(curveNames[i, j], res);
                            }
                        }

                        view.SendSolvingResultType2Mass(results, fe);

                        break;
                    }
                case ViewEventType.UpdateParams:
                    {
                        t0 = (double) args.Parameters[0];
                        t1 = (double) args.Parameters[1];
                        y0 = (double) args.Parameters[2];
                        y00 = (double) args.Parameters[3];
                        y01 = (double) args.Parameters[4];

                        rkN = Convert.ToInt32(args.Parameters[5]);
                        randN = Convert.ToInt32(args.Parameters[6]);
                        z1min = (double) args.Parameters[7];
                        z1max = (double) args.Parameters[8];
                        z2min = (double) args.Parameters[9];
                        z2max = (double) args.Parameters[10];
                        break;
                    }
            }
            return null;
        }
Exemplo n.º 5
0
        private void btnSolveRK_Click(object sender, EventArgs e)
        {
            try
            {
                btnSolveRK.Enabled = false;
                isReal = false;
                view.Output.ClearScreen();
                grcTabRes.DataSource = null;
                n = int.Parse(txtN.Text, enUsCulture);
                setCount = rbN1.Checked ? 1 : 2;
                //this.gamma = double.Parse(txtGamma.Text);

                if (setCount == 1)
                {
                    view.ConfigGraphPane(zgcMainChart2, this, PaneLayout.SingleColumn, new List<ChartInfo>
                                                                                           {
                                                                                               new ChartInfo
                                                                                                   {
                                                                                                       Title = "(t,z1)",
                                                                                                       XAxisTitle = "t",
                                                                                                       YAxisTitle = "z1"
                                                                                                   },
                                                                                           });
                }
                else
                {
                    view.ConfigGraphPane(zgcMainChart2, this, PaneLayout.SingleColumn, new List<ChartInfo>
                                                                                           {
                                                                                               new ChartInfo
                                                                                                   {
                                                                                                       Title = "(t,z1)",
                                                                                                       XAxisTitle = "t",
                                                                                                       YAxisTitle = "z1"
                                                                                                   },
                                                                                               new ChartInfo
                                                                                                   {
                                                                                                       Title = "(z1,z2)",
                                                                                                       XAxisTitle = "z1",
                                                                                                       YAxisTitle = "z2"
                                                                                                   }
                                                                                           });
                }
                //ZedGraph.GraphPane gp = this.zgcMainChart2.GraphPane;
                //gp.CurveList.Clear();
                curves.Clear();

                if (setCount == 1)
                {
                    curves.Add(curveName, new PointPairList());
                    zgcMainChart2.MasterPane[0].AddCurve(curveName, curves[curveName], Color.Black, SymbolType.None);
                }
                else
                {
                    curves.Add(curveName, new PointPairList());
                    curves.Add(curveName + curveNameSuff, new PointPairList());
                    zgcMainChart2.MasterPane[0].AddCurve(curveName + curveNameSuff, curves[curveName + curveNameSuff],
                                                         Color.Black, SymbolType.None);
                    zgcMainChart2.MasterPane[1].AddCurve(curveName, curves[curveName], Color.Black, SymbolType.None);
                }

                var p = new List<double>();

                var gammaList = new List<double>();
                for (int i = 0; i < dgvGama.ColumnCount; i++)
                {
                    double d = double.Parse(dgvGama.Rows[0].Cells[i].Value.ToString().Replace(',', '.'), enUsCulture);
                    gammaList.Add(d);
                }

                var list = new List<TaskParameter>();
                for (int i = 0; i < dgvParameters.RowCount; i++)
                {
                    var tp = new TaskParameter(dgvParameters.ColumnCount);
                    for (int j = 0; j < dgvParameters.ColumnCount; j++)
                    {
                        double val = double.Parse(dgvParameters.Rows[i].Cells[j].Value.ToString().Replace(',', '.'),
                                                  enUsCulture);
                        tp.Param[j] = val;
                    }
                    list.Add(tp);
                }

                tps = new TaskParameters(list, gammaList);
                string mfName = setCount == 1 ? "MF1" : "MF2";
                funcDescr = new Functions_2_2(mfName, fnames[(sfList.SelectedItem as string).ToUpper()]);

                tw = new TaskWorker(tps, funcDescr.GetType(), funcDescr.MainFuncName, funcDescr.SecFuncName, funcDescr);

                startVector = setCount == 1
                                  ? new Vector(1, double.Parse(txtY0.Text.Replace(',', '.'), enUsCulture))
                                  : new Vector(2, double.Parse(txtY00.Text.Replace(',', '.'), enUsCulture),
                                               double.Parse(txtY01.Text.Replace(',', '.'), enUsCulture));

                var rk = new RKVectorForm(tw, curveName, double.Parse(txtT0.Text.Replace(',', '.'), enUsCulture),
                                          double.Parse(txtT1.Text.Replace(',', '.'), enUsCulture), startVector);
                res = rk.SolveWithConstH(n, RKMetodType.RK4_1);

                if (rbN1.Checked)
                {
                    for (int i = 0; i < res.Count - 1; i++)
                    {
                        curves[curveName].Add(res[i].X, res[i].Y[0]);
                    }
                    //res.ForEach(r => this.curves[curveName].Add(r.X, r.Y[0]));
                }
                else
                {
                    for (int i = 0; i < res.Count - 2; i++)
                    {
                        curves[curveName + curveNameSuff].Add(res[i].X, res[i].Y[0]);
                        curves[curveName].Add(res[i].Y[0], res[i].Y[1]);
                    }
                    //res.ForEach(r => this.curves[curveName].Add(r.Y[0], r.Y[1]));
                }
                zgcMainChart2.AxisChange();
                zgcMainChart2.Refresh();

                if (setCount == 1)
                {
                    tblResList = new List<ResPointViewType1>();
                    for (int i = 0; i < res.Count - 1; i++)
                        (tblResList as List<ResPointViewType1>).Add(new ResPointViewType1(res[i].X, res[i].Y[0], -1, -1));
                    grcTabRes.DataSource = null;
                    grcTabRes.DataSource = tblResList;
                }
                else
                {
                    tblResList = new List<ResPointViewType2>();
                    for (int i = 0; i < res.Count - 2; i++)
                        (tblResList as List<ResPointViewType2>).Add(new ResPointViewType2(res[i].X, res[i].Y[0],
                                                                                          res[i].Y[1], -1, -1));
                    grcTabRes.DataSource = null;
                    grcTabRes.DataSource = tblResList;
                }
                grcTabRes.RefreshDataSource();
            }
            finally
            {
                btnSolveRK.Enabled = true;
            }
        }