示例#1
0
        public PlayAction Decide(World world)
        {
            // Convert world to model data
            var worldValue = WorldToValue();

            PlayAction action = PlayAction.NOP;
            int        odd    = rnd.Next(100);

            if (odd >= world.Epsilon)
            {
                // Trace.WriteLine("Calculated Action");

                inputDataMap[inputVariable] = Value.CreateBatch <float>(model.Arguments[0].Shape, worldValue, device);
                outputDataMap[model.Output] = null;

                model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);

                var output = outputDataMap[model].GetDenseData <float>(model)[0];

                // Convert output to PlayAction
                int maxIndex = CNTKHelper.CNTKHelper.ArgMax(output);
                if (maxIndex == -1)
                {
                    return(PlayAction.NOP);
                }
                action = (PlayAction)maxIndex;
            }
            else
            {
                //Trace.WriteLine("Random Action");
                action = RandomAgent.GetRandomAction();
            }
            return(action);
        }
示例#2
0
        public PlayAction Decide(World world)
        {
            PlayAction action = PlayAction.NOP;

            // Find closest coin
            if (world.Coins.Count == 0)
            {
                return(action);
            }

            if (path == null)
            {
                var coin = world.Coins.Select(c => new { c, Dist = (world.Pacman.Position.X - c.Position.X) * (world.Pacman.Position.X - c.Position.X) + (world.Pacman.Position.Y - c.Position.Y) * (world.Pacman.Position.Y - c.Position.Y) }).OrderBy(c => c.Dist).First().c;

                var dx = coin.Position.X - world.Pacman.Position.X;
                var dy = coin.Position.Y - world.Pacman.Position.Y;

                if (Math.Abs(dx) > Math.Abs(dy))
                {
                    action = dx > 0 ? PlayAction.Right : PlayAction.Left;
                }
                else
                {
                    action = dy > 0 ? PlayAction.Down : PlayAction.Up;
                }

                if (world.CanGo(action))
                {
                    return(action);
                }
                else
                {
                    // Find best route using graph
                    var nodes = Graph.FindShortestRoute(world.Pacman.Position, coin.Position, SearchAlgorithm.Dijkstra);
                    if (nodes == null)
                    {
                        return(RandomAgent.GetRandomAction());
                    }

                    nodes.RemoveAt(0);
                    path = new Queue <INode <Position> >(nodes);
                    var node = path.Dequeue();

                    if (path.Count == 0)
                    {
                        path = null;
                    }
                    else
                    {
                        GameObject.Renderer.DrawPath(path);
                    }

                    if (world.Pacman.Position.X - node.Value.X > 0)
                    {
                        return(PlayAction.Left);
                    }
                    if (world.Pacman.Position.X - node.Value.X < 0)
                    {
                        return(PlayAction.Right);
                    }
                    if (world.Pacman.Position.Y - node.Value.Y > 0)
                    {
                        return(PlayAction.Up);
                    }
                    if (world.Pacman.Position.Y - node.Value.Y < 0)
                    {
                        return(PlayAction.Down);
                    }
                }
            }
            else
            {
                var node = path.Dequeue();

                if (path.Count == 0)
                {
                    path = null;
                }
                else
                {
                    GameObject.Renderer.DrawPath(path);
                }

                if (world.Pacman.Position.X - node.Value.X > 0)
                {
                    return(PlayAction.Left);
                }
                if (world.Pacman.Position.X - node.Value.X < 0)
                {
                    return(PlayAction.Right);
                }
                if (world.Pacman.Position.Y - node.Value.Y > 0)
                {
                    return(PlayAction.Up);
                }
                if (world.Pacman.Position.Y - node.Value.Y < 0)
                {
                    return(PlayAction.Down);
                }
            }
            return(action);
        }