-
Notifications
You must be signed in to change notification settings - Fork 111
/
RewardHelper.py
95 lines (74 loc) · 3.54 KB
/
RewardHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
from grid2op.Reward.BaseReward import BaseReward
from grid2op.Reward.ConstantReward import ConstantReward
class RewardHelper:
"""
This class aims at making the creation of rewards class more automatic by the :class:`grid2op.Environment`.
It is not recommended to derived or modified this class. If a different reward need to be used, it is recommended
to build another object of this class, and change the :attr:`RewardHelper.rewardClass` attribute.
Attributes
----------
rewardClass: ``type``
Type of reward that will be use by this helper. Note that the type (and not an instance / object of that type)
must be given here. It defaults to :class:`ConstantReward`
template_reward: :class:`BaseReward`
An object of class :attr:`RewardHelper.rewardClass` used to compute the rewards.
"""
def __init__(self, rewardClass=ConstantReward):
self.rewardClass = rewardClass
self.template_reward = rewardClass()
def initialize(self, env):
"""
This function initializes the template_reward with the environment. It is used especially for using
:func:`RewardHelper.range`.
Parameters
----------
env: :class:`grid2op.Environment.Environment`
The current used environment.
"""
self.template_reward.initialize(env)
def range(self):
"""
Provides the range of the rewards.
Returns
-------
res: ``(float, float)``
The minimum reward per time step (possibly infinity) and the maximum reward per timestep (possibly infinity)
"""
return self.template_reward.get_range()
def __call__(self, action, env, has_error, is_done, is_illegal, is_ambiguous):
"""
Gives the reward that follows the execution of the :class:`grid2op.BaseAction.BaseAction` action in the
:class:`grid2op.Environment.Environment` env;
Parameters
----------
action: :class:`grid2op.Action.Action`
The action performed by the BaseAgent.
env: :class:`grid2op.Environment.Environment`
The current environment.
has_error: ``bool``
Does the action caused an error, such a diverging powerflow for example= (``True``: the action caused
an error)
is_done: ``bool``
Is the game over (``True`` = the game is over)
is_illegal: ``bool``
Is the action legal or not (``True`` = the action was illegal). See
:class:`grid2op.Exceptions.IllegalAction` for more information.
is_ambiguous: ``bool``
Is the action ambiguous or not (``True`` = the action was ambiguous). See
:class:`grid2op.Exceptions.AmbiguousAction` for more information.
Returns
-------
"""
if not has_error:
res = self.template_reward(action, env, has_error, is_done, is_illegal, is_ambiguous)
else:
# no more data to consider, no powerflow has been run, reward is what it is
res = self.template_reward.reward_min
return res