Skip to content

Commit a159189

Browse files
committed
Minor cleanups
1 parent 8295136 commit a159189

File tree

3 files changed

+63
-66
lines changed

3 files changed

+63
-66
lines changed

Assets/ECS_MLAgents_v0/Example/SpaceMagic/Scripts/Manager.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ECS_MLAgents_v0.Example.SpaceMagic.Scripts
1212
public class SpaceSystemA : AgentSystem<Position, Acceleration>{ }
1313
public class SpaceSystemB : AgentSystem<Position, Acceleration>{ }
1414
public class SpaceSystemC : AgentSystem<Position, Acceleration>{ }
15-
15+
1616
/// <summary>
1717
/// Manager is responsible for instantiation the spheres and the IAgentSystem that will make
1818
/// them move.
@@ -32,20 +32,21 @@ public class SpaceSystemC : AgentSystem<Position, Acceleration>{ }
3232
///
3333
/// N : Give a Heuristic to the third IAgentSystem
3434
/// M : Give a NNModel to the third IAgentSystem
35-
///
35+
///
3636
/// </summary>
3737
public class Manager : MonoBehaviour
3838
{
3939
/// <summary>
40-
/// The distance at which the spheres are instantiated from the center
40+
/// The distance at which the spheres are instantiated from the center
4141
/// </summary>
4242
public float maxDistance;
43+
4344
private EntityManager manager;
4445

4546
private SpaceSystemA sA;
4647
private SpaceSystemB sB;
4748
private SpaceSystemC sC;
48-
49+
4950
/// <summary>
5051
/// The sphere prefab
5152
/// </summary>
@@ -57,34 +58,34 @@ public class Manager : MonoBehaviour
5758
public NNModel modelA;
5859
public NNModel modelB;
5960
public NNModel modelC;
60-
61-
61+
62+
6263
void Start()
6364
{
6465
manager = World.Active.GetOrCreateManager<EntityManager>();
65-
6666

67-
sA= World.Active.GetExistingManager<SpaceSystemA>();
67+
68+
sA= World.Active.GetExistingManager<SpaceSystemA>();
6869
sB= World.Active.GetExistingManager<SpaceSystemB>();
6970
sC= World.Active.GetExistingManager<SpaceSystemC>();
70-
71+
7172
// sA.Enabled = false;
7273
// sB.Enabled = false;
7374
// sC.Enabled = false;
7475

7576
sA.SetNewComponentGroup(typeof(SphereGroup));
7677
sB.SetNewComponentGroup(typeof(SphereGroup));
7778
sC.SetNewComponentGroup(typeof(SphereGroup));
78-
79+
7980
sA.SetFilter<SphereGroup>(new SphereGroup{Group = 0});
8081
sB.SetFilter<SphereGroup>(new SphereGroup{Group = 1});
8182
sC.SetFilter<SphereGroup>(new SphereGroup{Group = 2});
8283

8384
sA.Decision = new NNDecision<Position, Acceleration>(modelA);
8485
sB.Decision = new NNDecision<Position, Acceleration>(modelB);
8586
sC.Decision = new NNDecision<Position, Acceleration>(modelC);
86-
87-
87+
88+
8889
Spawn(1);
8990
}
9091

Assets/ECS_MLAgents_v0/Example/SpaceWars/Scripts/Manager.cs

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010

1111
namespace ECS_MLAgents_v0.Example.SpaceWars.Scripts
1212
{
13-
14-
public class SmartShipSystem : AgentSystem<ShipSensor, Steering> {}
15-
public class PlayerShipSystem : AgentSystem<ShipSensor, Steering> {}
13+
14+
public class SmartShipSystem : AgentSystem<ShipSensor, Steering> { }
15+
public class PlayerShipSystem : AgentSystem<ShipSensor, Steering> { }
1616

1717

1818
public class Manager : MonoBehaviour
1919
{
20-
21-
2220
public float TargetAngle;
2321
public GameObject target;
2422
public GameObject Camera;
2523

24+
public enum DecisionSelector { NeuralNetwork, ExternalDecision };
25+
public DecisionSelector shipDecisionSelector;
26+
public DecisionSelector playerDecisionSelector;
27+
2628
private EntityManager manager;
2729
public GameObject prefab;
28-
29-
30+
31+
3032
private SmartShipSystem _shipSystemA;
3133
private PlayerShipSystem _playerSystem;
3234

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

4042
void Start()
4143
{
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
5644
Time.captureFramerate = 60;
5745
QualitySettings.vSyncCount = 0;
5846
Application.targetFrameRate = -1;
59-
60-
61-
6247
manager = World.Active.GetOrCreateManager<EntityManager>();
63-
6448
_sensorSystem = World.Active.GetOrCreateManager<SensorPopulate>();
6549
_impactSystem = World.Active.GetOrCreateManager<ImpactSystem>();
6650
_impactSystem.Radius = 20;
6751

6852
_shipSystemA = World.Active.GetExistingManager<SmartShipSystem>();
69-
// _shipSystemA.Decision = new NNDecision<ShipSensor, Steering>(model);
70-
_shipSystemA.Decision =new NNDecision<ShipSensor, Steering>(model);
71-
// _shipSystemA.Decision = new ExternalDecision();
53+
if (shipDecisionSelector == DecisionSelector.ExternalDecision)
54+
{
55+
_shipSystemA.Decision = new ExternalDecision<ShipSensor, Steering>();
56+
}
57+
else
58+
{
59+
_shipSystemA.Decision = new NNDecision<ShipSensor, Steering>(model);
60+
}
7261
_playerSystem = World.Active.GetExistingManager<PlayerShipSystem>();
73-
// _playerSystem.Decision = new NNDecision<ShipSensor, Steering>(model);
74-
_playerSystem.Decision = new HumanDecision<ShipSensor>();
62+
if (playerDecisionSelector == DecisionSelector.ExternalDecision)
63+
{
64+
_playerSystem.Decision = new HumanDecision<ShipSensor>();
65+
}
66+
else
67+
{
68+
_playerSystem.Decision = new NNDecision<ShipSensor, Steering>(model);
69+
}
7570
_playerSystem.SetNewComponentGroup(typeof(PlayerFlag));
7671
_shipSystemA.DecisionRequester = new FixedTimeRequester(0.1f);
7772

78-
_playerEntity = manager.Instantiate(prefab);
73+
_playerEntity = manager.Instantiate(prefab);
7974
MakeSpaceShip(_playerEntity);
8075
manager.AddComponentData(_playerEntity, new PlayerFlag());
8176
manager.SetComponentData(_playerEntity, new Ship
@@ -85,36 +80,37 @@ void Start()
8580
MaxReloadTime = 1f
8681
});
8782

88-
83+
8984
Spawn(10);
90-
91-
// Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
92-
AttributeUtility.GetSensorMetaData(typeof(ShipSensor));
85+
86+
// Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
87+
AttributeUtility.GetSensorMetaData(typeof(ShipSensor));
9388
}
9489

9590

96-
void FixedUpdate(){
91+
void FixedUpdate()
92+
{
9793
// World.Active.GetOrCreateManager<SimulationSystemGroup>();
9894
}
9995

10096
void Update()
10197
{
10298

103-
99+
104100

105101
// for (var i = 0; i < 10; i++){
106102
// foreach(var behavior in World.Active.BehaviourManagers)
107103
// {
108104
// behavior.Update();
109105
// }
110106
// }
111-
// Debug.Log(Application.targetFrameRate);
107+
// Debug.Log(Application.targetFrameRate);
112108
float3 targetPos = 100 * new float3(math.cos(TargetAngle), 0, math.sin(TargetAngle));
113109
_sensorSystem.Center = targetPos;
114110
_impactSystem.Center = targetPos;
115111
target.transform.position = targetPos;
116112

117-
TargetAngle += Time.deltaTime/ 20f;
113+
TargetAngle += Time.deltaTime / 20f;
118114
if (Input.GetKeyDown(KeyCode.A))
119115
{
120116
Spawn(1);
@@ -133,8 +129,8 @@ void Update()
133129
var camPosition = manager.GetComponentData<Position>(_playerEntity).Value;
134130
var camRotation = manager.GetComponentData<Rotation>(_playerEntity).Value;
135131
camPosition += math.mul(camRotation, new float3(0, 0, 5));
136-
Camera.transform.position = Vector3.Lerp(Camera.transform.position,camPosition,0.1f);
137-
Camera.transform.rotation = Quaternion.Lerp(Camera.transform.rotation,camRotation,0.1f);
132+
Camera.transform.position = Vector3.Lerp(Camera.transform.position, camPosition, 0.1f);
133+
Camera.transform.rotation = Quaternion.Lerp(Camera.transform.rotation, camRotation, 0.1f);
138134

139135
}
140136

@@ -146,18 +142,19 @@ void Spawn(int amount)
146142
for (int i = 0; i < amount; i++)
147143
{
148144
MakeSpaceShip(entities[i]);
149-
145+
150146
}
151147
entities.Dispose();
152148
}
153-
154-
private void MakeSpaceShip(Entity ent){
149+
150+
private void MakeSpaceShip(Entity ent)
151+
{
155152
float valX = Random.Range(-1f, 1f);
156153
float valY = Random.Range(-1f, 1f);
157154
float valZ = Random.Range(-1f, 1f);
158155
float valD = Random.Range(0f, 1f);
159-
160-
156+
157+
161158

162159
float3 SpawnOffset = valD *
163160
Globals.SPAWN_DISTANCE *
@@ -167,15 +164,15 @@ private void MakeSpaceShip(Entity ent){
167164
manager.SetComponentData(ent,
168165
new Position
169166
{
170-
Value = SpawnOffset
167+
Value = SpawnOffset
171168
});
172169
manager.SetComponentData(ent,
173170
new Rotation
174171
{
175172
Value = quaternion.EulerXYZ(
176173
math.normalize(new float3(
177-
Random.Range(-1f, 1f),
178-
Random.Range(-1f, 1f),
174+
Random.Range(-1f, 1f),
175+
Random.Range(-1f, 1f),
179176
Random.Range(-1f, 1f)))
180177
)
181178
});
@@ -198,7 +195,7 @@ private void MakeSpaceShip(Entity ent){
198195
}
199196

200197
}
201-
198+
202199

203200
}
204201

Assets/python_communication.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import mmap
22
import struct
33
import numpy as np
4-
import time
5-
6-
74

85
class UnityCommunication:
96
FILE_CAPACITY = 200000
@@ -31,7 +28,8 @@ def read_sensor(self) -> np.ndarray:
3128
number_agents = self.get_int(self.NUMBER_AGENTS_POSITION)
3229

3330
sensor = np.frombuffer(
34-
buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION + 4*sensor_size*number_agents],
31+
buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION
32+
+ 4*sensor_size*number_agents],
3533
dtype=np.float32,
3634
count=sensor_size * number_agents,
3735
offset=0
@@ -84,6 +82,7 @@ def close(self):
8482
steps += 1
8583
s = comm.read_sensor()
8684
nag, nse, nac = comm.get_parameters()
85+
print('Number of agents is {}'.format(nag))
8786
# print(s.shape)
8887
# time.sleep(0.1)
8988
comm.write_actuator(

0 commit comments

Comments
 (0)