Skip to content
Browse files

linprog workarounds for bug #814

  • Loading branch information...
1 parent d5172ca commit 6422d302fcf43e6a90157f86004671c99edbd857 @carlobaldassi committed May 8, 2012
Showing with 23 additions and 24 deletions.
  1. +3 −6 extras/glpk.jl
  2. +2 −3 extras/linprog.jl
  3. +18 −15 test/linprog.jl
View
9 extras/glpk.jl
@@ -2,8 +2,6 @@
### GLPK API Wrapper
###
-# Note: be sure to load "sparse.jl" before this file
-
## Shared library interface setup
#{{{
load("glpk_h.jl")
@@ -128,10 +126,9 @@ end
# In this framework, optional arguments can be passed either
# as an empty vector [] or as the 'nothing' constant
-typealias VecOrNothing{T} Union(AbstractVector{T}, AbstractVector{None}, Nothing)
-typealias MatOrNothing{T} Union(AbstractMatrix{T}, AbstractVector{None}, Nothing)
-_jl_glpk__is_empty{T}(x::Union(VecOrNothing{T}, MatOrNothing{T})) =
- isa(x, Union(AbstractVector{None}, Nothing))
+typealias VecOrNothing{T} Union(AbstractVector{T}, Vector{None})
+typealias MatOrNothing{T} Union(AbstractMatrix{T}, Vector{None})
+_jl_glpk__is_empty(x) = isempty(x)
# General exception: all GLP functions
# throw this in case of errors
View
5 extras/linprog.jl
@@ -3,6 +3,8 @@
## for optimization and constraint satisfaction problems
##
+# note: be sure to load "sparse.jl" and "glpk.jl" before this file
+
# General notes: the interface is provided as a collection of
# high-level functions which use the glpk library.
# Most functions have almost same interface
@@ -40,8 +42,6 @@
# Some methods have slightly different function calls, see
# individual notes for additional information
-include("glpk.jl")
-
# Linear Programming, Interior point method (default)
#{{{
@@ -345,7 +345,6 @@ function _jl_linprog__check_A_b{T}(A::MatOrNothing{T}, b::VecOrNothing{T}, n::In
error("b is empty but a is not")
end
if size(b, 1) != m
- #printf(f"m=%i\n", m)
error("invalid b size: $(size(b))")
end
else
View
33 test/linprog.jl
@@ -1,8 +1,8 @@
### Linear programming
-setcwd("../extras")
-load("sparse.jl")
-load("linprog.jl")
+load("../extras/sparse.jl")
+load("../extras/glpk.jl")
+load("../extras/linprog.jl")
## Simplex method
@@ -19,13 +19,16 @@ f = [ 3.; 2. ];
A = [ 2. 1. ;
1. 1. ];
b = [ 100.; 80 ];
-lb = [ 0.; 0;];
+lb = [ 0.; 0.];
-(z, x, flag) = linprog_simplex(-f, A, b, [], [], lb, [], lps_opts);
+vM = Array(Float64,0,0)
+vV = Array(Float64,0)
+
+(z, x, flag) = linprog_simplex(-f, A, b, vM, vV, lb, vV, lps_opts);
@assert flag == 0
@assert z == -180.0
-@assert x == [20.; 60.]
+@assert isequal(x, [20.; 60.])
# A constraint satisfaction (matching) problem
@@ -47,13 +50,13 @@ beq = ones(Float64, 6);
lb = zeros(Float64, 9);
ub = ones(Float64, 9);
-(z, x, flag) = linprog_simplex(f, [], [], Aeq, beq, lb, ub, lps_opts);
+(z, x, flag) = linprog_simplex(f, vM, vV, Aeq, beq, lb, ub, lps_opts);
@assert flag == 0
@assert z == 5.
-@assert x == [ 0.; 0.; 1. ;
- 0.; 1.; 0. ;
- 1.; 0.; 0. ]
+@assert isequal(x, [ 0.; 0.; 1. ;
+ 0.; 1.; 0. ;
+ 1.; 0.; 0. ])
## Interior point method
@@ -63,7 +66,7 @@ ub = ones(Float64, 9);
lpi_opts = GLPInteriorParam()
lpi_opts["msg_lev"] = GLP_MSG_ERR
-(z, x, ret) = linprog(f, [], [], Aeq, beq, lb, ub, lpi_opts);
+(z, x, ret) = linprog(f, vM, vV, Aeq, beq, lb, ub, lpi_opts);
tol = 1e-4
@@ -85,10 +88,10 @@ mip_opts["presolve"] = GLP_ON
# Use binary variables
colkind = int32([ GLP_BV for i = 1 : 9 ])
-(z, x, ret, ret_ps) = mixintprog(f, [], [], Aeq, beq, [], [], colkind, mip_opts);
+(z, x, ret, ret_ps) = mixintprog(f, vM, vV, Aeq, beq, vV, vV, colkind, mip_opts);
@assert flag == 0
@assert z == 5.
-@assert x == [ 0.; 0.; 1. ;
- 0.; 1.; 0. ;
- 1.; 0.; 0. ]
+@assert isequal(x, [ 0.; 0.; 1. ;
+ 0.; 1.; 0. ;
+ 1.; 0.; 0. ])

0 comments on commit 6422d30

Please sign in to comment.
Something went wrong with that request. Please try again.