-
Notifications
You must be signed in to change notification settings - Fork 18
/
Environment.swift
299 lines (268 loc) · 10.6 KB
/
Environment.swift
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2019, The Jelly Bean World Authors. All Rights Reserved.
//
// 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.
import Foundation
import ReinforcementLearning
import TensorFlow
public struct Environment: ReinforcementLearning.Environment {
public let batchSize: Int
public let configurations: [Configuration]
public let actionSpace: Discrete
public let observationSpace: ObservationSpace
public let parallelizedBatchProcessing: Bool
@usableFromInline internal var states: [State]
@usableFromInline internal var step: Step<Observation, Tensor<Float>>
@inlinable public var currentStep: Step<Observation, Tensor<Float>> { step }
/// Dispatch queue used to synchronize updates on mutable shared objects when using parallelized
/// batch processing.
@usableFromInline internal let dispatchQueue: DispatchQueue =
DispatchQueue(label: "Jelly Bean World Environment")
@inlinable
public init(configurations: [Configuration], parallelizedBatchProcessing: Bool = true) throws {
let batchSize = configurations.count
self.batchSize = batchSize
self.configurations = configurations
self.actionSpace = Discrete(withSize: 3, batchSize: batchSize)
self.observationSpace = ObservationSpace(batchSize: batchSize)
self.parallelizedBatchProcessing = parallelizedBatchProcessing
self.states = try configurations.map { configuration -> State in
let simulator = try Simulator(
using: configuration.simulatorConfiguration,
serverConfiguration: configuration.serverConfiguration)
let agent = Agent()
try simulator.add(agent: agent)
return State(simulator: simulator, agent: agent)
}
let observation = Observation.stack(zip(configurations, states).map { (configuration, state) in
let agentState = state.simulator.agentStates.values.first!
let rewardFunction = configuration.rewardSchedule.reward(forStep: state.simulator.time)
return Observation(
vision: Tensor<Float>(agentState.vision),
scent: Tensor<Float>(agentState.scent),
moved: Tensor<Float>(zeros: []),
inventory: agentState.items,
rewardFunction: rewardFunction)
})
self.step = Step(
kind: StepKind.first(batchSize: batchSize),
observation: observation,
reward: Tensor<Float>(zeros: [batchSize]))
}
/// Performs a step in this environment using the provided action and returns information about
/// the performed step.
@inlinable
@discardableResult
public mutating func step(
taking action: Tensor<Int32>
) throws -> Step<Observation, Tensor<Float>> {
let actions = action.unstacked()
// Check if we need to use the parallelized version.
if parallelizedBatchProcessing {
var steps = [Step<Observation, Tensor<Float>>?](repeating: nil, count: batchSize)
DispatchQueue.concurrentPerform(iterations: batchSize) { batchIndex in
// TODO: What if self.step throws?
let step = try! self.step(taking: actions[batchIndex], batchIndex: batchIndex)
dispatchQueue.sync { steps[batchIndex] = step }
}
step = Step<Observation, Tensor<Float>>.stack(steps.map { $0! })
return step
}
step = Step<Observation, Tensor<Float>>.stack(try (0..<batchSize).map { batchIndex in
try self.step(taking: actions[batchIndex], batchIndex: batchIndex)
})
return step
}
/// Performs a step in this environment for the specified batch index, using the provided action,
/// and returns information about the performed step.
@inlinable
internal mutating func step(
taking action: Tensor<Int32>,
batchIndex: Int
) throws -> Step<Observation, Tensor<Float>> {
let previousAgentState = states[batchIndex].simulator.agentStates.values.first!
states[batchIndex].agent.nextAction = Int(action.scalarized())
try states[batchIndex].simulator.step()
let agentState = states[batchIndex].simulator.agentStates.values.first!
let rewardFunction = configurations[batchIndex].rewardSchedule.reward(
forStep: states[batchIndex].simulator.time)
let observation = Observation(
vision: Tensor<Float>(agentState.vision),
scent: Tensor<Float>(agentState.scent),
moved: Tensor<Float>(agentState.position != previousAgentState.position ? 1 : 0),
inventory: agentState.items,
rewardFunction: rewardFunction)
let reward = Tensor<Float>(rewardFunction(for: AgentTransition(
previousState: previousAgentState,
currentState: agentState)))
return Step(kind: StepKind.transition(), observation: observation, reward: reward)
}
/// Resets the environment.
@inlinable
@discardableResult
public mutating func reset() throws -> Step<Observation, Tensor<Float>> {
states = try configurations.map { configuration -> State in
let simulator = try Simulator(using: configuration.simulatorConfiguration)
let agent = Agent()
try simulator.add(agent: agent)
return State(simulator: simulator, agent: agent)
}
let observation = Observation.stack(zip(configurations, states).map { (configuration, state) in
let agentState = state.simulator.agentStates.values.first!
let rewardFunction = configuration.rewardSchedule.reward(forStep: state.simulator.time)
return Observation(
vision: Tensor<Float>(agentState.vision),
scent: Tensor<Float>(agentState.scent),
moved: Tensor<Float>(zeros: []),
inventory: agentState.items,
rewardFunction: rewardFunction)
})
step = Step(
kind: StepKind.first(batchSize: batchSize),
observation: observation,
reward: Tensor<Float>(zeros: [batchSize]))
return step
}
/// Returns a copy of this environment that is reset before being returned.
@inlinable
public func copy() throws -> Environment {
try Environment(configurations: configurations)
}
}
extension Environment {
public struct Configuration {
public let simulatorConfiguration: Simulator.Configuration
public let rewardSchedule: RewardSchedule
public let serverConfiguration: Simulator.ServerConfiguration?
@inlinable
public init(
simulatorConfiguration: Simulator.Configuration,
rewardSchedule: RewardSchedule,
serverConfiguration: Simulator.ServerConfiguration? = nil
) {
self.simulatorConfiguration = simulatorConfiguration
self.rewardSchedule = rewardSchedule
self.serverConfiguration = serverConfiguration
}
}
}
extension Environment {
public struct Observation: Differentiable, KeyPathIterable {
public var vision: Tensor<Float>
public var scent: Tensor<Float>
@noDerivative public var moved: Tensor<Float>
@noDerivative public var inventory: [Item: Int]
@noDerivative public var rewardFunction: JellyBeanWorld.Reward
@inlinable
public init(
vision: Tensor<Float>,
scent: Tensor<Float>,
moved: Tensor<Float>,
inventory: [Item: Int],
rewardFunction: JellyBeanWorld.Reward
) {
self.vision = vision
self.scent = scent
self.moved = moved
self.inventory = inventory
self.rewardFunction = rewardFunction
}
}
}
extension Environment {
@usableFromInline internal struct State {
@usableFromInline internal let simulator: Simulator
@usableFromInline internal var agent: Agent
@inlinable
internal init(simulator: Simulator, agent: Agent) {
self.simulator = simulator
self.agent = agent
}
}
}
extension Environment {
public class Agent: JellyBeanWorld.Agent {
@usableFromInline internal var nextAction: Int? = nil
@inlinable
internal init() {}
@inlinable
public func act(using state: AgentState) -> Action {
switch nextAction {
case 0: return .move(direction: .up, stepCount: 1)
case 1: return .turn(direction: .left)
case 2: return .turn(direction: .right)
case _: return .none
}
}
}
}
extension Environment {
public struct ObservationSpace: Space {
public let distribution: ValueDistribution
@inlinable
public init(batchSize: Int) {
self.distribution = ValueDistribution()
}
@inlinable
public var description: String {
"JellyBeanWorldObservationSpace"
}
@inlinable
public func contains(_ value: Observation) -> Bool {
true // TODO: Check for the range of values.
}
// TODO: How do we sample the inventory and the reward function?
public struct ValueDistribution: DifferentiableDistribution, KeyPathIterable {
@usableFromInline internal var visionDistribution: Uniform<Float> = Uniform<Float>(
lowerBound: Tensor<Float>(0),
upperBound: Tensor<Float>(1))
// TODO: Should we limit the range of the following values?
@usableFromInline internal var scentDistribution: Uniform<Float> = Uniform<Float>(
lowerBound: Tensor<Float>(-Float.greatestFiniteMagnitude),
upperBound: Tensor<Float>(Float.greatestFiniteMagnitude))
@usableFromInline internal var movedDistribution: Bernoulli<Int32> = Bernoulli<Int32>(
probabilities: Tensor<Float>(0.5))
@inlinable
public init() {}
@inlinable
@differentiable(wrt: self)
public func logProbability(of value: Observation) -> Tensor<Float> {
visionDistribution.logProbability(of: value.vision) +
scentDistribution.logProbability(of: value.scent) +
movedDistribution.logProbability(of: Tensor<Int32>(value.moved))
}
@inlinable
@differentiable(wrt: self)
public func entropy() -> Tensor<Float> {
visionDistribution.entropy() + scentDistribution.entropy() + movedDistribution.entropy()
}
@inlinable
public func mode() -> Observation {
Observation(
vision: visionDistribution.mode(),
scent: scentDistribution.mode(),
moved: Tensor<Float>(movedDistribution.mode() .> 0),
inventory: [:],
rewardFunction: ZeroReward())
}
@inlinable
public func sample() -> Observation {
Observation(
vision: visionDistribution.sample(),
scent: scentDistribution.sample(),
moved: Tensor<Float>(movedDistribution.sample() .> 0),
inventory: [:],
rewardFunction: ZeroReward())
}
}
}
}