示例#1
0
        public QState GetState()
        {
            var p = PositionToState(transform.position);
            var g = PositionToState(Goal.Position);

            _grid.Populate(bounds => {
                var ray = new Ray(new Vector3(bounds.center.x, 2, bounds.center.z), Vector3.down);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 3.0f))
                {
                    return(hit.collider.gameObject == Goal.Instance.gameObject ? 1f : 0.1f);
                }
                return(0f);
            });
            var dead = !IsAboveGround();
            var goal = p.SequenceEqual(g);
            var line = VectorToGoal(transform.position, Goal.Position);
            var v    = line.normalized;

            _linearState.At(0, v.x);
            _linearState.At(1, v.z);

            var terminal = dead || goal || DetectCycle();
            var spatial  = Matrix <float> .Build.Dense(1, 1, 1f);

            var state = new QState(
                new [] { spatial },
                _linearState.Clone(),
                goal ? 1 : 0,
                terminal
                );

            return(state);
        }
示例#2
0
        public QState GetState()
        {
            var   winner = _ball.IsTerminal();
            float reward;
            bool  terminal;
            //var terminal = winner.HasValue;
            //var reward = terminal ? (winner.Value == Side ? 1 : 0) : 0;
            var b          = PongGame.BoundsFromTransform(_ball.transform);
            var controller = PongGame.BoundsFromTransform(transform);

            controller.size += new Vector3(0.2f, 0, 0);
            PongGame.DebugDrawBounds(controller, Color.blue);
            if (b.Intersects(controller))
            {
                reward   = 1;
                terminal = true;
                if (PongBenchmark.Running)
                {
                    terminal = winner.HasValue;
                }
            }
            else
            {
                terminal = winner.HasValue;
                reward   = terminal ? (winner.Value == Side ? 1 : 0) : 0;
            }

            var bp  = _ball.transform.position;
            var rbp = bp - transform.position;

            var gbp = _grid.Locate(bp);
            var bpy = gbp.HasValue ? gbp.Value.y : -1;

            _grid.Populate((bo, c) => {
                //var x = bo.center.x;
                //var v = bo.Contains(new Vector3(x, _game.Border.min.y)) || bo.Contains(new Vector3(x, _game.Border.max.y)) ? 0.3f : 0; // walls
                var ham = gbp.HasValue ? HammingDistance(gbp.Value, c) : int.MaxValue; // Hamming distance
                var v   = ham <= 0 ? 1f : ham <= 1 ? 0.5f : 0;                         // ball
                //v = bo.Contains(bp + _ball.Velocity.normalized * 2) ? 150f : v;
                //v = bo.Intersects(controller) ? 100 : v;
                return(v);
            });

            var state = _grid.Matrix;

            return(new QState(new[] { state }, _vect.Clone(), reward, terminal));
        }