def test_ctm_adjoint():
    scen, control = small_scenario()
    state = CTMSimulator().simulate(scen, control)
    hx = CTMAdjoint(scen).hx(state, control)
    hu = CTMAdjoint(scen).hu(state, control)
    assert_array_equal(hx, np.tril(hx))
    u = np.ones(control.flatten().shape) * 0.01
    adjoint = CTMAdjoint(scen)
    assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u), 3)
def test_ttt():
    scen, control = small_scenario()
    state = CTMSimulator().simulate(scen, control)
    adjoint = CTMAdjoint(scen)
    jx_test = adjoint.jx(state, control)
    jx = np.zeros(scen.N * scen.T * 8)
    for i in range(scen.N):
        for t in range(scen.T):
            jx[t * scen.N * 8 + i] = 1.0
            jx[t * scen.N * 8 + scen.N + i] = 1.0
    assert_array_equal(jx, jx_test)
    ju = adjoint.ju(state, control)
    assert_array_equal(ju, np.zeros(scen.N * scen.T))
Example #3
0
def test_fs_controlled():
    beta_op.beta = 1.0
    scen, control = small_scenario()
    ca = CTMAdjoint(scen)
    for _ in range(5):
        c = rand(scen.N * scen.T, 1).flatten()
        assert_array_almost_equal(ca.grad(c), ca.grad_fd(c))
        # fs = CTMSimulator().simulate(scen, control)
    ca = ControlledCtmAdjoint(True, scen)
    control = ca.construct_control(ca.no_control())
    fs = ca.fs(control)
    # assert_array_equal(fs.density, [[.9, 1.0, .9, .1, 0, 0, 0, 0, 0, 0, 0], [0, 1.0, .9, .9, .1, 0, 0, 0, 0, 0, 0]])
    assert_array_almost_equal(fs.density, [[0.9, 1.0, 0.9, 0.1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
    control[1][:] += 2.0
    fs = ca.fs(control)
    assert_array_almost_equal(
        fs.density, np.array([[0.9, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0]])
    )
def test_simplest():
    scen, control = smallest_possible()
    u = control.flatten()
    adjoint = CTMAdjoint(scen)
    assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u))