コード例 #1
0
        public double Run()
        {
            var solution = hillClimb.Solve();

            Result         = smallestBoundary.Objective(solution);
            OriginalLength = smallestBoundary.Objective(smallestBoundary.points);
            return(Result);
        }
コード例 #2
0
        public void Init(string path, Random gen)
        {
            smallestBoundary = new SmallestBoundaryPolygon();
            smallestBoundary.LoadPointsFromFile(path);

            hillClimb = new HillClimber <List <Vector2> >();

            hillClimb.gen           = gen;
            hillClimb.Epsilon       = 10;
            hillClimb.MaxIterations = 1000;

            hillClimb.Fitness = (solution) => smallestBoundary.Objective(solution);
            hillClimb.LowerFitnessIsBetter();

            hillClimb.RandomStart = () =>
            {
                var    points = new List <Vector2>();
                double rangeX = HighestPoint.X - LowestPoint.X;
                double rangeY = HighestPoint.Y - LowestPoint.Y;

                foreach (var point in smallestBoundary.points)
                {
                    var x = (gen.NextDouble() * rangeX) + LowestPoint.X;
                    var y = (gen.NextDouble() * rangeY) + LowestPoint.Y;
                    points.Add(new Vector2((float)x, (float)y));
                }

                while (smallestBoundary.Constraint(points) < 0)
                {
                    points.Clear();
                    foreach (var point in smallestBoundary.points)
                    {
                        var x = (gen.NextDouble() * rangeX) + LowestPoint.X;
                        var y = (gen.NextDouble() * rangeY) + LowestPoint.Y;
                        points.Add(new Vector2((float)x, (float)y));
                    }
                }

                return(points);
            };

            hillClimb.TakeValidStep = (state) =>
            {
                var newState = new List <Vector2>();
                foreach (var point in state)
                {
                    var x = gen.Next(-1, 1);
                    var y = gen.Next(-1, 1);
                    var v = new Vector2(x, y);

                    v  = Vector2.Normalize(v);
                    v *= (float)hillClimb.Epsilon;
                    v += point;

                    newState.Add(v);
                }
                return(newState);
            };
        }