I've been trying to use glpk-hs in my Haskell project but I'm completely lost on how to do it. I'm using stack to run my program and my confusion most likely comes from my lack of knowledge on how it works.
That being said, I've downloaded glpk-hs-0.7 and put the extracted folder in my project's folder. I'm running my code with stack ghci my_program.hs, with the following dependencies on a stack.yaml file:
"extra-deps:
- gasp-1.2.0.0
- glpk-hs-0.7"
It successfully installs gasp but fails the glpk installation with this error: glpk-hs > * Missing (or bad) C library: glpk.
I've also tried installing glpk for Windows by downloading the executables and adding it to the PATH, and when I run glpsol on cmd it gives this message:
GLPSOL: GLPK LP/MIP Solver, v4.65
No input problem file specified; try glpsol --help
What am I missing? I've noticed that in the glpk-hs-0.7/glpk folder there is a glpk.c file. Should I compile and execute this program?
Btw, I'm running on Windows 10 now but I mostly work on a Linux machine, so I would appreciate a solution that works for both systems, if there is one.
Thank you!
The
glpk-hspackage provides a GHC interface to a separately installed GLPK library, but installingglpk-hsdoesn't automatically install that required library. Compiling the package's"glpk.c"file wouldn't help, as that's just some stub C code to help in building the interface. (The vast majority of GHC packages that provide a "bridge" to other libraries are designed this way, soglpk-hsisn't a special case.)Under Linux, you would need to install the development version of the GLPK package in the "usual" way using your distribution's package manager (e.g., for Debian-based distributions, you'd need to run
apt install libglpk-dev) before trying to (re-)install theglpk-hspackage.Under Windows, I guess it's probably easiest to download the zipfile with precompiled binaries from the GLPK for Windows Project Page. Unpack it somewhere convenient and, as per the instructions on that web page, copy either the 32-bit or 64-bit DLLs, as appropriate, to the
c:\windows\system32directory.In order for
stackto build against the library, it needs to have some extra library and include file directories specified. In your project-specificstack.yaml(or in a globalconfig.yaml), you'll want to add lines pointing to the appropriate unpacked paths. For example, something like (assuming a 64-bit environment):Also, I ran into a further problem as Cabal was looking for
glpk.liband notglpk_4_65.lib, so I had to copy the library over. I'm not sure if there's a better way to do this.Now, the latest
glpk-hs-0.7package isn't compatible with the current GHCcontainersversion, so you'll need to use an earlier resolver in yourstack.yamlfile. Thelts-12.26resolver worked for me:Finally, GLPK only works with the threaded runtime, so add the flags to your
.cabalfile:You can grab an example from https://github.com/jyp/glpk-hs/blob/master/examples/example1.hs. Delete the
import Algebraic.Classesline because it isn't needed, and with the followingexecutableclause in your.cabalfile:and the following
stack.yaml:you should be able to
stack buildandstack exec glpktestthat example. If the executable builds correctly but running it produced no output, it may be because the DLLs aren't being found. Make sure the right set of DLLs have been copied intoc:\windows\system32.To sum up all the steps assuming a 64-bit environment:
w64directory toc:\windows\system32w64directory, copyglpk_4_65.libtoglpk.lib.stack.yamlabove with resolverlts-12.26and appropriate directory settings.stack buildtheExample1.hslinked above (deleting the unnecessaryimportline first) with theexecutableclause given above copied into your.cabalfile (including the-threadedGHC option)stack exec glpktest, and you'll hopefully see a solution printed (x1=40, x2=50, and x3=0).