Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 1 addition & 71 deletions Assets/ECSEnvironment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import struct
import numpy as np

from python_communication import UnityCommunication
from .brain import BrainInfo, BrainParameters


class ECSEnvironment(object):
VEC_SIZE = 8
ACT_SIZE = 3
Expand Down Expand Up @@ -90,76 +90,6 @@ def external_brain_names(self):





class UnityCommunication:
FILE_CAPACITY = 200000
NUMBER_AGENTS_POSITION = 0
SENSOR_SIZE_POSITION = 4
ACTUATOR_SIZE_POSITION = 8
UNITY_READY_POSITION = 12
SENSOR_DATA_POSITION = 13

PYTHON_READY_POSITION = 100000
ACTUATOR_DATA_POSITION = 100001

# FILE_NAME = "../../../ml-agents-ecs/Assets/shared_communication_file.txt"
FILE_NAME = "shared_communication_file.txt" # This is relative to where the script is called

def __init__(self):
with open(self.FILE_NAME, "r+b") as f:
# memory-map the file, size 0 means whole file
self.accessor = mmap.mmap(f.fileno(), 0)

def get_int(self, position : int) -> int:
return struct.unpack("i", self.accessor[position:position + 4])[0]

def read_sensor(self) -> np.ndarray:
sensor_size = self.get_int(self.SENSOR_SIZE_POSITION)
number_agents = self.get_int(self.NUMBER_AGENTS_POSITION)

sensor = np.frombuffer(
buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION + 4*sensor_size*number_agents],
dtype=np.float32,
count=sensor_size * number_agents,
offset=0
)
return np.reshape(sensor, (number_agents, sensor_size))

def get_parameters(self) -> (int, int, int):
return self.get_int(self.NUMBER_AGENTS_POSITION), \
self.get_int(self.SENSOR_SIZE_POSITION), \
self.get_int(self.ACTUATOR_SIZE_POSITION)

def write_actuator(self, actuator: np.ndarray):
actuator_size = self.get_int(self.ACTUATOR_SIZE_POSITION)
number_agents = self.get_int(self.NUMBER_AGENTS_POSITION)

# TODO : Support more types ?
if actuator.dtype != np.float32:
actuator = actuator.astype(np.float32)

try:
assert(actuator.shape == (number_agents, actuator_size))
except:
print("_________")
print(actuator.shape)
print((number_agents, actuator_size))

self.accessor[self.ACTUATOR_DATA_POSITION: self.ACTUATOR_DATA_POSITION + 4*actuator_size*number_agents] = \
actuator.tobytes()

def set_ready(self):
self.accessor[self.UNITY_READY_POSITION: self.UNITY_READY_POSITION+1] = bytearray(struct.pack("b", False))
self.accessor[self.PYTHON_READY_POSITION: self.PYTHON_READY_POSITION+1] = bytearray(struct.pack("b", True))

def unity_ready(self) -> bool:
return self.accessor[self.UNITY_READY_POSITION]

def close(self):
self.accessor.close()


# if __name__ == "__main__":
# comm = UnityCommunication()
#
Expand Down
25 changes: 13 additions & 12 deletions Assets/ECS_MLAgents_v0/Example/SpaceMagic/Scripts/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ECS_MLAgents_v0.Example.SpaceMagic.Scripts
public class SpaceSystemA : AgentSystem<Position, Acceleration>{ }
public class SpaceSystemB : AgentSystem<Position, Acceleration>{ }
public class SpaceSystemC : AgentSystem<Position, Acceleration>{ }

/// <summary>
/// Manager is responsible for instantiation the spheres and the IAgentSystem that will make
/// them move.
Expand All @@ -32,20 +32,21 @@ public class SpaceSystemC : AgentSystem<Position, Acceleration>{ }
///
/// N : Give a Heuristic to the third IAgentSystem
/// M : Give a NNModel to the third IAgentSystem
///
///
/// </summary>
public class Manager : MonoBehaviour
{
/// <summary>
/// The distance at which the spheres are instantiated from the center
/// The distance at which the spheres are instantiated from the center
/// </summary>
public float maxDistance;

private EntityManager manager;

private SpaceSystemA sA;
private SpaceSystemB sB;
private SpaceSystemC sC;

/// <summary>
/// The sphere prefab
/// </summary>
Expand All @@ -57,34 +58,34 @@ public class Manager : MonoBehaviour
public NNModel modelA;
public NNModel modelB;
public NNModel modelC;


void Start()
{
manager = World.Active.GetOrCreateManager<EntityManager>();


sA= World.Active.GetExistingManager<SpaceSystemA>();

sA= World.Active.GetExistingManager<SpaceSystemA>();
sB= World.Active.GetExistingManager<SpaceSystemB>();
sC= World.Active.GetExistingManager<SpaceSystemC>();

// sA.Enabled = false;
// sB.Enabled = false;
// sC.Enabled = false;

sA.SetNewComponentGroup(typeof(SphereGroup));
sB.SetNewComponentGroup(typeof(SphereGroup));
sC.SetNewComponentGroup(typeof(SphereGroup));

sA.SetFilter<SphereGroup>(new SphereGroup{Group = 0});
sB.SetFilter<SphereGroup>(new SphereGroup{Group = 1});
sC.SetFilter<SphereGroup>(new SphereGroup{Group = 2});

sA.Decision = new NNDecision<Position, Acceleration>(modelA);
sB.Decision = new NNDecision<Position, Acceleration>(modelB);
sC.Decision = new NNDecision<Position, Acceleration>(modelC);


Spawn(1);
}

Expand Down
97 changes: 47 additions & 50 deletions Assets/ECS_MLAgents_v0/Example/SpaceWars/Scripts/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@

namespace ECS_MLAgents_v0.Example.SpaceWars.Scripts
{
public class SmartShipSystem : AgentSystem<ShipSensor, Steering> {}
public class PlayerShipSystem : AgentSystem<ShipSensor, Steering> {}

public class SmartShipSystem : AgentSystem<ShipSensor, Steering> { }
public class PlayerShipSystem : AgentSystem<ShipSensor, Steering> { }


public class Manager : MonoBehaviour
{


public float TargetAngle;
public GameObject target;
public GameObject Camera;

public enum DecisionSelector { NeuralNetwork, ExternalDecision };
public DecisionSelector shipDecisionSelector;
public DecisionSelector playerDecisionSelector;

private EntityManager manager;
public GameObject prefab;


private SmartShipSystem _shipSystemA;
private PlayerShipSystem _playerSystem;

Expand All @@ -39,43 +41,36 @@ public class Manager : MonoBehaviour

void Start()
{














Time.captureFramerate = 60;
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = -1;



manager = World.Active.GetOrCreateManager<EntityManager>();

_sensorSystem = World.Active.GetOrCreateManager<SensorPopulate>();
_impactSystem = World.Active.GetOrCreateManager<ImpactSystem>();
_impactSystem.Radius = 20;

_shipSystemA = World.Active.GetExistingManager<SmartShipSystem>();
// _shipSystemA.Decision = new NNDecision<ShipSensor, Steering>(model);
_shipSystemA.Decision =new NNDecision<ShipSensor, Steering>(model);
// _shipSystemA.Decision = new ExternalDecision();
if (shipDecisionSelector == DecisionSelector.ExternalDecision)
{
_shipSystemA.Decision = new ExternalDecision<ShipSensor, Steering>();
}
else
{
_shipSystemA.Decision = new NNDecision<ShipSensor, Steering>(model);
}
_playerSystem = World.Active.GetExistingManager<PlayerShipSystem>();
// _playerSystem.Decision = new NNDecision<ShipSensor, Steering>(model);
_playerSystem.Decision = new HumanDecision<ShipSensor>();
if (playerDecisionSelector == DecisionSelector.ExternalDecision)
{
_playerSystem.Decision = new HumanDecision<ShipSensor>();
}
else
{
_playerSystem.Decision = new NNDecision<ShipSensor, Steering>(model);
}
_playerSystem.SetNewComponentGroup(typeof(PlayerFlag));
_shipSystemA.DecisionRequester = new FixedTimeRequester(0.1f);

_playerEntity = manager.Instantiate(prefab);
_playerEntity = manager.Instantiate(prefab);
MakeSpaceShip(_playerEntity);
manager.AddComponentData(_playerEntity, new PlayerFlag());
manager.SetComponentData(_playerEntity, new Ship
Expand All @@ -85,36 +80,37 @@ void Start()
MaxReloadTime = 1f
});


Spawn(10);
// Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
AttributeUtility.GetSensorMetaData(typeof(ShipSensor));

// Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
AttributeUtility.GetSensorMetaData(typeof(ShipSensor));
}


void FixedUpdate(){
void FixedUpdate()
{
// World.Active.GetOrCreateManager<SimulationSystemGroup>();
}

void Update()
{



// for (var i = 0; i < 10; i++){
// foreach(var behavior in World.Active.BehaviourManagers)
// {
// behavior.Update();
// }
// }
// Debug.Log(Application.targetFrameRate);
// Debug.Log(Application.targetFrameRate);
float3 targetPos = 100 * new float3(math.cos(TargetAngle), 0, math.sin(TargetAngle));
_sensorSystem.Center = targetPos;
_impactSystem.Center = targetPos;
target.transform.position = targetPos;

TargetAngle += Time.deltaTime/ 20f;
TargetAngle += Time.deltaTime / 20f;
if (Input.GetKeyDown(KeyCode.A))
{
Spawn(1);
Expand All @@ -133,8 +129,8 @@ void Update()
var camPosition = manager.GetComponentData<Position>(_playerEntity).Value;
var camRotation = manager.GetComponentData<Rotation>(_playerEntity).Value;
camPosition += math.mul(camRotation, new float3(0, 0, 5));
Camera.transform.position = Vector3.Lerp(Camera.transform.position,camPosition,0.1f);
Camera.transform.rotation = Quaternion.Lerp(Camera.transform.rotation,camRotation,0.1f);
Camera.transform.position = Vector3.Lerp(Camera.transform.position, camPosition, 0.1f);
Camera.transform.rotation = Quaternion.Lerp(Camera.transform.rotation, camRotation, 0.1f);

}

Expand All @@ -146,18 +142,19 @@ void Spawn(int amount)
for (int i = 0; i < amount; i++)
{
MakeSpaceShip(entities[i]);

}
entities.Dispose();
}

private void MakeSpaceShip(Entity ent){

private void MakeSpaceShip(Entity ent)
{
float valX = Random.Range(-1f, 1f);
float valY = Random.Range(-1f, 1f);
float valZ = Random.Range(-1f, 1f);
float valD = Random.Range(0f, 1f);



float3 SpawnOffset = valD *
Globals.SPAWN_DISTANCE *
Expand All @@ -167,15 +164,15 @@ private void MakeSpaceShip(Entity ent){
manager.SetComponentData(ent,
new Position
{
Value = SpawnOffset
Value = SpawnOffset
});
manager.SetComponentData(ent,
new Rotation
{
Value = quaternion.EulerXYZ(
math.normalize(new float3(
Random.Range(-1f, 1f),
Random.Range(-1f, 1f),
Random.Range(-1f, 1f),
Random.Range(-1f, 1f),
Random.Range(-1f, 1f)))
)
});
Expand All @@ -198,7 +195,7 @@ private void MakeSpaceShip(Entity ent){
}

}


}

Loading