Esempio n. 1
0
    public override Option <List <float> > FloatListObs(BaseAgent agent)
    {
        List <float> observations = new List <float>();

        foreach (GameObject targetObj in area.FindGameObjectsWithTagInChildren(Tag))
        {
            ObservableFields target = targetObj.GetComponent <ObservableFields>();
            if (target.gameObject == agent.gameObject)
            {
                continue;
            }

            for (int i = 0; i < NumFields; i++)
            {
                float f = 0;
                target.FieldsHash.TryGetValue(String.Concat(FieldName, i), out f);
                if (ShowFields)
                {
                    var level = new List <string>()
                    {
                        ".", ".", ".", "."
                    };
                    level[(int)(f * 4)] = "|";
                    //TAG: MakeEvent area.Logger.Log(Logger.CreateMessage(LogMessageType.Agent, String.Join("", level)), agent);
                }
                observations.Add(f);
            }
        }

        return(observations.SomeNotNull());
    }
Esempio n. 2
0
    public override void AddReward(BaseAgent agent, float[] vectorActions)
    {
        if (resetNextFrame)
        {
            agent.Done();
            myArea.ResetArea();
            resetNextFrame = false;
        }
        else
        {
            List <GameObject> objs = myArea.FindGameObjectsWithTagInChildren(Tag);
            foreach (GameObject obj in objs)
            {
                if ((obj.transform.position - agent.gameObject.transform.position).sqrMagnitude < Distance * Distance)
                {
                    if (ResetTime < 0)
                    {
                        ResetTime = Time.time;
                    }

                    ResetAreaAfter = AcademyParameters.Update(academy, ResetAreaAfterKeyVal, ResetAreaAfter);
                    if (Time.time - ResetTime >= ResetAreaAfter)
                    {
                        agent.AddReward(Reward);
                        resetNextFrame = true;
                    }
                }
                else
                {
                    ResetTime = -1;
                }
            }
        }
    }
    public override void AddReward(BaseAgent agent, float[] vectorActions)
    {
        List <ObservableFields> LabelObjects = new List <ObservableFields>();
        List <GameObject>       objs         = new List <GameObject>();

        foreach (string tag in Tags)
        {
            objs.AddRange(myArea.FindGameObjectsWithTagInChildren(tag));
        }

        foreach (GameObject obj in objs)
        {
            ObservableFields lobj = obj.GetComponent <ObservableFields>();
            if (lobj != null)
            {
                LabelObjects.Add(lobj);
            }
        }

        Reward = AcademyParameters.Update(academy, RewardKeyVal, Reward);

        if (AddedLastRound.Contains(agent.gameObject.GetInstanceID()))
        {
            AddedLastRound.Remove(agent.gameObject.GetInstanceID());

            if (AreaReset)
            {
                myArea.ResetArea();
            }

            return;
        }

        bool allHaveTag = true;

        foreach (ObservableFields labels in LabelObjects)
        {
            foreach (string label in Labels)
            {
                allHaveTag &= labels.LabelsHash.Contains(label);
            }
        }

        if (allHaveTag)
        {
            // TAG: MakeEvent myArea.Logger.Log(String.Concat("All ", String.Join(",", Tags), " have labels ", String.Join(",", Labels), " Adding reward: ", Reward));
            AddedLastRound.Add(agent.gameObject.GetInstanceID());
            agent.AddReward(Reward * (MultRewardByTagCount ? objs.Count : 1));

            if (Remove)
            {
                foreach (ObservableFields labels in LabelObjects)
                {
                    foreach (string label in Labels)
                    {
                        labels.LabelsHash.Remove(label);
                    }
                }
            }

            if (Reset)
            {
                agent.Done();
                agent.Reset();
            }
        }
    }