From 25e656802362c41e17723683d93b0d6eb8f8ff8a Mon Sep 17 00:00:00 2001 From: Daniel Lovell Date: Thu, 12 Sep 2024 23:58:47 -0700 Subject: [PATCH] Fixed to target assignment, 3 salvo hydra70 config --- Assets/Materials/MicromissileMat.mat | 2 +- Assets/Scripts/Agent.cs | 10 +- Assets/Scripts/Assignment/ThreatAssignment.cs | 24 +++- Assets/Scripts/Interceptors/Micromissile.cs | 3 +- Assets/Scripts/Missile.cs | 2 +- Assets/Scripts/SimManager.cs | 14 ++- Assets/SimulationConfigHydra70.asset | 114 ++++++++++++++++-- 7 files changed, 144 insertions(+), 25 deletions(-) diff --git a/Assets/Materials/MicromissileMat.mat b/Assets/Materials/MicromissileMat.mat index ad00ada..c79ca67 100644 --- a/Assets/Materials/MicromissileMat.mat +++ b/Assets/Materials/MicromissileMat.mat @@ -106,7 +106,7 @@ Material: - _Parallax: 0.02 - _QueueOffset: 0 - _ReceiveShadows: 1 - - _Smoothness: 0.556 + - _Smoothness: 0.33 - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 - _SrcBlend: 1 diff --git a/Assets/Scripts/Agent.cs b/Assets/Scripts/Agent.cs index 41638a0..be239fb 100644 --- a/Assets/Scripts/Agent.cs +++ b/Assets/Scripts/Agent.cs @@ -65,6 +65,10 @@ public abstract class Agent : MonoBehaviour _target = target; } + public Agent GetAssignedTarget() { + return _target; + } + public bool HasAssignedTarget() { return _target != null; } @@ -90,7 +94,7 @@ public abstract class Agent : MonoBehaviour } public void TerminateAgent() { - _flightPhase = FlightPhase.TERMINATED; + _flightPhase = FlightPhase.TERMINATED; transform.position = new Vector3(0, 0, 0); gameObject.SetActive(false); } @@ -103,6 +107,10 @@ public abstract class Agent : MonoBehaviour public void MarkAsMiss() { _isMiss = true; + if(_target != null) { + SimManager.Instance.RegisterTargetMiss(_target as Target); + _target = null; + } TerminateAgent(); } diff --git a/Assets/Scripts/Assignment/ThreatAssignment.cs b/Assets/Scripts/Assignment/ThreatAssignment.cs index 52a9c6a..aa29d2d 100644 --- a/Assets/Scripts/Assignment/ThreatAssignment.cs +++ b/Assets/Scripts/Assignment/ThreatAssignment.cs @@ -34,9 +34,27 @@ public class ThreatAssignment : IAssignment if (missiles[missileIndex].HasAssignedTarget()) continue; if (threatInfos.Count == 0) break; - ThreatInfo highestThreat = threatInfos[0]; - assignments.Add(new IAssignment.AssignmentItem(missileIndex, highestThreat.TargetIndex)); - threatInfos.RemoveAt(0); + // Find the optimal target for this missile based on distance and threat + ThreatInfo optimalTarget = null; + float optimalScore = float.MinValue; + + foreach (ThreatInfo threat in threatInfos) + { + float distance = Vector3.Distance(missiles[missileIndex].transform.position, targets[threat.TargetIndex].transform.position); + float score = threat.ThreatLevel / distance; // Balance threat level with proximity + + if (score > optimalScore) + { + optimalScore = score; + optimalTarget = threat; + } + } + + if (optimalTarget != null) + { + assignments.Add(new IAssignment.AssignmentItem(missileIndex, optimalTarget.TargetIndex)); + threatInfos.Remove(optimalTarget); + } } return assignments; } diff --git a/Assets/Scripts/Interceptors/Micromissile.cs b/Assets/Scripts/Interceptors/Micromissile.cs index 4377dd4..347f567 100644 --- a/Assets/Scripts/Interceptors/Micromissile.cs +++ b/Assets/Scripts/Interceptors/Micromissile.cs @@ -4,7 +4,7 @@ using UnityEngine; public class Micromissile : Missile { - [SerializeField] private float _navigationGain = 3f; // Typically 3-5 + [SerializeField] private float _navigationGain = 5f; // Typically 3-5 private SensorOutput _sensorOutput; private Vector3 _accelerationCommand; @@ -32,7 +32,6 @@ public class Micromissile : Missile SensorOutput sensorOutput = GetComponent().Sense(_target); if(sensorOutput.velocity.range > 1000f) { this.MarkAsMiss(); - _target.MarkAsMiss(); } // Calculate the acceleration input diff --git a/Assets/Scripts/Missile.cs b/Assets/Scripts/Missile.cs index 053a3e0..388b28a 100644 --- a/Assets/Scripts/Missile.cs +++ b/Assets/Scripts/Missile.cs @@ -29,7 +29,7 @@ public class Missile : Agent protected override void UpdateReady(double deltaTime) { Vector3 accelerationInput = Vector3.zero; Vector3 acceleration = CalculateAcceleration(accelerationInput); - GetComponent().AddForce(acceleration, ForceMode.Acceleration); + //GetComponent().AddForce(acceleration, ForceMode.Acceleration); } protected override void Update() { diff --git a/Assets/Scripts/SimManager.cs b/Assets/Scripts/SimManager.cs index 08777ae..f7caf28 100644 --- a/Assets/Scripts/SimManager.cs +++ b/Assets/Scripts/SimManager.cs @@ -15,8 +15,8 @@ public class SimManager : MonoBehaviour private List _missiles = new List(); - private HashSet _unassignedTargets = new HashSet(); - private HashSet _targets = new HashSet(); + private List _unassignedTargets = new List(); + private List _targets = new List(); private float _elapsedSimulationTime = 0f; private float endTime = 100f; // Set an appropriate end time private bool simulationRunning = false; @@ -83,6 +83,10 @@ public class SimManager : MonoBehaviour AssignMissilesToTargets(_missiles); } + public void RegisterTargetMiss(Target target) { + _unassignedTargets.Add(target); + } + public void AssignMissilesToTargets(List missilesToAssign) { @@ -100,12 +104,14 @@ public class SimManager : MonoBehaviour if (assignment.MissileIndex < missilesToAssign.Count) { Missile missile = missilesToAssign[assignment.MissileIndex]; - Target target = _targets.ElementAt(assignment.TargetIndex); + Target target = _unassignedTargets[assignment.TargetIndex]; missile.AssignTarget(target); Debug.Log($"Missile {missile.name} assigned to target {target.name}"); - _unassignedTargets.Remove(target); + } } + // TODO this whole function should be optimized + _unassignedTargets.RemoveAll(target => missilesToAssign.Any(missile => missile.GetAssignedTarget() == target)); } public Missile CreateMissile(AgentConfig config) diff --git a/Assets/SimulationConfigHydra70.asset b/Assets/SimulationConfigHydra70.asset index 5f6adff..2fa729b 100644 --- a/Assets/SimulationConfigHydra70.asset +++ b/Assets/SimulationConfigHydra70.asset @@ -12,25 +12,25 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 79f1fe138866d6a40b209a4edcf2ee06, type: 3} m_Name: SimulationConfigHydra70 m_EditorClassIdentifier: - timeScale: 0.2 + timeScale: 1 missile_swarm_configs: - - num_agents: 20 + - num_agents: 10 agent_config: missile_type: 0 target_type: 0 initial_state: - position: {x: 0, y: 10, z: 0} + position: {x: 0, y: 20, z: 0} rotation: {x: -45, y: 0, z: 0} velocity: {x: 0, y: 10, z: 10} standard_deviation: - position: {x: 10, y: 10, z: 10} - velocity: {x: 5, y: 0, z: 5} + position: {x: 10, y: 0, z: 10} + velocity: {x: 5, y: 0, z: 1} dynamic_config: launch_config: launch_time: 0 sensor_config: type: 0 - frequency: 1 + frequency: 1000 plotting_config: color: {r: 0, g: 0, b: 0, a: 0} linestyle: 0 @@ -38,13 +38,101 @@ MonoBehaviour: submunitions_config: num_submunitions: 7 launch_config: - launch_time: 1.5 + launch_time: 2 agent_config: missile_type: 1 initial_state: - position: {x: -38.074936, y: 286.19226, z: 293.8709} + position: {x: -276.48438, y: 585.0613, z: 640.66565} rotation: {x: 0, y: 0, z: 0} - velocity: {x: -20.437624, y: 181.02332, z: 193.21309} + velocity: {x: -115.62746, y: 195.53085, z: 266.85275} + standard_deviation: + position: {x: 5, y: 5, z: 5} + velocity: {x: 0, y: 0, z: 0} + dynamic_config: + launch_config: + launch_time: 0 + sensor_config: + type: 0 + frequency: 1 + plotting_config: + color: {r: 0, g: 0, b: 0, a: 0} + linestyle: 0 + marker: 0 + - num_agents: 10 + agent_config: + missile_type: 0 + target_type: 0 + initial_state: + position: {x: 200, y: 20, z: 0} + rotation: {x: -60, y: 0, z: 0} + velocity: {x: 0, y: 10, z: 12} + standard_deviation: + position: {x: 10, y: 0, z: 10} + velocity: {x: 5, y: 0, z: 1} + dynamic_config: + launch_config: + launch_time: 10 + sensor_config: + type: 0 + frequency: 1000 + plotting_config: + color: {r: 0, g: 0, b: 0, a: 0} + linestyle: 0 + marker: 0 + submunitions_config: + num_submunitions: 7 + launch_config: + launch_time: 12 + agent_config: + missile_type: 1 + initial_state: + position: {x: -142.09969, y: 732.03265, z: 973.45154} + rotation: {x: 0, y: 0, z: 0} + velocity: {x: -127.14329, y: 235.90274, z: 367.02448} + standard_deviation: + position: {x: 5, y: 5, z: 5} + velocity: {x: 0, y: 0, z: 0} + dynamic_config: + launch_config: + launch_time: 0 + sensor_config: + type: 0 + frequency: 1 + plotting_config: + color: {r: 0, g: 0, b: 0, a: 0} + linestyle: 0 + marker: 0 + - num_agents: 10 + agent_config: + missile_type: 0 + target_type: 0 + initial_state: + position: {x: -100, y: 20, z: 0} + rotation: {x: -45, y: 0, z: 0} + velocity: {x: 0, y: 10, z: 10} + standard_deviation: + position: {x: 10, y: 0, z: 10} + velocity: {x: 5, y: 0, z: 3} + dynamic_config: + launch_config: + launch_time: 20 + sensor_config: + type: 0 + frequency: 1000 + plotting_config: + color: {r: 0, g: 0, b: 0, a: 0} + linestyle: 0 + marker: 0 + submunitions_config: + num_submunitions: 7 + launch_config: + launch_time: 23 + agent_config: + missile_type: 1 + initial_state: + position: {x: -117.94113, y: 473.32446, z: 1092.305} + rotation: {x: 0, y: 0, z: 0} + velocity: {x: -5.810343, y: 133.54488, z: 394.63327} standard_deviation: position: {x: 5, y: 5, z: 5} velocity: {x: 0, y: 0, z: 0} @@ -64,12 +152,12 @@ MonoBehaviour: missile_type: 0 target_type: 0 initial_state: - position: {x: 0, y: 400, z: 3000} + position: {x: 0, y: 600, z: 6000} rotation: {x: 90, y: 0, z: 0} - velocity: {x: 0, y: 0, z: -150} + velocity: {x: 0, y: 0, z: -50} standard_deviation: - position: {x: 500, y: 200, z: 100} - velocity: {x: 0, y: 0, z: 50} + position: {x: 1000, y: 200, z: 100} + velocity: {x: 0, y: 0, z: 25} dynamic_config: launch_config: launch_time: 0