I'm trying to convert this MS Solver Foundation example from C# to F#, but constantly running into problems with type conversions, in particular in section 5, where C# accepts implicit conversion from double to Rational, something F# does not accept - any ideas how to resolve? The Rational type in itself is a puzzle for me as seems virtually impossible to initialize apart from setting it to the predefined Rational.One or Rational.Zero. Any ideas? See a minimalist down-scaled version below (without using any arrays or anything).
let main argv =
printfn "%A" argv
Console.WriteLine("\nBegin Solver demo\n")
let mean = 1.0
let solver = new InteriorPointSolver()
let allocation = ref 0
solver.AddVariable("MSFT", allocation) |> ignore
solver.SetBounds(!allocation, Rational.Zero, Rational.One)
let expRet = ref 0
solver.AddRow("expRet", expRet) |> ignore
solver.SetBounds(!expRet, Rational.Zero, Rational.PositiveInfinity)
let unity = ref 0
solver.AddRow("Investments sum to one", unity) |> ignore
solver.SetBounds(!unity, Rational.One, Rational.One)
solver.SetCoefficient(!expRet, !allocation, Rational.)
solver.SetCoefficient(!unity, !allocation, Rational.One);
Console.WriteLine("\nEnd Solver demo\n")
Console.ReadLine() |> ignore
0 // return an integer exit code
This is an old post, but no one has answered, and this information may be useful to others.
I don't know F# (so anyone who does, please edit my syntax), but the following methods in the Rational class should be of use. You will also need the BigInteger class from the Microsoft.SolverFoundation.Common name space. (see https://learn.microsoft.com/en-us/previous-versions/visualstudio/ff526610)
First, as you point out, you can construct a Rational directly from other types using any of the many "implicit" constructors:
where I am guessing F# syntax.
As another quite illustrative example, monetary values (e.g. $10.43) can rarely be represented exactly with doubles. (The only "cents" values that wind up exact in a double are xx.00, xx.25, xx.50 and xx.75, all others wind up with numerical errors/differences.) So we often have to be careful when constructing a rational from a double that purports to represent a monetary value. This provides a good sample of another method of constructing rationals:
and so we have constructed a Rational, from two BigInteger types, that exactly represents a monetary value mv stored as a double.
Here is the entire Rational interface, albeit in c#-syntax:
The other very useful bit of information that can be found at the above link is this table:
"The following table lists how special cases of rational numbers are represented.
Dividing a nonzero value by zero results in unsigned infinity because 0 is unsigned. Dividing a finite value by any infinite value results in 0."