Skip to content
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

Add support for using Problem as an atom #646

Merged
merged 6 commits into from
May 9, 2024
Merged

Add support for using Problem as an atom #646

merged 6 commits into from
May 9, 2024

Conversation

odow
Copy link
Member

@odow odow commented May 9, 2024

Take II at #310

See also #636

src/problems.jl Show resolved Hide resolved
Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.89%. Comparing base (19ab3a9) to head (2df5f43).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #646   +/-   ##
=======================================
  Coverage   97.88%   97.89%           
=======================================
  Files          88       88           
  Lines        5112     5120    +8     
=======================================
+ Hits         5004     5012    +8     
  Misses        108      108           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/problems.jl Outdated Show resolved Hide resolved
@odow odow requested a review from ericphanson May 9, 2024 03:05
Copy link
Collaborator

@ericphanson ericphanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code changes look good.

I think we should also test non-scalar "objectives", since those should be allowed in this setting (but multiobjective in general is not supported). Similar to the last example of https://cvxr.com/cvx/doc/advanced.html#new-functions-via-partially-specified-problems. Taking our huber atom and writing it as a partially specified problem:

function huber(c, M=1.0)
    s = Variable(size(c))
    n = Variable(size(c))
    return minimize(square(s) + 2 * M * abs(n), c == s + n)
end

src/problems.jl Show resolved Hide resolved
@odow
Copy link
Member Author

odow commented May 9, 2024

Okay, so the benefit of writing out huber like that is simplicity: it becomes a reformulation instead of an atom.

The downside is that multiple calls to huber(x) won't be cached and re-used in the same way they would it if was an atom? And there will also be more nodes in the expression graph of the problem?

That's an interesting trade-off.

@ericphanson
Copy link
Collaborator

so I think actually they will be cached, since the Problem is an AbstractExpr, and we cache the conic_form for all AbstractExpr's. (We cache if you have the same object in multiple places, like x= huber(...) and you use x in multiple places, but not if two instances are just isequal but distinct objects, since I think that wouldn't be fully correct in some cases, and also we would need to hash which can be slow).

So I think the main limitation is that there isn't a way (currently) to promise additional DCP properties that can't be derived (like maybe it is positive or increasing or something like that but it couldn't be derived automatically), which we can "inject" with atoms by defining sign etc. There also isn't the opportunity to choose how to lower to the MOI level (with a custom new_conic_form!) which lets us do fast scalar stuff and add constraints that don't have Convex.jl-level syntax etc, though hopefully that isn't needed frequently.

@odow odow force-pushed the od/problem-as-atom branch from 5ad1ec5 to f30dc64 Compare May 9, 2024 21:48
@odow
Copy link
Member Author

odow commented May 9, 2024

Added a test for caching and the array return.

test/test_atoms.jl Outdated Show resolved Hide resolved
@odow odow merged commit 1b3a4bb into master May 9, 2024
10 checks passed
@odow odow deleted the od/problem-as-atom branch May 9, 2024 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants