Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Commit d4e71ad

Browse files
author
marcrasi
committed
polish up the demos
1 parent cd529b3 commit d4e71ad

File tree

2 files changed

+5676
-5498
lines changed

2 files changed

+5676
-5498
lines changed

notebooks/talk_demos/Differentiable_Physics.ipynb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
" var balls: [Ball]\n",
212212
"\n",
213213
" /// Minimum ball distance (over all previous states) to each target.\n",
214-
" var minDistanceToTarget: [Float]\n",
214+
" var targetDistances: [Float]\n",
215215
"\n",
216216
" /// Amount of time simulation has been running.\n",
217217
" var t: Float\n",
@@ -225,13 +225,13 @@
225225
" @differentiable\n",
226226
" init(\n",
227227
" balls: [Ball],\n",
228-
" minDistanceToTarget: [Float],\n",
228+
" targetDistances: [Float],\n",
229229
" t: Float,\n",
230230
" targets: [Vec2],\n",
231231
" walls: [Wall]\n",
232232
" ) {\n",
233233
" self.balls = balls\n",
234-
" self.minDistanceToTarget = minDistanceToTarget\n",
234+
" self.targetDistances = targetDistances\n",
235235
" self.t = t\n",
236236
" self.targets = targets\n",
237237
" self.walls = walls\n",
@@ -257,7 +257,7 @@
257257
" init(balls: [Ball], targets: [Vec2], walls: [Wall]) {\n",
258258
" self.init(\n",
259259
" balls: balls,\n",
260-
" minDistanceToTarget: Array(repeating: Float.infinity, count: withoutDerivative(at: targets.count)),\n",
260+
" targetDistances: Array(repeating: Float.infinity, count: withoutDerivative(at: targets.count)),\n",
261261
" t: 0,\n",
262262
" targets: targets,\n",
263263
" walls: walls\n",
@@ -325,16 +325,16 @@
325325
" let distTo2 = (updatedBalls[1].position - targets[i]).magnitude\n",
326326
" var curTargetDistance = distTo1 < distTo2 ? distTo1 : distTo2\n",
327327
" if curTargetDistance < 2 * Ball.ballRadius { curTargetDistance = 2 * Ball.ballRadius }\n",
328-
" if curTargetDistance < minDistanceToTarget[i] {\n",
328+
" if curTargetDistance < targetDistances[i] {\n",
329329
" newMinTargetDistance = newMinTargetDistance + [curTargetDistance]\n",
330330
" } else {\n",
331-
" newMinTargetDistance = newMinTargetDistance + [minDistanceToTarget[i]]\n",
331+
" newMinTargetDistance = newMinTargetDistance + [targetDistances[i]]\n",
332332
" }\n",
333333
" }\n",
334334
"\n",
335335
" return World(\n",
336336
" balls: updatedBalls,\n",
337-
" minDistanceToTarget: newMinTargetDistance.withDerivative { [count = withoutDerivative(at: newMinTargetDistance.count)] (d: inout Array<Float>.DifferentiableView) -> () in\n",
337+
" targetDistances: newMinTargetDistance.withDerivative { [count = withoutDerivative(at: newMinTargetDistance.count)] (d: inout Array<Float>.DifferentiableView) -> () in\n",
338338
" if d.base.count == 0 {\n",
339339
" d = Array.DifferentiableView(Array(repeating: 0, count: count))\n",
340340
" }\n",
@@ -501,7 +501,7 @@
501501
" r += animate(attributeName: \"cy\", from: String(position.y), to: String(nextPosition.y))\n",
502502
" }\n",
503503
"\n",
504-
" for (targetIndex, (targetDistance, nextTargetDistance)) in zip(state.minDistanceToTarget, nextState.minDistanceToTarget).enumerated() {\n",
504+
" for (targetIndex, (targetDistance, nextTargetDistance)) in zip(state.targetDistances, nextState.targetDistances).enumerated() {\n",
505505
" if targetDistance > 2 && nextTargetDistance <= 2 {\n",
506506
" r += \"\"\"\n",
507507
" <animate\n",
@@ -595,7 +595,7 @@
595595
"metadata": {
596596
"id": "Ht1A90N00Nzv",
597597
"colab_type": "code",
598-
"outputId": "afe4cc52-c198-4ced-9338-cfa7f70c523f",
598+
"outputId": "9a4bfb71-72d7-42a2-c792-c247df422930",
599599
"colab": {
600600
"base_uri": "https://localhost:8080/",
601601
"height": 245
@@ -605,7 +605,7 @@
605605
"var v0 = Vector2(20, 0.01)\n",
606606
"drawSimulation(ball1InitialVelocity: v0)"
607607
],
608-
"execution_count": 73,
608+
"execution_count": 13,
609609
"outputs": [
610610
{
611611
"output_type": "display_data",
@@ -10562,7 +10562,7 @@
1056210562
" let finalState = simulate(initialState)\n",
1056310563
"\n",
1056410564
" // Sum the closest approaches to the targets.\n",
10565-
" return finalState.minDistanceToTarget.sum()\n",
10565+
" return finalState.targetDistances.sum()\n",
1056610566
"}\n",
1056710567
"\n",
1056810568
"loss(v0)"
@@ -10630,6 +10630,19 @@
1063010630
],
1063110631
"execution_count": 0,
1063210632
"outputs": []
10633+
},
10634+
{
10635+
"cell_type": "code",
10636+
"metadata": {
10637+
"id": "NjYQ033qCOKK",
10638+
"colab_type": "code",
10639+
"colab": {}
10640+
},
10641+
"source": [
10642+
""
10643+
],
10644+
"execution_count": 0,
10645+
"outputs": []
1063310646
}
1063410647
]
1063510648
}

0 commit comments

Comments
 (0)