def test_case30_compare_classical_wls_opt_wls(): net = nw.case30() pp.runpp(net) add_virtual_meas_from_loadflow(net) try: success = estimate(net, init='flat', algorithm="opt", estimator='wls') assert success except: # if failed give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 3) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="wls") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) net_wls = deepcopy(net) estimate(net_wls) assert np.allclose(net_wls.res_bus_est.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net_wls.res_bus_est.va_degree, net.res_bus_est.va_degree, atol=1e-2)
def test_continuous_element_numbering(): from pandapower.estimation.util import add_virtual_meas_from_loadflow net = nw.example_multivoltage() # Add noises to index with some large number net.line.rename(index={4: 280}, inplace=True) net.trafo.rename(index={0: 300}, inplace=True) net.trafo.rename(index={1: 400}, inplace=True) net.trafo3w.rename(index={0: 540}, inplace=True) net.switch.loc[(net.switch.et == "l") & (net.switch.element == 4), "element"] = 280 net.switch.loc[(net.switch.et == "t") & (net.switch.element == 0), "element"] = 300 net.switch.loc[(net.switch.et == "t") & (net.switch.element == 1), "element"] = 400 pp.runpp(net) add_virtual_meas_from_loadflow(net) assert net.measurement["element"].max() == 540 net = tb.create_continuous_elements_index(net) assert net.line.index.max() == net.line.shape[0] - 1 assert net.trafo.index.max() == net.trafo.shape[0] - 1 assert net.trafo3w.index.max() == net.trafo3w.shape[0] - 1 assert net.measurement["element"].max() == net.bus.shape[0] - 1
def test_irwls_shgm(): net = nw.case14() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) success = estimate(net, algorithm="irwls", estimator="shgm", a=3, maximum_iterations=50) assert success assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, 1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, 1e-2)
def test_lp_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) estimate(net, algorithm="lp") if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or\ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_lp_lav(): net = nw.case14() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) estimate(net, algorithm="lp") assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2)
def test_irwls_comp_wls(): # it should be the same since wls will not update weight matrix net = nw.case14() pp.runpp(net) add_virtual_meas_from_loadflow(net) success = estimate(net, init='flat', algorithm="irwls", estimator='wls') assert success net_wls = deepcopy(net) estimate(net_wls) assert np.allclose(net_wls.res_bus_est.vm_pu, net.res_bus_est.vm_pu, 1e-6) assert np.allclose(net_wls.res_bus_est.va_degree, net.res_bus_est.va_degree, 1e-6)
def test_lp_lav(): ''' This will test the default LP solver installed. If OR-Tools is installed, it will use it. Otherwise scipy is used. ''' net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) estimate(net, algorithm="lp") if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_ql_qc(): net = nw.case9() net.sn_mva = 1. pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) pf_vm_pu, pf_va_degree = net.res_bus.vm_pu, net.res_bus.va_degree # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, verbose=False) if not estimation_opt.successful: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, opt_method="Newton-CG", verbose=False) if not estimation_opt.successful: raise AssertionError("Estimation failed due to algorithm failing!") net = eppci2pp(net, ppc, eppci) if not np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!") # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-6, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, verbose=False) if not estimation_opt.successful: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, opt_method="Newton-CG", verbose=False) net = eppci2pp(net, ppc, eppci) if not np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_shgm_ps(): # we need an random eppci object to initialize estimator net = nw.case14() pp.runpp(net) add_virtual_meas_from_loadflow(net) _, _, eppci = pp2eppci(net) # Using the example from Mili's paper H = np.array([[10, -10], [1, 0], [-1, 0], [0, -1], [0, 1], [11, -10], [-1, -1]]) estm = SHGMEstimatorIRWLS(eppci, a=3) ps_estm = estm._ps(H) assert np.allclose(ps_estm, np.array([8.39, 0.84, 0.84, 0.84, 0.84, 8.82, 1.68]), atol=0.005)
def test_opt_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="lav", verbose=False) net = eppci2pp(net, ppc, eppci) if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_ql_qc(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) pf_vm_pu, pf_va_degree = net.res_bus.vm_pu, net.res_bus.va_degree # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) try: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3) assert estimation_opt.successful except: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, opt_method="Newton-CG") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2) # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-6, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) try: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3) assert estimation_opt.successful except: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, opt_method="Newton-CG") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2)
def test_opt_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="lav") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2)
def test_recycle_case30(): net = nw.case30() pp.runpp(net) add_virtual_meas_from_loadflow(net) se = StateEstimation(net, recycle=True) se.estimate() assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-1) # Run SE again net.load.p_mw -= 10 pp.runpp(net) add_virtual_meas_from_loadflow(net) assert se.estimate() assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=1)
def test_lp_ortools_lav(): ''' If OR-Tools is installed, run this test. ''' # Set the solver LPAlgorithm.ortools_available = True net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_ortools_lp = LPAlgorithm(1e-3, 5) estimation_ortools = estimation_ortools_lp.estimate(eppci, with_ortools=True) net = eppci2pp(net, ppc, eppci) if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_case9_compare_classical_wls_opt_wls(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net) # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 3) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="wls", verbose=False) if not estimation_opt.successful: raise AssertionError("Estimation failed due to algorithm failing!") net = eppci2pp(net, ppc, eppci) net_wls = net.deepcopy() estimate(net_wls) if not np.allclose(net_wls.res_bus_est.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net_wls.res_bus_est.va_degree, net.res_bus_est.va_degree, atol=1e-2): raise AssertionError("Estimation failed!")