Ejemplo n.º 1
0
        def _score_with_reward_function(references: np.array,
                                        hypotheses: np.array) -> np.array:
            """Score (time, batch) arrays with sentence-based reward function.

            Parts of the sentence after generated <pad> or </s> are ignored.
            BPE-postprocessing is also included.

            :param references: indices of references, shape (time, batch)
            :param hypotheses: indices of hypotheses, shape (time, batch)
            :return: an array of batch length with float rewards
            """
            rewards = []
            for refs, hyps in zip(references.transpose(),
                                  hypotheses.transpose()):
                ref_seq = []
                hyp_seq = []
                for r_token in refs:
                    token = self.decoder.vocabulary.index_to_word[r_token]
                    if token in (END_TOKEN, PAD_TOKEN):
                        break
                    ref_seq.append(token)
                for h_token in hyps:
                    token = self.decoder.vocabulary.index_to_word[h_token]
                    if token in (END_TOKEN, PAD_TOKEN):
                        break
                    hyp_seq.append(token)
                # join BPEs, split on " " to prepare list for evaluator
                refs_tokens = " ".join(ref_seq).replace("@@ ", "").split(" ")
                hyps_tokens = " ".join(hyp_seq).replace("@@ ", "").split(" ")
                reward = float(self.reward_function([hyps_tokens],
                                                    [refs_tokens]))
                rewards.append(reward)
            return np.array(rewards, dtype=np.float32)
Ejemplo n.º 2
0
    def update(self, y_t: np.array, y_pred: np.array):
        y_t = y_t.flatten()
        y_pred = y_pred.flatten()
        n_obs = self.model_info.endog.shape[0]
        m = self.model

        # Residual Calculation
        eps_t = y_t - y_pred
        m.resid = np.insert(m.resid, n_obs, eps_t, axis=0)
        # Add data point
        self.model_info.endog = np.insert(self.model_info.endog, n_obs, y_t, axis=0)
Ejemplo n.º 3
0
def array2pil(a: np.array) -> Image:
    if a.dtype == np.dtype("B"):
        if a.ndim == 2:
            return Image.frombytes("L", (a.shape[1], a.shape[0]),
                                   a.tostring())
        elif a.ndim == 3:
            return Image.frombytes("RGB", (a.shape[1], a.shape[0]),
                                   a.tostring())
        else:
            raise Exception("bad image rank")
    elif a.dtype == np.dtype('float32'):
        return Image.frombytes("F", (a.shape[1], a.shape[0]), a.tostring())
    else:
        raise Exception("unknown image type")
Ejemplo n.º 4
0
 def __init__(self, A: np.array=np.zeros((3,4)), b: np.array=np.zeros(3), d: np.array=np.zeros(4)):
     self.c = A.flatten()
     self.shape = A.shape
     self.A_eq = np.hstack([np.identity(self.shape[1])] * self.shape[0])
     self.A_ub = block_diag(*[np.ones(self.shape[1])] * self.shape[0])
     self.b = b
     self.d = d
Ejemplo n.º 5
0
    def __init__(self, n_input: int, n_output: int, W: nparray, b: nparray, activation: Elemwise = T.tanh):
        """
        A layer of a neural network, computes s(Wx + b) where s is a nonlinearity and x is the input vector.

        :parameters:
            - rng: numpy random state
            - n_in: input dimensionality
            - n_out: output dimensionality
            - W: np.array, shape=(n_in, n_out)
                Optional weight matrix, if not given is initialised randomly.
            - b: np.array, shape=(n_out,)
                Optional bias vector, if not given is initialised randomly.
            - activation : theano.tensor.elemwise.Elemwise
                Activation function for layer output
        """
        assert W.shape == (n_input, n_output), \
            'W does not match the expected dimensionality (%d, %d) != %s' % (n_input, n_output, W.shape)
        assert b.shape == (n_output,), 'b does not match the expected dimensionality (%d,) != %s' % (n_output, b.shape)

        self.n_input = n_input
        self.n_output = n_output
        # All parameters should be shared variables.
        # They're used in this class to compute the layer output,
        # but are updated elsewhere when optimizing the network parameters.
        # Note that we are explicitly requiring that W_init has the theano.config.floatX dtype
        self.W = theano.shared(value=W.astype(theano.config.floatX),
                               # The name parameter is solely for printing purporses
                               name='W',
                               # Setting borrow=True allows Theano to use user memory for this object.
                               # It can make code slightly faster by avoiding a deep copy on construction.
                               # For more details, see
                               # http://deeplearning.net/software/theano/tutorial/aliasing.html
                               borrow=True)

        # We can force our bias vector b to be a column vector using numpy's reshape method.
        # When b is a column vector, we can pass a matrix-shaped input to the layer
        # and get a matrix-shaped output, thanks to broadcasting (described below)
        self.b = theano.shared(value=b.astype(theano.config.floatX),
                               name='b',
                               borrow=True)

        self.activation = activation

        # We'll compute the gradient of the cost of the network with respect to the parameters in this list.
        self.params = [self.W, self.b]
Ejemplo n.º 6
0
    def __init__(self, list_lagged_parameters: list,
                 response_array: np.array,
                 time_array: pd.DatetimeIndex,
                 use_constant: bool=True,
                 data_variance: np.array=None):
        self.list_lagged_parameters = list_lagged_parameters.copy()
        self.response_array = response_array.copy()
        self.time_array = time_array.values.copy()
        self.use_constant = use_constant
        self.use_weights = data_variance is not None

        self.num_params = len(self.list_lagged_parameters)
        self.list_labels = []
        self.max_shift = 0

        if self.use_constant:
            self.num_params += 1
            self.list_labels.append('Constant')

        # Find max shift, necessary to clip all data to the same size
        for param in self.list_lagged_parameters:
            self.list_labels.append(param.label)
            if param.shift_size > self.max_shift:
                self.max_shift = param.shift_size

        # Clip initial points in the un-shifted response and time data
        self.response_array = self.response_array[self.max_shift:]
        self.time_array = self.time_array[self.max_shift:]
        self.clipped_data_length = len(self.response_array)
        if self.use_weights:
            self.weights = 1.0 / data_variance[self.max_shift:]

        self.list_lagged_arrays = []
        if self.use_constant:
            self.constant_array = np.ones(self.clipped_data_length)
            self.list_lagged_arrays.append(self.constant_array)

        # Extract clipped views of the lagged parameters
        for param in self.list_lagged_parameters:
            shift = self.max_shift - param.shift_size
            self.list_lagged_arrays.append(param.data[shift:])

        # Range of data to use in fit
        self.start_index = 0
        self.end_index = self.clipped_data_length - 1

        self.has_results = False
        self.fit_matrix = None
        self.model = None
        self.results = None
        self.auto_correlation_matrix = None
        self.time_axis = None
        self.response_cut = None
Ejemplo n.º 7
0
def causal_rect_window_filter(data: np.array, window_length: int) -> np.array:
    window_length = np.round(window_length)
    if window_length > 0:
        front_pad = window_length - 1
        end_pad = 0
        norm_window = np.repeat(1.0, window_length) / window_length
        data_long = np.pad(data, ((front_pad, end_pad),), mode='edge')
        # avoid fftconvolve as it distributes one input NaN to entire output
        data_filtered = sps.convolve(data_long, norm_window, 'valid')
    else:
        data_filtered = data.copy()
    return data_filtered
Ejemplo n.º 8
0
def causal_hann_window_filter(data: np.array, window_length: int) -> np.array:
    window_length = np.round(window_length)
    if window_length > 0:
        front_pad = window_length
        end_pad = 1
        norm_window = sps.hann(window_length + 2)
        norm_window /= np.sum(norm_window)
        data_long = np.pad(data, ((front_pad, end_pad),), mode='edge')
        # avoid fftconvolve as it distributes one input NaN to entire output
        data_filtered = sps.convolve(data_long, norm_window, 'valid')
    else:
        data_filtered = data.copy()
    return data_filtered
Ejemplo n.º 9
0
    def __init__(self, X: np.array, Y: np.array, tune_parameters=False):
        super().__init__(X, Y, tune_parameters=False)
        self.X = X.todense()  # TensorFlow/Skflow doesn't support sparse matrices
        output_layer = len(np.unique(Y))
        if tune_parameters:
            self.param_dist_random = {'learning_rate': random.random(100),
                                      'optimizer': ['Adam'],
                                      'hidden_units': [sp_randint(50, 500), sp_randint(50, 500)]}

        self.clf = skflow.TensorFlowDNNClassifier(hidden_units=self.hidden_units,
                                                  n_classes=output_layer, steps=self.steps,
                                                  learning_rate=self.learning_rate, verbose=0,
                                                  optimizer=self.optimizer)
Ejemplo n.º 10
0
def rect_window_filter(data: np.array, window_length: int) -> np.array:
    window_length = np.round(window_length)
    if window_length:
        if window_length % 2 == 0:  # even length window
            front_pad = window_length/2
            end_pad = window_length/2
            norm_window = np.repeat(1.0, window_length + 1)
            norm_window[0] = 0.5
            norm_window[-1] = 0.5
            norm_window /= window_length
        else:  # odd length window
            front_pad = (window_length - 1)/2
            end_pad = front_pad
            norm_window = np.repeat(1.0, window_length) / window_length
        data_long = np.pad(data, ((front_pad, end_pad),), mode='edge')
        data_filtered = sps.convolve(data_long, norm_window, 'valid')
    else:
        data_filtered = data.copy()
    return data_filtered
Ejemplo n.º 11
0
    def __init__(self, load: np.array,
                 name:str='xxxxxx',
                 powerpool: str='UD-E_PowerPool',  # ATTR_NOT_ASSIGNED not allowed
                 max_flex: np.array=None,
                 min_flex: np.array=None,
                 api=remote.API('127.0.0.1', '')
                 ):
        super().__init__()
        self.API = api
        self.name = name
        self.powerpool = slugify(powerpool)
        # self.API = remote.API('127.0.0.1', '')
        # self.API = remote.API('ud-e-data-science.westeurope.cloudapp.azure.com', 'EnergyIsFun', port=8123, use_ssl=True)
        self._load = load
        self.load = load.tolist()
        # self.max_flex = max_flex or (0.1 * load).tolist()
        self.max_flex = max_flex or (load * np.random.rand(load.shape[0]) * 0.2).tolist()
        # self.min_flex = min_flex or (np.random.uniform(0, 1) * load).tolist()
        self.min_flex = min_flex or savgol_filter((load * np.random.rand(load.shape[0])), 9, 2).tolist()

        self._check_powerpool()
Ejemplo n.º 12
0
    def __init__(self, data: np.array,
                 label: str,
                 transition_length: int,
                 shift_size: int=0,
                 filter_type: str='hann'):
        """
        Process data to be lagged with a smooth transition determined by the filter type and shift.
        :param data: Data to be lagged.
        :param label: Label for data.
        :param transition_length: Number of points over which the lag transition occurs.
        :param shift_size: Number of points to shift the data.
        :param filter_type: Type of filter to use to generate the transition.
        :raise ValueError: Error if the filter_type is not valid.
        """
        self.data = data.copy()
        self.label = label
        self.transition_length = transition_length
        self.window_size = self.transition_length + 1
        self.shift_size = np.round(shift_size)

        dict_filter_type_func = {'none': None,
                                'rect': causal_rect_window_filter,
                                'hann': causal_hann_window_filter
                                }

        try:
            filter_func = dict_filter_type_func[filter_type]
        except KeyError:
            type_str = ', '.join(dict_filter_type_func.keys())
            raise ValueError(filter_type+' is not a valid type of binning. Valid types: '+type_str)

        self.use_shift = self.shift_size > 0
        if filter_func is not None:
            self.data = filter_func(self.data, self.window_size)

        if self.use_shift:
            self.data = self.data[:-self.shift_size]
Ejemplo n.º 13
0
 def convert(self, arr: np.array):
     arr.uri = self.png_convertor(arr)
Ejemplo n.º 14
0
def bpbounds_calc_tri_z3(p: np.array):
    # assuming that p is in the following order
    # notation for conditional probabilities is p(y,x|z)
    p = p.ravel()
    p000 = p[0]
    p100 = p[1]
    p010 = p[2]
    p110 = p[3]
    p001 = p[4]
    p101 = p[5]
    p011 = p[6]
    p111 = p[7]
    p002 = p[8]
    p102 = p[9]
    p012 = p[10]
    p112 = p[11]

    # bounds on probabilities
    p10low1 = p100
    p10low2 = p101
    p10low3 = p102
    p10low4 = p100 + p110 + p101 + p011 - 1
    p10low5 = p100 + p010 + p101 + p111 - 1
    p10low6 = p101 + p111 + p102 + p012 - 1
    p10low7 = p101 + p011 + p102 + p112 - 1
    p10low8 = p102 + p112 + p100 + p010 - 1
    p10low9 = p102 + p012 + p100 + p110 - 1
    p10upp1 = 1 - p000
    p10upp2 = 1 - p001
    p10upp3 = 1 - p002
    p10upp4 = p100 + p010 + p101 + p111
    p10upp5 = p100 + p110 + p101 + p011
    p10upp6 = p101 + p011 + p102 + p112
    p10upp7 = p101 + p111 + p102 + p012
    p10upp8 = p102 + p012 + p100 + p110
    p10upp9 = p102 + p112 + p100 + p010
    p10low = max([
        p10low1, p10low2, p10low3, p10low4, p10low5, p10low6, p10low7, p10low8,
        p10low9
    ])
    p10upp = min([
        p10upp1, p10upp2, p10upp3, p10upp4, p10upp5, p10upp6, p10upp7, p10upp8,
        p10upp9
    ])
    p11low1 = p110
    p11low2 = p111
    p11low3 = p112
    p11low4 = p100 + p110 - p101 - p011
    p11low5 = -p100 - p010 + p101 + p111
    p11low6 = p101 + p111 - p102 - p012
    p11low7 = -p101 - p011 + p102 + p112
    p11low8 = p102 + p112 - p100 - p010
    p11low9 = -p102 - p012 + p100 + p110
    p11upp1 = 1 - p010
    p11upp2 = 1 - p011
    p11upp3 = 1 - p012
    p11upp4 = p100 + p110 - p101 - p011 + 1
    p11upp5 = -p100 - p010 + p101 + p111 + 1
    p11upp6 = p101 + p111 - p102 - p012 + 1
    p11upp7 = -p101 - p011 + p102 + p112 + 1
    p11upp8 = p102 + p112 - p100 - p010 + 1
    p11upp9 = -p102 - p012 + p100 + p110 + 1

    p11low = max([
        p11low1, p11low2, p11low3, p11low4, p11low5, p11low6, p11low7, p11low8,
        p11low9
    ])
    p11upp = min([
        p11upp1, p11upp2, p11upp3, p11upp4, p11upp5, p11upp6, p11upp7, p11upp8,
        p11upp9
    ])
    p10lower = [
        p10low1, p10low2, p10low3, p10low4, p10low5, p10low6, p10low7, p10low8,
        p10low9
    ]
    p10upper = [
        p10upp1, p10upp2, p10upp3, p10upp4, p10upp5, p10upp6, p10upp7, p10upp8,
        p10upp9
    ]
    p11lower = [
        p11low1, p11low2, p11low3, p11low4, p11low5, p11low6, p11low7, p11low8,
        p11low9
    ]
    p11upper = [
        p11upp1, p11upp2, p11upp3, p11upp4, p11upp5, p11upp6, p11upp7, p11upp8,
        p11upp9
    ]
    retlist = {
        "p10low": p10low,
        "p10upp": p10upp,
        "p11low": p11low,
        "p11upp": p11upp,
        "p10lower": p10lower,
        "p10upper": p10upper,
        "p11lower": p11lower,
        "p11upper": p11upper
    }

    # bounds on causal risk ratio
    rrlow = p11low / p10upp
    rrupp = p11upp / p10low
    retlist["crrlb"] = rrlow
    retlist["crrub"] = rrupp

    # monotonicity bounds
    m1 = (p102 <= p101) & (p101 <= p100)
    m2 = (p110 <= p111) & (p111 <= p112)
    m3 = (p010 <= p011) & (p011 <= p012)
    m4 = (p002 <= p001) & (p001 <= p000)
    monoinequality = (m1 == True & m2 == True & m3 == True & m4 == True)
    retlist["monoinequality"] = monoinequality
    if monoinequality:
        mlow = p112 + p000 - 1
        mupp = 1 - p100 - p110
        retlist["monobplb"] = mlow
        retlist["monobpub"] = mupp

        # bounds on intervention probabilities assuming monotonicity
        monop10low = p100
        monop10upp = 1 - p000
        monop11low = p112
        monop11upp = 1 - p012
        retlist["monop10lb"] = monop10low
        retlist["monop10ub"] = monop10upp
        retlist["monop11lb"] = monop11low
        retlist["monop11ub"] = monop11upp

        # bounds on causal risk ratio assuming monotonicity
        monocrrlow = monop11low / monop10upp
        monocrrupp = monop11upp / monop10low
        retlist["monocrrlb"] = monocrrlow
        retlist["monocrrub"] = monocrrupp
    else:
        retlist["monobplb"] = 0
        retlist["monobpub"] = 0
        retlist["monop10lb"] = 0
        retlist["monop10ub"] = 0
        retlist["monop11lb"] = 0
        retlist["monop11ub"] = 0
        retlist["monocrrlb"] = 0
        retlist["monocrrub"] = 0
    return retlist
Ejemplo n.º 15
0
def rayleigh_quotient(matrix: scipy.sparse.csr_matrix,
                      vector: np.array) -> float:
    """Compute the Rayleigh quotient of a matrix and a vector.

    """
    return matrix.dot(vector).T.dot(vector) / vector.dot(vector)
Ejemplo n.º 16
0
def create_matrices(
    time: np.array,
    offx: np.array,
    offy: np.array,
    dt: float,
    duration: float,
    t0: float,
    nfft: int,
    nfftime: np.array,
    tt: np.array,
):
    """Returns a tuple of numpy arrays.

    :param time: Time values in seconds (numpy array).
    :param offx: Sample offsets (numpy array).
    :param offy: Line offsets (numpy array).
    :param dt: Time difference between the FROM and MATCH times (float).
    :param duration: The total time duration (float).
    :param t0: Zero time to start the Fourier Transform steps (float).
    :param nfft: Number of divisions to use for the Fourier Transform (int).
    :param nfftime: *nfft* time values starting at *t0* (numpy array).
    :param tt: Fractional values in [0:1] that correspond to the nfft number
        (numpy array).
    :return: There are seven elements in the tuple:
        0: Interpolation of *offx* at the values of *nfftime*
        1: Interpolation of *offy* at the values of *nfftime*
        2: Fourier transform of xinterp * (2 / *nfft*)
        3: Fourier transform of yinterp * (2 / *nfft*)
        4: phase difference
        5: overxx: ?
        6: overyy: ?
    """
    t_shift = time - t0

    fx = PchipInterpolator(t_shift, offx, extrapolate=False)
    fy = PchipInterpolator(t_shift, offy, extrapolate=False)
    xinterp = fx(nfftime - t0)
    yinterp = fy(nfftime - t0)

    np.nan_to_num(xinterp, copy=False, nan=np.mean(offx))
    np.nan_to_num(yinterp, copy=False, nan=np.mean(offy))

    # getting the frequencies of the Fourier transform
    freq = np.linspace(0, nfft / 2, int(nfft / 2), endpoint=False)

    # taking the fourier transform of the offsets
    x = 2 * np.fft.fft(xinterp) / nfft
    y = 2 * np.fft.fft(yinterp) / nfft

    # separating sines and cosines
    xa = x[:int(nfft / 2)].real
    xb = -1 * x[:int(nfft / 2)].imag
    ya = y[:int(nfft / 2)].real
    yb = -1 * y[:int(nfft / 2)].imag

    # calculates the phase difference
    twopi = math.pi * 2
    ddt_temp = (dt / duration * twopi) * freq
    ddt = ddt_temp - twopi * np.floor(ddt_temp / twopi)

    # the coeficients for the frequencies
    with np.errstate(divide="ignore"):
        aaax = (-0.5 * (-1 * xa * np.cos(ddt) + np.sin(ddt) * xb - xa) /
                np.sin(ddt))
        aaay = (-0.5 * (-1 * ya * np.cos(ddt) + np.sin(ddt) * yb - ya) /
                np.sin(ddt))

    with np.errstate(invalid="ignore"):
        bbbx = -0.5 * (xb * np.cos(ddt) + np.sin(ddt) * xa + xb) / np.sin(ddt)
        bbby = -0.5 * (yb * np.cos(ddt) + np.sin(ddt) * ya + yb) / np.sin(ddt)

    # create series of sines and cosines
    ft = freq.reshape(-1, 1) * tt.reshape(1, -1) * twopi
    sn = np.sin(ft)
    cn = np.cos(ft)
    aaax_rep = np.repeat(aaax.reshape(-1, 1), tt.size, axis=1)
    bbbx_rep = np.repeat(bbbx.reshape(-1, 1), tt.size, axis=1)
    aaay_rep = np.repeat(aaay.reshape(-1, 1), tt.size, axis=1)
    bbby_rep = np.repeat(bbby.reshape(-1, 1), tt.size, axis=1)
    with np.errstate(invalid="ignore"):
        overxx = aaax_rep * sn + bbbx_rep * cn
        overyy = aaay_rep * sn + bbby_rep * cn

    # Outputs
    # ArrayXd & tt, ArrayXd & ET, ArrayXd & ET_shift,
    # ArrayXd & ddt,
    # ArrayXd & xinterp, ArrayXd & yinterp,
    # ArrayXcd & X, ArrayXcd & Y, MatrixXd & overxx, MatrixXd & overyy
    #
    return xinterp, yinterp, x, y, ddt, overxx, overyy
Ejemplo n.º 17
0
    def predict(self, X: np.array, threshold: float = 0.5) -> np.array:
        X = np.array(X)

        if X.ndim == 1:
            X = X.reshape((X.shape[0], 1))
        return self.predict_probs(X).round()
Ejemplo n.º 18
0
 def encode(f: FileOut, x: np.array) -> None:
     assert x.dtype == dtype, f"expected array type: {dtype}, received: {x.dtype}"
     l_enc(f, x.shape)
     bytesC_enc(f, x.tobytes())
Ejemplo n.º 19
0
    def __init__(self,
                 stateDim: int,
                 actionDim: int,
                 actionMin: np.array,
                 actionMax: np.array,
                 learningRate=0.0005,
                 gamma=0.99,
                 GAElambda=0.95,
                 PPOepsilon=0.2,
                 PPOentropyLossWeight=0,
                 nHidden: int = 2,
                 nUnitsPerLayer: int = 128,
                 mode="PPO-CMA-m",
                 activation="lrelu",
                 H: int = 9,
                 entropyLossWeight: float = 0,
                 sdLowLimit=0.01,
                 useScaler: bool = True,
                 criticTimestepScale=0.001,
                 initialMean: np.array = None,
                 initialSd: np.array = None):
        #Create policy network
        print("Creating policy")
        self.actionMin = actionMin.copy()
        self.actionMax = actionMax.copy()
        self.actionDim = actionDim
        self.stateDim = stateDim
        self.useScaler = useScaler
        if useScaler:
            self.scaler = Scaler(stateDim)
        self.scalerInitialized = False
        self.normalizeAdvantages = True
        self.gamma = gamma
        self.GAElambda = GAElambda
        self.criticTimestepScale = 0 if gamma == 0 else criticTimestepScale  #with gamma==0, no need for this
        piEpsilon = None
        nHistory = 1
        negativeAdvantageAvoidanceSigma = 0
        if mode == "PPO-CMA" or mode == "PPO-CMA-m":
            usePPOLoss = False  #if True, we use PPO's clipped surrogate loss function instead of the standard -A_i * log(pi(a_i | s_i))
            separateVarAdapt = True
            self.reluAdvantages = True if mode == "PPO-CMA" else False
            nHistory = H  #policy mean adapts immediately, policy covariance as an aggreagate of this many past iterations
            useSigmaSoftClip = True
            negativeAdvantageAvoidanceSigma = 1 if mode == "PPO-CMA-m" else 0
        elif mode == "PPO":
            usePPOLoss = True  #if True, we use PPO's clipped surrogate loss function instead of the standard -A_i * log(pi(a_i | s_i))
            separateVarAdapt = False
            # separateSigmaAdapt=False
            self.reluAdvantages = False
            useSigmaSoftClip = True
            piEpsilon = 0
        elif mode == "PG":
            usePPOLoss = False  #if True, we use PPO's clipped surrogate loss function instead of the standard -A_i * log(pi(a_i | s_i))
            separateVarAdapt = False
            # separateSigmaAdapt=False
            self.reluAdvantages = False
            useSigmaSoftClip = True
            piEpsilon = 0
        elif mode == "PG-pos":
            usePPOLoss = False  #if True, we use PPO's clipped surrogate loss function instead of the standard -A_i * log(pi(a_i | s_i))
            separateVarAdapt = False
            # separateSigmaAdapt=False
            self.reluAdvantages = True
            useSigmaSoftClip = True
            piEpsilon = 0
        else:
            raise ("Unknown mode {}".format(mode))
        self.policy = Policy(
            stateDim,
            actionDim,
            actionMin,
            actionMax,
            entropyLossWeight=PPOentropyLossWeight,
            networkActivation=activation,
            networkDepth=nHidden,
            networkUnits=nUnitsPerLayer,
            networkSkips=False,
            learningRate=learningRate,
            minSigma=sdLowLimit,
            PPOepsilon=PPOepsilon,
            usePPOLoss=usePPOLoss,
            separateVarAdapt=separateVarAdapt,
            nHistory=nHistory,
            useSigmaSoftClip=useSigmaSoftClip,
            piEpsilon=piEpsilon,
            negativeAdvantageAvoidanceSigma=negativeAdvantageAvoidanceSigma)

        #Create critic network, +1 stateDim because at least in OpenAI gym, episodes are time-limited and the value estimates thus depend on simulation time.
        #Thus, we use time step as an additional feature for the critic.
        #Note that this does not mess up generalization, as the feature is not used for the policy during training or at runtime
        print("Creating critic network")
        self.critic = Critic(stateDim=stateDim + 1,
                             learningRate=learningRate,
                             nHidden=nHidden,
                             networkUnits=nUnitsPerLayer,
                             networkActivation=activation,
                             useSkips=False,
                             lossType="L1")

        #Experience trajectory buffers for the memorize() and updateWithMemorized() methods
        self.experienceTrajectories = []
        self.currentTrajectory = []

        #Init may take as argument a desired initial action mean and sd. These need to be remembered for the first iteration's act,
        #which samples the initial mean and sd directly instead of utilizing the policy network.
        if initialMean is not None:
            self.initialMean = initialMean.copy()
        else:
            self.initialMean = 0.5 * (self.actionMin +
                                      self.actionMax) * np.ones(self.actionDim)
        if initialSd is not None:
            self.initialSd = initialSd.copy()
        else:
            self.initialSd = 0.5 * (self.actionMax - self.actionMin) * np.ones(
                self.actionDim)
Ejemplo n.º 20
0
def mag(x: np.array):
    return (x.dot(x))**0.5
Ejemplo n.º 21
0
 def __top(model: np.array, n: int=1):
     return [i for i in model.argsort()[-n:][::-1] if model[i] != 0.0]
Ejemplo n.º 22
0
def compute_logistic_gradient(y: np.array, tx: np.array, w: np.array):
    pred = sigmoid(tx.dot(w))
    gradient = tx.T.dot(pred - y)
    return gradient
Ejemplo n.º 23
0
def shoulderVecFromR(R: np.array):
    return R.reshape([-1, 3, 3])[:, 1].reshape(R.shape[:-1])
Ejemplo n.º 24
0
 def convert_pixels_to_8_bits(visual_data: np.array) -> np.array:
     return visual_data.astype(np.uint8)
Ejemplo n.º 25
0
 def normalize(visual_data: np.array) -> np.array:
     return 255 * ((visual_data - visual_data.min()) /
                   (visual_data.max() - visual_data.min()))
Ejemplo n.º 26
0
def write_edf(output_path: str,
              raw_data: np.array,
              ch_names: List[str],
              sfreq: float,
              events: List[Tuple[float, float, str]],
              overwrite=False) -> Path:
    """
    Converts BciPy raw_data to the EDF+ filetype using pyEDFlib.

    Adapted from: https://github.com/holgern/pyedflib

    Parameters
    ----------
    output_path - optional path to write converted data; defaults to writing
        a file named raw.edf in the raw_data_dir.
    raw_data - raw data with a row for each channel
    ch_names - names of the channels
    sfreq - sample frequency
    events - List[Tuple(onset_in_seconds: float, duration_in_seconds: float, description: str)]
    overwrite - If True, the destination file (if it exists) will be overwritten.
        If False (default), an error will be raised if the file exists.

    Returns
    -------
        Path to new edf file
    """
    if not overwrite and os.path.exists(output_path):
        raise OSError('EDF file already exists.')

    # set conversion parameters
    dmin, dmax = [-32768, 32767]
    pmin, pmax = [raw_data.min(), raw_data.max()]
    n_channels = len(raw_data)

    try:
        writer = EdfWriter(str(output_path),
                           n_channels=n_channels,
                           file_type=FILETYPE_EDFPLUS)
        channel_info = []
        data_list = []

        for i in range(n_channels):
            ch_dict = {
                'label': ch_names[i],
                'dimension': 'uV',
                'sample_rate': sfreq,
                'physical_min': pmin,
                'physical_max': pmax,
                'digital_min': dmin,
                'digital_max': dmax,
                'transducer': '',
                'prefilter': ''
            }

            channel_info.append(ch_dict)
            data_list.append(raw_data[i])

        writer.setSignalHeaders(channel_info)
        writer.writeSamples(data_list)

        if events:
            for onset, duration, label in events:
                writer.writeAnnotation(onset, duration, label)
    except Exception as error:
        logging.getLogger(__name__).info(error)
        return None
    finally:
        writer.close()
    return output_path
Ejemplo n.º 27
0
def _transform_response(response: np.array) -> json:
    return json.dumps({"outputs": response.tolist()[0]})
Ejemplo n.º 28
0
    def query_by_embedding(
            self,
            query_emb: np.array,
            filters: Optional[Dict[str, List[str]]] = None,
            top_k: int = 10,
            index: Optional[str] = None,
            return_embedding: Optional[bool] = None) -> List[Document]:
        if index is None:
            index = self.index

        return_embedding = return_embedding or self.return_embedding

        if not self.embedding_field:
            raise RuntimeError(
                "Please specify arg `embedding_field` in ElasticsearchDocumentStore()"
            )
        else:
            # +1 in similarity to avoid negative numbers (for cosine sim)
            body = {
                "size": top_k,
                "query": {
                    "script_score": {
                        "query": {
                            "match_all": {}
                        },
                        "script": {
                            # offset score to ensure a positive range as required by Elasticsearch
                            "source":
                            f"{self.similarity_fn_name}(params.query_vector,'{self.embedding_field}') + 1000",
                            "params": {
                                "query_vector": query_emb.tolist()
                            }
                        }
                    }
                }
            }  # type: Dict[str,Any]

            if filters:
                for key, values in filters.items():
                    if type(values) != list:
                        raise ValueError(
                            f'Wrong filter format for key "{key}": Please provide a list of allowed values for each key. '
                            'Example: {"name": ["some", "more"], "category": ["only_one"]} '
                        )
                body["query"]["script_score"]["query"] = {"terms": filters}

            excluded_meta_data: Optional[list] = None

            if self.excluded_meta_data:
                excluded_meta_data = deepcopy(self.excluded_meta_data)

                if return_embedding is True and self.embedding_field in excluded_meta_data:
                    excluded_meta_data.remove(self.embedding_field)
                elif return_embedding is False and self.embedding_field not in excluded_meta_data:
                    excluded_meta_data.append(self.embedding_field)
            elif return_embedding is False:
                excluded_meta_data = [self.embedding_field]

            if excluded_meta_data:
                body["_source"] = {"excludes": excluded_meta_data}

            logger.debug(f"Retriever query: {body}")
            result = self.client.search(index=index,
                                        body=body,
                                        request_timeout=300)["hits"]["hits"]

            documents = [
                self._convert_es_hit_to_document(
                    hit, adapt_score_for_embedding=True) for hit in result
            ]
            return documents
Ejemplo n.º 29
0
 def Forward(self, data: np.array):
     return np.fft.fftn(data.astype(complex))
Ejemplo n.º 30
0
 def hypothesis(self, X: np.array) -> float:
     if X.ndim == 0:
         X = X.reshape([1,1])
     return self.theta[0] + np.matmul(X, self.theta[1:])
Ejemplo n.º 31
0
    def print_data_statistics(x_train: np.array, x_val: np.array,
                              x_test: np.array, y_train: np.array,
                              y_val: np.array) -> None:
        """

        """
        Total_data_num = len(x_train) + len(x_val) + len(x_test)
        print("\n________Table : Data portions info_________")
        t = Texttable()
        t.add_rows([['Data Portion', 'Number', 'Percent'],
                    ['Total', Total_data_num, "{:.0%}".format(1)],
                    [
                        'Train data',
                        len(x_train),
                        "{:.2%}".format(len(x_train) / Total_data_num)
                    ],
                    [
                        'val data',
                        len(x_val),
                        "{:.2%}".format(len(x_val) / Total_data_num)
                    ],
                    [
                        'Test data',
                        len(x_test),
                        "{:.2%}".format(len(x_test) / Total_data_num)
                    ]])
        print(t.draw())
        print("//////////////////////////////////////////////////\n")

        print(
            "_______________________Table : Data shape info________________________"
        )
        t = Texttable()
        t.add_rows([['Name', 'Shape', 'Min', 'Max', 'Type'],
                    [
                        'x train', x_train.shape,
                        x_train.min(),
                        x_train.max(),
                        type(x_train)
                    ],
                    [
                        'y train', y_train.shape,
                        y_train.min(),
                        y_train.max(),
                        type(y_train)
                    ],
                    [
                        'x validation', x_val.shape,
                        x_val.min(),
                        x_val.max(),
                        type(x_val)
                    ],
                    [
                        'y validation', y_val.shape,
                        y_val.min(),
                        y_val.max(),
                        type(y_val)
                    ],
                    [
                        'x test', x_test.shape,
                        x_test.min(),
                        x_test.max(),
                        type(x_test)
                    ]])
        print(t.draw())
        print("//////////////////////////////////////////////////\n")
Ejemplo n.º 32
0
def HMC_ensemble_run(model: keras.models.Model,
            x_train: np.array,
            y_train: np.array,
            N_mc: int,
            ep: float,
            tau: int,
            burn_in: int,
            sample_every: int,
            return_extra=False,
            verbose=True,
            verbose_n = 100,
            ):
    """
    Takes a keras model and a dataset (x_train, y_train) and returns a list of numpy
    arrays to be used as weights for an ensemble predictor.
    """
    step_size = ep
    n_steps = tau

    Ws = model.weights
    lossfn = model.loss #this is kinda cheeky
    X = model.input
    Y_ = model.output
    Y = K.placeholder(shape=Y_.shape)
    L = K.sum(lossfn(Y, Y_)) #the sum accross the batch
    Gs = K.gradients(L, Ws)
    eval_loss_and_grads = K.function([X,Y], [L] + Gs)
    def get_loss_and_grads(x, y):
        res = eval_loss_and_grads([x, y])
        return res[0], res[1:]
    losses = []
    i = 0
    weights = []
    ensemble = []
    accept_n = 0
    ep_lo = 0.8 * step_size
    ep_hi = 1.2 * step_size

    tau_lo = int(0.5 * n_steps)
    tau_hi = int(1.5 * n_steps)
    
    obj = 0

    while len(ensemble) < N_mc:
        ep = ep_lo + (ep_hi - ep_lo) * np.random.random()
        tau = np.random.randint(tau_lo, high=tau_hi)

        obj, gs = get_loss_and_grads(x_train, y_train)
        losses.append(obj)


        if verbose and i % verbose_n == 0:
            acc = np.mean(model.predict(x_train).argmax(axis=1) == y_train.argmax(axis=1))
            accept_ratio = accept_n / i if i > 0 else 0
            print("iter: ", i, 'accuracy :', acc, 'loss: ', obj, 'accept_ratio: ', accept_ratio)

        i += 1 

        ps = [np.random.normal(size=w.shape) for w in Ws]  # momentum variables

        H = .5 * sum([np.sum(p ** 2) for p in ps]) + obj

        ws = [K.eval(w) for w in Ws]
        weights.append(ws)
        ws_old = [K.eval(w) for w in Ws]
        # store the values of the weights in case we need to go back
        for t in range(tau):
            for p, g in zip(ps, gs):
                p -= .5 * ep * g

            for w, p in zip(ws, ps):
                w += ep * p

            # evaluate new weights
            for (weight, value) in zip(Ws, ws):
                K.set_value(weight, value)
            obj, gs = get_loss_and_grads(x_train, y_train)

            for p, g in zip(ps, gs):
                p -= .5 * ep * g

        H_new = .5 * sum([np.sum(p ** 2) for p in ps]) + obj

        dH = H_new - H

        if (dH < 0) or np.random.rand() < np.exp(-dH):
            # in this case, we acccept the new values
            accept_n += 1
            pass
        else:
            # reverse the step
            for weight, value in zip(Ws, ws_old):
                K.set_value(weight, value)

        if i > burn_in and i % sample_every == 0:
           ensemble.append([K.eval(w) for w in Ws])

    if return_extra:
        weights = np.array(weights)
        return ensemble, losses, weights, accept_n / i
    else:
        return ensemble
Ejemplo n.º 33
0
def value_iteration(env: EnvWithModel, initV: np.array,
                    theta: float) -> Tuple[np.array, Policy]:
    """
    inp:
        env: environment with model information, i.e. you know transition dynamics and reward function
        initV: initial V(s); numpy array shape of [nS,]
        theta: exit criteria
    return:
        value: optimal value function; numpy array shape of [nS]
        policy: optimal deterministic policy; instance of Policy class
    """
    class PiStar(Policy):
        def __init__(self, optActionProb, optPolicy):
            self.optActionProb = optActionProb
            self.optPolicy = optPolicy

        def action_prob(self, state, action):
            return self.optActionProb[state, action]

        def action(self, state):
            return self.optPolicy[state]

    V = initV.copy()
    #    print('initV: \n',initV.reshape((4,4)))
    #    print('V: \n',V.reshape((4,4)))
    delta = theta
    i = 0
    while delta >= theta:
        i += 1
        delta = 0
        for s in range(env.spec.nS):
            v = V[s]
            update = np.zeros(env.spec.nA)
            for a in range(env.spec.nA):
                update[a] = np.sum(env.TD[s, a, :] *
                                   (env.R[s, a, :] + env.spec.gamma * V))
            V[s] = update.max()
            delta = max(delta, np.abs(v - V[s]))
#        print('V: \n',V.reshape((4,4)))
#        print('V: \n',V)
#        print('Delta:',delta)
#    print('iterations: ',i)
    Q = np.zeros((env.spec.nS, env.spec.nA))
    optActionProb = np.zeros((env.spec.nS, env.spec.nA))
    optPolicy = np.zeros((env.spec.nS))

    for s in range(env.spec.nS):
        for a in range(env.spec.nA):
            Q[s, a] = np.sum(env.TD[s, a, :] *
                             (env.R[s, a, :] + env.spec.gamma * V))
        aStar = np.argmax(Q[s, :])
        optActionProb[s, aStar] = 1
        optPolicy[s] = aStar
#    print('Q: \n',Q.reshape((4,16)))

#    print('Q: \n',Q)

    pi = PiStar(optActionProb, optPolicy)
    #####################
    # TODO: Implement Value Iteration Algorithm (Hint: Sutton Book p.83)
    #####################

    return V, pi
Ejemplo n.º 34
0
    def allocate(self, mu: np.array, cov: np.array) -> np.array:
        """
        Perform the NCO method described in section 4.3 of "A Robust Estimator of the Efficient Frontier"

        Excerpt from section 4.3:

        The NCO method estimates 𝜔̂∗ while controlling for the signal-induced estimation errors
        explained in section 3.2. NCO works as follows:

        First, we cluster the covariance matrix into subsets of highly-correlated variables.
        One possible clustering algorithm is the partitioning method discussed in López de Prado and Lewis [2019],
        but hierarchical methods may also be applied. The result is a partition of the original set,
        that is, a collection of mutually disjoint nonempty subsets of variables.

        Second, we compute optimal allocations for each of these clusters separately.
        This allows us to collapse the original covariance matrix into a reduced covariance matrix,
        where each cluster is represented as a single variable. The collapsed correlation matrix is
        closer to an identity matrix than the original correlation matrix was, and therefore more
        amenable to optimization problems (recall the discussion in section 3.2).

        Third, we compute the optimal allocations across the reduced covariance matrix.

        Fourth,
        the final allocations are the dot-product of the intra-cluster allocations and the inter-cluster allocations.

        By splitting the problem into two separate tasks, NCO contains the instability within each cluster:
        the instability caused by intra-cluster noise does not propagate across clusters.
        See López de Prado [2019] for examples, code and additional details regarding NCO.

        :param cov: Covariance matrix
        :param mu: Expected return vector
        :return: Min variance portfolio if mu is None, max sharpe ratio portfolio if mu is not None
        """
        cov = pd.DataFrame(cov)

        if mu is not None:
            mu = pd.Series(mu.flatten())
            assert mu.size == cov.shape[0], 'mu and cov dimension must be the same size'

        # get correlation matrix
        corr = cov_to_corr(cov)

        # find the optimal partition of clusters
        clusters = self._cluster_k_means_base(corr)

        # calculate intra-cluster allocations by finding the optimal portfolio for each cluster
        intra_cluster_allocations = pd.DataFrame(0, index=cov.index, columns=clusters.keys())
        for cluster_id, cluster in clusters.items():
            cov_ = cov.loc[cluster, cluster].values
            mu_ = mu.loc[cluster].values.reshape(-1, 1) if mu is not None else None
            intra_cluster_allocations.loc[cluster, cluster_id] = self._get_optimal_portfolio(cov_, mu_)

        # reduce covariance matrix
        cov = intra_cluster_allocations.T.dot(np.dot(cov, intra_cluster_allocations))
        mu = intra_cluster_allocations.T.dot(mu) if mu is not None else None

        # calculate inter_cluster allocations on reduced covariance matrix
        inter_cluster_allocations = pd.Series(self._get_optimal_portfolio(cov, mu), index=cov.index)

        # final allocations are the dot-product of the intra-cluster allocations and the inter-cluster allocations
        return intra_cluster_allocations \
            .mul(inter_cluster_allocations, axis=1) \
            .sum(axis=1).values \
            .reshape(-1, 1) \
            .flatten()
Ejemplo n.º 35
0
def model_inference_helper(x_0: np.array, x_1: np.array, time_step: float):
    """ Input: x_0, x_1; Output: y_0 """
    x_0 = torch.from_numpy(x_0).type(args.dtype)
    x_1 = torch.from_numpy(x_1).type(args.dtype)
    y_0 = torch.FloatTensor()

    intWidth = x_0.size(2)
    intHeight = x_0.size(1)
    channel = x_0.size(0)
    assert channel == 3, "input frame's channel is not equal to 3."

    if intWidth != ((intWidth >> 7) << 7):
        intWidth_pad = ((intWidth >> 7) + 1) << 7  # more than necessary
        intPaddingLeft = int((intWidth_pad - intWidth) / 2)
        intPaddingRight = intWidth_pad - intWidth - intPaddingLeft
    else:
        intWidth_pad = intWidth
        intPaddingLeft = 32
        intPaddingRight = 32

    if intHeight != ((intHeight >> 7) << 7):
        intHeight_pad = ((intHeight >> 7) + 1) << 7  # more than necessary
        intPaddingTop = int((intHeight_pad - intHeight) / 2)
        intPaddingBottom = intHeight_pad - intHeight - intPaddingTop
    else:
        intHeight_pad = intHeight
        intPaddingTop = 32
        intPaddingBottom = 32

    # torch.set_grad_enabled(False)
    x_0 = Variable(torch.unsqueeze(x_0, 0))
    x_1 = Variable(torch.unsqueeze(x_1, 0))
    x_0 = torch.nn.ReplicationPad2d(
        [intPaddingLeft, intPaddingRight, intPaddingTop,
         intPaddingBottom])(x_0)
    x_1 = torch.nn.ReplicationPad2d(
        [intPaddingLeft, intPaddingRight, intPaddingTop,
         intPaddingBottom])(x_1)

    # if use_cuda:
    x_0 = x_0.cuda()
    x_1 = x_1.cuda()

    # y_s, offset, filter = model(torch.stack((X0, X1), dim=0))
    if time_step == 0.5:
        y_s, _, _ = model_05(torch.stack((x_0, x_1), dim=0))
    if time_step == 0.33:
        y_s, _, _ = model_033(torch.stack((x_0, x_1), dim=0))
    if time_step == 0.25:
        y_s, _, _ = model_025(torch.stack((x_0, x_1), dim=0))
    else:
        y_s, _, _ = model_020(torch.stack((x_0, x_1), dim=0))

    y_0 = y_s[args.save_which]

    torch.cuda.empty_cache()

    if not isinstance(y_0, list):
        y_0 = y_0.data.cpu().numpy()
    else:
        y_0 = [item.data.cpu().numpy() for item in y_0]
    y_0 = [
        np.transpose(
            255.0 *
            item.clip(0, 1.0)[0, :, intPaddingTop:intPaddingTop + intHeight,
                              intPaddingLeft:intPaddingLeft + intWidth, ],
            (1, 2, 0),
        ) for item in y_0
    ]

    return y_0
Ejemplo n.º 36
0
def identity_copy(x: _np.array) -> _np.array:
    return x.copy()
Ejemplo n.º 37
0
def online_qp(velqp: VelQP,
              v_ini: float,
              kappa: np.ndarray,
              delta_s: np.ndarray,
              P_max: np.array = None,
              ax_max: np.array = None,
              ay_max: np.array = None,
              x0_v: np.ndarray = None,
              v_max: np.ndarray = None,
              v_end: float = None,
              F_ini: float = None,
              s_glob: float = None,
              v_max_cstr: np.ndarray = None) -> tuple:
    """
    Python version: 3.5
    Created by: Thomas Herrmann ([email protected])
    Created on: 01.11.2019

    Documentation: Creates an SQP that optimizes a velocity profile for a given path.

    Inputs:
    velqp: QP solver object used within the SQP
    v_ini: initial velocity hard constraint [m/s]
    kappa: kappa profile of given path [rad/m]
    delta_s: discretization step length [m]
    P_max: max. allowed power [kW]
    ax_max: max. allowed longitudinal acceleration [m/s^2]
    ay_max: max. allowed ongitudial acceleration [m/s^2]
    x0_v: initial guess of optimal velocity [m/s]
    v_max: max. should velocity (objective function) [m/s]
    v_end: constrained end velocity in optimization horizon [m/s]
    F_ini: initial force constraint [kN]
    s_glob: global s coordinate of current vehicle position [m]
    v_max_cstr: max. must velocity (hard constraint) [m/s]

    Outputs:
    v_op: optimized velocity using OSQP as QP solver [m/s]
    s_t_op: optimized slack values [-]
    qp_status: status of last QP within SQP [-]
    """

    # --- Steplength reduction parameter for Armijo rule
    beta = 2 / 4

    # --- Initialization of logging variables
    x0_v_log = None
    x0_s_t_log = None
    kappa_log = None
    delta_s_log = None
    ax_max_log = None
    ay_max_log = None
    v_ini_log = None
    v_max_log = None
    v_end_log = None
    F_ini_log = None

    ####################################################################################################################
    # --- Preparing input parameters for SQP
    ####################################################################################################################

    # --- Emergency SQP
    # upper bound for last velocity entry depending on current velocity
    if velqp.sid == 'EmergSQP':
        # --- Upper bound for last velocity point
        v_end = 0.4

        v_max = np.array(
            [v_ini - (x + 1) * 4 for x in range(velqp.sqp_stgs['m'])])
        # set all values below threshold to threshold
        v_max[v_max < 0.0] = 0.0

        # --- Assume linear velocity decrease to v_end from current velocity
        x0_v = np.array([
            v_ini + x * (v_max[-1] - v_ini) / velqp.sqp_stgs['m']
            for x in range(velqp.sqp_stgs['m'])
        ])

        # Initialize slack variables with zero values
        x0_s_t = np.array([0] * velqp.n)

        # Overwrite None
        F_ini = 0
        s_glob = 0

    # --- /Emergency SQP

    # --- Performance SQP
    else:
        x0_s_t = np.array([0] * velqp.n)
    # --- /Performance SQP

    # --- Make sure to always have a numeric velocity > 0.1
    if v_ini < velqp.sym_sc_['vmin_mps_']:
        v_ini = velqp.sym_sc_['vmin_mps_']
    x0_v[0] = v_ini

    # --- Initialization of optimization variables
    # velocity [m/s]
    v_op = np.zeros(velqp.sqp_stgs['m'], )
    # slack variable on tires
    s_t_op = np.zeros(velqp.n, )

    qp_iter = 0
    qp_status = 0

    # SQP mean-error
    err = np.inf
    # SQP infinity-error
    err_inf = np.inf

    # SQP-counter
    n_sqp = 0
    # SQP-timer
    dt = 0

    # time limit of SQP loop [s]
    dt_lim = velqp.sqp_stgs['t_sqp_max']

    # --- Save inputs to SQP here for logging purpose
    x0_v_log = copy.deepcopy(x0_v.tolist())
    x0_s_t_log = copy.deepcopy(x0_s_t.tolist())
    kappa_log = kappa.tolist()
    delta_s_log = delta_s.tolist()
    v_ini_log = v_ini
    try:
        v_max_log = copy.deepcopy(v_max.tolist())
    except AttributeError:
        v_max_log = copy.deepcopy(v_max)

    v_end_log = v_end
    F_ini_log = F_ini

    if isinstance(ax_max, np.ndarray) and isinstance(ay_max, np.ndarray):
        ax_max_log = ax_max.tolist()
        ay_max_log = ay_max.tolist()
    else:
        ax_max_log = velqp.sym_sc_['axmax_mps2_']
        ay_max_log = velqp.sym_sc_['aymax_mps2_']

    if isinstance(P_max, np.ndarray):
        Pmax_log = P_max.tolist()
    else:
        Pmax_log = velqp.sym_sc_['Pmax_kW_']

    # --- Start SQP-loop
    t_start = time.time()
    while (err > velqp.err or err_inf > velqp.err_inf
           ) and n_sqp < velqp.sqp_stgs['n_sqp_max'] and dt < dt_lim:

        # --- Update parameters of QP
        if len(x0_v) is not int(velqp.sqp_stgs['m']):
            print("Error in x0-length in ", velqp.sid)
            print(x0_v)
        if len(v_max) is not int(velqp.sqp_stgs['m']):
            print("Error in v_max-length in ", velqp.sid)
            print(v_max)

        # --- Update QP matrices
        velqp.osqp_update_online(x0_v=x0_v,
                                 x0_s_t=x0_s_t,
                                 v_ini=v_ini,
                                 v_max=v_max,
                                 v_end=v_end,
                                 F_ini=F_ini,
                                 kappa=kappa,
                                 delta_s=delta_s,
                                 P_max=P_max,
                                 ax_max=ax_max,
                                 ay_max=ay_max,
                                 vmax_cstr=v_max_cstr)

        # --- Solve the QP
        sol, qp_iter, qp_status = velqp.osqp_solve()

        # --- Check primal infeasibility of problem and return v = 0
        if qp_status == -3:
            break

        # --- Store solution from previous SQP-iteration
        o_old = np.append(v_op, x0_s_t)
        # --- Store errors from previous SQP-iteration
        err_old = err
        err_inf_old = err_inf
        try:
            ############################################################################################################
            # --- Armijo: decrease steplength alpha if SQP-error increases between iterations
            ############################################################################################################
            k = 0  # counter for Armijo-loop
            while True:
                # --- Choose a steplength
                alpha = beta**k  # steplength e {1, beta, beta^2, ...}

                # --- Restructure solution from QP-solution
                # Add leading "0" as ini-velocity must be kept constant as given
                v_op = alpha * np.insert(sol[0:velqp.m - 1], 0, 0) + x0_v

                s_t_op = alpha * sol[velqp.m - 1:] + x0_s_t

                # --- Calculate SQP iteration error
                o = np.append(v_op, s_t_op)
                err = np.sqrt(np.matmul(o - o_old, o - o_old)) / o.shape[0]
                err_inf = np.max(np.abs(o - o_old))

                # --- Break Armijo-loop in case a suitable steplength alpha was found
                if err < err_old and err_inf < err_inf_old:
                    break
                # --- Increase Armijo-loop's counter and restart loop
                else:
                    k += 1

                if velqp.sqp_stgs['b_print_sqp_alpha']:
                    print(velqp.sid + " | alpha: " + str(alpha))

            ############################################################################################################
            # --- Postprocessing Armijo-loop: Create new operating point for optimization variables
            ############################################################################################################
            # --- Create new operating-point for velocity variables
            x0_v = v_op
            # --- Create new operating-point for tire slack variables
            x0_s_t = s_t_op

        except TypeError:
            # --- Do different initialization till n_sqp_max is reached
            if velqp.sid == 'EmergSQP':
                x0_v = (v_ini - 0.05) * np.ones((velqp.m, ))
                v_op = np.zeros(velqp.sqp_stgs['m'], )
                x0_s_t = np.zeros(velqp.n, )
                s_t_op = np.zeros(velqp.n, )
                print(
                    "No solution for emerg. line found. Retrying with different initialization ..."
                )

                # Reset SQP-counter
                n_sqp = 0

        if not velqp.sqp_stgs['b_online_mode']:
            print('Optimized velocity profile: ', v_op[0:velqp.sqp_stgs['m']])
            if velqp.sqp_stgs['obj_func'] == 'slacks':
                print('Slacks on velocity: ',
                      v_op[velqp.sqp_stgs['m']:2 * velqp.sqp_stgs['m']])

        if velqp.sqp_stgs['b_print_sqp_err']:
            print(velqp.sid + " | SQP err: " + str(err))
            print(velqp.sid + " | SQP inf.-err: " + str(err_inf))

        ################################################################################################################
        # --- Check termination criteria for SQP-loop
        ################################################################################################################
        # increase SQP-iteration counter
        n_sqp += 1

        if n_sqp >= velqp.sqp_stgs['n_sqp_max']:
            print(velqp.sid + " reached max. SQP iteration-number!")

        # update timer
        dt = time.time() - t_start
        if dt >= dt_lim:
            print(velqp.sid + " took too long!")

    if velqp.sqp_stgs['b_print_SQP_runtime']:
        print(velqp.sid + " | SQP time [ms]: " + str(dt * 1000))

    # Only write to log-file after SQP-iterations
    if velqp.sid == 'PerfSQP' and velqp.logger_perf is not None:
        velqp.logger_perf.debug('%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s',
                                str(datetime.datetime.now().time()), s_glob,
                                json.dumps(x0_v_log), json.dumps(x0_s_t_log),
                                json.dumps(kappa_log),
                                json.dumps(delta_s_log), v_ini_log,
                                json.dumps(v_max_log), v_end_log, F_ini_log,
                                Pmax_log, json.dumps(qp_iter),
                                json.dumps(qp_status), json.dumps(dt * 1000))

        velqp.logger_perf.debug('%s', v_op.tolist())

        velqp.logger_perf.debug('%s', s_t_op.tolist())

        velqp.logger_perf.debug('%s;%s', ax_max_log, ay_max_log)

    elif velqp.sid == 'EmergSQP' and velqp.logger_emerg is not None:
        velqp.logger_emerg.debug('%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s',
                                 str(datetime.datetime.now().time()), s_glob,
                                 json.dumps(x0_v_log), json.dumps(x0_s_t_log),
                                 json.dumps(kappa_log),
                                 json.dumps(delta_s_log), v_ini_log,
                                 json.dumps(v_max_log), v_end_log, F_ini_log,
                                 Pmax_log, json.dumps(qp_iter),
                                 json.dumps(qp_status), json.dumps(dt * 1000))

        velqp.logger_emerg.debug('%s', v_op.tolist())

        velqp.logger_emerg.debug('%s', s_t_op.tolist())

        velqp.logger_emerg.debug('%s;%s', ax_max_log, ay_max_log)

    if velqp.sqp_stgs['b_trajectory_check']:
        ax_norm = np.abs(kappa[0:velqp.sqp_stgs['m'] - 1] *
                         v_op[0:velqp.sqp_stgs['m'] - 1]**2) / 13.5
        ay_norm = np.abs(
            (v_op[1:velqp.sqp_stgs['m']]**2 -
             v_op[0:velqp.sqp_stgs['m'] - 1]**2) / (2 * np.array(delta_s)) +
            (velqp.sym_sc_['c_res_'] * v_op[0:velqp.sqp_stgs['m'] - 1]**2) /
            (1000 * velqp.sym_sc_['m_t_'])) / 13.5

        perf_check = (ax_norm + ay_norm) > 1

        if perf_check[:-1].any():
            print(ax_norm + ay_norm)
            print('*** SQP: Trajectory not OK! ', velqp.sid, ' ***')

    if velqp.sqp_stgs['b_print_n_sqp']:
        print(velqp.sid + ' | nSQP ' + str(n_sqp))

    if velqp.sqp_stgs['b_print_s_v_val']:
        print(velqp.sid + ' | s_v_tires ' + str(x0_s_t))

    if velqp.sqp_stgs['b_print_J']:
        print("(v - v_max) ** 2", np.sum((v_op - v_max)**2))
        print("Tre. slacks", velqp.sym_sc_['s_tre_w_'] * np.sum(s_t_op**2))

    return v_op, s_t_op, qp_status
Ejemplo n.º 38
0
def bpbounds_calc_tri_z2(p: np.array) -> Dict:
    # assuming that p is in the following order
    # notation for conditional probabilities is p(y,x|z)
    p = p.ravel()
    p000 = p[0]
    p100 = p[2]
    p010 = p[1]
    p110 = p[3]
    p001 = p[4]
    p101 = p[6]
    p011 = p[5]
    p111 = p[7]

    # pearl bounds on probabilities
    p10low1 = p101
    p10low2 = p100
    p10low3 = p100 + p110 - p001 - p111
    p10low4 = p010 + p100 - p001 - p011
    p10upp1 = 1 - p001
    p10upp2 = 1 - p000
    p10upp3 = p010 + p100 + p101 + p111
    p10upp4 = p100 + p110 + p011 + p101
    p10low = max([p10low1, p10low2, p10low3, p10low4])
    p10upp = min([p10upp1, p10upp2, p10upp3, p10upp4])
    p11low1 = p110
    p11low2 = p111
    p11low3 = -p000 - p010 + p001 + p111
    p11low4 = -p010 - p100 + p101 + p111
    p11upp1 = 1 - p011
    p11upp2 = 1 - p010
    p11upp3 = p000 + p110 + p101 + p111
    p11upp4 = p100 + p110 + p001 + p111
    p11low = max([p11low1, p11low2, p11low3, p11low4])
    p11upp = min([p11upp1, p11upp2, p11upp3, p11upp4])

    p10lower = [p10low1, p10low2, p10low3, p10low4]
    p10upper = [p10upp1, p10upp2, p10upp3, p10upp4]
    p11lower = [p11low1, p11low2, p11low3, p11low4]
    p11upper = [p11upp1, p11upp2, p11upp3, p11upp4]

    retlist = {
        "p10low": p10low,
        "p10upp": p10upp,
        "p11low": p11low,
        "p11upp": p11upp,
        "p10lower": p10lower,
        "p10upper": p10upper,
        "p11lower": p11lower,
        "p11upper": p11upper
    }

    # bounds on causal risk ratio
    rrlow = p11low / p10upp
    rrupp = p11upp / p10low
    retlist["crrlb"] = rrlow
    retlist["crrub"] = rrupp

    # monotonicity bounds
    m1 = p000 - p001 >= 0
    m2 = p011 - p010 >= 0
    m3 = p100 - p101 >= 0
    m4 = p111 - p110 >= 0
    mlow = p000 - p001 - p011 - p101
    mupp = p000 + p010 + p110 - p011
    monoinequality = (m1 == True & m2 == True & m3 == True & m4 == True)
    if monoinequality:
        retlist["monobplb"] = mlow
        retlist["monobpub"] = mupp

        # bounds on intervention probabilities assuming monotonicity
        monop10low = p100
        monop10upp = 1 - p000
        monop11low = p111
        monop11upp = 1 - p011
        retlist["monop10lb"] = monop10low
        retlist["monop10ub"] = monop10upp
        retlist["monop11lb"] = monop11low
        retlist["monop11ub"] = monop11upp

        # bounds on causal risk ratio assuming monotonicity
        monocrrlow = monop11low / monop10upp
        monocrrupp = monop11upp / monop10low
        retlist["monocrrlb"] = monocrrlow
        retlist["monocrrub"] = monocrrupp
    else:
        retlist["monobplb"] = 0
        retlist["monobpub"] = 0
        retlist["monop10lb"] = 0
        retlist["monop10ub"] = 0
        retlist["monop11lb"] = 0
        retlist["monop11ub"] = 0
        retlist["monocrrlb"] = 0
        retlist["monocrrub"] = 0

    retlist["monoinequality"] = monoinequality
    return retlist
Ejemplo n.º 39
0
 def __init__(self, image: np.array, copy=False):
     """construct from numpy array"""
     if copy:
         self.mat = image.copy()
     else:
         self.mat = image
Ejemplo n.º 40
0
def L_BFGS(f, f_grad, x_0: np.array, tol: float, stepsize: str, max_iter: int, m_lbfgs: int):
    """
    Input: funtion: f, the gradient of f: f_grad, initial point: x_0, tolerence: tol,
           step size strategies: stepsize, maximium iteration times: max_iter, 
           memory parameter: m_lbfgs
           
    Output: 2-d list: result. Every element of result is a list for each iteration. 
            e.g. ['iteration k', 'x_k', 'stepsize alpha_k', 'f(x_k)', 'norm of f_grad(x_k)'] :: may change
    """
    
    k, x_k = 0, np.array(x_0.tolist())
    alpha_k = 0
    norm_gradient_list = []
    
    result = [['iteration k', 'x_k', 'alpha_k', 'f(x_k)', 'norm of f_grad(x_k)']]
    result.append([k, x_k.tolist(), alpha_k, f(x_k), np.linalg.norm(f_grad(x_k))])
    norm_gradient_list.append(np.linalg.norm(f_grad(x_k)))
    
    #initiate yk_list and sk_list
    y_list, s_list = [], []
    for i in np.arange(m_lbfgs):
        y_list.append(np.zeros(n+1))
        s_list.append(np.zeros(n+1))
    ###Update first x_next by using backtracking and gradient method
    d_k = -f_grad(x_k)
        
    stepsize_strategy = stepsize_strategies[stepsize]
    
    if stepsize == 'backtrack':
        alpha_k = stepsize_strategy(f, f_grad, x_k, d_k, s=1, sigma=0.5, gamma=0.1)
    
    x_next = x_k + alpha_k*d_k
    k = k + 1
    norm_gradient_list.append(np.linalg.norm(f_grad(x_next)))
    result.append([k, x_next.tolist(), alpha_k, f(x_next), np.linalg.norm(f_grad(x_next))]) #first step
    while np.linalg.norm(f_grad(x_next)) >= tol:
        
        yk = f_grad(x_next) - f_grad(x_k)
        
        #update sk
        sk = x_next - x_k
        
        y_list.append(yk)
        s_list.append(sk) # add from back to forward
        y_list.pop(0)
        s_list.pop(0)

        #compute gamma_k
        if sk.dot(yk) < 1e-14:
            gamma_k = 1
        else:
            gamma_k = sk.dot(yk)/np.linalg.norm(yk)**2

        alpha_list = []
        q = f_grad(x_k)
        #Recursive for initializing {H_k}^0
        for i in range(m_lbfgs-1, -1, -1):  ##倒着来
            if s_list[i].dot(y_list[i]) < 1e-14:
                alpha_list.append(0)
                continue
            else:
                alpha_i = s_list[i].dot(q)/(s_list[i].dot(y_list[i]))
                q = q - alpha_i*y_list[i]
                alpha_list.append(alpha_i)           
        
        r = gamma_k*q
            
        #Iterative for our final r
        for i in range(m_lbfgs): ##顺着来
            if s_list[i].dot(y_list[i]) < 1e-14:
                continue
            else:
                beta = y_list[i].dot(r)/(s_list[i].dot(y_list[i]))
                r = r + (alpha_list[i] - beta)*s_list[i]
        
        d_k = -r
        ## For Stepsize
        if stepsize == 'backtrack':
            alpha_k = stepsize_strategy(f, f_grad, x_k, d_k, s=1, sigma=0.5, gamma=0.1)
        
        #update x_k, x_next
        x_k = np.array(x_next.tolist())
        x_next = x_next + alpha_k*d_k
    
        k += 1
        norm_gradient_list.append(np.linalg.norm(f_grad(x_next)))
        result.append([k, x_next.tolist(), alpha_k, f(x_next), np.linalg.norm(f_grad(x_next))])
        
        if k == max_iter:
            print('max iteration:', 
                  k, 'the function value is', f(x_next), 'the norm of gradient is:', np.linalg.norm(f_grad(x_next)))
            break 
    
    xk_result = np.array(result[-1][1]).reshape(-1,1)
    norm_gradient_list = np.array(norm_gradient_list)
    plot_convergence(norm_gradient_list, 3)
    print("iterations:", k)
    plot_result(m, xk_result, 3)
    print("accuracy:", test(xk_result, dataset_num))
    return result
Ejemplo n.º 41
0
 def _process_depth(self, depth: np.array) -> np.array:
     depth = depth.astype(np.float32)
     return depth
Ejemplo n.º 42
0
def find_the_way(start: tuple, wall_map: np.array, coin_pos: np.array, max_dist = 100):
	'''
	finds the best way to go by applying dyjkstras algorithm
	:start: tuple of starting coordinates
	:wall_map: np.array of the whole board with 0 where there is a free tile
	:coin_pos: np.array of coordinates of target positions
	:ans: np.array of features (1 if this way is the ideal way, 0 if not)
	'''
	#board dimensions
	dim = wall_map.shape
	#some ausiliary functions
	def isin(pos, array):
		ans = False
		for e in array:
			if e[0] == pos[0] and e[1] == pos[1]: 
				ans = True
				break
		return ans

	def within_bounds(pos):
		return (np.array(pos) < np.array(dim)).all() and (np.array(pos) >= 0).all()
	
	#initialize all four features as 0, the one that ist best to go along will be set to 1 then
	ans = np.zeros(5) #up, right, down, left, dist
	
	#some auxiliary stacks and dicts
	inspecting = [start]
	done = []
	parents = {start:start}
	distances = {start: 0}
	translations = [(0,-1), (1,0), (0,1), (-1,0)] #up, right, down, left
	found = False
	
	#if there arent any coins return 0 0 0 0
	if len(coin_pos) == 0: return ans
	
	#if not find the shortest way to the closest coin
	while len(inspecting) > 0:
		#check all the tiles in consideration
		relevant_dists = []
		for i in range(len(inspecting)):
			relevant_dists.append(distances[inspecting[i]])
		#now consider the tile with the smallest distance from the start
		current = inspecting.pop(np.argmin(relevant_dists))
		#append it to current so it wont get considered again
		done.append(current)
		
		#break if that is a coin
		if isin(current, coin_pos):
			#final = current
			found = True
			break

		#else give all the neighbors a distance based on the distance of the current tile
		for t in translations:
			neigh = (current[0]+t[0], current[1]+t[1])
			if within_bounds(neigh) and wall_map[neigh] == 0 and distances[current] < max_dist: #check if that is a free tile and the max distance hasnt been exceeded yet
				if neigh not in inspecting and neigh not in done: inspecting.append(neigh) #check if this tile hasnt been used yet
				if neigh not in distances or distances[neigh] > distances[current] + 1:  #check if the current distance is smaller than the previous one
					distances[neigh] = distances[current] + 1
					parents[neigh] = current
	ans[4] = distances[tuple(current)]
	#if there is no way to any of the targets
	if not found:
		return ans
	#trace back the path to the coin and stop one tile before the starting tile so we know which way to go
	while parents[current] != start:
		current = parents[current]
	#find the right move that gets you to the target
	for i in range(4):
		t = translations[i]
		if current == (start[0] + t[0], start[1] + t[1]):
			ans[i] = 1
			break
	#check if going any other way would get you to a target with the same distance
	u, r, d, l, dist = tuple(ans)
	new_map = wall_map.copy()
	while (np.array([u, r, d, l]) == 1).any() and max_dist == 100: #max recursion depth is 1
		t = translations[np.where(np.array([u, r, d, l]) == 1)[0][0]]
		coord = (start[0]+t[0], start[1]+t[1])
		new_map[coord] = -1
		u, r, d, l, dist = find_the_way(start, new_map, coin_pos, max_dist=ans[4])
		if dist == ans[4] and (np.array([u, r, d, l]) == 1).any(): ans += np.array([u, r, d, l, 0])
	return tuple(ans)
Ejemplo n.º 43
0
def to_one_hot(y: np.array, categories='auto') -> np.array:
    encoder = OneHotEncoder(categories=categories, sparse=False)
    return encoder.fit_transform(y.reshape(-1, 1))