Tuesday, October 6, 2009

Linear Programming with Erlang

So you have to solve a linear program, so naturally the first language you think of is Erlang. Actually, it's not a natural first choice for most people, but if you are solving a linear program as part of an automatic control strategy for an internet facing application, the choice is better motivated.

Since I faced this situation recently I wrote a binding for GLPK for Erlang. Writing port drivers is a drag so I actually wrote a program to generate the C and Erlang for me. Perhaps with the new FFI these sort of games will not be necessary, but I was happy with the approach because I anticipate experimenting with several linear programming packages, which should be much easier to accommodate.

Available at Google code.

8 comments:

Jean-Lou Dupont said...

I've packaged a library for interfacing between C/C++ and Erlang. Have a look at: http://epapi.googlecode.com/

Jamen Shively said...

Hello fellow Erlangers! Question: is there a linear programming solver written in Erlang? If not, what tips do you have for how I might go about writing one? Thank you in advance!

Paul Mineiro said...

@Jamen: If you are looking to have fun, I think writing a linear program solver in Erlang is great way to pass the time.

If you are looking to solve an actual problem, I don't think this is a good approach. Leaving aside Erlang specific issues (floating point performance), there are extremely good linear program solvers out there and it would be much more efficient (both programmer time and execution time) to integrate with one of them.

Marcel van Pinxteren said...

Is glpkerldrv still usable (and used)? I plan to integrate it in a Phoenix app. Problem data comes in via REST, I hand it over to the solver, and send back the solution.
A sample (in Elixir or Erlang) would be very helpful.

Paul Mineiro said...

@marcel: I haven't used it in a long while, but you are welcome to try. For code examples, see testglpkerl which contains unit tests that invoke the driver and solve some simple LPs.

Marcel van Pinxteren said...

I am getting a compilation error: glpkerldrv.c:5:24: fatal error: erl_driver.h: No such file or directory
A few lines before this, I get a warning: configure: WARNING: compilation may fail due to bad CFLAGS and LIBS
I'm new to C, so it all makes little sense to me. Sorry.
Any ideas?

Paul Mineiro said...

Probably missing a path to a header. Alternatively, the erlang driver interface may have changed since I wrote this (?)

If you are new to C, the probability that this is going to be successful is very low.

Marcel van Pinxteren said...

I installed erlang-dev, and getting a step further now.
If I run configure with --disable-hardcore, I get only one error (and a lot of warnings):
handlers.c: In function ‘decode_from’:
handlers.c:853:63: error: ‘INT_MAX’ undeclared (first use in this function)
res.it_lim = deserialize_optional (int32_t, buf, buflen, INT_MAX, deserialize_int32_t (buf, buflen));
I have found someone with more experience in C and Erlang to help me, so I have some confidence we will succeed. With a little help from you, we should be able to get this working.
I am working in https://github.com/pinx/glpkerl