| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | // The maximum speed assignment class assigns interceptors to the threats to maximize the intercept |
| | 6 | | // speed by defining a cost of the assignment equal to the speed lost for the maneuver. |
| | 7 | | public class MaxSpeedAssignment : IAssignment { |
| | 8 | | // Minimum fractional speed to prevent division by zero. |
| | 9 | | private const float _minFractionalSpeed = 1e-6f; |
| | 10 | |
|
| | 11 | | // Maximum cost to prevent overflow. |
| | 12 | | private const float _maxCost = 1e12f; |
| | 13 | |
|
| | 14 | | // Assign a threat to each interceptor that has not been assigned a threat yet. |
| | 15 | | public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, |
| 0 | 16 | | in IReadOnlyList<Threat> threats) { |
| 0 | 17 | | List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>(); |
| | 18 | |
|
| 0 | 19 | | List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors); |
| 0 | 20 | | if (assignableInterceptors.Count == 0) { |
| 0 | 21 | | Debug.LogWarning("No assignable interceptors found."); |
| 0 | 22 | | return assignments; |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | List<Threat> activeThreats = IAssignment.GetActiveThreats(threats); |
| 0 | 26 | | if (activeThreats.Count == 0) { |
| 0 | 27 | | Debug.LogWarning("No active threats found."); |
| 0 | 28 | | return assignments; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | // Find all pairwise assignment costs. |
| 0 | 32 | | float[] assignmentCosts = new float[assignableInterceptors.Count * activeThreats.Count]; |
| 0 | 33 | | for (int interceptorIndex = 0; interceptorIndex < assignableInterceptors.Count; |
| 0 | 34 | | ++interceptorIndex) { |
| 0 | 35 | | Interceptor interceptor = assignableInterceptors[interceptorIndex]; |
| | 36 | |
|
| | 37 | | // The speed decays exponentially with the travelled distance and with the bearing change. |
| 0 | 38 | | float distanceTimeConstant = |
| | 39 | | 2 * (interceptor.staticConfig.BodyConfig?.Mass ?? 0) / |
| | 40 | | ((float)Constants.CalculateAirDensityAtAltitude(interceptor.GetPosition().y) * |
| | 41 | | (interceptor.staticConfig.LiftDragConfig?.DragCoefficient ?? 0) * |
| | 42 | | (interceptor.staticConfig.BodyConfig?.CrossSectionalArea ?? 0)); |
| 0 | 43 | | float angleTimeConstant = interceptor.staticConfig.LiftDragConfig?.LiftDragRatio ?? 1; |
| | 44 | | // During the turn, the minimum radius dictates the minimum distance needed to make the turn. |
| 0 | 45 | | float minTurningRadius = (float)(interceptor.GetVelocity().sqrMagnitude / |
| | 46 | | interceptor.CalculateMaxNormalAcceleration()); |
| | 47 | |
|
| 0 | 48 | | for (int threatIndex = 0; threatIndex < activeThreats.Count; ++threatIndex) { |
| 0 | 49 | | Threat threat = activeThreats[threatIndex]; |
| 0 | 50 | | Vector3 directionToThreat = threat.GetPosition() - interceptor.GetPosition(); |
| 0 | 51 | | float distanceToThreat = directionToThreat.magnitude; |
| 0 | 52 | | float angleToThreat = |
| | 53 | | Vector3.Angle(interceptor.GetVelocity(), directionToThreat) * Mathf.Deg2Rad; |
| | 54 | |
|
| | 55 | | // The fractional speed is the product of the fractional speed after traveling the distance |
| | 56 | | // and of the fractional speed after turning. |
| 0 | 57 | | float fractionalSpeed = Mathf.Exp( |
| | 58 | | -((distanceToThreat + angleToThreat * minTurningRadius) / distanceTimeConstant + |
| | 59 | | angleToThreat / angleTimeConstant)); |
| | 60 | | // Prevent division by zero. |
| 0 | 61 | | fractionalSpeed = Mathf.Max(fractionalSpeed, _minFractionalSpeed); |
| 0 | 62 | | float cost = (float)interceptor.GetSpeed() / fractionalSpeed; |
| 0 | 63 | | assignmentCosts[interceptorIndex * activeThreats.Count + threatIndex] = |
| | 64 | | Mathf.Min(cost, _maxCost); |
| 0 | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | // Solve the assignment problem. |
| 0 | 69 | | int[] assignedInterceptorIndices = new int[assignableInterceptors.Count]; |
| 0 | 70 | | int[] assignedThreatIndices = new int[assignableInterceptors.Count]; |
| 0 | 71 | | Plugin.StatusCode status = Assignment.Assignment_EvenAssignment_Assign( |
| | 72 | | assignableInterceptors.Count, activeThreats.Count, assignmentCosts, |
| | 73 | | assignedInterceptorIndices, assignedThreatIndices, out int numAssignments); |
| 0 | 74 | | if (status != Plugin.StatusCode.StatusOk) { |
| 0 | 75 | | Debug.Log($"Failed to assign the interceptors to the threats with status code {status}."); |
| 0 | 76 | | return assignments; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | for (int i = 0; i < numAssignments; ++i) { |
| 0 | 80 | | int interceptorIndex = assignedInterceptorIndices[i]; |
| 0 | 81 | | int threatIndex = assignedThreatIndices[i]; |
| 0 | 82 | | assignments.Add(new IAssignment.AssignmentItem(assignableInterceptors[interceptorIndex], |
| | 83 | | activeThreats[threatIndex])); |
| 0 | 84 | | } |
| 0 | 85 | | return assignments; |
| 0 | 86 | | } |
| | 87 | | } |