Пример #1
0
        /// <summary>
        /// Learns a model that can map the given inputs to the given outputs by using (gradient free) optimization algorithm
        /// </summary>
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="gradientFreeSolver">The optimization solver<paramref name="gradientFreeSolver">inputs</paramref>.</param>
        /// <returns>
        /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />.
        /// </returns>
        public NonlinearRegression Learn(Double[][] x, double[] y, BaseOptimizationMethod gradientFreeSolver)
        {
            NonlinearRegression problemFormulation = null;

            Func <double[][], double[], double[], double> error = ErrorFunction;
            var errorForInputAndOutput = error.Curry()(x)(y);

            gradientFreeSolver.Function          = errorForInputAndOutput;
            gradientFreeSolver.NumberOfVariables = this.NumberOfParameters;

            bool success = gradientFreeSolver.Minimize();

            problemFormulation = new NonlinearRegression(gradientFreeSolver.NumberOfVariables, this.Function);

            for (int idx = 0; idx < gradientFreeSolver.Solution.Length; idx++)
            {
                problemFormulation.Coefficients.SetValue(gradientFreeSolver.Solution[idx], idx);
            }

            return(problemFormulation);
        }
Пример #2
0
        public Network(double width, double height, ArrayList users, BaseOptimizationMethod mehod, Canvas canvas)
        {
            this.users                    = users;
            this.mehod                    = mehod;
            this.width                    = width;
            this.height                   = height;
            this.canvas                   = canvas;
            this.lbp                      = new ArrayList();
            this.ubp                      = new ArrayList();
            this.InitialParameters        = new ArrayList();
            this.mehod.FitnessFunction    = FitnessFunction;
            this.mehod.GenerationCreated += ShowAntibodies;
            X = (int)this.width / (R[R.Length - 1] * 2);
            Y = (int)this.height / (R[R.Length - 1] * 2);
            this.actualRegionindex = 0;
            for (int i = 0; i < Y; i++)
            {
                for (int j = 0; j < X; j++)
                {
                    int         heightDown = i * (R[R.Length - 1] * 2);
                    int         heightUp   = (i + 1) * (R[R.Length - 1] * 2);
                    int         widhtDown  = j * (R[R.Length - 1] * 2);
                    int         widhtUp    = (j + 1) * (R[R.Length - 1] * 2);
                    List <User> regionUser = new List <User>();
                    foreach (Point user in this.users)
                    {
                        if (user.X >= widhtDown && user.X <= widhtUp && user.Y >= heightDown && user.Y <= heightUp)
                        {
                            regionUser.Add(new User()
                            {
                                position = user, isCover = false
                            });
                        }
                    }
                    Region region = new Region(actualRegionindex++, new Point(j, i), regionUser.ToArray(), 0, heightDown, heightUp, widhtDown, widhtUp);
                    region.Towers = new Tower[1];
                    int r = (int)(Math.Sqrt((R[R.Length - 1] * R[R.Length - 1]) + (R[R.Length - 1] * R[R.Length - 1])) / 2);
                    region.Towers[0] = new Tower(new Point(widhtDown + R[R.Length - 1], heightDown + R[R.Length - 1]), r * 2, regionUser.Count);
                    regions.Add(region);
                }
            }
            this.actualRegionindex = 0;
            List <int> neighbors;

            foreach (Region region in this.regions)
            {
                neighbors = new List <int>();
                foreach (Region neighbor in this.regions)
                {
                    if (neighbor.pos.Y == region.pos.Y - 1)
                    {
                        if (neighbor.pos.X == region.pos.X - 1 || neighbor.pos.X == region.pos.X || neighbor.pos.X == region.pos.X + 1)
                        {
                            neighbors.Add(neighbor.id);
                        }
                    }
                    else
                    if (neighbor.pos.Y == region.pos.Y)
                    {
                        if (neighbor.pos.X == region.pos.X - 1 || neighbor.pos.X == region.pos.X + 1)
                        {
                            neighbors.Add(neighbor.id);
                        }
                    }
                    else
                    if (neighbor.pos.Y == region.pos.Y + 1)
                    {
                        if (neighbor.pos.X == region.pos.X - 1 || neighbor.pos.X == region.pos.X || neighbor.pos.X == region.pos.X + 1)
                        {
                            neighbors.Add(neighbor.id);
                        }
                    }
                }
                region.Neighbor = neighbors.ToArray();
            }
        }