Improve ComponentMap / ComponentSet - #3970
Conversation
…nt is not callable
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3970 +/- ##
==========================================
+ Coverage 90.13% 90.15% +0.01%
==========================================
Files 917 917
Lines 109086 109201 +115
==========================================
+ Hits 98329 98448 +119
+ Misses 10757 10753 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| x1 = var_map[id(m.x1)] | ||
| x2 = var_map[id(m.x2)] | ||
| x3 = var_map[id(m.x3)] | ||
| y1 = var_map[id(m.y1)] | ||
| y2 = var_map[id(m.y2)] | ||
| y3 = var_map[id(m.y3)] | ||
| z1 = var_map[id(m.z1)] | ||
| x1 = var_map[m.x1] | ||
| x2 = var_map[m.x2] | ||
| x3 = var_map[m.x3] | ||
| y1 = var_map[m.y1] | ||
| y2 = var_map[m.y2] | ||
| y3 = var_map[m.y3] | ||
| z1 = var_map[m.z1] |
There was a problem hiding this comment.
I don't understand why these needed changed without changing the gurobi-minlp visitor.
There was a problem hiding this comment.
These tests were exploiting a bug in ComponentMap where a VarData could be looked up by either the object or it's integer id() (i.e., we were not handling a hash collision correctly). This PR resolves that bug, so these tests needed to be updated.
Fixes #755
Summary/Motivation:
This PR Updates
COmponentMap/ComponentSetto prevent key collisions betweenints and theid()of unhashable Components. The final solution proposed here is to store all keys coming fromid()as a 2-tuple (combined with an internal class "flag"). This ended up being significantly more efficient than building a specializedHashKeyclass that would store theid()and not compare equal toint.These changes make
ComponentMap/ComponentSetslightly slower (~10-12%). To mitigate this, the PR includes two new containers, 'ObjectIdMapandObjectIdSet. These containers are more efficient than theComponentMap/ComponentSet, and approach the efficiency of a direct (manual) "dict-of-ids" implementation (see below).As part of developing this PR, several other improvements were implemented:
ComponentMap:.keys(),.values(), and.items()to match the behavior ofdictKeyErrors raised here match those raised bydictComponentMapto another.update()API / behavior matchdictComponentSet:ComponentSetto another.update()API / behavior matchsetExpressionReplacementVisitorto officially acceptComponentMapfor the substitution mapPerformance data
Given the following test code:
The original
ComponentMap/ComponentSetimplementation (from main), tested with 100k shots on Python 3.10 - 3.14:Results for the same tests on this PR:
And if we use
ObjectIdMap()/ObjectIdSetin lieu of theComponentMap/ComponentSet:Changes proposed in this PR:
AI-Use Disclosure
or
AI tools contributed to the development of this PR
Review process (select ONE):
Notes for reviewers (optional):
Legal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: