File size: 2,690 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using Unity.MLAgents.Actuators;
using UnityEngine;
using UnityEngine.Serialization;

namespace Unity.MLAgents.Integrations.Match3
{
    /// <summary>
    /// Actuator component for a Match3 game. Generates a Match3Actuator at runtime.
    /// </summary>
    [AddComponentMenu("ML Agents/Match 3 Actuator", (int)MenuGroup.Actuators)]
    public class Match3ActuatorComponent : ActuatorComponent
    {
        [HideInInspector, SerializeField, FormerlySerializedAs("ActuatorName")]
        string m_ActuatorName = "Match3 Actuator";

        /// <summary>
        /// Name of the generated Match3Actuator object.
        /// Note that changing this at runtime does not affect how the Agent sorts the actuators.
        /// </summary>
        public string ActuatorName
        {
            get => m_ActuatorName;
            set => m_ActuatorName = value;
        }

        [HideInInspector, SerializeField, FormerlySerializedAs("RandomSeed")]
        int m_RandomSeed = -1;

        /// <summary>
        /// A random seed used in the actuator's heuristic, if needed.
        /// </summary>
        public int RandomSeed
        {
            get => m_RandomSeed;
            set => m_RandomSeed = value;
        }

        [HideInInspector, SerializeField, FormerlySerializedAs("ForceHeuristic")]
        [Tooltip("Force using the Agent's Heuristic() method to decide the action. This should only be used in testing.")]
        bool m_ForceHeuristic;

        /// <summary>
        /// Force using the Agent's Heuristic() method to decide the action. This should only be used in testing.
        /// </summary>
        public bool ForceHeuristic
        {
            get => m_ForceHeuristic;
            set => m_ForceHeuristic = value;
        }

        /// <inheritdoc/>
        public override IActuator[] CreateActuators()
        {
            var board = GetComponent<AbstractBoard>();
            if (!board)
            {
                return Array.Empty<IActuator>();
            }

            var seed = m_RandomSeed == -1 ? gameObject.GetInstanceID() : m_RandomSeed + 1;
            return new IActuator[] { new Match3Actuator(board, m_ForceHeuristic, seed, m_ActuatorName) };
        }

        /// <inheritdoc/>
        public override ActionSpec ActionSpec
        {
            get
            {
                var board = GetComponent<AbstractBoard>();
                if (board == null)
                {
                    return ActionSpec.MakeContinuous(0);
                }

                var numMoves = Move.NumPotentialMoves(board.GetMaxBoardSize());
                return ActionSpec.MakeDiscrete(numMoves);
            }
        }
    }
}