2
2
#
3
3
# This source code is licensed under the MIT license found in the
4
4
# LICENSE file in the root directory of this source tree.
5
+ import warnings
5
6
from typing import List , Optional , Tuple , Union
6
7
7
8
import numpy as np
@@ -38,7 +39,10 @@ class Reward(Scalar):
38
39
39
40
def __init__ (
40
41
self ,
41
- name : str ,
42
+ # NOTE(github.com/facebookresearch/CompilerGym/issues/381): Once `id`
43
+ # argument has been removed, the default value for `name` can be
44
+ # removed.
45
+ name : str = None ,
42
46
observation_spaces : Optional [List [str ]] = None ,
43
47
default_value : RewardType = 0 ,
44
48
min : Optional [RewardType ] = None ,
@@ -47,6 +51,10 @@ def __init__(
47
51
success_threshold : Optional [RewardType ] = None ,
48
52
deterministic : bool = False ,
49
53
platform_dependent : bool = True ,
54
+ # NOTE(github.com/facebookresearch/CompilerGym/issues/381): Backwards
55
+ # compatability workaround for deprecated parameter, will be removed in
56
+ # v0.2.4.
57
+ id : Optional [str ] = None ,
50
58
):
51
59
"""Constructor.
52
60
@@ -56,33 +64,53 @@ def __init__(
56
64
(:class:`space.id <compiler_gym.views.ObservationSpaceSpec>` values)
57
65
that are used to compute the reward. May be an empty list if no
58
66
observations are requested. Requested observations will be provided
59
- to the :code:`observations` argument of
60
- :meth:`reward.update() <compiler_gym.spaces.Reward.update>`.
67
+ to the :code:`observations` argument of :meth:`reward.update()
68
+ <compiler_gym.spaces.Reward.update>`.
61
69
:param default_value: A default reward. This value will be returned by
62
- :meth:`env.step() <compiler_gym.envs.CompilerEnv.step>` if
63
- the service terminates.
70
+ :meth:`env.step() <compiler_gym.envs.CompilerEnv.step>` if the
71
+ service terminates.
64
72
:param min: The lower bound of the reward.
65
73
:param max: The upper bound of the reward.
66
74
:param default_negates_returns: If true, the default value will be
67
75
offset by the sum of all rewards for the current episode. For
68
76
example, given a default reward value of *-10.0* and an episode with
69
- prior rewards *[0.1, 0.3, -0.15]*, the default value is:
70
- *-10.0 - sum(0.1, 0.3, -0.15)*.
77
+ prior rewards *[0.1, 0.3, -0.15]*, the default value is: *-10.0 -
78
+ sum(0.1, 0.3, -0.15)*.
71
79
:param success_threshold: The cumulative reward threshold before an
72
80
episode is considered successful. For example, episodes where reward
73
81
is scaled to an existing heuristic can be considered “successful”
74
82
when the reward exceeds the existing heuristic.
75
83
:param deterministic: Whether the reward space is deterministic.
76
84
:param platform_dependent: Whether the reward values depend on the
77
85
execution environment of the service.
86
+ :param id: The name of the reward space.
87
+
88
+ .. deprecated:: 0.2.3
89
+ Use :code:`name` instead.
78
90
"""
79
91
super ().__init__ (
80
92
name = name ,
81
93
min = - np .inf if min is None else min ,
82
94
max = np .inf if max is None else max ,
83
95
dtype = np .float64 ,
84
96
)
85
- self .name = name
97
+
98
+ # NOTE(github.com/facebookresearch/CompilerGym/issues/381): Backwards
99
+ # compatability workaround for deprecated parameter, will be removed in
100
+ # v0.2.4.
101
+ if id is not None :
102
+ warnings .warn (
103
+ "The `id` argument of "
104
+ "compiler_gym.spaces.Reward.__init__() "
105
+ "has been renamed `name`. This will break in a future release, "
106
+ "please update your code." ,
107
+ DeprecationWarning ,
108
+ )
109
+ self .name = name or id
110
+ self .id = self .name
111
+ if not self .name :
112
+ raise TypeError ("No name given" )
113
+
86
114
self .observation_spaces = observation_spaces or []
87
115
self .default_value : RewardType = default_value
88
116
self .default_negates_returns : bool = default_negates_returns
0 commit comments