Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3422d9a
Add sentence that was in the problem set and applies here too
bzinberg Feb 6, 2020
26ea704
Looks like the prose was referring to `generate`, which is no longer …
bzinberg Feb 6, 2020
fc6c260
copy over from problem set
bzinberg Feb 6, 2020
1d564ad
more axis labels (copied from the problem set)
bzinberg Feb 6, 2020
181ada8
isempty check (copied from problem set)
bzinberg Feb 6, 2020
2f5c266
more axis labels (copied from pset)
bzinberg Feb 8, 2020
c35878d
more stuff copied over from pset
bzinberg Feb 8, 2020
082b69d
labels
bzinberg Feb 8, 2020
229a934
eliminate most of the unnecessary diffs between the tutorial and the …
bzinberg Feb 8, 2020
fff62ac
wrap long lines (we were already doing this half the time, let's be c…
bzinberg Feb 8, 2020
7be3265
copy explanation of tracing from the pset
bzinberg Feb 8, 2020
0a1af92
`return n` -> `return nothing`, otherwise it looks like the return va…
bzinberg Feb 8, 2020
06f458a
line wrap and whitespace
bzinberg Feb 8, 2020
2931e96
delete unneeded return values and `n`s
bzinberg Feb 9, 2020
17e1e76
redundant
bzinberg Feb 9, 2020
2eda278
wrap lines
bzinberg Feb 9, 2020
1daa896
wordo
bzinberg Feb 9, 2020
2ed1f74
line wrap
bzinberg Feb 9, 2020
5105e7d
line wrap
bzinberg Feb 9, 2020
a76dcb2
line wrap and pset reconciliation for the Data-Driven tutorial
bzinberg Feb 9, 2020
a0aa948
line wrap
bzinberg Feb 9, 2020
39d71ed
line wraps and renames, in coordination with pset
bzinberg Feb 11, 2020
9a2a06d
the easy parts of reconcilitaion for the particle filtering notebook
bzinberg Feb 11, 2020
8393153
include versions of the rejuvenation move with both resimulation MH a…
bzinberg Feb 11, 2020
2e5159c
line wrap
bzinberg Feb 11, 2020
c64e1fa
update Gen function name
bzinberg Feb 11, 2020
1de37a8
minimize diff from problem set
bzinberg Feb 11, 2020
3b9f520
fake -> synthetic
bzinberg Feb 11, 2020
03c080f
update names to match the pset
bzinberg Feb 14, 2020
f6d7b15
line wrap
bzinberg Feb 14, 2020
0d92f67
`line_model_2` -> `line_model_fancy`
bzinberg Feb 14, 2020
b9b20dd
Update name
bzinberg Feb 15, 2020
1a0a1ec
update Gen dependency
bzinberg Feb 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ version = "0.5.1"

[[CMake]]
deps = ["BinDeps"]
git-tree-sha1 = "c67a8689dc5444adc5eb2be7d837100340ecba11"
git-tree-sha1 = "50a8b41d2c562fccd9ab841085fc7d1e2706da82"
uuid = "631607c0-34d2-5d66-819e-eb0f9aa2061a"
version = "1.1.2"
version = "1.2.0"

[[CMakeWrapper]]
deps = ["BinDeps", "CMake", "Libdl", "Parameters", "Test"]
Expand Down Expand Up @@ -65,9 +65,9 @@ version = "2.2.0"

[[Conda]]
deps = ["JSON", "VersionParsing"]
git-tree-sha1 = "9a11d428dcdc425072af4aea19ab1e8c3e01c032"
git-tree-sha1 = "7a58bb32ce5d85f8bf7559aa7c2842f9aecf52fc"
uuid = "8f4d0f93-b110-5947-807f-2305c1781a2d"
version = "1.3.0"
version = "1.4.1"

[[DataAPI]]
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
Expand Down Expand Up @@ -102,9 +102,9 @@ version = "1.0.2"

[[DiffRules]]
deps = ["NaNMath", "Random", "SpecialFunctions"]
git-tree-sha1 = "10dca52cf6d4a62d82528262921daf63b99704a2"
git-tree-sha1 = "eb0c34204c8410888844ada5359ac8b96292cfd1"
uuid = "b552c78f-8df3-52c6-915a-8e097449b14b"
version = "1.0.0"
version = "1.0.1"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
Expand All @@ -118,18 +118,18 @@ version = "0.22.4"

[[FileIO]]
deps = ["Pkg"]
git-tree-sha1 = "74585bf1f7ed7259e166011e89f49363d7fa89a6"
git-tree-sha1 = "2c84c57aced468fa21763c66d3bef33adcd09ec7"
uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
version = "1.2.1"
version = "1.2.2"

[[FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays"]
git-tree-sha1 = "fec413d4fc547992eb62a5c544cedb6d7853c1f5"
git-tree-sha1 = "85c6b57e2680fa28d5c8adc798967377646fbf66"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.8.4"
version = "0.8.5"

[[FixedPointNumbers]]
git-tree-sha1 = "4aaea64dd0c30ad79037084f8ca2b94348e65eaa"
Expand Down Expand Up @@ -162,7 +162,7 @@ version = "0.5.1"

[[Gen]]
deps = ["DataStructures", "Distributions", "FunctionalCollections", "JSON", "LinearAlgebra", "MacroTools", "Random", "ReverseDiff", "SpecialFunctions"]
git-tree-sha1 = "8b1686eca997088b31541e4a2b92d479bf2de24d"
git-tree-sha1 = "677426251655bfbf71f69842c2dc02784fea3683"
repo-rev = "master"
repo-url = "https://github.com/probcomp/Gen"
uuid = "ea4f424c-a589-11e8-07c0-fd5c91b9da4a"
Expand Down Expand Up @@ -357,9 +357,9 @@ version = "1.0.1"

[[ReverseDiff]]
deps = ["DiffResults", "DiffRules", "ForwardDiff", "FunctionWrappers", "LinearAlgebra", "NaNMath", "Random", "SpecialFunctions", "StaticArrays", "Statistics"]
git-tree-sha1 = "6a5aae862f93c577f89a607334f09630439ec119"
git-tree-sha1 = "5f7dafd314ff2ada3076797b1edfb71e52151cb9"
uuid = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
version = "1.0.0"
version = "1.1.0"

[[Rmath]]
deps = ["BinaryProvider", "Libdl", "Random", "Statistics"]
Expand Down Expand Up @@ -420,9 +420,9 @@ version = "0.32.0"

[[StatsFuns]]
deps = ["Rmath", "SpecialFunctions"]
git-tree-sha1 = "79982835d2ff3970685cb704500909c94189bde9"
git-tree-sha1 = "f290ddd5fdedeadd10e961eb3f4d3340f09d030a"
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
version = "0.9.3"
version = "0.9.4"

[[SuiteSparse]]
deps = ["Libdl", "LinearAlgebra", "SparseArrays"]
Expand Down
301 changes: 244 additions & 57 deletions tutorials/Data-Driven Proposals in Gen.jl

Large diffs are not rendered by default.

670 changes: 513 additions & 157 deletions tutorials/Introduction to Modeling in Gen.jl

Large diffs are not rendered by default.

355 changes: 266 additions & 89 deletions tutorials/Iterative inference in Gen.jl

Large diffs are not rendered by default.

110 changes: 85 additions & 25 deletions tutorials/Modeling with Black-Box Julia Code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@

# # Modeling with black-box Julia code

# This notebook shows how "black-box" code like algorithms and simulators can be included in probabilistic models that are expressed as generative functions.
# This notebook shows how "black-box" code like algorithms and simulators can
# be included in probabilistic models that are expressed as generative
# functions.

# We will write a generative probabilistic model of the motion of an intelligent agent that is navigating a two-dimensional scene. The model will be *algorithmic* --- it will invoke a path planning algorithm implemented in regular Julia code to generate the agent's motion plan from its destination its map of the scene.
# We will write a generative probabilistic model of the motion of an
# intelligent agent that is navigating a two-dimensional scene. The model will
# be *algorithmic* --- it will invoke a path planning algorithm implemented in
# regular Julia code to generate the agent's motion plan from its destination
# its map of the scene.

using Gen

Expand All @@ -29,7 +35,11 @@ using GenViz
viz_server = VizServer(8090)
sleep(1)

# First, we load some basic geometric primitives for a two-dimensional scene in the following file:
# ##### Warning -- do not execute the above cell twice.
#
#
# First, we load some basic geometric primitives for a two-dimensional scene in
# the following file:

include("../inverse-planning/geometric_primitives.jl");

Expand All @@ -39,15 +49,19 @@ point = Point(1.0, 2.0)
println(point.x)
println(point.y)

# The file also defines an `Obstacle` data type, which represents a polygonal obstacle in a two-dimensional scene, that is constructed from a list of vertices. Here, we construct a square:
# The file also defines an `Obstacle` data type, which represents a polygonal
# obstacle in a two-dimensional scene, that is constructed from a list of
# vertices. Here, we construct a square:

obstacle = Obstacle([Point(0.0, 0.0), Point(1.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0)]);

# Next, we load the definition of a `Scene` data type that represents a two-dimensional scene.
# Next, we load the definition of a `Scene` data type that represents a
# two-dimensional scene.

include("../inverse-planning/scene.jl");

# The scene spans a rectangle of on the two-dimensional x-y plane, and contains a list of obstacles, which is initially empty:
# The scene spans a rectangle of on the two-dimensional x-y plane, and contains
# a list of obstacles, which is initially empty:
#
# `scene = Scene(xmin::Real, xmax::Real, ymin::Real, ymax::real)`

Expand All @@ -59,11 +73,15 @@ add_obstacle!(scene, obstacle);

# The file also defines functions that simplify the construction of obstacles:
#
# `make_square(center::Point, size::Float64)` constructs a square-shaped obstacle centered at the given point with the given side length:
# `make_square(center::Point, size::Float64)` constructs a square-shaped
# obstacle centered at the given point with the given side length:

obstacle = make_square(Point(0.30, 0.20), 0.1)

# `make_line(vertical::Bool, start::Point, length::Float64, thickness::Float64)` constructs an axis-aligned line (either vertical or horizontal) with given thickness that extends from a given strating point for a certain length:
# `make_line(vertical::Bool, start::Point, length::Float64,
# thickness::Float64)` constructs an axis-aligned line (either vertical or
# horizontal) with given thickness that extends from a given strating point for
# a certain length:

obstacle = make_line(false, Point(0.20, 0.40), 0.40, 0.02)

Expand All @@ -82,37 +100,55 @@ add_obstacle!(scene, make_line(horizontal, Point(0.60 - 0.15, 0.80), 0.15 + wall
add_obstacle!(scene, make_line(horizontal, Point(0.20, 0.80), 0.15, wall_thickness))
add_obstacle!(scene, make_line(vertical, Point(0.20, 0.40), 0.40, wall_thickness));

# We visualize the scene below. Don't worry about the details of this cell. It should display a two-dimensional map of the scene.
# We visualize the scene below. Don't worry about the details of this cell. It
# should display a two-dimensional map of the scene.

info = Dict("scene" => scene)
viz = Viz(viz_server, joinpath(@__DIR__, "../inverse-planning/overlay-viz/dist"), info)
displayInNotebook(viz)

# Next, we load a file that defines a `Path` data type (a sequence of `Points`), and a `plan_path` method, which uses a path planning algorithm based on rapidly exploring random tree (RRT, [1]) to find a sequence of `Point`s beginning with `start` and ending in `dest` such that the line segment between each consecutive pair of points does nt intersect any obstacles in the scene. The planning algorithm may fail to find a valid path, in which case it will return a value of type `Nothing`.
# Next, we load a file that defines a `Path` data type (a sequence of
# `Points`), and a `plan_path` method, which uses a path planning algorithm
# based on rapidly exploring random tree (RRT, [1]) to find a sequence of
# `Point`s beginning with `start` and ending in `dest` such that the line
# segment between each consecutive pair of points does nt intersect any
# obstacles in the scene. The planning algorithm may fail to find a valid path,
# in which case it will return a value of type `Nothing`.
#
# `path::Union{Path,Nothing} = plan_path(start::Point, dest::Point, scene::Scene, planner_params::PlannerParams)`
# `path::Union{Path,Nothing} = plan_path(start::Point, dest::Point,
# scene::Scene, planner_params::PlannerParams)`
#
# [1] Rapidly-exploring random trees: A new tool for path planning. S. M. LaValle. TR 98-11, Computer Science Dept., Iowa State University, October 1998,
# [1] Rapidly-exploring random trees: A new tool for path planning. S. M.
# LaValle. TR 98-11, Computer Science Dept., Iowa State University, October
# 1998,

include("../inverse-planning/planning.jl");

# Let's use `plan_path` to plan a path from the lower-left corner of the scene into the interior of the box.
# Let's use `plan_path` to plan a path from the lower-left corner of the scene
# into the interior of the box.

start = Point(0.1, 0.1)
dest = Point(0.5, 0.5)
planner_params = PlannerParams(300, 3.0, 2000, 1.)
path = plan_path(start, dest, scene, planner_params)

# We visualize the path below. The start location is shown in blue, the destination in red, and the path in orange. Run the cell above followed by the cell below a few times to see the variability in the paths generated by `plan_path` for these inputs.
# We visualize the path below. The start location is shown in blue, the
# destination in red, and the path in orange. Run the cell above followed by
# the cell below a few times to see the variability in the paths generated by
# `plan_path` for these inputs.

info = Dict("start"=> start, "dest" => dest, "scene" => scene, "path_edges" => get_edges(path))
viz = Viz(viz_server, joinpath(@__DIR__, "../inverse-planning/overlay-viz/dist"), info)
displayInNotebook(viz)

# We also need a model for how the agent moves along its path.
# We will assume that the agent moves along its path a constant speed. The file loaded above also defines a method (`walk_path`) that computes the locations of the agent at a set of timepoints (sampled at time intervals of `dt` starting at time `0.`), given the path and the speed of the agent:
# We will assume that the agent moves along its path a constant speed. The file
# loaded above also defines a method (`walk_path`) that computes the locations
# of the agent at a set of timepoints (sampled at time intervals of `dt`
# starting at time `0.`), given the path and the speed of the agent:
#
# `locations::Vector{Point} = walk_path(path::Path, speed::Float64, dt::Float64, num_ticks::Int)`
# `locations::Vector{Point} = walk_path(path::Path, speed::Float64,
# dt::Float64, num_ticks::Int)`

speed = 1.
dt = 0.1
Expand Down Expand Up @@ -161,14 +197,20 @@ println(locations)
return (planning_failed, maybe_path)
end;

# We can now perform a traced execution of `agent_model` and print out the random choices it made:
# We can now perform a traced execution of `agent_model` and print out the
# random choices it made:

planner_params = PlannerParams(300, 3.0, 2000, 1.)
(trace, _) = Gen.generate(agent_model, (scene, dt, num_ticks, planner_params));
choices = Gen.get_choices(trace)
println(choices)

# Next we explore the assumptions of the model by sampling many traces from the generative function and visualizing them. We have created a visualization specialized for this generative function for use with the `GenViz` package, in the directory `../inverse-planning/grid-viz/dist`. We have also defined a `trace_to_dict` method to convert the trace into a value that can be automatically serialized into a JSON string for use by the visualization:
# Next we explore the assumptions of the model by sampling many traces from the
# generative function and visualizing them. We have created a visualization
# specialized for this generative function for use with the `GenViz` package,
# in the directory `../inverse-planning/grid-viz/dist`. We have also defined a
# `trace_to_dict` method to convert the trace into a value that can be
# automatically serialized into a JSON string for use by the visualization:

function trace_to_dict(trace)
args = Gen.get_args(trace)
Expand Down Expand Up @@ -202,7 +244,8 @@ function trace_to_dict(trace)
return d
end;

# Let's visualize some traces of the function, with the start location fixed to a point in the lower-left corner:
# Let's visualize some traces of the function, with the start location fixed to
# a point in the lower-left corner:

# +
constraints = Gen.choicemap()
Expand All @@ -217,23 +260,34 @@ end
displayInNotebook(viz)
# -

# In this visualization, the start location is represented by a blue dot, and the destination is represented by a red dot. The measured coordinates at each time point are represented by black dots. The path, if path planning was succesfull, is shown as a gray line fro the start point to the destination point. Notice that the speed of the agent is different in each case.
# In this visualization, the start location is represented by a blue dot, and
# the destination is represented by a red dot. The measured coordinates at each
# time point are represented by black dots. The path, if path planning was
# succesfull, is shown as a gray line fro the start point to the destination
# point. Notice that the speed of the agent is different in each case.

# ### Exercise
#
# Constrain the start and destination points to two particular locations in the scene, and visualize the distribution on paths. Find a start point and destination point such that the distributions on paths is multimodal (i.e. there are two spatially separated paths that the agent may take). Describe the variability in the planned paths.
# Constrain the start and destination points to two particular locations in the
# scene, and visualize the distribution on paths. Find a start point and
# destination point such that the distributions on paths is multimodal (i.e.
# there are two spatially separated paths that the agent may take). Describe
# the variability in the planned paths.

# ### Solution
#
#

# We now write a simple algorithm for inferring the destination of an agent given (i) the scene, (ii) the start location of the agent, and (iii) a sequence of measured locations of the agent for each tick.
# We now write a simple algorithm for inferring the destination of an agent
# given (i) the scene, (ii) the start location of the agent, and (iii) a
# sequence of measured locations of the agent for each tick.
#
# We will assume the agent starts in the lower left-hand corner.

start = Point(0.1, 0.1);

# We will infer the destination of the agent for the given sequence of observed locations:
# We will infer the destination of the agent for the given sequence of observed
# locations:

measurements = [
Point(0.0980245, 0.104775),
Expand All @@ -258,7 +312,7 @@ displayInNotebook(viz)
function do_inference_agent_model(scene::Scene, dt::Float64, num_ticks::Int, planner_params::PlannerParams, start::Point,
measurements::Vector{Point}, amount_of_computation::Int)

# Create an "Assignment" that maps model addresses (:y, i)
# Create a ChoiceMap that maps model addresses (:y, i)
# to observed values ys[i]. We leave :slope and :intercept
# unconstrained, because we want them to be inferred.
observations = Gen.choicemap()
Expand Down Expand Up @@ -289,7 +343,13 @@ end
displayInNotebook(viz)

# ### Exercise
# The first argument to `PlannerParams` is the number of iterations of the RRT algorithm to use. The third argument to `PlannerParams` is the number of iterations of path refinement. These parameters affect the distribution on paths of the agent. Visualize traces of the `agent_model` for with a couple different settings of these two parameters to the path planning algorithm for fixed starting point and destination point. Try setting them to smaller values. Discuss.
# The first argument to `PlannerParams` is the number of iterations of the RRT
# algorithm to use. The third argument to `PlannerParams` is the number of
# iterations of path refinement. These parameters affect the distribution on
# paths of the agent. Visualize traces of the `agent_model` with a couple of
# different settings of these two parameters to the path planning algorithm for
# fixed starting point and destination point. Try setting them to smaller
# values. Discuss.

# ### Solution
#
Expand Down
Loading