Skip to content

Commit 300725f

Browse files
committed
LP is working
1 parent d83ccf1 commit 300725f

File tree

6 files changed

+555
-0
lines changed

6 files changed

+555
-0
lines changed

src/Gurobi.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Gurobi
2+
3+
export gurobi_model, update_model!
4+
export add_var!, add_vars!, add_cvar!, add_cvars!
5+
export add_const!, add_constrs!
6+
export optimize
7+
8+
export get_status, OptimInfo, get_optim_info, get_objval
9+
export get_solution
10+
11+
import Base.convert, Base.show, Base.copy
12+
13+
include("find_gurobi.jl")
14+
include("grb_env.jl")
15+
include("grb_model.jl")
16+
include("grb_solve.jl")
17+
end

src/find_gurobi.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Locating Gurobi library
2+
#
3+
# Current approach simply looks at an environment variable GUROBI_LIB
4+
#
5+
6+
function _find_library_path()
7+
if !has(ENV, "GUROBI_LIB")
8+
error("Please set the environment variable GUROBI_LIB to specify the dynamic library path.")
9+
end
10+
p = ENV["GUROBI_LIB"]
11+
if !isfile(p)
12+
error("Cannot find library file $p.")
13+
end
14+
p
15+
end
16+
17+
const libgurobi_path = _find_library_path()
18+
const lib_gurobi = dlopen(libgurobi_path, RTLD_GLOBAL)
19+
20+
# function pointers
21+
22+
macro grb_def(fun)
23+
local fptr = symbol(string(fun, "_ptr"))
24+
@eval begin
25+
$(fptr) = C_NULL
26+
function $(fun)()
27+
global $(fptr)
28+
if $(fptr) == C_NULL
29+
$(fptr) = dlsym(lib_gurobi, $(Meta.quot(fun)))
30+
end
31+
$(fptr)::Ptr{Void}
32+
end
33+
end
34+
end
35+
36+
@grb_def GRBloadenv
37+
@grb_def GRBfreeenv
38+
@grb_def GRBgeterrormsg
39+
40+
@grb_def GRBnewmodel
41+
@grb_def GRBcopymodel
42+
@grb_def GRBfreemodel
43+
@grb_def GRBupdatemodel
44+
45+
@grb_def GRBaddvars
46+
47+
@grb_def GRBgetintattr
48+
@grb_def GRBgetdblattr
49+
@grb_def GRBgetstrattr
50+
@grb_def GRBgetintattrarray
51+
@grb_def GRBgetdblattrarray
52+
@grb_def GRBgetstrattrarray
53+
54+
@grb_def GRBoptimize

src/grb_env.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Gurobi environment and other supporting facilities
2+
3+
4+
type Env
5+
ptr_env::Ptr{Void}
6+
7+
function Env()
8+
a = Array(Ptr{Void}, 1)
9+
ret = ccall(GRBloadenv(), Cint, (Ptr{Ptr{Void}}, Ptr{Uint8}),
10+
a, C_NULL)
11+
if ret != 0
12+
error("Failed to create environment (error code = $ret).")
13+
end
14+
env = new(a[1])
15+
finalizer(env, free_env)
16+
env
17+
end
18+
end
19+
20+
convert(ty::Type{Ptr{Void}}, env::Env) = env.ptr_env::Ptr{Void}
21+
22+
function is_valid(env::Env)
23+
env.ptr_env != C_NULL
24+
end
25+
26+
function free_env(env::Env)
27+
if env.ptr_env != C_NULL
28+
ccall(GRBfreeenv(), Void, (Ptr{Void},), env.ptr_env)
29+
env.ptr_env = C_NULL
30+
end
31+
end
32+
33+
function get_error_msg(env::Env)
34+
@assert env.ptr_env != C_NULL
35+
sz = ccall(GRBgeterrormsg(), Ptr{Uint8}, (Ptr{Void},), env.ptr_env)
36+
bytestring(sz)
37+
end
38+
39+
40+
type GurobiError
41+
code::Int
42+
msg::ASCIIString
43+
44+
function GurobiError(env::Env, code::Integer)
45+
new(convert(Int, code), get_error_msg(env))
46+
end
47+
end
48+

0 commit comments

Comments
 (0)