Thursday, July 26, 2012

"Big Math"[1] : Solver Foundation and F#

It would be powerful for 'big math' users of Powershell if we could integrate F# and Microsoft's Solver Foundation into Powershell 3.0. Microsoft's Solver Foundation provides for "Programming in the Optimization Modeling Language (OML), in C# imperatively, in F# functionally, or in any .NET language".  
So will it work with a DLR language like Powershell 3.0 Beta?  I don't know yet but here is some exploratory script.:



First, install Microsoft Solver Foundation. Alternatively, you probably could just GAC the Solver Foundation dll. Then:


$Solver=add-type -path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Solver.Foundation\v4.0_3.0.1.10599__31bf3856ad364e35\Microsoft.Solver.Foundation.dll" -passthru
[array[]]$SolverArrayUnique=(($Solver | gm -static).Typename) | get-unique
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SolverFoundation.Common")
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SolverFoundation.Services")
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SolverFoundation.Solvers")
$SAU=foreach ($i in $SolverArrayUnique) {write "[$i]" }
# the first fifty object types seem to mostly have static members:
$SAU[0..50]

[Microsoft.SolverFoundation.Common.License]
[Microsoft.SolverFoundation.Properties.Resources]
[Microsoft.SolverFoundation.Common.Arithmetic`1]
[Microsoft.SolverFoundation.Common.RationalArithmetic]
[Microsoft.SolverFoundation.Common.DoubleArithmetic]
[Microsoft.SolverFoundation.Common.BigRegister]
[Microsoft.SolverFoundation.Common.DebugContracts]
[Microsoft.SolverFoundation.Common.BigInteger]
[Microsoft.SolverFoundation.Services.ILinearSolver]
[Microsoft.SolverFoundation.Services.LinearEntry]
[Microsoft.SolverFoundation.Services.QuadraticEntry]
[Microsoft.SolverFoundation.Services.LinearSolutionQuality]
[Microsoft.SolverFoundation.Services.SpecialOrderedSetType]
[Microsoft.SolverFoundation.Services.LinearResult]
[Microsoft.SolverFoundation.Services.LinearValueState]
....


Here's an example:


[Microsoft.SolverFoundation.Solvers.SimplexCosting] | gm -static


   TypeName: Microsoft.SolverFoundation.Solvers.SimplexCosting


Name              MemberType Definition
----              ---------- ----------
...


Microsoft.SolverFoundation.Solvers.SimplexCosting Automatic {get;}
BestReducedCost   Property   static Microsoft.SolverFoundation.Solvers.SimplexCosting BestReducedCost {get;}
Default           Property   static Microsoft.SolverFoundation.Solvers.SimplexCosting Default {get;}
NewPartial        Property   static Microsoft.SolverFoundation.Solvers.SimplexCosting NewPartial {get;}
Partial           Property   static Microsoft.SolverFoundation.Solvers.SimplexCosting Partial {get;}
SteepestEdge      Property   static Microsoft.SolverFoundation.Solvers.SimplexCosting SteepestEdge {get;}




The 'add-type' help for Powershell Beta 3.0 has and example detailing using Powershell to compile F# code, but I was baffled by the help examples. As if they were not current with the latest versions of Powershell and F#. You will have to find the correct path and objects for now. I notice that Doug Finke has an example of how to make this work for .NET 4.0 .


#(See 'man add-type -full'):
Add-Type -Path FSharp.Compiler.CodeDom.dll
Add-Type -Path FSharp.Compiler.CodeDom.dll
$provider = New-Object Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider
$fSharpCode = @"
 let rec loop n =
 if n <= 0 then () else begin
 print_endline (string_of_int n);
 loop (n-1)
 end
 "@
$fsharpType = Add-Type -TypeDefinition $fSharpCode -CodeDomProvider $provider -PassThru | where { $_.IsPublic }
 C:\PS> $fsharpType::loop(4)
 4
 3
 2
 1




However, I did find I could instantiate F# object types in Powershell. Install F# first and then find the paths to the appropriate dlls:


$FSharpCompiler=add-type -path 'C:\Program Files (x86)\Microsoft F#\v4.0\FSharp.Compiler.dll' -passthru
FSharpCore=add-type -path 'C:\Program Files (x86)\FSharp-2.0.0.0\bin\FSharp.Core.dll' -passthru
$FSharpCore | gm -static | more


[Microsoft.FSharp.Quotations.FSharpExpr] | gm -static

   TypeName: Microsoft.FSharp.Quotations.FSharpExpr


Name                         MemberType Definition
----                         ---------- ----------
AddressOf                    Method     static Microsoft.FSharp.Quotations.FSharpExpr AddressOf(Microsoft.FSharp.Quotations.FSharpExpr target)
AddressSet                   Method     static Microsoft.FSharp.Quotations.FSharpExpr AddressSet(Microsoft.FSharp.Quotations.FSharpExpr target, Microsoft.FSharp.Quotations.FSharpExpr value)
Application                  Method     static Microsoft.FSharp.Quotations.FSharpExpr 
....

No comments: