Comments (10)
Once I'm done with the interface work, I will come back and deal with this. I don't think the base types in Flips are going to change, but I'd rather be sure of that before doing the work to update the hashing logic.
from flips.
Update: it only does that in debug builds, I'm not sure how/if you want to address it.
from flips.
this one does a stack overflow:
#r @"C:\dev\src\github.com\matthewcrews\flips\bin\netstandard2.0\Flips.dll" (Flips.Types.LinearExpression.OfFloat 1.0).GetHashCode()
I think a simpler reproduction is
LinearExpression.Empty.GetHashCode()
However, the devops keeps getting in my way. Sometimes I can't build main
either (just like I can't build dev
as I said in #138 (comment)).
I think a good fix would be to replace
Lines 261 to 262 in 73e4065
with
override this.GetHashCode () =
match this with
| Empty -> 0
| AddFloat (f, le) -> hash (f, le)
| AddDecision (f, d) -> hash (f, d)
| Multiply (f, le) -> hash (f, le)
| AddLinearExpression (le1, le2) -> hash (le1, le2)
However, the above devops issue as well as another devops issue have blocked me from confirming this.
it only does that in debug builds
That is interesting. I don't know how to explain this. I am currently unable to confirm this because of the devops issues.
I tried to reproduce this in a minimal working example. In debug
, the stack overflows and in release
, the behavior seemed to be an infinite loop (presumably because of tail call optimization).
IIRC, Option.None
is represented at runtime as null
. Maybe Empty
is represented in a certain way a runtime in release mode that prevents the infinite recursion.
from flips.
I tried to reproduce this in a minimal working example. In debug, the stack overflows and in release, the behavior seemed to be an infinite loop (presumably because of tail call optimization).
Yes, that is a challenge with the difference when compiling in Debug vs Release
The GetHashCode
will likely need to perform a reduction of the LinearExpression
before computing the hash so that you don't get a stack overflow error. This is why the ReducedLinearExpression
type exists. The LinearExpression
type was intended for just creating a the tree which models the mathematical expression. Writing a reducer of the tree which did not stack overflow took some work.
from flips.
Quoting #141 (comment)
[Approximate equality is] for testing.
Based on that, I think GetHashCode
is not being used within the library. It is not intended for public use either even though it is publically exposed.
I think the proper fix for this issue is to implement the approach to infinite precision testing that I suggested in #141 (comment). Then this issue would be fixed by not overriding GetHashCode
.
from flips.
@smoothdeveloper Were you using GetHashCode
for anything?
from flips.
That is interesting. I don't know how to explain this. I am currently unable to confirm this because of the devops issues.
I tried to reproduce this in a minimal working example. In debug, the stack overflows and in release, the behavior seemed to be an infinite loop (presumably because of tail call optimization).
@TysonMN I think this is related to how recursion is optimized or not by default by F# compiler, instead of compiling a while loop, it makes actual method calls.
Your proposed code looks better than my work around 🙂, I think this bug is important enough that we should have a fix in main
.
@smoothdeveloper Were you using GetHashCode for anything?
Storing in a hashset or as a key in a dictionary would hit this code IIRC.
from flips.
Your proposed code looks better than my work around 🙂, I think this bug is important enough that we should have a fix in
main
.
I just created PR #142 that contributes that fix. Hopefully it works.
Storing in a hashset or as a key in a dictionary would hit this code IIRC.
You are correct. So, are these being stored in a hashset or dictionary?
Based on the comments by @matthewcrews in #141 (especially #141 (comment) as I quoted in #132 (comment)), I expect that they are not being stored in a hashset or dictionary. Instead, my guess is that only Equals
is being used.
This is another disadvantage of using overloads of Equals
and GetHashCode
. If instead new members MyEquals
and MyGetHashCode
were created and combined in a custom instance of IEqualityComparer<>
, then it would be a trivial symbolic search in Visual Studio to find all uses of these two methods.
from flips.
You are correct. So, are these being stored in a hashset or dictionary?
Not in the implementation of the library AFAIK, but the client code I was writing (toying around with my current implementation for Cplex solver).
I think we can expect that people will keep Decision
, LinearExpression
and Objective
values in their own structures in code consuming Flips:
- they can't access the representation of
Model
which hold those - even if they could, it wouldn't fit the needs to map the model and the solution with their own domain in convenient fashion, they could likely rely on hashsets and dictionaries
the usage of such solution for the mapping in client code could be simplified if Flips can work with "representation agnostic" IDecision
and the like, and the client code deal with embedding the "ties" that would otherwise be achieved with dictionaries / hashsets.
from flips.
Not in the implementation of the library AFAIK, but the client code I was writing (toying around with my current implementation for Cplex solver).
Can you elaborate on this? PR #142 fixes the stack overflow, but the contract for Equals
and GetHashCode
are still being violated (c.f. #141). It is important to see how you want to use hashsets and dictionaries in order to select the correct fix.
from flips.
Related Issues (20)
- LinearExpression.Evaluate is incorrect HOT 2
- Update Examples to current API HOT 3
- Flips 3.0 design: explore gener(ic)alization space
- Add tests that ensure recursion uses tail calls HOT 4
- Would you be interesting in some background / related links ? HOT 2
- why is #r "nuget: Flips,2.4.4" saying .net framework and not netstandard? HOT 8
- Units of Measure missing overload HOT 1
- Sample code snippet and introduction for the fslab website
- Out of memory only for CBC and GLOP HOT 7
- question - validate decisions dynamically based on values of other decisions HOT 4
- Question - Using objective expression in a constraint HOT 6
- Version 2.4.7 locked to FSharp.Core 5.0.0 HOT 2
- Add up-for-grabs issues HOT 4
- Question - Division or multiplication between decisions in linear expression HOT 6
- Incorrect UoM for float multiplied with LinearExpression
- [docs] Missing `WriteMPSFile` field in settings type of introduction
- It throws me an System.BadImageFormatException exception HOT 3
- [DOC] Add license information in readme HOT 1
- LIght/Dark Toggle button
- Is it possible to match LinearExpressions conditionally? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flips.