Skip to content

Commit

Permalink
Merge pull request #694 from dstl/optimised_SM_edits
Browse files Browse the repository at this point in the history
Fix OptimizeBruteSensorManager
  • Loading branch information
sdhiscocks authored Aug 16, 2022
2 parents f8b8edf + c1a71e7 commit e7101f6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
30 changes: 25 additions & 5 deletions stonesoup/sensormanager/optimise.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ class OptimizeBruteSensorManager(_OptimizeSensorManager):
doc="Number of grid points to search along axis. See Ns in "
":func:`~.scipy.optimize.brute`. "
"Default is 10.")
full_output: bool = Property(default=False,
doc="If True, returns the evaluation grid and the objective "
"function's values on it.")
generate_full_output: bool = Property(default=False,
doc="If True, returns the evaluation grid "
"and the objective "
"function's values on it.")
finish: bool = Property(default=False,
doc="A polishing function can be applied to the result of brute "
"force minimisation. If True this is set as "
Expand All @@ -94,15 +95,34 @@ def _optimiser(self, optimise_func, all_action_generators):
result = brute(optimise_func,
ranges=ranges,
Ns=self.n_grid_points,
full_output=self.full_output,
full_output=self.generate_full_output,
finish=self.finish_func,
disp=self.disp)

if self.full_output:
if self.generate_full_output:
self.full_output = result
result = result[0]

return np.atleast_1d(result)

def get_full_output(self):
"""
Returns the output generated when `generate_full_output=True` for the most recent
time step.
This returns the evaluation grid and reward function's values on it, as generated by the
:meth:`optimize.brute` method.
See
`Scipy documentation
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.brute.html#scipy.optimize.brute>`_
for full details.
Returns
-------
full_output: tuple
"""
if self.full_output:
return self.full_output


class OptimizeBasinHoppingSensorManager(_OptimizeSensorManager):
"""
Expand Down
24 changes: 21 additions & 3 deletions stonesoup/sensormanager/tests/test_sensormanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_random_choose_actions():
assert isinstance(actions[0], ChangeDwellAction)


def test_brute_force_choose_actions():
def test_uncertainty_based_managers():
time_start = datetime.now()

tracks = [Track(states=[
Expand Down Expand Up @@ -114,18 +114,34 @@ def test_brute_force_choose_actions():
max_range=np.inf,
)}

for sensor_set in [sensorsA, sensorsB, sensorsC]:
sensorsD = {RadarRotatingBearingRange(
position_mapping=(0, 2),
noise_covar=np.array([[np.radians(0.5) ** 2, 0],
[0, 0.75 ** 2]]),
position=np.array([[0], [0]]),
ndim_state=4,
rpm=60,
fov_angle=np.radians(30),
dwell_centre=StateVector([0.0]),
max_range=np.inf,
)}

for sensor_set in [sensorsA, sensorsB, sensorsC, sensorsD]:
for sensor in sensor_set:
sensor.timestamp = time_start

sensor_managerA = BruteForceSensorManager(sensorsA, reward_function)
sensor_managerB = OptimizeBruteSensorManager(sensorsB, reward_function)
sensor_managerC = OptimizeBasinHoppingSensorManager(sensorsC,
reward_function)
sensor_managerD = OptimizeBruteSensorManager(sensorsD, reward_function,
generate_full_output=True,
finish=True)

sensor_managers = [sensor_managerA,
sensor_managerB,
sensor_managerC]
sensor_managerC,
sensor_managerD]

timesteps = []
for t in range(3):
Expand Down Expand Up @@ -159,3 +175,5 @@ def test_brute_force_choose_actions():

assert all_dwell_centres[0][0] == all_dwell_centres[1][0] == all_dwell_centres[2][0]
assert all_dwell_centres[0][1] == all_dwell_centres[1][1] == all_dwell_centres[2][1]

assert isinstance(sensor_managerD.get_full_output(), tuple)

0 comments on commit e7101f6

Please sign in to comment.