Esempio n. 1
0
        public static ControlConstraints BoxConstraints(PhasePoint ld, PhasePoint sizes, PhasePoint steps)
        {
            ControlConstraints constr = new ControlConstraints();

            int[]
            upperBounds = new int[ld.Dim],
            cnt = new int[ld.Dim];
            for (int i = 0; i < ld.Dim; i++)
            {
                upperBounds[i] = (int)(sizes[i] / steps[i]);
                cnt[i]         = 0;
            }

            int curind;

            do
            {
                PhasePoint curpoint = new PhasePoint(ld, steps, cnt);
                constr.Add(curpoint, null);

                curind = 0;
                cnt[curind]++;
                while (curind < ld.Dim && cnt[curind] > upperBounds[curind])
                {
                    cnt[curind] = 0;
                    curind++;
                    if (curind < ld.Dim)
                    {
                        cnt[curind]++;
                    }
                }
            } while (curind < ld.Dim);

            return(constr);
        }
Esempio n. 2
0
        public PhasePoint f(double t, PhasePoint x, PhasePoint u, PhasePoint v)
        {
#if _DEBUG
            if (u.Dim != v.Dim)
            {
                throw new Exception("Simple motion dynamics; f: controls of different dimensions!" `);
            }
#endif
            return(u + v);
        }
Esempio n. 3
0
        public static double Minmax(IDynamics dynam, double t, PhasePoint x, ControlConstraints u,
                                    ControlConstraints v, Grid grid)
        {
            double     maxmin = -1e38;
            PhasePoint neigh;

            foreach (var b in v.Keys)
            {
                double min = 1e38;
                foreach (var a in u.Keys)
                {
                    PhasePoint speed = dynam.f(t, x, a, b);
                    double     sum   = 0;
                    for (int i = 0; i < x.Dim; i++)
                    {
                        if (speed[i] > 0)
                        {
                            if (grid.GetNeighbour(x, i, -1, out neigh))
                            {
                                sum += speed[i] * (grid[x][t] - grid[neigh][t]) / grid.Steps[i];
                            }
                            else
                            {
                                // Что делать??? Как аппроксимировать на краю сетки???
                                sum += 0;
                            }
                        }
                        else
                        {
                            if (grid.GetNeighbour(x, i, +1, out neigh))
                            {
                                sum += speed[i] * (grid[neigh][t] - grid[x][t]) / grid.Steps[i];
                            }
                            else
                            {
                                // Что делать??? Как аппроксимировать на краю сетки???
                                sum += 0;
                            }
                        }
                    }
                    if (min > sum)
                    {
                        min = sum;
                    }
                }

                if (maxmin < min)
                {
                    maxmin = min;
                }
            }

            return(maxmin);
        }
Esempio n. 4
0
        public static ControlConstraints SegmentConstraints(PhasePoint beginning, PhasePoint end, PhasePoint steps)
        {
            ControlConstraints constr       = new ControlConstraints();
            PhasePoint         currentpoint = beginning;

            while (currentpoint.CompareTo(end) != 0)
            {
                constr.Add(currentpoint, null);
                currentpoint += steps;
            }

            return(constr);
        }
Esempio n. 5
0
        public double fi(int i, double t, PhasePoint x, PhasePoint u, PhasePoint v)
        {
#if _DEBUG
            if (u.Dim != v.Dim)
            {
                throw new Exception("Simple motion dynamics; fi: controls of different dimensions!" `);
            }
            if (i < 0 || i >= u.Dim)
            {
                throw new Exception("Simple motion dynamics; fi: index out of range!" `);
            }
#endif
            return(u[i] + v[i]);
        }
Esempio n. 6
0
        // Сетка, накинутая на шар
        public static Grid BallGrid(PhasePoint center, double radius, PhasePoint steps)
        {
            Grid grid = new Grid();

            grid.Steps = steps;

            int[]
            lowerBounds = new int[center.Dim],
            upperBounds = new int[center.Dim],
            cnt         = new int[center.Dim];

            lowerBounds[0] = -(int)(radius / steps[0]);
            upperBounds[0] = -lowerBounds[0];
            cnt[0]         = lowerBounds[0] - 1;

            int curind = 0;

            while (curind >= 0)
            {
                cnt[curind]++;
                if (cnt[curind] > upperBounds[curind])
                {
                    curind--;
                }
                else
                {
                    curind++;
                    if (curind < center.Dim)
                    {
                        lowerBounds[curind] =
                            (int)Math.Ceiling(-Math.Sqrt(radius * radius -
                                                         cnt
                                                         .Take(curind)
                                                         .Zip(steps, (i, s) => i * s)
                                                         .Select(x => x * x).Sum()) / steps[curind]);
                        upperBounds[curind] = -lowerBounds[curind];
                        cnt[curind]         = lowerBounds[curind] - 1;
                    }
                    else
                    {
                        PhasePoint curpoint = new PhasePoint(center, steps, cnt);
                        grid.Add(curpoint, null);
                        curind--;
                    }
                }
            }

            return(grid);
        }
Esempio n. 7
0
            public double chi(double t, PhasePoint x)
            {
                double norm = Math.Sqrt(x
                                        .Select(a => a * a)
                                        .Sum());

                if (norm > r)
                {
                    return((norm / r) - 1);
                }
                else
                {
                    return(0);
                }
            }
Esempio n. 8
0
        public static Grid SegmentGrid(PhasePoint beginning, PhasePoint end, PhasePoint steps)
        {
            Grid grid = new Grid();

            grid.Steps = steps;

            PhasePoint currentpoint = beginning;

            while (currentpoint.CompareTo(end) != 0)
            {
                grid.Add(currentpoint, null);
                currentpoint += steps;
            }

            return(grid);
        }
Esempio n. 9
0
        // Фабрики

        // Сетка, накинутая на прямоугольный параллелепипед
        public static Grid BoxGrid(PhasePoint ld, PhasePoint sizes, PhasePoint steps)
        {
            Grid grid = new Grid();

            grid.Steps = steps;

            int[]
            upperBounds = new int[ld.Dim],
            cnt = new int[ld.Dim];
            for (int i = 0; i < ld.Dim; i++)
            {
                upperBounds[i] = (int)(sizes[i] / steps[i]);
                cnt[i]         = 0;
            }

            int curind;

            do
            {
                PhasePoint curpoint = new PhasePoint(ld, steps, cnt);
                grid.Add(curpoint, null);

                curind = 0;
                cnt[curind]++;
                while (curind < ld.Dim && cnt[curind] > upperBounds[curind])
                {
                    cnt[curind] = 0;
                    curind++;
                    if (curind < ld.Dim)
                    {
                        cnt[curind]++;
                    }
                }
            } while (curind < ld.Dim);

            return(grid);
        }
Esempio n. 10
0
        // Может быть, еще что-то...

        public bool GetNeighbour(PhasePoint p1, int coordIdx, int dir, out PhasePoint neigh)
        {
            neigh = new PhasePoint(p1);
            neigh.coords[coordIdx] += Steps[coordIdx] * dir;
            return(ContainsKey(neigh));
        }
Esempio n. 11
0
        static public void Main(string[] args)
        {
            string DLLPath = "./bin/Debug/netcoreapp2.1/";
            string inPath  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string outPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            double t1    = 0;
            double T     = 5;
            double delta = 0.025;
            int    nT    = (int)Math.Ceiling((T - t1) / delta);

            double[] time = new double[nT + 1];
            for (int i = 0; i <= nT; i++)
            {
                time[nT - i] = T - i * delta;
            }

/*      List<string> inArgs = new List<string>();
 *    using (StreamReader sr = new StreamReader(Path.Combine(inPath, "In.txt")))
 *    {
 *      string line;
 *      while ((line = sr.ReadLine()) != null)
 *      {
 *        inArgs.Add(line);
 *      }
 *    }
 *
 *    double T = Convert.ToDouble(inArgs[0]);
 *    double t1 = Convert.ToDouble(inArgs[1]);
 *    double delta = Convert.ToDouble(inArgs[2]);
 *    int nT = (int)Math.Ceiling((T - t1) / delta);
 *    double[] time = new double[nT+1];
 *    for (int i = 0; i <= nT; i++)
 *    {
 *      time[nT - i] = T - i * delta;
 *    }
 *
 *    PhasePoint
 *      center = new PhasePoint(new List<double> {Convert.ToDouble(inArgs[4]),
 *        Convert.ToDouble(inArgs[5])}),
 *      xSteps = new PhasePoint(new List<double> {Convert.ToDouble(inArgs[7]),
 *        Convert.ToDouble(inArgs[8])}),
 *      pSteps = xSteps,
 *      qSteps = new PhasePoint(new List<double> {Convert.ToDouble(inArgs[19]),
 *        Convert.ToDouble(inArgs[20])});
 *    double radiusX = Convert.ToDouble(inArgs[6]);
 *    double radiusP = Convert.ToDouble(inArgs[12]);
 *    double radiusQ = Convert.ToDouble(inArgs[18]);
 *    if (inArgs[3] == "Ball")
 *    {
 *      Grid grid = Grid.BallGrid(center, Convert.ToDouble(inArgs[0]), xSteps);
 *    }
 *    if (inArgs[9] == "Ball")
 *    {
 *      ControlConstraints P = ControlConstraints.BallConstraints(center, radiusP, pSteps);
 *    }
 *    if (inArgs[15] == "Ball")
 *    {
 *      ControlConstraints Q = ControlConstraints.BallConstraints(center, radiusQ, qSteps);
 *    }
 */
            LibLinking <IDynamics> loader = new LibLinking <IDynamics>();
            IDynamics dynam = loader.FindLib(DLLPath + "SimpleMotion.dll");

            double finalR = 1.0;
            double finalT = T;

//      LibLinking<IChi> chi1loader = new LibLinking<IChi>();
//      IChi chi1 = chi1loader.FindLib(DLLPath + "CylinderSet.dll", finalR, finalT);

//      LibLinking<ITheta> theta1loader = new LibLinking<ITheta>();
//      ITheta theta1 = theta1loader.FindLib(DLLPath + "CylinderSet.dll", finalR, finalT);

/*      double t1 = 0;
 *    double T = 5;
 *    double delta = 0.1;
 *    int nT = (int)Math.Ceiling((T - t1) / delta);
 *    double[] time = new double[nT+1];
 *    for (int i = 0; i <= nT; i++)
 *    {
 *      time[nT - i] = T - i * delta;
 *    }
 */
            LibLinking <IChi> chiloader = new LibLinking <IChi>();
            IChi chi = chiloader.FindLib(DLLPath + "CylinderSet_NoConstraints.dll", finalR);

            LibLinking <ITheta> thetaloader = new LibLinking <ITheta>();
            ITheta theta = thetaloader.FindLib(DLLPath + "CylinderSet_NoConstraints.dll", finalR);

            /*          ControlConstraints cc =
             *            ControlConstraints.BallConstraints(new PhasePoint(2), 1,
             *                new PhasePoint(new List<double> {0.1, 0.1}));
             *        Grid g = Grid.BallGrid(new PhasePoint(2), 1,
             *            new PhasePoint(new List<double> {0.1, 0.1}));
             *
             *        PhasePoint
             *            u = new PhasePoint(new List<double> {1, 0}),
             *            v = new PhasePoint(new List<double> {-2, 1}),
             *            speed = dynam.f(0, new PhasePoint(), u, v);
             *        Console.WriteLine(speed);
             */

            PhasePoint
                center = new PhasePoint(new List <double> {
                0, 0
            }),
                xSteps = new PhasePoint(new List <double> {
                0.1, 0.1
            }),
                pSteps = xSteps,
                qSteps = new PhasePoint(new List <double> {
                0.1, 0.1
            });
            double             radiusX = 4.6;
            double             radiusP = 1;
            double             radiusQ = 0.5;
            Grid               grid    = Grid.BallGrid(center, radiusX, xSteps);
            ControlConstraints P       = ControlConstraints.BallConstraints(center, radiusP, pSteps);
            ControlConstraints Q       = ControlConstraints.BallConstraints(center, radiusQ, qSteps);

            foreach (PhasePoint x in grid.Keys.ToList())
            {
                grid[x] = new SortedDictionary <double, double> {
                    { T, chi.chi(T, x) }
                }
            }
            ;

            int length = time.Length;

            /*     Parallel.For(0, length  , i =>
             *   {
             *     foreach (PhasePoint x in grid.Keys)
             *     {
             *       double velocity = Minmax(dynam, time[length - i], x, P, Q, grid);
             *       grid[x].Add(time[length - i - 1], Math.Max(Math.Min(chi.chi(time[length - i - 1], x),
             *           grid[x][time[length - i]] + (time[length - i] - time[length - i - 1]) * velocity),
             *         theta.theta(time[length - i - 1], x)));
             *     }
             *   });*/


            for (int i = time.Length - 1; i > 0; i--)
            {
                foreach (PhasePoint x in grid.Keys)
                {
                    double velocity = Minmax(dynam, time[i], x, P, Q, grid);
                    grid[x].Add(time[i - 1], Math.Max(Math.Min(chi.chi(time[i - 1], x),
                                                               grid[x][time[i]] + (time[i] - time[i - 1]) * velocity),
                                                      theta.theta(time[i - 1], x)));
                }
            }

            using (StreamWriter outputFile = new StreamWriter(Path.Combine(outPath, "Result6.txt")))
            {
//        int i = 1;
                foreach (PhasePoint x in grid.Keys)
                {
                    foreach (double t in grid[x].Keys)
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                        //outputFile.WriteLine(i+") "+"Coordinates:" + " " + x + " " + "Time:" + " " + t + " " + "Result:" + " "+ Math.Round(grid[x][t], 8));
                        outputFile.WriteLine(x.First() + " " + x.Last() + " " + t + " " + Math.Round(grid[x][t], 8));
//            i++;
                    }
                }
            }

            Console.WriteLine(grid);
            Console.ReadKey();
        }
    }
Esempio n. 12
0
 public double theta(double t, PhasePoint x)
 {
     return(0);
 }
Esempio n. 13
0
 public double sigma(PhasePoint x)
 {
     return(Math.Sqrt(x
                      .Select(a => a * a)
                      .Sum()));
 }