Raw File
target_space.pyc
ó
‡oŠ\c@s?ddlZddlmZd„Zdefd„ƒYZdS(iÿÿÿÿNi(t
ensure_rngcCsttt|ƒƒS(s3 ensure that an point is hashable by a python dict (ttupletmaptfloat(tx((s$N:\TCAD\SLALOM\Bayes\target_space.pyt	_hashablestTargetSpacecBs×eZdZdd„Zd„Zd„Zed„ƒZed„ƒZ	ed„ƒZ
ed„ƒZed„ƒZed	„ƒZ
d
„Zd„Zd„Zd
„Zd„Zd„Zd„Zd„Zd„ZRS(sÓ
    Holds the param-space coordinates (X) and target values (Y)
    Allows for constant-time appends while ensuring no duplicates are added

    Example
    -------
    >>> def target_func(p1, p2):
    >>>     return p1 + p2
    >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
    >>> space = TargetSpace(target_func, pbounds, random_state=0)
    >>> x = space.random_points(1)[0]
    >>> y = space.register_point(x)
    >>> assert self.max_point()['max_val'] == y
    cCs®t|ƒ|_||_t|ƒ|_tjgt|jƒdd„ƒD]}|d^qIdtjƒ|_	tj
dd|jfƒ|_tj
ddƒ|_
i|_dS(sp
        Parameters
        ----------
        target_func : function
            Function to be maximized.

        pbounds : dict
            Dictionary with parameters names as keys and a tuple with minimum
            and maximum values.

        random_state : int, RandomState, or None
            optionally specify a seed for a random number generator
        tkeycSs|dS(Ni((R((s$N:\TCAD\SLALOM\Bayes\target_space.pyt<lambda>0sitdtypetshapeiN(Rtrandom_statettarget_functsortedt_keystnptarraytitemsRt_boundstemptytdimt_paramst_targett_cache(tselfRtpboundsRtitem((s$N:\TCAD\SLALOM\Bayes\target_space.pyt__init__s	2cCst|ƒ|jkS(N(RR(RR((s$N:\TCAD\SLALOM\Bayes\target_space.pyt__contains__;scCs1t|jƒt|jƒks$t‚t|jƒS(N(tlenRRtAssertionError(R((s$N:\TCAD\SLALOM\Bayes\target_space.pyt__len__>s$cCst|ƒdkS(Ni(R(R((s$N:\TCAD\SLALOM\Bayes\target_space.pyRBscCs|jS(N(R(R((s$N:\TCAD\SLALOM\Bayes\target_space.pytparamsFscCs|jS(N(R(R((s$N:\TCAD\SLALOM\Bayes\target_space.pyttargetJscCs
t|jƒS(N(RR(R((s$N:\TCAD\SLALOM\Bayes\target_space.pyRNscCs|jS(N(R(R((s$N:\TCAD\SLALOM\Bayes\target_space.pytkeysRscCs|jS(N(R(R((s$N:\TCAD\SLALOM\Bayes\target_space.pytboundsVscCs‹y%t|ƒt|jƒks$t‚Wn<tk
rctdjt|ƒƒdj|jƒƒ‚nXtjg|jD]}||^qtƒS(NsParameters' keys ({}) do s(not match the expected set of keys ({}).(tsetR"Rt
ValueErrortformatR
Rtasarray(RR R((s$N:\TCAD\SLALOM\Bayes\target_space.pytparams_to_arrayZs%
cCs€y%t|ƒt|jƒks$t‚WnBtk
ritdjt|ƒƒdjt|jƒƒƒ‚nXtt|j|ƒƒS(Ns)Size of array ({}) is different than the s#expected number of parameters ({}).(RR"RR%R&tdicttzip(RR((s$N:\TCAD\SLALOM\Bayes\target_space.pytarray_to_paramsds%
 cCs­ytj|dtƒ}Wn tk
r;|j|ƒ}nX|jƒ}y|j|jksct‚WnBtk
r¨t	dj
t|ƒƒdj
t|jƒƒƒ‚nX|S(NR	s)Size of array ({}) is different than the s#expected number of parameters ({}).(
RR'Rt	TypeErrorR(traveltsizeRRR%R&RR"(RR((s$N:\TCAD\SLALOM\Bayes\target_space.pyt	_as_arrayns

 cCs•|j|ƒ}||kr3tdj|ƒƒ‚n||jt|jƒƒ<tj|j|j	ddƒgƒ|_tj|j
|ggƒ|_
dS(s¾
        Append a point and its target value to the known data.

        Parameters
        ----------
        x : ndarray
            a single point, with len(x) == self.dim

        y : float
            target function value

        Raises
        ------
        KeyError:
            if the point is not unique

        Notes
        -----
        runs in ammortized constant time

        Example
        -------
        >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
        >>> space = TargetSpace(lambda p1, p2: p1 + p2, pbounds)
        >>> len(space)
        0
        >>> x = np.array([0, 0])
        >>> y = 1
        >>> space.add_observation(x, y)
        >>> len(space)
        1
        sData point {} is not uniqueiiÿÿÿÿN(R/tKeyErrorR&RRR-RtconcatenateRtreshapeR(RR R!R((s$N:\TCAD\SLALOM\Bayes\target_space.pytregister~s!'cCsu|j|ƒ}y|jt|ƒ}WnHtk
rptt|j|ƒƒ}|j|}|j||ƒnX|S(s—
        Evaulates a single point x, to obtain the value y and then records them
        as observations.

        Notes
        -----
        If x has been previously seen returns a cached value of y.

        Parameters
        ----------
        x : ndarray
            a single point, with len(x) == self.dim

        Returns
        -------
        y : float
            target function value.
        (	R/RRR0R)R*RRR3(RR RR!((s$N:\TCAD\SLALOM\Bayes\target_space.pytprobe©s
cCsjtjd|jfƒ}xEt|jƒD]4\}\}}|jj||ddƒ|j|<q(W|jƒS(sò
        Creates random points within the bounds of the space.

        Returns
        ----------
        data: ndarray
            [num x dim] array points with dimensions corresponding to `self._keys`

        Example
        -------
        >>> target_func = lambda p1, p2: p1 + p2
        >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
        >>> space = TargetSpace(target_func, pbounds, random_state=0)
        >>> space.random_points(1)
        array([[ 55.33253689,   0.54488318]])
        iR.(	RRRt	enumerateRRtuniformtTR-(Rtdatatcoltlowertupper((s$N:\TCAD\SLALOM\Bayes\target_space.pyt
random_sampleÆs"&cCsayCi|jjƒd6tt|j|j|jjƒƒƒd6}Wntk
r\i}nX|S(s;Get maximum target value found and corresponding parametes.R!R (R!tmaxR)R*R"R targmaxR%(Rtres((s$N:\TCAD\SLALOM\Bayes\target_space.pyR=Ýs-

cCseg|jD]}tt|j|ƒƒ^q
}gt|j|ƒD] \}}i|d6|d6^qAS(s8Get all target values found and corresponding parametes.R!R (R R)R*R"R!(RtpR R!tparam((s$N:\TCAD\SLALOM\Bayes\target_space.pyR?ês.cCsDx=t|jƒD],\}}||kr|||j|<qqWdS(sÙ
        A method that allows changing the lower and upper searching bounds

        Parameters
        ----------
        new_bounds : dict
            A dictionary with the parameter name and its new bounds
        N(R5R"R(Rt
new_boundstrowR((s$N:\TCAD\SLALOM\Bayes\target_space.pyt
set_boundsós	N(t__name__t
__module__t__doc__tNoneRRRtpropertyRR R!RR"R#R(R+R/R3R4R<R=R?RD(((s$N:\TCAD\SLALOM\Bayes\target_space.pyR
s&"			
	
		+			
		(tnumpyRtutilRRtobjectR(((s$N:\TCAD\SLALOM\Bayes\target_space.pyt<module>s	
back to top