コード例 #1
0
    def test_split_in_3(self):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore")
            env = make("educ_case14_redisp", test=True)
            gym_env = GymEnv(env)
            act_space = gym_env.action_space
            act_space = act_space.reencode_space(
                "redispatch",
                ContinuousToDiscreteConverter(
                    nb_bins=3, init_space=act_space["redispatch"]))

            # with 3 interval like [-10, 10] (the second generator)
            # should be split => 0 -> [-10, -3.33), 1 => [-3.33, 3.33), [3.33, 10.]
            # test the "all 0" action (all 0 => encoded to 1, because i have 3 bins)
            g2op_object = np.array([0., 0., 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [1, 1, 0, 0, 0, 1])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(res2 == 0.)

            # test the all 0 action, but one is not 0 (negative)
            g2op_object = np.array([0., -3.2, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [1, 1, 0, 0, 0, 1])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(res2 == 0.)

            # test the all 0 action, but one is not 0 (positive)
            g2op_object = np.array([0., 3.2, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [1, 1, 0, 0, 0, 1])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(res2 == 0.)

            # test one is 2
            g2op_object = np.array([0., 3.4, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [1, 2, 0, 0, 0, 1])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(np.abs(res2 - [0., 5.0, 0., 0., 0., 0.]) <= self.tol)

            # test one is 0
            g2op_object = np.array([0., -3.4, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [1, 0, 0, 0, 0, 1])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., -5.0, 0., 0., 0., 0.]) <= self.tol)
コード例 #2
0
ファイル: test_pickling.py プロジェクト: colin-fox/Grid2Op
    def test_basic(self):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore")
            env = grid2op.make("l2rpn_case14_sandbox",
                               test=True,
                               _add_to_name="for_mp_test")
        env_gym = GymEnv(env)

        obs_gym = env_gym.reset()

        # 3. (optional) customize it (see section above for more information)
        ## customize action space
        env_gym.action_space = env_gym.action_space.ignore_attr(
            "set_bus").ignore_attr("set_line_status")
        env_gym.action_space = env_gym.action_space.reencode_space(
            "redispatch", ContinuousToDiscreteConverter(nb_bins=11))
        env_gym.action_space = env_gym.action_space.reencode_space(
            "change_bus", MultiToTupleConverter())
        env_gym.action_space = env_gym.action_space.reencode_space(
            "change_line_status", MultiToTupleConverter())
        env_gym.action_space = env_gym.action_space.reencode_space(
            "redispatch", MultiToTupleConverter())

        ## customize observation space
        ob_space = env_gym.observation_space
        ob_space = ob_space.keep_only_attr(
            ["rho", "gen_p", "load_p", "topo_vect", "actual_dispatch"])
        ob_space = ob_space.reencode_space(
            "actual_dispatch",
            ScalerAttrConverter(substract=0., divide=env.gen_pmax))
        ob_space = ob_space.reencode_space(
            "gen_p", ScalerAttrConverter(substract=0., divide=env.gen_pmax))
        ob_space = ob_space.reencode_space(
            "load_p",
            ScalerAttrConverter(substract=obs_gym["load_p"],
                                divide=0.5 * obs_gym["load_p"]))
        env_gym.observation_space = ob_space

        ctx = mp.get_context('spawn')
        env_gym1 = copy.deepcopy(env_gym)
        env_gym2 = copy.deepcopy(env_gym)
        with ctx.Pool(2) as p:
            p.map(TestMultiProc.f, [env_gym1, env_gym2])
コード例 #3
0
    def test_split_in_5(self):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore")
            env = make("educ_case14_redisp", test=True)
            gym_env = GymEnv(env)
            act_space = gym_env.action_space
            act_space = act_space.reencode_space(
                "redispatch",
                ContinuousToDiscreteConverter(
                    nb_bins=5, init_space=act_space["redispatch"]))

            # with 5
            g2op_object = np.array([0., 0., 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 2, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(res2 == 0.)

            # test the all 0 action, but one is not 0 (negative)
            g2op_object = np.array([0., -1.9, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 2, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(res2 == 0.)

            # positive side
            g2op_object = np.array([0., 2.1, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 3, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., 3.33333, 0., 0., 0., 0.]) <= self.tol)

            g2op_object = np.array([0., 5.9, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 3, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., 3.33333, 0., 0., 0., 0.]) <= self.tol)

            g2op_object = np.array([0., 6.1, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 4, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., 6.666666, 0., 0., 0., 0.]) <= self.tol)

            # negative side
            g2op_object = np.array([0., -2.1, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 1, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., -3.3333, 0., 0., 0., 0.]) <= self.tol)

            g2op_object = np.array([0., -5.9, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 1, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., -3.33333, 0., 0., 0., 0.]) <= self.tol)

            g2op_object = np.array([0., -6.1, 0., 0., 0., 0.])
            res = act_space._keys_encoding["_redispatch"].g2op_to_gym(
                g2op_object)
            assert np.all(res == [2, 0, 0, 0, 0, 2])
            res2 = act_space._keys_encoding["_redispatch"].gym_to_g2op(res)
            assert np.all(
                np.abs(res2 - [0., -6.666666, 0., 0., 0., 0.]) <= self.tol)