Example #1
0
    def gen_state_output_vars(self, variables=None,
                              x_k=None, omega_tilde_k=None,
                              N_p=None, N_tilde=None,
                              mld_numeric_k: MldModel = None, mld_numeric_tilde=None,
                              mld_info_k: MldInfo = None):

        state_output_vars = StructDict({var_name: EvoVariableStruct() for var_name in self._state_output_vars})

        x_k = x_k if x_k is not None else self.x_k
        omega_tilde_k = omega_tilde_k if omega_tilde_k is not None else self.omega_tilde_k

        variables = variables or self

        if mld_info_k.nx:
            state_output_vars.x.var_N_tilde = (
                    matmul(self.mld_evo_matrices.state_input['Phi_x_N_tilde'], x_k) +
                    matmul(self.mld_evo_matrices.state_input['Gamma_v_N_tilde'], variables.v.var_N_tilde) +
                    matmul(self.mld_evo_matrices.state_input['Gamma_omega_N_tilde'], omega_tilde_k) +
                    self.mld_evo_matrices.state_input['Gamma_5_N_tilde']
            )
        else:
            state_output_vars.x.var_N_tilde = np.empty((0, 1))

        if mld_info_k.ny:
            state_output_vars.y.var_N_tilde = (
                    matmul(self.mld_evo_matrices.output['L_x_N_tilde'], x_k) +
                    matmul(self.mld_evo_matrices.output['L_v_N_tilde'], variables['v']['var_N_tilde']) +
                    matmul(self.mld_evo_matrices.output['L_omega_N_tilde'], omega_tilde_k) +
                    self.mld_evo_matrices.output['L_5_N_tilde']
            )
        else:
            state_output_vars.y.var_N_tilde = np.empty((0, 1))

        for var_name in self._state_output_vars:
            var_N_tilde = state_output_vars[var_name].var_N_tilde
            self._set_var_using_var_N_tilde(variable=state_output_vars[var_name],
                                            var_dim=mld_info_k.get_var_dim(var_name),
                                            var_N_tilde=var_N_tilde,
                                            N_p=N_p, N_tilde=N_tilde)

        return state_output_vars
 def apply_atom_with_matrix_weight(self, variable, weight, var_dim=None):
     mat_weighted = matmul(weight, variable.var_N_tilde[:weight.shape[1]])
     return cvx.sum(
         cvx.norm1(cvx.reshape(mat_weighted,
                               (var_dim, mat_weighted.size / var_dim)),
                   axis=0))
 def apply_atom_with_matrix_weight(self, variable, weight, var_dim=None):
     return cvx.norm1(matmul(weight,
                             variable.var_N_tilde[:weight.shape[1]]))
 def apply_atom_with_vector_weight(self, variable, weight, var_dim=None):
     return matmul(weight.T, variable.var_N_tilde[:weight.shape[0]])
    def gen_evo_constraints(self,
                            x_k=None,
                            omega_tilde_k=None,
                            omega_scenarios_k=ParNotSet,
                            N_p=ParNotSet,
                            N_tilde=ParNotSet,
                            mld_numeric_k=ParNotSet,
                            mld_numeric_tilde=ParNotSet,
                            mld_evo_matrices=ParNotSet):
        # H_v @ v_tilde <= H_x @ x_0 + H_omega @ omega_tilde + H_5

        x_k = x_k if x_k is not None else self.x_k
        omega_tilde_k = omega_tilde_k if omega_tilde_k is not None else self.omega_tilde_k

        if mld_evo_matrices is ParNotSet:
            mld_numeric_k = mld_numeric_k if mld_numeric_k is not self.mld_numeric_k else ParNotSet
            mld_numeric_tilde = mld_numeric_tilde if mld_numeric_tilde is not self.mld_numeric_tilde else ParNotSet

            N_p = N_p if N_p is not ParNotSet else self.N_p
            N_tilde = N_tilde if N_tilde is not ParNotSet else self.N_tilde

            if not N_tilde <= self.N_tilde:
                raise ValueError(
                    f"N_tilde: {N_tilde} must be less or equal to self.N_tilde: {self.N_tilde}"
                )

            if not N_p <= self.N_tilde:
                raise ValueError(
                    f"N_p: {N_tilde} must be less or equal to self.N_tilde: {self.N_tilde}"
                )

            if not eq_all_val(mld_numeric_k, mld_numeric_tilde, val=ParNotSet):
                mld_evo_matrices = MldEvoMatrices(
                    self,
                    N_p=N_p,
                    N_tilde=N_tilde,
                    mld_numeric_k=mld_numeric_k,
                    mld_numeric_tilde=mld_numeric_tilde)
            elif N_tilde == self.N_tilde:
                mld_evo_matrices = self._mld_evo_matrices
            else:
                mld_evo_matrices = self._mld_evo_matrices.get_evo_matrices_N_tilde(
                    N_tilde=N_tilde)

        if mld_evo_matrices is not None and omega_scenarios_k is not None:
            if mld_evo_matrices.mld_info_k.n_constraints:
                if omega_scenarios_k is not ParNotSet:
                    H_omega_omega = np.min(matmul(
                        mld_evo_matrices.constraint['H_omega_N_tilde'],
                        omega_scenarios_k),
                                           axis=1,
                                           keepdims=True)
                else:
                    H_omega_omega = matmul(
                        mld_evo_matrices.constraint['H_omega_N_tilde'],
                        omega_tilde_k)

                LHS = (matmul(mld_evo_matrices.constraint['H_v_N_tilde'],
                              self._variables['v']['var_N_tilde']))
                RHS = (
                    matmul(mld_evo_matrices.constraint['H_x_N_tilde'], x_k) +
                    H_omega_omega + mld_evo_matrices.constraint['H_5_N_tilde'])

                return LHS <= RHS
            else:
                return cvx.Constant(0) <= cvx.Constant(0)
        else:
            return None