Skip to content

BalancingLearner: add a "cycle" strategy, sampling the learners one by one #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions adaptive/learner/balancing_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Iterable
from contextlib import suppress
from functools import partial
import itertools
from operator import itemgetter

import numpy as np
Expand Down Expand Up @@ -50,10 +51,11 @@ class BalancingLearner(BaseLearner):
function : callable
A function that calls the functions of the underlying learners.
Its signature is ``function(learner_index, point)``.
strategy : 'loss_improvements' (default), 'loss', or 'npoints'
strategy : 'loss_improvements' (default), 'loss', 'npoints', or 'cycle'.
The points that the `BalancingLearner` choses can be either based on:
the best 'loss_improvements', the smallest total 'loss' of the
child learners, or the number of points per learner, using 'npoints'.
child learners, the number of points per learner, using 'npoints',
or by cycling through the learners one by one using 'cycle'.
One can dynamically change the strategy while the simulation is
running by changing the ``learner.strategy`` attribute.

Expand Down Expand Up @@ -90,10 +92,11 @@ def __init__(self, learners, *, cdims=None, strategy="loss_improvements"):

@property
def strategy(self):
"""Can be either 'loss_improvements' (default), 'loss', or 'npoints'
The points that the `BalancingLearner` choses can be either based on:
the best 'loss_improvements', the smallest total 'loss' of the
child learners, or the number of points per learner, using 'npoints'.
"""Can be either 'loss_improvements' (default), 'loss', 'npoints', or
'cycle'. The points that the `BalancingLearner` choses can be either
based on: the best 'loss_improvements', the smallest total 'loss' of
the child learners, the number of points per learner, using 'npoints',
or by going through all learners one by one using 'cycle'.
One can dynamically change the strategy while the simulation is
running by changing the ``learner.strategy`` attribute."""
return self._strategy
Expand All @@ -107,10 +110,13 @@ def strategy(self, strategy):
self._ask_and_tell = self._ask_and_tell_based_on_loss
elif strategy == "npoints":
self._ask_and_tell = self._ask_and_tell_based_on_npoints
elif strategy == "cycle":
self._ask_and_tell = self._ask_and_tell_based_on_cycle
self._cycle = itertools.cycle(range(len(self.learners)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the cycle will be reset every time the strategy is set dynamically. I'm not sure what the best thing to do here is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be the intention? We could also just put in in __init__...

else:
raise ValueError(
'Only strategy="loss_improvements", strategy="loss", or'
' strategy="npoints" is implemented.'
'Only strategy="loss_improvements", strategy="loss",'
' strategy="npoints", or strategy="cycle" is implemented.'
)

def _ask_and_tell_based_on_loss_improvements(self, n):
Expand Down Expand Up @@ -173,6 +179,17 @@ def _ask_and_tell_based_on_npoints(self, n):
points, loss_improvements = map(list, zip(*selected))
return points, loss_improvements

def _ask_and_tell_based_on_cycle(self, n):
points, loss_improvements = [], []
for _ in range(n):
index = next(self._cycle)
point, loss_improvement = self.learners[index].ask(n=1)
points.append((index, point[0]))
loss_improvements.append(loss_improvement[0])
self.tell_pending((index, point[0]))

return points, loss_improvements

def ask(self, n, tell_pending=True):
"""Chose points for learners."""
if n == 0:
Expand Down
8 changes: 6 additions & 2 deletions adaptive/tests/test_balancing_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from adaptive.runner import simple


strategies = ["loss", "loss_improvements", "npoints", "cycle"]


def test_balancing_learner_loss_cache():
learner = Learner1D(lambda x: x, bounds=(-1, 1))
learner.tell(-1, -1)
Expand All @@ -26,7 +29,7 @@ def test_balancing_learner_loss_cache():
assert bl.loss(real=True) == real_loss


@pytest.mark.parametrize("strategy", ["loss", "loss_improvements", "npoints"])
@pytest.mark.parametrize("strategy", strategies)
def test_distribute_first_points_over_learners(strategy):
for initial_points in [0, 3]:
learners = [Learner1D(lambda x: x, bounds=(-1, 1)) for i in range(10)]
Expand All @@ -41,7 +44,7 @@ def test_distribute_first_points_over_learners(strategy):
assert len(set(i_learner)) == len(learners)


@pytest.mark.parametrize("strategy", ["loss", "loss_improvements", "npoints"])
@pytest.mark.parametrize("strategy", strategies)
def test_ask_0(strategy):
learners = [Learner1D(lambda x: x, bounds=(-1, 1)) for i in range(10)]
learner = BalancingLearner(learners, strategy=strategy)
Expand All @@ -55,6 +58,7 @@ def test_ask_0(strategy):
("loss", lambda l: l.loss() < 0.1),
("loss_improvements", lambda l: l.loss() < 0.1),
("npoints", lambda bl: all(l.npoints > 10 for l in bl.learners)),
("cycle", lambda l: l.loss() < 0.1),
],
)
def test_strategies(strategy, goal):
Expand Down