forked from google-deepmind/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_c_api_example.cc
More file actions
188 lines (154 loc) · 5.78 KB
/
Copy pathenv_c_api_example.cc
File metadata and controls
188 lines (154 loc) · 5.78 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016-2017 Google Inc.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <cstdlib>
#include <functional>
#include <memory>
#include <string>
#include "third_party/rl_api/env_c_api.h"
#include "third_party/rl_api/env_c_api_bind.h"
namespace {
const int kDiscreteActionCount = 2;
const char* const kDiscreteActionNames[] = {"PADX", "PADY"};
const int kContinuousActionCount = 1;
const char* const kContinuousActionNames[] = {"TRIGGER"};
const struct {
int min_value;
int max_value;
} kDiscreteActionBounds[] = {{-1, 1}, {-1, 1}};
const struct {
double min_value;
double max_value;
} kContinuousActionBounds[] = {{-1.0, 1.0}};
const int kObservationCount = 3;
enum ObservationTypes {
ObservationTypes_Bytes,
ObservationTypes_Doubles,
ObservationTypes_String
};
const char* const kObservationNames[] = {"BYTES", "DOUBLES", "STRING"};
const int kObservationBytesDim[] = {10, 2};
const int kObservationDoublesDim[] = {7};
const int kObservationStringDim[] = {0};
const EnvCApi_ObservationSpec kObservationSpecs[] = {
{EnvCApi_ObservationBytes, 2, kObservationBytesDim},
{EnvCApi_ObservationDoubles, 1, kObservationDoublesDim},
{EnvCApi_ObservationString, 1, kObservationStringDim}};
class MyGame {
public:
int Setting(const char* key, const char* value) {
// Doesn't accept any settings.
return 1;
}
int Init() { return 0; }
// Launch level using episode id and seed.
int Start(int episode_id, int seed) {
steps_ = 0;
episode_id_ = episode_id;
seed_ = seed;
for (auto& byte_ref : observation_bytes_) {
byte_ref = 127;
}
for (auto& double_ref : observation_doubles_) {
double_ref = 127;
}
string_ = std::to_string(episode_id) + ":" + std::to_string(steps_);
return 0;
}
const char* ErrorMessage() const { return "No error message."; }
const char* EnvironmentName() const { return "example_environment"; }
// The number of discrete actions.
int ActionDiscreteCount() const { return kDiscreteActionCount; }
// Discrete action name. 'discrete_idx' < action_discrete_count().
const char* ActionDiscreteName(int discrete_idx) const {
assert(discrete_idx < kDiscreteActionCount);
return kDiscreteActionNames[discrete_idx];
}
// The discrete action inclusive range.
void ActionDiscreteBounds(int discrete_idx, int* min_value,
int* max_value) const {
*min_value = kDiscreteActionBounds[discrete_idx].min_value;
*max_value = kDiscreteActionBounds[discrete_idx].max_value;
}
// The number of continuous actions.
int ActionContinuousCount() const { return kContinuousActionCount; }
// Continuous action name. 'continuous_idx' < action_continuous_count().
const char* ActionContinuousName(int continuous_idx) const {
assert(continuous_idx < kContinuousActionCount);
return kContinuousActionNames[continuous_idx];
}
// The continuous action inclusive range.
void ActionContinuousBounds(int continuous_idx, double* min_value_out,
double* max_value_out) const {
*min_value_out = kContinuousActionBounds[continuous_idx].min_value;
*max_value_out = kContinuousActionBounds[continuous_idx].max_value;
}
int ObservationCount() const { return kObservationCount; }
const char* ObservationName(int observation_idx) {
assert(observation_idx < kObservationCount);
return kObservationNames[observation_idx];
}
// The shape of the observation parameter.
void ObservationSpec(int observation_idx,
EnvCApi_ObservationSpec* spec) const {
assert(observation_idx < kObservationCount);
*spec = kObservationSpecs[observation_idx];
}
int EventTypeCount() const { return 0; }
const char* EventTypeName(int event_type_idx) const { std::abort(); }
int Fps() { return 60; }
void Observation(int observation_idx, EnvCApi_Observation* observation) {
ObservationSpec(observation_idx, &observation->spec);
switch (observation_idx) {
case ObservationTypes_Bytes:
observation->payload.bytes = observation_bytes_;
break;
case ObservationTypes_Doubles:
observation->payload.doubles = observation_doubles_;
break;
case ObservationTypes_String:
string_shape_[0] = string_.size();
observation->payload.string = string_.c_str();
observation->spec.shape = string_shape_;
break;
}
}
int EventCount() { return 0; }
void Event(int event_idx, EnvCApi_Event* event) {}
void Act(const int actions_discrete[], const double actions_continuous[]) {}
EnvCApi_EnvironmentStatus Advance(int num_steps, double* reward) {
steps_ += num_steps;
*reward = reward_;
reward_ = 0;
string_ = std::to_string(episode_id_) + ":" + std::to_string(steps_);
return EnvCApi_EnvironmentStatus_Running;
}
private:
int episode_id_;
int seed_;
int steps_;
double reward_;
unsigned char observation_bytes_[10 * 2];
double observation_doubles_[7];
int string_shape_[1];
std::string string_;
};
} // namespace
extern "C" int env_c_api_example_connect(EnvCApi* env_c_api, void** context) {
auto game = std::unique_ptr<MyGame>(new MyGame());
deepmind::rl_api::Bind(std::move(game), env_c_api, context);
return 0;
}