deploy: 54c7e5fbc7
This commit is contained in:
40
coverage/editmode/Assets/Scripts/Assignment/Assignment.cs
Normal file
40
coverage/editmode/Assets/Scripts/Assignment/Assignment.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
// The assignment class is an interface for assigning a threat to each interceptor.
|
||||
public interface IAssignment {
|
||||
// Assignment item type.
|
||||
// The first element corresponds to the interceptor index, and the second element
|
||||
// corresponds to the threat index.
|
||||
public struct AssignmentItem {
|
||||
public Interceptor Interceptor;
|
||||
public Threat Threat;
|
||||
|
||||
public AssignmentItem(Interceptor interceptor, Threat threat) {
|
||||
Interceptor = interceptor;
|
||||
Threat = threat;
|
||||
}
|
||||
}
|
||||
|
||||
// A list containing the interceptor-target assignments.
|
||||
|
||||
// Assign a target to each interceptor that has not been assigned a target yet.
|
||||
[Pure]
|
||||
public abstract IEnumerable<AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, in IReadOnlyList<ThreatData> threatTable);
|
||||
|
||||
// Get the list of assignable interceptor indices.
|
||||
[Pure]
|
||||
protected static List<Interceptor> GetAssignableInterceptors(in IReadOnlyList<Interceptor> interceptors) {
|
||||
return interceptors.Where(interceptor => interceptor.IsAssignable()).ToList();
|
||||
}
|
||||
|
||||
// Get the list of active threats.
|
||||
[Pure]
|
||||
protected static List<ThreatData> GetActiveThreats(in IReadOnlyList<ThreatData> threats) {
|
||||
return threats.Where(t => t.Status != ThreatStatus.DESTROYED).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95c1ee01149331443bb91460f4d1e3a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Diagnostics.Contracts;
|
||||
// The round-robin assignment class assigns interceptors to the targets in a
|
||||
// round-robin order using the new paradigm.
|
||||
public class RoundRobinAssignment : IAssignment {
|
||||
// Previous target index that was assigned.
|
||||
private int prevTargetIndex = -1;
|
||||
|
||||
// Assign a target to each interceptor that has not been assigned a target yet.
|
||||
[Pure]
|
||||
public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, in IReadOnlyList<ThreatData> targets) {
|
||||
List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>();
|
||||
|
||||
// Get the list of interceptors that are available for assignment.
|
||||
List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors);
|
||||
if (assignableInterceptors.Count == 0) {
|
||||
return assignments;
|
||||
}
|
||||
|
||||
// Get the list of active threats that need to be addressed.
|
||||
List<ThreatData> activeThreats = IAssignment.GetActiveThreats(targets);
|
||||
if (activeThreats.Count == 0) {
|
||||
return assignments;
|
||||
}
|
||||
|
||||
// Perform round-robin assignment.
|
||||
foreach (Interceptor interceptor in assignableInterceptors) {
|
||||
// Determine the next target index in a round-robin fashion.
|
||||
int nextTargetIndex = (prevTargetIndex + 1) % activeThreats.Count;
|
||||
ThreatData selectedThreat = activeThreats[nextTargetIndex];
|
||||
|
||||
// Assign the interceptor to the selected threat.
|
||||
assignments.Add(new IAssignment.AssignmentItem(interceptor, selectedThreat.Threat));
|
||||
|
||||
// Update the previous target index.
|
||||
prevTargetIndex = nextTargetIndex;
|
||||
}
|
||||
|
||||
return assignments;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f668f6d7413495c4093550e492ce36bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using System.Diagnostics.Contracts;
|
||||
// The threat assignment class assigns interceptors to the targets based
|
||||
// on the threat level of the targets.
|
||||
public class ThreatAssignment : IAssignment {
|
||||
// Assign a target to each interceptor that has not been assigned a target yet.
|
||||
[Pure]
|
||||
public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, in IReadOnlyList<ThreatData> targets) {
|
||||
List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>();
|
||||
|
||||
List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors);
|
||||
if (assignableInterceptors.Count == 0) {
|
||||
Debug.LogWarning("No assignable interceptors found");
|
||||
return assignments;
|
||||
}
|
||||
|
||||
List<ThreatData> activeThreats = IAssignment.GetActiveThreats(targets);
|
||||
if (activeThreats.Count == 0) {
|
||||
Debug.LogWarning("No active threats found");
|
||||
return assignments;
|
||||
}
|
||||
|
||||
Vector3 positionToDefend = Vector3.zero;
|
||||
List<ThreatInfo> threatInfos =
|
||||
CalculateThreatLevels(activeThreats, positionToDefend);
|
||||
|
||||
// Sort ThreatInfo first by ThreatData.Status (UNASSIGNED first, then ASSIGNED)
|
||||
// Within each group, order by ThreatLevel descending
|
||||
threatInfos = threatInfos.OrderByDescending(t => t.ThreatData.Status == ThreatStatus.UNASSIGNED)
|
||||
.ThenByDescending(t => t.ThreatLevel)
|
||||
.ToList();
|
||||
|
||||
var assignableInterceptorsEnumerator = assignableInterceptors.GetEnumerator();
|
||||
if (assignableInterceptorsEnumerator.MoveNext()) // Move to the first element
|
||||
{
|
||||
foreach (ThreatInfo threatInfo in threatInfos) {
|
||||
assignments.Add(new IAssignment.AssignmentItem(assignableInterceptorsEnumerator.Current, threatInfo.ThreatData.Threat));
|
||||
if (!assignableInterceptorsEnumerator.MoveNext()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return assignments;
|
||||
}
|
||||
|
||||
|
||||
private List<ThreatInfo> CalculateThreatLevels(List<ThreatData> threatTable,
|
||||
Vector3 defensePosition) {
|
||||
List<ThreatInfo> threatInfos = new List<ThreatInfo>();
|
||||
|
||||
foreach (ThreatData threatData in threatTable) {
|
||||
Threat threat = threatData.Threat;
|
||||
float distanceToMean = Vector3.Distance(threat.transform.position, defensePosition);
|
||||
float velocityMagnitude = threat.GetVelocity().magnitude;
|
||||
|
||||
// Calculate threat level based on proximity and velocity
|
||||
float threatLevel = (1 / distanceToMean) * velocityMagnitude;
|
||||
|
||||
threatInfos.Add(new ThreatInfo(threatData, threatLevel));
|
||||
}
|
||||
|
||||
// Sort threats in descending order
|
||||
return threatInfos.OrderByDescending(t => t.ThreatLevel).ToList();
|
||||
}
|
||||
|
||||
private class ThreatInfo {
|
||||
public ThreatData ThreatData { get; }
|
||||
public float ThreatLevel { get; }
|
||||
|
||||
public ThreatInfo(ThreatData threatData, float threatLevel) {
|
||||
ThreatData = threatData;
|
||||
ThreatLevel = threatLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e9829915a9eb41409ea03fb46910432
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user