-
Notifications
You must be signed in to change notification settings - Fork 80
/
template.py
105 lines (88 loc) · 3.41 KB
/
template.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
96
97
98
99
100
101
102
103
104
105
# coding=utf-8
# Copyright 2022 The ML Fairness Gym Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python2, python3
"""Template environment definition."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
from typing import Optional
import attr
import core
from gym import spaces
import numpy as np
@attr.s
class Params(core.Params):
"""Params object for my environment."""
# Use this to define any parameters you want to pass in at initialization.
# TODO(): Fill this section in with relevant parameters.
foo = attr.ib(default=0.0)
# `cmp` must be set to False here to use core.State's equality methods.
@attr.s(cmp=False)
class State(core.State):
"""State object for my environments."""
# Use this to define any state variables of the environment.
rng = attr.ib() # type: np.random.RandomState
params = attr.ib() # type: Params
# TODO(): Fill this section in with relevant state variables.
bar = attr.ib(default=0.5)
bat = attr.ib(default=1.0)
class ExampleEnv(core.FairnessEnv):
"""Example Environment."""
metadata = {'render.modes': ['human']}
def __init__(self, params = None):
if params is None:
params = Params()
# TODO(): Fill this section in with action_space.
# Use a gym.Space to describe the action space. In the example below,
# the action space has two discrete actions.
self.action_space = spaces.Discrete(2) # Two possible discrete actions.
# TODO(): Fill this in with a dict mapping from state variable names to
# gym.Spaces.
# In the example below, 'bar' is an observable float between [-10., 10].
# `bat` is a state variable but it is not observable, so it is not in this
# dictionary.
self.observable_state_vars = {
'bar': spaces.Box(np.array(-10.), np.array(10.), dtype=float)
}
# This call sets up env.initial_params and history.
super(ExampleEnv, self).__init__(params)
self._state_init()
def _state_init(self, rng=None):
"""Initialize the environment's state."""
self.state = State(
rng=rng or np.random.RandomState(),
params=copy.deepcopy(self.initial_params),
# TODO(): Initialize any other state variables here.
)
def _step_impl(self, state, action):
"""Run one timestep of the environment's dynamics.
Args:
state: A `State` object containing the current state.
action: An action in `action_space`.
Returns:
A `State` object containing the updated state.
"""
# TODO(): Change the state in response to the input action here.
# In the example below, `bar` is increased by `foo`.
# `bat` is decreased by `foo` if action is 0.
state.bar += state.params.foo
if not action:
state.bat -= state.params.foo
return state
def reset(self):
"""Resets the environment."""
self._state_init(self.state.rng)
return super(ExampleEnv, self).reset()