Skip to content

Commit

Permalink
【MA基礎開発】ドローンの送受信データ形式で、任意のJSONデータを渡せるように変更。アルゴリズムをpocaに設定 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyu12345 committed Feb 25, 2024
1 parent 85056bc commit 0436424
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
14 changes: 8 additions & 6 deletions MAEasySimulator/Assets/DroneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ public void FlyingCtrl(ActionBuffers actions) {
/// <summary>
/// 他のドローンにメッセージを送信する。
/// </summary>
public bool Communicate(string message) {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, communicationRange); //範囲内にあるコライダーを取得
public bool Communicate(Types.MessageData messageData) {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, communicationRange);
var result = false;
foreach (var hitCollider in hitColliders) {
if (CommunicateTargetTags.Contains(hitCollider.tag)) {
hitCollider.GetComponent<DroneController>().ReceiveMessage(message);
var json = messageData.ToJson();
hitCollider.GetComponent<DroneController>().ReceiveMessage(json);
result = true;
}
}
Expand All @@ -154,9 +155,10 @@ public bool Communicate(string message) {
/// <summary>
/// 他のドローンからメッセージを受信する(させる)。
/// </summary>
public void ReceiveMessage(string message) {
Debug.Log("Message received: " + message);
onReceiveMsg?.Invoke(message);
public void ReceiveMessage(string json) {
var messageData = Types.MessageData.FromJson(json);
Debug.Log($"Message received: Type={messageData.type}, Content={messageData.content}");
onReceiveMsg?.Invoke(json);
}


Expand Down
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/NNParam.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
behaviors:
Surpplier:
trainer_type: ppo
trainer_type: poca
hyperparameters:
batch_size: 5120
buffer_size: 10240
Expand All @@ -24,7 +24,7 @@ behaviors:
time_horizon: 120
summary_freq: 1000
Spy:
trainer_type: ppo
trainer_type: poca
hyperparameters:
batch_size: 5120
buffer_size: 10240
Expand Down
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/Scenes/test0.4.unity
Git LFS file not shown
10 changes: 9 additions & 1 deletion MAEasySimulator/Assets/SpyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpyAgent : Agent {
//TODO:以下複数の避難所を検出する場合の対応
private string targetTag = "Shelter";
private Vector3 targetPos = Vector3.zero;
private bool isFindTarget = false;

private DroneController _controller;
private EnvManager _env;
Expand All @@ -35,6 +36,7 @@ void Start() {

public override void OnEpisodeBegin() {
_env.InitializeRandomPositions();
Reset();
}

/// <summary>
Expand All @@ -54,6 +56,7 @@ public override void CollectObservations(VectorSensor sensor) {
}
}
sensor.AddObservation(targetPos);
sensor.AddObservation(isFindTarget);
}

public override void OnActionReceived(ActionBuffers actions) {
Expand Down Expand Up @@ -96,8 +99,13 @@ public override void Heuristic(in ActionBuffers actionsOut) {
private void _OnFindShelter(Vector3 pos) {
//検出情報を発信
_findCount++;
_controller.Communicate(pos.ToString());
var data = new Types.MessageData {
type = "Shelter",
content = pos.ToString()
};
_controller.Communicate(data);
Debug.Log(LogPrefix + "Find shelter at " + pos.ToString());
isFindTarget = true; //TODO:この情報を観測に追加するように
}


Expand Down
3 changes: 3 additions & 0 deletions MAEasySimulator/Assets/SurpplieAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SurpplieAgent : Agent {
private EnvManager env;
private int GetSupplieCount = 0;
private Vector3 shelterPosition = Vector3.zero;
private bool isGetShelterPos = false;
private string LogPrefix = "[Agent Surpplier]";
private Vector3 StartPosition;

Expand Down Expand Up @@ -53,6 +54,7 @@ public override void CollectObservations(VectorSensor sensor) {
sensor.AddObservation(Ctrl.Rbody.velocity);
//偵察エージェントから受け取った情報を観測
sensor.AddObservation(shelterPosition);
sensor.AddObservation(isGetShelterPos);
}

public override void OnActionReceived(ActionBuffers actions) {
Expand Down Expand Up @@ -96,6 +98,7 @@ private void OnReceiveMessage(string message) {

//観察に追加
shelterPosition = new Vector3(x, y, z);
isGetShelterPos = true;
}

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions MAEasySimulator/Assets/Types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Types : MonoBehaviour {

[System.Serializable]
public class MessageData {
public string type;
public string content;

public string ToJson() {
return JsonUtility.ToJson(this);
}

public static MessageData FromJson(string json) {
return JsonUtility.FromJson<MessageData>(json);
}
}
}
11 changes: 11 additions & 0 deletions MAEasySimulator/Assets/Types.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0436424

Please sign in to comment.