Ejemplo n.º 1
0
    def test_constructor(self, finalPositions, targetIds, targetPriorities):
        # Create a new TargetGroup instance using only the target positions
        targets = TargetGroup(finalPositions)

        # Check that the number of targets and the positions are correct
        assert targets.nTargets == len(finalPositions)
        assert np.all(targets.positions == finalPositions)

        # Check that the default ids and priorities are correct
        assert np.all(targets.ids == ["0", "1", "2", "3"])
        assert np.all(targets.priorities == [1, 1, 1, 1])

        # Create a new TargetGroup instance with all the inputs
        targets = TargetGroup(finalPositions, targetIds, targetPriorities)

        # Check that the ids, positions and priorities are correct
        assert np.all(targets.ids == targetIds)
        assert np.all(targets.positions == finalPositions)
        assert np.all(targets.priorities == targetPriorities)

        # Create a new TargetGroup instance with one NULL target
        targetIds[1] = NULL_TARGET_ID
        targets = TargetGroup(finalPositions, targetIds, targetPriorities)

        # Check that the ids, positions and priorities are correct
        notNull = targetIds != NULL_TARGET_ID
        assert np.all(targets.ids[notNull] == targetIds[notNull])
        assert np.all(targets.positions[notNull] == finalPositions[notNull])
        assert np.all(targets.priorities[notNull] == targetPriorities[notNull])
        assert np.all(targets.ids[~notNull] == NULL_TARGET_ID)
        assert np.all(targets.positions[~notNull] == NULL_TARGET_POSITION)
        assert np.all(targets.priorities[~notNull] == NULL_TARGET_PRIORITY)
Ejemplo n.º 2
0
    def test_constructor_exception(self, finalPositions, targetIds,
                                   targetPriorities):
        # Check that the constructor raises a exception when the input array
        # lengths do not coincide
        with pytest.raises(ValueError):
            TargetGroup(finalPositions, targetIds[1:], targetPriorities)

        with pytest.raises(ValueError):
            TargetGroup(finalPositions, targetIds, targetPriorities[2:])
Ejemplo n.º 3
0
    def test_addToFigure_method(self, finalPositions, targetIds,
                                targetPriorities):
        # Create a new TargetGroup instance with all the inputs
        targets = TargetGroup(finalPositions, targetIds, targetPriorities)

        # Plot the targets
        plt.figure()
        targets.addToFigure()
        plt.show(block=False)
Ejemplo n.º 4
0
    def test_select_method(self, finalPositions, targetIds, targetPriorities):
        # Create a new TargetGroup instance with all the inputs
        targets = TargetGroup(finalPositions, targetIds, targetPriorities)

        # Select some of the targets
        indices = [1, 2]
        selectedTargets = targets.select(indices)

        # Check that the result makes sense
        assert selectedTargets.nTargets == len(indices)
        assert np.all(selectedTargets.ids == targetIds[indices])
        assert np.all(selectedTargets.positions == finalPositions[indices])
        assert np.all(selectedTargets.priorities == targetPriorities[indices])
Ejemplo n.º 5
0
    def test_getSelectedTargets_method(self):
        # Create a basic bench with 2 cobras
        cobraCenters = np.array([0, 5], dtype=np.complex)
        bench = Bench(cobraCenters)

        # Create some targets
        finalPositions = np.array([0, 3, NULL_TARGET_POSITION, 6])
        targets = TargetGroup(finalPositions)

        # Create a dummy TargetSelector
        selector = TargetSelectorSubclass(bench, targets)

        # Assign the cobras to valid targets
        indices = np.array([3, 0])
        selector.assignedTargetIndices = indices

        # Get the selected targets
        selectedTargers = selector.getSelectedTargets()

        # Check that we get the correct targets
        assert np.all(selectedTargers.positions == finalPositions[indices])
        assert np.all(selectedTargers.ids == indices.astype(np.str))
        assert np.all(selectedTargers.priorities == 1)

        # Assign one of the cobras to the NULL target
        indices = np.array([1, 2])
        selector.assignedTargetIndices = indices

        # Get the selected targets
        selectedTargers = selector.getSelectedTargets()

        # Check that we get the NULL target
        assert selectedTargers.positions[1] == NULL_TARGET_POSITION
        assert selectedTargers.ids[1] == NULL_TARGET_ID
        assert selectedTargers.priorities[1] == NULL_TARGET_PRIORITY
Ejemplo n.º 6
0
    def test_saveToFile_method(self, finalPositions, targetIds,
                               targetPriorities, tmpdir):
        # Create a new TargetGroup instance with all the inputs
        targets = TargetGroup(finalPositions, targetIds, targetPriorities)

        # Write the targets information into a file
        fileName = str(tmpdir.join("myTestTargets.dat"))
        targets.saveToFile(fileName)

        # Read back the targets file
        targets = TargetGroup.fromFile(fileName)

        # Check that the ids, positions and priorities are correct
        assert np.all(targets.ids == targetIds)
        assert np.all(targets.positions == finalPositions)
        assert np.all(targets.priorities == targetPriorities)
Ejemplo n.º 7
0
    def test_fromFile_method(self, finalPositions, targetIds, targetPriorities,
                             tmpdir):
        # Write the targets information into a file
        fileName = str(tmpdir.join("myTestTargets.dat"))

        with open(fileName, "w+") as f:
            for p, i, pr in zip(finalPositions, targetIds, targetPriorities):
                f.write("%s, %s, %s, %s\n" % (np.real(p), np.imag(p), i, pr))

        # Create a new TargetGroup instance from the file
        targets = TargetGroup.fromFile(fileName)

        # Check that the ids, positions and priorities are correct
        assert np.all(targets.ids == targetIds)
        assert np.all(targets.positions == finalPositions)
        assert np.all(targets.priorities == targetPriorities)
Ejemplo n.º 8
0
    def test_getTargetsInsidePatrolArea_method(self):
        # Create a basic bench with 2 cobras
        cobraCenters = np.array([0, 5], dtype=np.complex)
        bench = Bench(cobraCenters)

        # Create some targets
        finalPositions = np.array([0, cobraCenters.mean(), -3, 6])
        targets = TargetGroup(finalPositions)

        # Create a dummy TargetSelector
        selector = TargetSelectorSubclass(bench, targets)

        # Get the targets that fall inside the first cobra
        indices, positions, distances = selector.getTargetsInsidePatrolArea(0)

        # Check that we get what we expected
        expectedIndices = np.array([1, 2])
        expectedPositions = finalPositions[expectedIndices]
        expectedDistances = np.abs(cobraCenters[0] - expectedPositions)
        assert np.all(indices == expectedIndices)
        assert np.all(positions == expectedPositions)
        assert np.all(np.abs(distances - expectedDistances) < 1e-10)

        # Get the targets that fall inside the second cobra
        indices, positions, distances = selector.getTargetsInsidePatrolArea(1)

        # Check that we get what we expected
        expectedIndices = np.array([3, 1])
        expectedPositions = finalPositions[expectedIndices]
        expectedDistances = np.abs(cobraCenters[1] - expectedPositions)
        assert np.all(indices == expectedIndices)
        assert np.all(positions == expectedPositions)
        assert np.all(np.abs(distances - expectedDistances) < 1e-10)

        # Construct the KD tree
        selector.constructKDTree()

        # Get again the targets that fall inside the second cobra
        indices, positions, distances = selector.getTargetsInsidePatrolArea(1)

        # Check that we get what we expected
        expectedIndices = np.array([3, 1])
        expectedPositions = finalPositions[expectedIndices]
        expectedDistances = np.abs(cobraCenters[1] - expectedPositions)
        assert np.all(indices == expectedIndices)
        assert np.all(positions == expectedPositions)
        assert np.all(np.abs(distances - expectedDistances) < 1e-10)
Ejemplo n.º 9
0
    def test_constructKDTree_method(self):
        # Create a basic bench with 2 cobras
        cobraCenters = np.array([0, 5], dtype=np.complex)
        bench = Bench(cobraCenters)

        # Create some random targets
        finalPositions = 8 * np.random.random(50) + 8j * np.random.random(50)
        targets = TargetGroup(finalPositions)

        # Create a dummy TargetSelector
        selector = TargetSelectorSubclass(bench, targets)

        # Check that the KD tree is not available
        assert selector.kdTree is None

        # Construct the KD tree
        selector.constructKDTree()

        # Check that the KD tree is available
        assert selector.kdTree is not None

        # Check that it contains the expected data
        assert np.all(selector.kdTree.data == np.column_stack(
            (finalPositions.real, finalPositions.imag)))
Ejemplo n.º 10
0
    def test_solveEndPointCollisions_method(self):
        # Create a basic bench with 2 cobras
        cobraCenters = np.array([0, 5], dtype=np.complex)
        bench = Bench(cobraCenters)

        # Create some targets
        rMin = bench.cobras.rMin
        finalPositions = np.array([cobraCenters[0] + 1.2 * rMin[0],
                                    cobraCenters.mean() - 0.1,
                                    cobraCenters.mean(),
                                    cobraCenters[1] + 1.2 * rMin[1]])
        targets = TargetGroup(finalPositions)

        # Create a dummy TargetSelector
        selector = TargetSelectorSubclass(bench, targets)

        # Calculate the accessible targets for each cobra
        selector.calculateAccessibleTargets()

        # Assign the targets in a way that we have an end-point collision
        selector.assignedTargetIndices = np.array([1, 2])

        # Solve the cobra end-point collisions
        selector.solveEndPointCollisions()

        # Check that we get the correct assignment
        assert np.all(selector.assignedTargetIndices == [0, 3])

        # Assign the targets in a way that we don't have an end-point collision
        selector.assignedTargetIndices = np.array([0, 2])

        # Solve the cobra end-point collisions
        selector.solveEndPointCollisions()

        # Check that we get the same assignment
        assert np.all(selector.assignedTargetIndices == [0, 2])