-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Minkowski penalties #648
Conversation
@jiriminarcik do you mind pasting in paths to examples from this branch? Would be very helpful if you can point us to where you were having performance problems with the autodiff codegen. |
Deploying with Cloudflare Pages
|
Codecov Report
@@ Coverage Diff @@
## main #648 +/- ##
==========================================
- Coverage 65.50% 65.34% -0.17%
==========================================
Files 46 46
Lines 7900 7992 +92
Branches 1502 1518 +16
==========================================
+ Hits 5175 5222 +47
- Misses 2716 2761 +45
Partials 9 9
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks great to me! I haven't looked at the theory. It would be good if someone (maybe @keenancrane?) can have a look at the math here. Just two comments on AD/perf below.
add( | ||
varOf(10e-15), | ||
add( | ||
squared(max(sub(xp, xr), varOf(0.0))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% certain on this: should constOf
be used here? Looking at the impl of them, seems like the only difference is constOf
names the node differently (
return variableAD(x, String(x), "const"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see, there is no difference in the functionality.. And it seems to be used interchangeably across the codebase. But I'll change it to constOf
everywhere I have an explicit number inside.
// Compute coordinates of the new rectangle | ||
const xs = [sub(xa1, xb1), sub(xa2, xb2), sub(xa1, xb2), sub(xa2, xb1)]; | ||
const ys = [sub(ya1, yb1), sub(ya2, yb2), sub(ya1, yb2), sub(ya2, yb1)]; | ||
const xc1 = xs.reduce((x, y) => min(x, y, true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the reduce
...min
operation is quite common. Perhaps it makes sense to write potentially more performant minimum
and maximum
functions that directly operate on VarAD[]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added minN
and maxN
. This also led to some readability improvements.
Looks great to me! The PDF has a reference to [4], which seems to be missing. (But probably fine to go ahead with the PR…) |
fixed |
Description
This PR reimplements the
disjoint
function inpackages/core/src/contrib/Constraints.ts
for rectangles and convex polygons. The disjoint constraint for shapesA
andB
is implemented as a distance from the origin to the Minkowski sum ofA
and-B
. The mathematical approach is described in more detail here.Example of optimization for axis-aligned rectangles:
Example of optimization for polygons (custom convex partitioning was used for the static shape):
This implementation also works for 1-simplexes (line segments):
Implementation strategy and design decisions
The current implementation supports convex polygons only. The support for non-convex polygons can be added by implementing a convex partitioning algorithm in the function
convexPartitions
inpackages/core/src/contrib/Constraints.ts
.This approach can lead to large computational graphs, which cause slow code generation step of the autodiff engine (see #652 for more details). The full computational graphs and their properties for two basic interactions are listed below.
Two axis-aligned rectangles (187 nodes, 269 edges and tree depth is 21):
Two triangles (866 nodes, 1312 edges and tree depth is 35):
Checklist
yarn test
yarn docs
and there were no errors when generating the HTML site