def test_set_density_simulator(): scen, control = test_ctm.small_scenario() set_densities = {1: [.5] * scen.T} sim = ctm_admm.ControlledCtmSimulator(set_densities) state = sim.simulate(scen, control) state.plot() assert_array_equal(state.density[set_densities.keys(), :-1], set_densities.values())
def test_ctm_admm_adjoint_run(): scen, control = test_ctm.small_scenario() set_cells = [] set_ramps = [] adjoint = ctm_admm.AdmmCTMAdjoint(scen, set_cells, set_ramps) u_star = AcceleratedGradient.ramp_metering().solve(adjoint, ctm_admm.AdmmCTMAdjoint.no_control(scen, set_cells)) state = adjoint.fs(u_star) #state.plot() assert_almost_equal(adjoint.j(state, u_star), 7.7, delta=.1) set_cells = [0, 1] set_ramps = [0, 1] adjoint = ctm_admm.AdmmCTMAdjoint(scen, set_cells, set_ramps) u_star = AcceleratedGradient.ramp_metering().solve(adjoint, ctm_admm.AdmmCTMAdjoint.no_control(scen, set_cells)) state = adjoint.fs(u_star) #state.plot() assert_almost_equal(adjoint.j(state, u_star), 2.0, delta=0.01) set_cells = [] set_ramps = [0, 1] adjoint = ctm_admm.AdmmCTMAdjoint(scen, set_cells, set_ramps) u_star = AcceleratedGradient.ramp_metering().solve(adjoint, ctm_admm.AdmmCTMAdjoint.no_control(scen, set_cells)) state = adjoint.fs(u_star) assert_almost_equal(adjoint.j(state, u_star), 7.7, delta=0.05) #state.plot() u_0 = u_star.copy() u_0[10:] = 0.0 state_0 = adjoint.fs(u_0)
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_multi_split(): scen, control = test_ctm.small_scenario() adjoint_left = ctm_admm.AdmmCTMAdjoint(scen, True) adjoint_right = ctm_admm.AdmmCTMAdjoint(scen, False) problem = CtmAdmm([adjoint_left, adjoint_right], 100) from multi_agent_admm import settings settings.beta = .7 problem.solve() problem.plot_history()
def test_interpretation(self): scen, control = small_scenario() dummy_links = [0] control = numpy.array( [ 1.0, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.9, 0.9, 0.0, 0, 0, 0, 0, 0, 0, ] ) dummy = DummyControl(dummy_links, scen) dummy.alpha = 0.0 self.assertAlmostEqual(7.7, dummy.eval(control)) dummy.alpha = 5.0 self.assertAlmostEqual(7.7, dummy.eval(control)) control[20] = 0.0 self.assertAlmostEqual(7.7 + dummy.alpha * 0.9 ** 2, dummy.eval(control)) control[20] = 0.9 grad = dummy.grad(control) assert_array_almost_equal(grad[20:], numpy.zeros(10)) control[1] = 0.5 print dummy.grad(control) control[20] = 0.5 grad = dummy.grad(control) self.assertAlmostEqual(grad[20], -dummy.alpha * 2 * (0.4)) control[20] = 1.1 grad = dummy.grad(control) self.assertAlmostEqual(grad[20], dummy.alpha * 2 * (0.2)) control[20] = 0.9 print AcceleratedGradient.ramp_metering().solve(dummy, control)
def test_reshaping(self): scen, control = small_scenario() dummy_links = [0] control = numpy.array( [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20] ) dummy = DummyControl(dummy_links, scen) assert_array_almost_equal(dummy.reshape_metering(control), numpy.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] * 2)) assert_array_almost_equal(dummy.reshape_dummy_control(control), numpy.array([[20] * 10]))
def test_something(self): scen, control = small_scenario() dummy_links = [0] print control.flatten().shape control = numpy.concatenate((control.flatten(), numpy.zeros(len(dummy_links) * scen.T)), 1) # sln = AcceleratedGradient.ramp_metering(30).solve(DummyControl(dummy_links, scen), control) sln = IpOptSolver(n_iters=50).solve(DummyControl(dummy_links, scen), control) print scen.ttt(scen.simulate(sln[:-10])) print sln[-10:] # - scen.simulate(sln[:-10]).density[dummy_links, :-1]
def test_no_ramp(): scen, control = test_ctm.small_scenario() set_cells_left = [1] set_ramps_left = [] adjoint_left = ctm_admm.AdmmCTMAdjoint(scen, set_cells_left, set_ramps_left) set_cells_right = [0] set_ramps_right = [] adjoint_right = ctm_admm.AdmmCTMAdjoint(scen, set_cells_right, set_ramps_right) sln = CtmAdmm([adjoint_left, adjoint_right]) sln.solve()
def test_better(): beta_op.beta = 200.0 beta_op.factor = 1.000 beta_op.beta_admm_factor = 1.000 beta_op.inner_iters = 15 beta = 1.0 scen_l, _ = small_scenario() sln = CtmAdjointAdmm(scen_l, scen_l, scen_l, beta=beta, n_iters=200).solve() sln.density_summary() best_score = min(sln.history.ttt[-10:]) assert_less(best_score, 7.71) assert_greater(best_score, 7.7)
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))
def test_hx(): scen, control = small_scenario() cl = ControlledCtmAdjoint(True, scen) nc = cl.construct_control(cl.no_control()) state = cl.fs(nc) hx = cl.hx(state, nc) n, t = scen.N, scen.T for i in range(t): assert_equal(hx[i * 8 * n + n - 1, i * 8 * n + n - 1], 1.0) assert_equal(sum(abs(hx[i * 8 * n + n - 1, :])), 1.0) cl = ControlledCtmAdjoint(False, scen) nc = cl.construct_control(cl.no_control()) state = cl.fs(nc) hx = cl.hx(state, nc) n, t = scen.N, scen.T for i in range(t): assert_equal(hx[i * 8 * n, i * 8 * n], 1.0) assert_equal(sum(abs(hx[i * 8 * n, :])), 1.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_fs_adjoint(): beta_op.beta = 1.0 scen, control = small_scenario() ca = ControlledCtmAdjoint(True, scen) control = np.array([0.0] * scen.T * scen.N + [2.0] * scen.T) u_star = ca.construct_control(ca.solve(control)) u_nc = ca.construct_control(ca.no_control()) assert_less(ca.j(ca.fs(u_star), u_star), 5.0) assert_almost_equals(ca.j(ca.fs(u_nc), u_nc), 5.0, 4) ca = ControlledCtmAdjoint(False, scen) control = np.array([0.0] * scen.T * scen.N + [2.0] * scen.T) u_star = ca.construct_control(ca.solve(control)) u_nc = ca.construct_control(ca.no_control()) fs_star = ca.fs(u_star) fs_nc = ca.fs(u_nc) # fs_nc.plot() # fs_star.plot() assert_almost_equals(ca.j(fs_star, u_star), 2.2, 2) assert_almost_equals(ca.j(fs_nc, u_nc), 2.2, 4)
def test_hu(): scen, control = small_scenario() cl = ControlledCtmAdjoint(True, scen) nc = cl.construct_control(cl.no_control()) state = cl.fs(nc) hu = cl.hu(state, nc) n, t = scen.N, scen.T for i in range(t): assert_equal(hu[i * 8 * n + n - 1, n * t + i], -1.0) assert_equal(abs(hu[:, n * t :]).sum().sum(), t) cl = ControlledCtmAdjoint(False, scen) nc = cl.construct_control(cl.no_control()) state = cl.fs(nc) hu = cl.hu(state, nc) n, t = scen.N, scen.T for i in range(t): print i * 8 * n, n * t + i assert_equal(hu[i * 8 * n, n * t + i], -1.0) assert_equal(abs(hu[:, n * t :]).sum().sum(), t) print hu[:, -1]
def test_split_network(): scen, control = test_ctm.small_scenario() set_cells_left = [1] set_ramps_left = [1] adjoint_left = ctm_admm.AdmmCTMAdjoint(scen, set_cells_left, set_ramps_left) set_cells_right = [0] set_ramps_right = [0] adjoint_right = ctm_admm.AdmmCTMAdjoint(scen, set_cells_right, set_ramps_right) T = scen.T xy_map_left = range(T) + range(2 * T, 3 * T) xy_map_right = range(T, 2 * T) + range(3 * T, 4 * T) uy_map_left = range(4 * T) uy_map_right = range(4 * T) yz_map_left = range(2 * T, 4 * T) + range(2 * T) + range(4 * T, 6 * T) yz_map_right = range(4 * T, 6 * T) + range(2 * T) + range(2 * T, 4 * T) sub_problems = [ AdjointAdmmSubProblem(AdjointAdmmData(adjoint_left, xy_map_left, uy_map_left, yz_map_left, ctm_admm.AdmmCTMAdjoint.no_control(scen, set_cells_left, set_ramps_left), rho=20.), solver=AcceleratedGradient(15, 2000, 0.0, 1.0))] + [ AdjointAdmmSubProblem(AdjointAdmmData(adjoint_right, xy_map_right, uy_map_right, yz_map_right, ctm_admm.AdmmCTMAdjoint.no_control(scen, set_cells_right, set_ramps_right), rho=20.), solver=AcceleratedGradient(15, 2000, 0.0, 1.0)) ] sln = Admm(sub_problems, n_iters=100) sln.solve() control = sln.z[:2 * T].reshape(T, 2).transpose() state = CTMSimulator().simulate(scen, control) state.plot() ttt = scen.ttt(state) print control print ttt sln.plot()
def test_full_run(): scen, control = small_scenario() adjoint = CTMAdjoint(scen) u_star = AcceleratedGradient.ramp_metering().solve(adjoint, control.ravel()) assert_almost_equal(scen.ttt(CTMSimulator().simulate(scen, u_star)), 7.7, 2)