Esempio n. 1
0
    public int GetOptimalAction()
    {
        int oa = -1;

        if (reward == 0 && actions.size > 0)
        {
            float maxV = StateValue;

            for (int i = 0, imax = actions.size; i < imax; i++)
            {
                int ai = actions[i];

                if (BlackBord.SearchRecord.Contains(ai))
                {
                    continue;
                }

                var   node = BlackBord.GetNode(ai);
                float v    = node.StateValue;
                BlackBord.SearchRecord.Add(ai);
                if (node.reward == 1)
                {
                    return(ai);
                }
                else if (maxV < v)
                {
                    maxV = v;
                    oa   = ai;
                }
            }
        }
        return(oa);
    }
Esempio n. 2
0
    public void ShowDebugData()
    {
        if (!showingDebugData)
        {
            for (int i = 0, imax = dataList.Length; i < imax; i++)
            {
                dataList[i].text = BlackBord.GetNode(i).StateValue.ToString("F5");
            }
            showingDebugData = true;
        }
        else
        {
            for (int i = 0, imax = dataList.Length; i < imax; i++)
            {
                if (i == BlackBord.StartLocationIndex)
                {
                    dataList[i].text = "Start";
                }
                else if (i == BlackBord.TargetLocationIndex)
                {
                    dataList[i].text = "Target";
                }
                else if (!BlackBord.GetNode(i).IsActived)
                {
                    dataList[i].text = "X";
                }
                else
                {
                    dataList[i].text = string.Empty;
                }
            }

            showingDebugData = false;
        }
    }
Esempio n. 3
0
    private void ResetStart(int _index)
    {
        if (_index == BlackBord.TargetLocationIndex)
        {
            StartCoroutine(ShowMessage(MSG_BADSTART, MSG_SETSTART));
            return;
        }

        if (lastStart != _index)
        {
            if (lastStart >= 0)
            {
                agentList[lastStart].SetAgentAsStart(false);
                dataList[lastTarget].text = string.Empty;
            }

            agentList[_index].SetAgentAsStart(true);
            dataList[_index].text        = "Start";
            BlackBord.StartLocationIndex = _index;
            BlackBord.GetNode(_index).ResetNode().ResetActions();
            lastStart = _index;

            nextStepBtn.interactable = true;
        }
    }
Esempio n. 4
0
 public void SetNodeActive(int _locationIndex, bool _isActived)
 {
     if (_locationIndex >= 0 && _locationIndex < BlackBord.Nodes.size)
     {
         BlackBord.GetNode(_locationIndex).IsActived = _isActived;
     }
 }
Esempio n. 5
0
    //计算理想状态价值分布,理想收敛系数gamma = 0.9 [0.9 ~ 0.99]
    //public void CalculateStateValueDistribution(AINode _node, AINode _lastNode)
    //{
    //    float gamma = BlackBord.Gamma;

    //    Debug.LogFormat("node{0}.stateValue =========>> {1}", _node.LocationIndex, _node.StateValue);
    //    foreach (var act in _node.actions)
    //    {
    //        if (_lastNode != null && _lastNode.actions.Contains(act))
    //            continue;

    //        if (n.IsOptimalAction(node))
    //            if (!calOnceList.Contains(act))
    //            {
    //                AINode n = BlackBord.GetNode(act);
    //                calOnceList.Add(_node.LocationIndex);
    //                n.StateValue = _node.Reward + gamma * _node.StateValue;
    //                CalculateStateValueDistribution(n, _node);
    //            }
    //    }

    //    foreach (var act in node.actions)
    //    {
    //        if (!calOnceList.Contains(act))
    //        {
    //            calOnceList.Add(act);
    //            CalculateStateValueDistribution(act);
    //        }
    //    }
    //}

    public void CalculateStateValueDistribution(int _targetIndex)
    {
        float            gamma   = BlackBord.Gamma;
        BetterList <int> allLeft = BlackBord.GetActiveNodeIndexes();

        int sum = BlackBord.GetNodesCount();
        int i   = 0;

        while (allLeft.size > 0)
        {
            int    index = (_targetIndex + i) % sum;
            AINode node  = BlackBord.GetNode(index);

            if (!node.IsActived || node.StateValue == -1)
            {
                i++;
                continue;
            }

            float nextStateValue = node.StateValue == 0 ?
                                   (node.Reward + gamma * node.StateValue) : node.StateValue;
            foreach (var act in node.actions)
            {
                AINode n = BlackBord.GetNode(act);

                float nsv = n.Reward + gamma * nextStateValue;
                if (nsv > n.StateValue && n.Reward < 1)
                {
                    n.StateValue = nsv;
                }
            }
            allLeft.Remove(index);
            i++;
        }
    }
Esempio n. 6
0
    public void ResetNodesActions()
    {
        int nodeCount = BlackBord.Nodes.size;

        for (int i = 0; i < nodeCount; i++)
        {
            BlackBord.GetNode(i).ResetActions();
        }
    }
Esempio n. 7
0
    public void InitGrid()
    {
        int nodeCount = BlackBord.GetNodesCount();

        for (int i = 0; i < nodeCount; i++)
        {
            BlackBord.Nodes.Add(new AINode(i));
        }
    }
Esempio n. 8
0
    public float GetActionReward(int _locationIndex)
    {
        float ar = -1f;
        int   i  = actions.IndexOf(_locationIndex);

        if (i >= 0)
        {
            AINode node = BlackBord.GetNode(i);
            ar = node.Reward;
        }
        return(ar);
    }
Esempio n. 9
0
    private void SetBlock(int _index)
    {
        bool isBlock = agentList[_index].SetAgentAsBlock();

        BlackBord.GetNode(_index).ResetNode().IsActived = !isBlock;

        if (isBlock)
        {
            BlackBord.BlockCount++;
            dataList[_index].text = "X";
        }
        else
        {
            BlackBord.BlockCount--;
            dataList[_index].text = string.Empty;
        }
    }
Esempio n. 10
0
    public void SetIntermediateNode(int _index)
    {
        AINode node     = BlackBord.GetNode(_index);
        bool   isIMNode = node.IsIMNode;

        if (isIMNode)
        {
            agentList[_index].SetAgentAsIMNode(false);
            dataList[_index].text = string.Empty;
        }
        else
        {
            agentList[_index].SetAgentAsIMNode(true);
            dataList[_index].text = IMNodeReward;
            AIObserver.Instance.SetIMNodeReward(_index, float.Parse(IMNodeReward));
        }
    }
Esempio n. 11
0
    public BetterList <int> GetOptimalActionList(int _locationIndex)
    {
        BetterList <int> oal = new BetterList <int>();

        BlackBord.SearchRecord.Clear();
        BlackBord.SearchRecord.Add(_locationIndex);

        int ptr = BlackBord.GetNode(_locationIndex).GetOptimalAction();

        while (ptr != -1)
        {
            oal.Add(ptr);
            ptr = BlackBord.GetNode(ptr).GetOptimalAction();
        }

        BlackBord.SearchRecord.Clear();
        return(oal);
    }
Esempio n. 12
0
    private void ResetTarget(int _index)
    {
        if (lastTarget != _index)
        {
            if (lastTarget >= 0)
            {
                agentList[lastTarget].SetAgentAsTarget(false);
                dataList[lastTarget].text = string.Empty;
                BlackBord.GetNode(lastTarget).ResetNode();
            }
            agentList[_index].SetAgentAsTarget(true);
            dataList[_index].text = "Target";
            BlackBord.SetTargetLocation(_index);
            lastTarget = _index;
        }

        if (BlackBord.TargetLocationIndex >= 0 && !nextStepBtn.interactable)
        {
            nextStepBtn.interactable = true;
        }
    }
Esempio n. 13
0
    public bool IsOptimalAction(AINode _node)
    {
        if (_node.reward == 1)
        {
            return(true);
        }

        float sv = _node.stateValue;

        foreach (var loc in actions)
        {
            if (loc == _node.locationIndex)
            {
                continue;
            }

            AINode node = BlackBord.GetNode(loc);
            if (node.stateValue > sv)
            {
                return(false);
            }
        }
        return(true);
    }
Esempio n. 14
0
    public void ResetActions()
    {
        if (!isActived)
        {
            return;
        }

        actions.Clear();
        int _locationIndex = locationIndex;

        CheckSlefLocation();

        int selfRS = locationRowSize;
        int selfLX = locationX;
        int selfLY = locationY;

        int ly = 0;
        int lx = 0;
        int rs = 0;

        int frs  = BlackBord.FirstRowSize;
        int rc   = BlackBord.RowCount;
        int nc   = BlackBord.Nodes.size;
        int half = rc >> 1;

        if (selfLY > 0)
        {
            int up = _locationIndex - (selfLY <= half ? selfRS : selfRS + 1);
            if (up >= 0)
            {
                CheckLocation(up, ref rs, ref lx, ref ly);
                if (ly == selfLY - 1 && BlackBord.GetNode(up).IsActived)
                {
                    actions.Add(up);
                }
            }

            CheckLocation(++up, ref rs, ref lx, ref ly);
            if (ly == selfLY - 1 && BlackBord.GetNode(up).IsActived)
            {
                actions.Add(up);
            }
        }

        if (selfLX > 0 && BlackBord.GetNode(_locationIndex - 1).IsActived)
        {
            actions.Add(_locationIndex - 1);
        }

        if (selfLX < selfRS - 1 && BlackBord.GetNode(_locationIndex + 1).IsActived)
        {
            actions.Add(_locationIndex + 1);
        }

        if (selfLY < rc - 1)
        {
            int down = _locationIndex + (selfLY < half ? selfRS + 1 : selfRS);
            if (down < nc)
            {
                CheckLocation(down, ref rs, ref lx, ref ly);
                if (ly == selfLY + 1 && BlackBord.GetNode(down).IsActived)
                {
                    actions.Add(down);
                }
            }

            CheckLocation(--down, ref rs, ref lx, ref ly);
            if (ly == selfLY + 1 && BlackBord.GetNode(down).IsActived)
            {
                actions.Add(down);
            }
        }

        //UnityEngine.Debug.LogFormat("Node[{0}]'s ResetActions result is ====================================", locationIndex);
        //for (int i = 0, imax = actions.size; i < imax; i++) {
        //    UnityEngine.Debug.LogFormat("Node[{0}].actions[{1}] is ====> <{2}>", locationIndex, i, actions[i]);
        //}
    }
Esempio n. 15
0
 public void SetIMNodeReward(int _index, float _reward)
 {
     BlackBord.GetTargetNode().Reward += _reward;
     BlackBord.GetNode(_index).Reward += _reward;
 }