Since the Microsoft Solver Foundation has been deprecated, I am trying to find an alternative or a reasonable way to create my own DSL.
What I am looking for is essential a DSL to describe a LP in F#, solve it with Clp and evaluate the results.
Before I reinvent the wheel: does someone know a good library that already provides a DSL for LPs?
Otherwise, how would you build such a DSL in F#? Essentially, I would like to be able to write something like
let xs = createVars 100 in 0..1
let ys = [| 1 .. 100 |]
let f i x = i*x
let lp =
minimize sumprod(xs, ys) subjectTo [
xs.[0] + xs.[1] = 1
sum(xs) <= 1
sum({for i in 1..100 -> f i xs.[i]}) <= 100
// ...
]
let solver = Clp()
let result = solver.solve lp
Before I reinvent the wheel: does someone know a good library that already provides a DSL for LPs?
No, I don't know any except ODSL from Microsoft SolverFoundation itself.
Otherwise, how would you build such a DSL in F#?
Decide the syntax of the language. Although you want to build an internal DSL, you have to decide what are allowed and what are inexpressible in your language.
Use F# quotations to obtain an AST. Why do you need reflection? First, you create a bunch of variables and combine them with float constants to form linear constraints. Later, you will fill up these variables with appropriate values. Reflection allows you to create placeholders and calculate results later.
Transform ASTs to linear programs in CLP and solve. It seems CLP doesn't have .NET API; you could communicate with the solver via command lines, but it isn't very convenient and robust. It is good idea to start building a low-level API before creating DSLs.
(Optional) In F# 3.0, you can create a query syntax for your DSL. You can have a look at a convincing example, the built-in query expression.
Since the Microsoft Solver Foundation has been deprecated, I am trying to find an alternative or a reasonable way to create my own DSL.
Anyway, MSF will provide valuable examples for your work. You can browse through ODSL source code in this codebase. Indeed, from the codebase you can see that ODSL has completed three first steps. I'm building an optimization query language on top of ODSL (step 4) and have only finished the surface syntax. For example, this original example
<#
let sa = var<Barrel/Day>()
let vz = var<Barrel/Day>()
minimise (20.0<Dollar/Barrel> * sa + 15.0<Dollar/Barrel> * vz)
where
[
0.3 * sa + 0.4 * vz >= 2000.<Barrel/Day>;
0.4 * sa + 0.2 * vz >= 1500.<Barrel/Day>;
0.2 * sa + 0.3 * vz >= 500.<Barrel/Day>;
sa <= 9000.<Barrel/Day>;
vz <= 6000.<Barrel/Day>;
sa >= 0.<Barrel/Day>;
vz >= 0.<Barrel/Day>
]
#>
would be transformed to
opt { let! sa = var<Barrel/Day>()
let! vz = var<_>()
assume (0.3 * sa + 0.4 * vz >= 2000.<_>)
assume (0.4 * sa + 0.2 * vz >= 1500.<_>)
assume (0.2 * sa + 0.3 * vz >= 500.<_>)
assume (sa <= 9000.<_> && sa >= 0.<_>)
assume (vz <= 6000.<_> && vz >= 0.<_>)
minimise (20.0<Dollar/Barrel> * sa + 15.0<_> * vz)
}
I have also translated a number of DSL examples here in case you're interested.
Related
I have been using Z3 to check if terms can be satisfied. But in addition I need to simplify terms for human consumption e.g. when n is an Int simplify And(n>4 , n != 5) to n > 5. Dose any one know how to do this in Z3 or via other tools?
As you probably already noticed Z3 has a simplifier exposed over the API and you can also use it from SMT-LIB. The tutorials on Z3 from rise4fun.com/z3 and rise4fun.com/z3py give several examples of the simplifier. However, the simplifier does not attempt any normal form conversions, so it will unlikely produce results of the style you hint you want. In particular it does not simplify the conjunction And(n > 4, n != 5) to n > 5.
Possible answer:
n = Int('n')
antecedent = And(n >4, n != 5)
claim1 = n > 5
prove(Implies(antecedent, claim1))
Output:
proved
Given an expression in Z3py, can I convert that to SMT-LIB2 language? (So I can feed this SMT-LIB2 expression to other SMT solvers that support SMT-LIB2)
If this is possible, please give one example.
Thanks a lot.
We can use the C API Z3_benchmark_to_smtlib_string. Every function in the C API is available in Z3Py. This function was initially used to dump benchmarks in SMT 1.0 format, and it predates SMT 2.0. That is why it has some parameters that may seem unnecessary. Now, by default, it will display benchmarks in SMT 2.0 format. The output is not meant to be human readable.
We can write the following Python function to make it more convenient to use:
def toSMT2Benchmark(f, status="unknown", name="benchmark", logic=""):
v = (Ast * 0)()
return Z3_benchmark_to_smtlib_string(f.ctx_ref(), name, logic, status, "", 0, v, f.as_ast())
Here is a small example using it (also available online here)
a = Int('a')
b = Int('b')
f = And(Or(a > If(b > 0, -b, b) + 1, a <= 0), b > 0)
print toSMT2Benchmark(f, logic="QF_LIA")
Is it possible to change the timeout value of the solver at runtime using C API?
In order to set the timeout the following can be done -
Z3_config cfg = Z3_mk_config();
Z3_set_param_value(cfg, "SOFT_TIMEOUT", "10000") // set timeout to 10 seconds
Z3_context ctx = Z3_mk_context(cfg);
....
Z3_check_and_get_model(ctx);
....
....
Z3_check_and_get_model(ctx);
However, suppose we want to change the timeout for next query while retaining the context, is it possible to change the timeout value in between?
Consider moving to Z3 4.0. Z3 4.0 has new API that allows the user to create many solvers in the same Z3_context. You can set different timeouts for each solver, and update them whenever you want. Z3 4.0 also comes with a C++ layer that makes the C API much more convenient to use.
Here is an short example that sets a timeout. On my machine, Z3 will return unknown when the 1 millisecond timeout is used, and sat when the s.set(p) command is removed.
context c;
expr x = c.real_const("x");
expr y = c.real_const("y");
solver s(c);
s.add(x >= 1);
s.add(y < x + 3);
params p(c);
p.set(":timeout", static_cast<unsigned>(1)); // in milliseconds
s.set(p);
std::cout << s.check() << std::endl;
I'm giving a small presentation to a group of C/C++ programmers who have very little experience with functional languages. Part of the presentation mentions Erlang, and I would like to give a specific small code example.
There is a ton of awesome information on StackOverflow about how/where Erlang is used and its advantages. One of the most common advantages I see is how it can do a lot with just a little terse code, especially compared to C/C++.
I am looking for a good code snippet of Erlang that simply illustrates these types of benefits. Especially something thats easily done in Erlang with few lines, that would be much more complicated in C/C++.
Anyone have any interesting suggestions?
Check out example 4 for an excellent example of Erlang's bit syntax. I'm sure there are a number of c/c++ developers that will appreciate the brevity of the syntax!
I would use an example which shows how easy it is to do concurrency.
So basically write map-reduce (but never ever use that word to describe it to a C programmer).
You could start with showing a program that plays Fizz Buzz, and then proceed to make it concurrent. Should easily fit a whiteboard, or two pages of powerpoint.
A Co-worker suggested using Merge-Sort as an example:
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Erlang
mergeSort(L) when length(L) == 1 -> L;
mergeSort(L) when length(L) > 1 ->
{L1, L2} = lists:split(length(L) div 2, L),
lists:merge(mergeSort(L1), mergeSort(L2)).
Multi-process version:
pMergeSort(L) when length(L) == 1 -> L;
pMergeSort(L) when length(L) > 1 ->
{L1, L2} = lists:split(length(L) div 2, L),
spawn(mergesort, pMergeSort2, [L1, self()]),
spawn(mergesort, pMergeSort2, [L2, self()]),
mergeResults([]).
pMergeSort2(L, Parent) when length(L) == 1 -> Parent ! L;
pMergeSort2(L, Parent) when length(L) > 1 ->
{L1, L2} = lists:split(length(L) div 2, L),
spawn(mergesort, pMergeSort2, [L1, self()]),
spawn(mergesort, pMergeSort2, [L2, self()]),
Parent ! mergeResults([]).
Pythagorean Triples. Get all number combinations below 30 whereby the 3 numbers make a right angled triangle as it is according to Pythagoras.
[{X,Y,Z} || X <- lists:seq(1,30),
Y <- lists:seq(1,30),
Z <- lists:seq(1,30), ((X * X) + (Y * Y)) == (Z * Z)].
Try doing that in C/C++ , or Java and see if you will avoid a for loop if not more than one depending on your skill level :)
the factorial code snippet is the best i have always used to show how short erlang programs can be
-module(factorial).
-export([calculate/1]).
calculate(0) -> 1;
calculate(N) -> N * calculate(N -1).
As simple as that. That short program illustrates not only how short Erlang programs can be, But also: Pattern Matching, Function Clauses and Last Call Optimization. I always had a C++ version of the same, below:
#include<iostream.h>
#include<conio.h>
long factorial(unsigned int a);
void main() {
unsigned int a;
long fac;
.....
.....
return factorial(a);
}
long factorial(unsigned int x) {
long fac=1;
if(x == 0) {return 1;}
else {
while(x > 0) {
fac *= x;
x -= 1 ;
}
return fac; }
}
Well, this may not be the shortest C++ version, but i know you get the idea.
I cannot find "do...while..."
I have to code like this:
let bubbleSort a=
let n = Array.length a
let mutable swapped = true
let mutable i = 0
while swapped do
swapped <- false
for j = 0 to n-i-2 do
if a.[j] > a.[j+1] then
let t = a.[j]
a.[j] <- a.[j+1]
a.[j+1] <- t
swapped <- true
i <- i+1
The code is bad without "do...while".
Sadly, "break/continue" are also not available.
F# is very much suitable for non-functional programming. In fact, being able to fine-tune parts of an algorithm in an imperative style is one of the major strong points of the language for me.
For example, in tackling a project euler problem, I started out with a clean functional solution using immutable sets and folds. It took 150 seconds to complete. Now having the framework of my algorithm in place allowed me to pick apart the data structures and folds operations one at a time until I managed to get the run time down to 5 seconds. My final solution was very much an imperative one (and even slightly faster than an equivalent C# version).
As you can see I solved it by coding a solution in functional style first and then rewrite small parts to an imperative style. Not having to deal with indices and other loop conditions explicitly kept the code more understandable for me.
Once you learn how to think like a functional programmer you'll find that you'll rarely want breaks and continues. That's what I experienced. But if you do need them, knowing how to think in a functional way helps in coming up with work-arounds, usually involving a tail-recursive version of what used to be a loop.
By the time you start thinking more in an idiomatic F# way, you'll probably see more and more (tail-)recursive code replacing what you used to do with looping constructs. Heck, writing F# for 2 years now has warped my mind so far that I'm more likely to pick recursion and folds over loops.
Whenever I think I need break/continue, I usually don't because there's a cleaner version of the algorithm hidden and waiting to get out. The biggest challenge is learning how to find that cleaner version. I'm afraid that lots of practice and good examples are the only way to get better at thinking functionally, but I believe that it's an effort well spent.
Edit: ironically, bubble sort is an algorithm which is actually designed for arrays with mutable contents. Any recursive bubble sort is likely to be harder to understand than an imperative version. I think I just killed my own post here.
It turns out to be quite easy to write a good enough do-while in F# as a higher-order function:
let doWhile f c =
f ()
while c () do
f ()
break and continue would be a really useful feature additions; they're reserved words, and maybe we'll see them in a future version of the language. The lack of them is an occasional minor annoyance, but hardly makes the language 'unsuitable'. In the mean time, a mutable sentinel works, as you have in your example.
See also
http://tomasp.net/blog/imperative-ii-break.aspx/
do/while is not available because F# is a functional language and this kind of construct is specific to imperative languages.
break/continue is also not available for the same reasons.
However, you can still write do/while in F#. The following code blocks are equivalent :
in C#
do
{
System.Console.WriteLine("processing something...");
System.Console.WriteLine("doing something complicated");
System.Console.Write("continue?");
} while (Console.ReadLine() == "y");
in F#
let doSomethingAndContinue() =
printfn "processing something..."
printfn "doing something complicated"
printf "continue?"
System.Console.ReadLine()="y"
while doSomethingAndContinue() do ignore None
Although a bit more verbose, you can use recursive functions to avoid the "do while" as in :
let swap (a:int[]) i j =
let t = a.[i]
a.[i] <- a.[j]
a.[j] <- t
let rec bubbleSortAux a nMax j swapped =
if j >= 0 && j <= nMax then
if a.[j] > a.[j+1] then
swap a j (j+1)
bubbleSortAux a nMax (j+1) true
else
bubbleSortAux a nMax (j+1) false
else
swapped
let rec bubbleSortLoop a nMax =
if bubbleSortAux a nMax 0 false then
bubbleSortLoop a (nMax - 1)
let bubbleSort a =
bubbleSortLoop a (a.Length - 2)
let bubbleSort (a: _ []) =
let mutable fin = false
while not fin do
fin <- true
for i=0 to a.Length-2 do
if a.[i] > a.[i+1] then
let t = a.[i]
a.[i] <- a.[i+1]
a.[i+1] <- t
fin <- false
I do not know about F# very well, but F# is a functional language. Usually, there is no such thing as "for" or "while" loops in functional programming languages.
Functional languages define functions in a mathematical sense (like f(x) => ...). Writing a program comes down to defining and combining a set of mathematical functions. This means that the only way of coding loops is using recursion.
In Mathematics, there is no way of saying:
f(x) => "do 5 times this"
What you'd do is define f like:
count > 0 : f(x, count-1)
f(x, count) => {
count <= 0 : ...
And then use this function as in:
y = f(x, 5)
This would be exactly how you implement functions in functional languages.
At least, this is true for purely functional languages like Haskell...
You can do something like
let mutable ind = 0
while (
//Do your stuff here
//Now condition part return some boolean value
ind < 10
) do ind <- ind +1
I just recently found this way. It feels a bit hacky but what I like is that you can build something more complex what usually caused issues in C#, C++.
let mutable ind = 0
while (
(some condition) && (
//do something
let someValue = Eval ....
//Now more complex condition
ind + someValue < 10
)
) do ind <- ind +1