1
1
import collections .abc
2
2
import itertools
3
3
import math
4
- from copy import deepcopy
4
+ from copy import copy , deepcopy
5
5
from typing import Any , Callable , Dict , List , Optional , Sequence , Set , Tuple , Union
6
6
7
7
import cloudpickle
@@ -290,6 +290,7 @@ def __init__(
290
290
self ._dx_eps = 2 * max (np .abs (bounds )) * np .finfo (float ).eps
291
291
292
292
self .bounds = list (bounds )
293
+ self .__missing_bounds = set (self .bounds ) # cache of missing bounds
293
294
294
295
self ._vdim : Optional [int ] = None
295
296
@@ -325,6 +326,8 @@ def npoints(self) -> int:
325
326
326
327
@cache_latest
327
328
def loss (self , real : bool = True ) -> float :
329
+ if self ._missing_bounds ():
330
+ return np .inf
328
331
losses = self .losses if real else self .losses_combined
329
332
if not losses :
330
333
return np .inf
@@ -604,6 +607,15 @@ def ask(self, n: int, tell_pending: bool = True) -> Tuple[List[float], List[floa
604
607
605
608
return points , loss_improvements
606
609
610
+ def _missing_bounds (self ) -> List [Real ]:
611
+ missing_bounds = []
612
+ for b in copy (self .__missing_bounds ):
613
+ if b in self .data :
614
+ self .__missing_bounds .remove (b )
615
+ elif b not in self .pending_points :
616
+ missing_bounds .append (b )
617
+ return sorted (missing_bounds )
618
+
607
619
def _ask_points_without_adding (self , n : int ) -> Tuple [List [float ], List [float ]]:
608
620
"""Return 'n' points that are expected to maximally reduce the loss.
609
621
Without altering the state of the learner"""
@@ -619,12 +631,7 @@ def _ask_points_without_adding(self, n: int) -> Tuple[List[float], List[float]]:
619
631
return [], []
620
632
621
633
# If the bounds have not been chosen yet, we choose them first.
622
- missing_bounds = [
623
- b
624
- for b in self .bounds
625
- if b not in self .data and b not in self .pending_points
626
- ]
627
-
634
+ missing_bounds = self ._missing_bounds ()
628
635
if len (missing_bounds ) >= n :
629
636
return missing_bounds [:n ], [np .inf ] * n
630
637
0 commit comments