def estimate(self, eppci: ExtendedPPCI, **kwargs): self.initialize(eppci) # matrix calculation object sem = BaseAlgebra(eppci) current_error, cur_it = 100., 0 # invert covariance matrix r_inv = csr_matrix(np.diagflat(1 / eppci.r_cov**2)) E = eppci.E while current_error > self.tolerance and cur_it < self.max_iterations: self.logger.debug("Starting iteration {:d}".format(1 + cur_it)) try: # residual r r = csr_matrix(sem.create_rx(E)).T # jacobian matrix H H = csr_matrix(sem.create_hx_jacobian(E)) # gain matrix G_m # G_m = H^t * R^-1 * H G_m = H.T * (r_inv * H) # state vector difference d_E # d_E = G_m^-1 * (H' * R^-1 * r) d_E = spsolve(G_m, H.T * (r_inv * r)) # Update E with d_E E += d_E.ravel() eppci.update_E(E) # prepare next iteration cur_it += 1 current_error = np.max(np.abs(d_E)) self.logger.debug( "Current error: {:.7f}".format(current_error)) except np.linalg.linalg.LinAlgError: self.logger.error( "A problem appeared while using the linear algebra methods." "Check and change the measurement set.") return False # check if the estimation is successfull self.check_result(current_error, cur_it) if self.successful: # store variables required for chi^2 and r_N_max test: self.R_inv = r_inv.toarray() self.Gm = G_m.toarray() self.r = r.toarray() self.H = H.toarray() # create h(x) for the current iteration self.hx = sem.create_hx(eppci.E) return eppci
def estimate(self, eppci: ExtendedPPCI, estimator="wls", verbose=True, **kwargs): opt_method = DEFAULT_OPT_METHOD if 'opt_method' not in kwargs else kwargs['opt_method'] # matrix calculation object estm = get_estimator(BaseEstimatorOpt, estimator)(eppci, **kwargs) jac = estm.create_cost_jacobian res = minimize(estm.cost_function, x0=eppci.E, method=opt_method, jac=jac, tol=self.tolerance, options={"disp": verbose}) self.successful = res.success if self.successful: E = res.x eppci.update_E(E) return eppci else: raise Exception("Optimiaztion failed! State Estimation not successful!")
def estimate(self, eppci: ExtendedPPCI, estimator="wls", **kwargs): self.initialize(eppci) # matrix calculation object sem = get_estimator(BaseEstimatorIRWLS, estimator)(eppci, **kwargs) current_error, cur_it = 100., 0 E = eppci.E while current_error > self.tolerance and cur_it < self.max_iterations: self.logger.debug("Starting iteration {:d}".format(1 + cur_it)) try: # residual r r = csr_matrix(sem.create_rx(E)).T # jacobian matrix H H = csr_matrix(sem.create_hx_jacobian(E)) # gain matrix G_m # G_m = H^t * Phi * H phi = csr_matrix(sem.create_phi(E)) G_m = H.T * (phi * H) # state vector difference d_E and update E d_E = spsolve(G_m, H.T * (phi * r)) E += d_E.ravel() eppci.update_E(E) # prepare next iteration cur_it += 1 current_error = np.max(np.abs(d_E)) self.logger.debug( "Current error: {:.7f}".format(current_error)) except np.linalg.linalg.LinAlgError: self.logger.error( "A problem appeared while using the linear algebra methods." "Check and change the measurement set.") return False # check if the estimation is successfull self.check_result(current_error, cur_it) # update V/delta return eppci
def estimate(self, eppci: ExtendedPPCI, with_ortools=True, **kwargs): if "estimator" in kwargs and kwargs["estimator"].lower() != "lav": # pragma: no cover self.logger.warning("LP Algorithm supports only LAV Estimator!! Set to LAV!!") # matrix calculation object sem = BaseAlgebra(eppci) current_error, cur_it = 100., 0 E = eppci.E while current_error > self.tolerance and cur_it < self.max_iterations: self.logger.debug("Starting iteration {:d}".format(1 + cur_it)) try: # residual r r = sem.create_rx(E) # jacobian matrix H H = sem.create_hx_jacobian(E) # state vector difference d_E # d_E = G_m^-1 * (H' * R^-1 * r) d_E = self._solve_lp(H, E, r, with_ortools=with_ortools) E += d_E eppci.update_E(E) # prepare next iteration cur_it += 1 current_error = np.max(np.abs(d_E)) self.logger.debug("Current error: {:.7f}".format(current_error)) except np.linalg.linalg.LinAlgError: # pragma: no cover self.logger.error("A problem appeared while using the linear algebra methods." "Check and change the measurement set.") return False # check if the estimation is successfull self.check_result(current_error, cur_it) return eppci
def estimate(self, eppci: ExtendedPPCI, **kwargs): # state vector built from delta, |V| and zero injections # Find pq bus with zero p,q and shunt admittance if not np.any(eppci["bus"][:, bus_cols + ZERO_INJ_FLAG]): raise UserWarning( "Network has no bus with zero injections! Please use WLS instead!" ) zero_injection_bus = np.argwhere( eppci["bus"][:, bus_cols + ZERO_INJ_FLAG] == True).ravel() eppci["bus"][np.ix_( zero_injection_bus, [bus_cols + P, bus_cols + P_STD, bus_cols + Q, bus_cols + Q_STD ])] = np.NaN # Withn pq buses with zero injection identify those who have also no p or q measurement p_zero_injections = zero_injection_bus q_zero_injections = zero_injection_bus new_states = np.zeros(len(p_zero_injections) + len(q_zero_injections)) num_bus = eppci["bus"].shape[0] # matrix calculation object sem = BaseAlgebraZeroInjConstraints(eppci) current_error, cur_it = 100., 0 r_inv = csr_matrix((np.diagflat(1 / eppci.r_cov)**2)) E = eppci.E # update the E matrix E_ext = np.r_[eppci.E, new_states] while current_error > self.tolerance and cur_it < self.max_iterations: self.logger.debug("Starting iteration {:d}".format(1 + cur_it)) try: c_x = sem.create_cx(E, p_zero_injections, q_zero_injections) # residual r r = csr_matrix(sem.create_rx(E)).T c_rxh = csr_matrix(c_x).T # jacobian matrix H H_temp = sem.create_hx_jacobian(E) C_temp = sem.create_cx_jacobian(E, p_zero_injections, q_zero_injections) H, C = csr_matrix(H_temp), csr_matrix(C_temp) # gain matrix G_m # G_m = H^t * R^-1 * H G_m = H.T * (r_inv * H) # building a new gain matrix for new constraints. A_1 = vstack([G_m, C]) c_ax = hstack([C, np.zeros((C.shape[0], C.shape[0]))]) c_xT = c_ax.T M_tx = csr_matrix(hstack( (A_1, c_xT))) # again adding to the new gain matrix rhs = H.T * (r_inv * r) # original right hand side C_rhs = vstack( (rhs, -c_rxh )) # creating the righ hand side with new constraints # state vector difference d_E and update E d_E_ext = spsolve(M_tx, C_rhs) E_ext += d_E_ext.ravel() E = E_ext[:E.shape[0]] eppci.update_E(E) # prepare next iteration cur_it += 1 current_error = np.max( np.abs(d_E_ext[:len(eppci.non_slack_buses) + num_bus])) self.logger.debug( "Current error: {:.7f}".format(current_error)) except np.linalg.linalg.LinAlgError: self.logger.error( "A problem appeared while using the linear algebra methods." "Check and change the measurement set.") return False # check if the estimation is successfull self.check_result(current_error, cur_it) return eppci