示例#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());
    }
示例#2
0
 public override void Initialize(BaseAgent agent)
 {
     Fields = agent.gameObject.GetComponent <ObservableFields>();
     if (Fields.FieldsHash == null)
     {
         Fields.FieldsHash = new Dictionary <string, float>();
     }
     for (int i = 0; i < NumFields; i++)
     {
         Fields.FieldsHash.Add(String.Concat(FieldName, i), 0);
     }
 }
    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();
            }
        }
    }
    public override void AddReward(BaseAgent agent, float[] vectorActions)
    {
        Reward           = AcademyParameters.Update(academy, RewardKeyVal, Reward);
        ContinuousReward = AcademyParameters.Update(academy, ContinuousRewardKeyVal, ContinuousReward);
        Penalty          = AcademyParameters.Update(academy, PenaltyKeyVal, Penalty);

        float labelValue;

        switch (LabelValue)
        {
        case LabelValueType.Boolean: labelValue = 1; break;

        case LabelValueType.Time: labelValue = Time.time; break;

        default: labelValue = 0; break;
        }


        Option <GameObject> triggerColCont =
            agent.TriggerCollider
            .Filter(tc => tc != null)
            .Map(tc => tc.gameObject)
            .Filter(gob => gob.tag == Tag);

        Option <Collider> prevFrame = PreviousFrame.Filter(p => p != null);

        Option <GameObject> triggerCol =
            triggerColCont
            .Filter(tc => !tc.SomeNotNull().Equals(prevFrame.Map(p => p.gameObject)));

        triggerCol
        .MatchSome(_ => {
            if (AgentCollisionMessage != "")
            {
                //TAG: MakeEvent myArea.Logger.Log(Logger.CreateMessage(LogMessageType.Agent, AgentCollisionMessage), agent);
            }

            //TAG: MakeEvent myArea.Logger.Log(String.Concat("Tagged ", agent.transform.position.y));
        });

        triggerColCont.MatchSome(_ => {
            agent.AddReward(ContinuousReward);
        });

        prevFrame
        .Filter(pc => triggerColCont.HasValue)
        .FlatMap(pc => pc.GetComponent <ObservableFields>().SomeNotNull())
        .MatchSome(lc => {
            if (RemoveOnLeave)
            {
                //TAG: MakeEvent myArea.Logger.Log(String.Concat("Removing label on leave ", Label));
                lc.FieldsHash.Remove(Label);
            }
        });

        triggerCol
        .Map(tc => tc.gameObject)
        .MatchSome(go => {
            ObservableFields lc = go.GetComponent <ObservableFields>();
            if ((lc == null || !lc.FieldsHash.ContainsKey(LabelPrevents)) && NewTag != "")
            {
                //TAG: MakeEvent myArea.Logger.Log(String.Concat("Adding tag ", NewTag));
                go.tag = NewTag;
                agent.area.EventSystem.RaiseEvent(TagEvent.Create(go));
                agent.area.EventSystem.RaiseEvent(TaggingEvent.Create(agent.gameObject, NewTag));
            }
        });

        ObservableFields selfFields = agent.gameObject.GetComponent <ObservableFields>();

        triggerCol
        .FlatMap(tc => tc.GetComponent <ObservableFields>().SomeNotNull())
        .MatchSome(lc => {
            if (lc.FieldsHash.ContainsKey(Label) && Time.time - lc.FieldsHash[Label] < Cooldown
                )
            {
                //TAG: MakeEvent myArea.Logger.Log(String.Concat("already there ", Label));
                if (Toggle)
                {
                    //TAG: MakeEvent myArea.Logger.Log(String.Concat("Removing label ", Label));
                    lc.FieldsHash.Remove(Label);
                }

                //TAG: MakeEvent myArea.Logger.Log(String.Concat("Penalizing already there ", Penalty));
                agent.AddReward(Penalty);

                if (DoneIfAlreadyThere)
                {
                    //TAG: MakeEvent myArea.Logger.Log(String.Concat("Done already there ", agent.gameObject.tag));
                    agent.Done();
                }

                if (ResetAreaIfAlreadyThere)
                {
                    //TAG: MakeEvent myArea.Logger.Log(String.Concat("Resetting already there ", agent.gameObject.tag));
                    myArea.ResetArea();
                }
            }
            else if (!lc.FieldsHash.ContainsKey(LabelPrevents) && !lc.FieldsHash.ContainsKey(Label) &&
                     (CooldownSelfTag == "" || selfFields == null || !selfFields.FieldsHash.ContainsKey(CooldownSelfTag) ||
                      Time.time - selfFields.FieldsHash[CooldownSelfTag] < Cooldown))
            {
                //TAG: MakeEvent myArea.Logger.Log(String.Concat("Adding reward ", Reward));
                agent.AddReward(Reward);
                if (Label != "")
                {
                    //TAG: MakeEvent myArea.Logger.Log(String.Concat("Adding label ", Label));
                    lc.FieldsHash.Add(Label, labelValue);
                }
            }
        });

        triggerCol
        .Filter((GameObject go) => Remove)
        .MatchSome(tc => GameObject.Destroy(tc));

        triggerCol
        .Filter((GameObject go) => Done)
        .MatchSome(_ => agent.Done());

        triggerCol
        .Filter((GameObject go) => Reset)
        .MatchSome(_ => {
            //TAG: MakeEvent myArea.Logger.Log(String.Concat("Reset on collide ", Tag));
            agent.Reset();
        });

        triggerCol
        .Filter((GameObject go) => ResetArea)
        .MatchSome(_ => myArea.ResetArea());

        PreviousFrame = agent.TriggerCollider;

        PreviousFrameTag = agent.gameObject.tag;
    }