コード例 #1
0
        private static IList <Vector> CreateGeneration(NodeGrid nodeGrid, IEnumerable <Vector> startingVectors)
        {
            const int GEN_SIZE = 50;
            const int STEPS    = 5;

            var gen = new List <Vector>();

            for (var i = 0; i < GEN_SIZE; i++)
            {
                var vector = startingVectors.Random();
                for (var j = 0; j < STEPS; j++)
                {
                    vector = vector
                             .GenerateAllNextMoves()
                             .Where(m => nodeGrid.IsValidNode(m.End))
                             ?.Random();

                    if (vector == null)
                    {
                        break;
                    }
                }

                if (vector != null)
                {
                    gen.Add(vector);
                }
            }

            return(gen);
        }
コード例 #2
0
        private static void ExtractNextGen(NodeGrid gridz, IList <Vector> moves, int gen)
        {
            var nextGenMoves = new List <Vector>();

            foreach (var nextMove in moves)
            {
                var vectors = nextMove.GenerateAllNextMoves()
                              .Where(z => gridz.IsValidNode(z.End) && !z.HasRepeatedAncestor(new List <Coordinate>())).ToList();
                nextGenMoves.AddRange(vectors);
            }

            nextGenMoves = nextGenMoves.ToList();
            Console.SetCursorPosition(85, 0);
            Console.WriteLine($"Move: {gen} [{nextGenMoves.Count}]");

            nextGenMoves = nextGenMoves.OrderBy(a => Guid.NewGuid()).Take(500).ToList();
            foreach (var nextGenMove in nextGenMoves)
            {
                Console.SetCursorPosition(nextGenMove.EndX, nextGenMove.EndY);
                Console.BackgroundColor = ConsoleColor.DarkYellow;
                Console.Write(" ");
            }

//            Console.ReadLine();

            Parallel.ForEach(moves, nextMove =>
            {
                if (gridz.GetAt(nextMove.EndX, nextMove.EndY).TrackType == NodeType.End)
                {
                    SuccessPaths.Add(nextMove);
                    var parent = nextMove.Parent;
                    do
                    {
                        Console.SetCursorPosition(parent.EndX, parent.EndY);
                        Console.BackgroundColor = ConsoleColor.Magenta;
                        Console.Write(" ");
                        Thread.Sleep(100);
                        parent = parent.Parent;
                    } while (parent != null);
                }
            });

            ExtractNextGen(gridz, nextGenMoves, ++gen);
        }
コード例 #3
0
        public static Fitness FindClosestWinner(this NodeGrid grid, IList <Move> movelist)
        {
            var distance = 0;
            var final    = movelist.Last().End;

            var node = grid.GetAt(final);

            if (node.IsWinner)
            {
                return(new Fitness
                {
                    ClosestNode = node,
                    Distance = distance,
                    Movelist = movelist
                });
            }

            distance++;

            for (var i = distance; i < MAX_DISTANCE; i++)
            {
                var winner = GetNeighbours(final, i).FirstOrDefault(m => grid.IsValidNode(m) && grid.GetAt(m).IsWinner);
                if (winner != null)
                {
                    return(new Fitness
                    {
                        ClosestNode = grid.GetAt(winner),
                        Distance = i,
                        Movelist = movelist
                    });
                }
            }

            return(new Fitness()
            {
                Movelist = movelist
            });
        }
コード例 #4
0
ファイル: GenerationService.cs プロジェクト: zeppu/Hackaton
 public GenerationService(Random random, NodeGrid grid)
 {
     _random = random;
     _grid   = grid;
 }
コード例 #5
0
        private static void Main(string[] args)
        {
            //			var x = new Coordinate(25, 25);
            //			DrawCoordinate(x, ConsoleColor.DarkRed, "0");
            //			Console.ReadLine();
            //
            //			var movement = new Movement(0, 0);
            //			var i = 1;
            //
            ////		    var points = new List<Coordinate> {x};
            //
            //			var move = x * movement;
            //			while (true)
            //			{
            //				move = move.GetNextMoves()
            //					.OrderBy(m => Guid.NewGuid())
            //					.First();
            //
            ////                moves = KnownMovements.Values.SelectMany(m => moves.Select(n=> n.NextMove(m))).Where(m => m != Movement.Empty).Distinct().ToList();
            ////		        points = points.SelectMany(m => moves.Select(n => m * n)).Distinct().OrderBy(m => Guid.NewGuid()).Take(1).ToList();
            //			    DrawCoordinate(move.End, ConsoleColor.Cyan, (i % 10).ToString());
            //				i++;
            //
            //				Console.ReadLine();
            //			}


            var grid =
                "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000000011111111100000000000000000000000000000000000000000000000000000000000000000000001111111111000000000000000000000000000000000000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000000000000000000000001111111111110000000000000000000000000000000000000000000000000000000000000000000011110011111100000000000000000000000000000000000000000000000000000000000000000000111100011111000000000011111100000000000000000000000000000000000000000000000000001111000111110000000001111111110000000000000000000000000000000000000000000000000111110001111100000001111111111110000000000000000000000000000000000000000000000001111100011111000001111111111111110000000000000000000000000000000000000000000000011111000111111101111111111111111111100000000000000000000000000000000000000000000111110000111111111111100000000011111111100000000000000000000000000000000000000001111100001111111111110000000000011111111111100000000000000000000000000000000000011111000001111111110000000000000001111111111111111000000000000000000000000000000111110000001111110000000000000000000111111111111111111111100000000000000000000001111110000000000000000000000000000000001111111111111111111111100000000000000000011111100000000000000000000000000000000000000011111111111111111111000000000000000111111000000000000000000000000000000000000000000000000011111111111100000000000001111111000000000000000000000000000000000000000000000000000001111111100000000000011111110000000000000000000000000000000000000000000000000000000111111100000000000111111100000000000000000000000000000000000000000000000000000000011111000000000001111111000000000000000000000000000000000000000000000000000000000011111000000000001111110000000000000000000000000000000000000000000000000000000000111110000000000011111100000000000000000000000000000000000000000000000000000000000111100000000000111111000000000000000000000000000000000000000000000000000000000011110000000000001111110000000000000000000000000000000000000000000000000000000000111100000000000011111100000000000000000000000000000000000000000000000000000000011111000000000000011111000000000000000000000000000000000000000000000000000000011111100000000000000111110000000000000000000000000000000000000000000000000000011111110000000000000001111100000000000000000000000000000000000000000000000000001111111000000000000000011111100000000000000000000000000000000000000000000000001111111100000000000000000111111000000000000000000000000000000000000000000000000111111100000000000000000001111110000000000000000000000000000000000000000000000011111110000000000000000000001111110000000000000000000000000000000000000000000001111111100000000000000000000011111111000000000000000000000000000000000000000000011111110000000000000000000000011111111100000000000000000000000000000000000000000111111000000000000000000000000111111111110000000000000000000000000000000000000001111110000000000000000000000000111111111110000000000000000000000000000000000000011111100000000000000000000000000111111111110000000000000000000000000000000000000111110000000000000000000000000000011111111100000000000000000000000000000000000011111100000000000000000000000000000000111111100000000000001110000000000000000000111111000000000000000000000000000000001111111000000000001111111100000000000000011111110000000000000000000000000000000001111111000000001111111111111000200000031111111100000000000000000000000000000000001111110000000111111111111111112000000311111110000000000000000000000000000000000001111110000111111000000111111120000003111111100000000000000000000000000000000000001111111111111000000000011111200000031111111000000000000000000000000000000000000001111111111100000000000000112000000311111100000000000000000000000000000000000000011111111111000000000000000000000003111110000000000000000000000000000000000000000011111111100000000000000000000000031100000000000000000000000000000000000000000000001111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            // "000000000000000000000000000000000000000021110000000000000000000000000000000000002111111000000000000000000000000000000000211111111000000000000000000000000000000021111111110000000000000000000000000000002111111111100000000000000000000000000000200000111110000000000000000000000000000000000001111100000000000000011111000000000000000011111000000000001111111111100000000000000111100000000001111111111111110000000000011110000000001111111111111111130000000001111100000001111111111111111113000000000111110000000111111111111111111300000000001111000000111111111111111111130000000000111100000011111111111111111113000000000011110000011111110000000000000000000000001111000001111100000000000000000000000000111100000111110000000000000000000000000011110000011111000000000000000000000000001111000001111100000000000000000000000000111110001111110000000000000000000000000011111000111111000000000000000000000000001111100011111000000000000000000000000000011111111111100000000000000000000000000000111111111110000000000000000000000000000001111111110000000000000000000000000000000001111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            //"000000000000000000000000000000000000000000000000000000000000222222222222222222222222222222222222222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111333333333333333333333333333333333333333333333333333333333333000000000000000000000000000000000000000000000000000000000000";
            const int COLS = 80;
            const int ROWS = 60;

            var nodelist = NodeGenerator.GenerateNodes(grid, COLS, ROWS);

            var nodeGrid = new NodeGrid(nodelist, COLS, ROWS);

            DrawGrid(nodelist);

            Console.BackgroundColor = ConsoleColor.Black;

            var startingNodes = nodeGrid.Nodes.Where(m => m.TrackType == NodeType.Start).ToList();

            startingNodes.ForEach(m => m.PathLength = 0);

            var neighbours = startingNodes;

            do
            {
                neighbours = neighbours.SelectMany(m =>
                {
                    var nodes = m.GetNeighbours(nodeGrid);
                    return(nodes.UpdateIfClosest(m));
                }).ToList();

                //                DrawCoordinates(neighbours.Select(m => m.Position).ToList(), ConsoleColor.Blue);
                //                Thread.Sleep(10);
                //                Console.ReadLine();
            } while (neighbours.All(m => m.TrackType != NodeType.End));

            var winner = neighbours.First(m => m.TrackType == NodeType.End);

            var prev       = winner;
            var winnerPath = new List <Node>();

            while (prev != null)
            {
                winnerPath.Add(prev);
                prev = prev.PreviousNode;
            }

            winnerPath.Reverse();
            winnerPath.ForEach(m => m.IsWinner = true);

            DrawCoordinates(winnerPath.Select(m => m.Position), ConsoleColor.Magenta);

            //            var startingPoints = nodelist.Where(m => m.TrackType == NodeType.Start)
            //                .Select(m => new Vector(m.Position, m.Position, null)).ToList();
            //
            //            ExtractNextGen(nodeGrid, startingPoints, 0);

            Console.SetCursorPosition(0, 60);
            Console.BackgroundColor = ConsoleColor.Black;
            Console.WriteLine(winner.PathLength);

            //
            //            string input;
            //
            //            do
            //            {
            //                input = Console.ReadLine();
            //                if (string.IsNullOrWhiteSpace(input))
            //                    continue;
            //
            //                var points = input.Split(",", StringSplitOptions.RemoveEmptyEntries);
            //                Coordinate c = (int.Parse(points[0]), int.Parse(points[1]));
            //                DrawCoordinate(c, ConsoleColor.Cyan);
            //                var closestWinner = nodeGrid.FindClosestWinner(c);
            //                DrawCoordinate(closestWinner.node.Position, ConsoleColor.DarkGreen, closestWinner.distance.ToString());
            //            } while (!string.IsNullOrWhiteSpace(input));



            //			var gen = CreateGeneration(nodeGrid, startingNodes);
            //
            //			var valueTuples = gen.Select(m => nodeGrid.FindClosestWinner(m)).OrderByDescending(m => m.Value).Take(5)
            //				.ToList();
            //			DrawCoordinates(valueTuples.Select(m => m.Point.End), ConsoleColor.DarkCyan, delay: 100);
            //
            //			IList<Vector> generation;
            //
            //			do
            //			{
            //				generation = CreateGeneration(nodeGrid, valueTuples.Select(m => m.Point));
            //
            //				valueTuples = generation.Select(m =>
            //					nodeGrid.FindClosestWinner(m))
            //					.Where(m => m != null)
            //					.OrderByDescending(m => m.Value)
            //					.Take(5)
            //					.ToList();
            //				DrawCoordinates(valueTuples.Select(m => m.Point.End), ConsoleColor.DarkYellow, delay: 20);
            //			} while (generation.All(m => nodeGrid.GetAt(m.End).TrackType != NodeType.End));
            //
            //			var winningPath = generation.FirstOrDefault(m => nodeGrid.GetAt(m.End).TrackType != NodeType.End);
            //
            //			var item = winningPath;
            //			var items = new List<Vector>();
            //			do
            //			{
            //				items.Add(item);
            //				item = item.Parent;
            //
            //			} while (item != null);
            //
            //			Console.SetCursorPosition(0, 60);
            //			Console.WriteLine(items.Count);
            //			Console.WriteLine(winningPath.MoveCount);
            //			DrawCoordinates(items.Select(m => m.End).Reverse(), ConsoleColor.DarkGreen, delay: 100);

            var generator  = new GenerationService(RandomGenerator, nodeGrid);
            var generation = generator.Generate(50, 5);

            var moveLists = generation.Individuals.Select(m => m.MoveList).ToList();

//
//            var moveLists = new List<IList<Move>>();
//
//            //var startingPositions = startingNodes.Select(m => m.Position).ToList();
//            var startingPositions = new List<Coordinate>
//            {
//                (25, 25)
//            };
//
//            DrawCoordinates(startingPositions, ConsoleColor.Red);
//
//            for (var i = 0; i < 50; i++)
//            {
//                var startingPoint = startingPositions.Random();
//
//                IList<int> gene = new List<int>();
//
//                for (var j = 0; j < 10; j++)
//                {
//                    gene.Add(RandomGenerator.Next(0, 8));
//                }
//
//                moveLists.Add(startingPoint * gene);
//            }

            DrawCoordinates(moveLists.Select(m => m.Last().End), ConsoleColor.Cyan, delay: 10);

//            foreach (var moveList in moveLists)
//            {
//                DrawCoordinates(moveList.Select(m=> m.End).ToList(), ConsoleColor.Blue, delay: 10);
//                Thread.Sleep(50);
//            }

            var fitnessGenerator = new FitnessFunctionEvaluator(nodeGrid);

            var health = generation.Individuals.Select(m => fitnessGenerator.GetFitness(m))
                         .OrderByDescending(m => m.Fitness).Cast <IndividualFitness>().ToList();

            DrawCoordinates(
                health.OrderByDescending(m => m.DistanceFromStart)
                .Select(m => m.Individual.MoveList[m.MoveCount - 1].End).Take(5), ConsoleColor.DarkRed, delay: 100);
//
//            var results = moveLists.Select(m => nodeGrid.FindClosestWinner(m)).OrderByDescending(m => m.Value).ToList();
//
//            foreach (var moveList in results)
//            {
//                var last = moveList.Movelist.Last();
//                DrawCoordinate(last.End, ConsoleColor.Blue, moveList.Movelist.Count.ToString());
//                Thread.Sleep(200);
//            }
//
//            var fittest = results.Take(10);
//            foreach (var moveList in fittest)
//            {
//                var last = moveList.Movelist.Last();
//                DrawCoordinate(last.End, ConsoleColor.Red, moveList.Movelist.Count.ToString());
//                Thread.Sleep(200);
//            }


            Console.ReadLine();
        }
コード例 #6
0
 public void EvaluateAgainstGrid(NodeGrid grid)
 {
 }
コード例 #7
0
 public FitnessFunctionEvaluator(NodeGrid grid)
 {
     _grid = grid;
 }