forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glpk.jl
2552 lines (2181 loc) · 70 KB
/
glpk.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###
### GLPK API Wrapper
###
module GLPK
export
# Types
Param,
Error,
Prob,
SimplexParam,
InteriorParam,
IntoptParam,
BasisFact,
Data,
MathProgWorkspace,
# Methods
version,
set_prob_name,
set_obj_name,
set_row_name,
set_col_name,
set_obj_dir,
add_rows,
add_cols,
set_row_bnds,
set_col_bnds,
set_obj_coef,
set_mat_row,
set_mat_col,
load_matrix,
check_dup,
sort_matrix,
del_rows,
del_cols,
copy_prob,
erase_prob,
get_prob_name,
get_obj_name,
get_obj_dir,
get_num_rows,
get_num_cols,
get_row_name,
get_col_name,
get_row_type,
get_row_lb,
get_row_ub,
get_col_type,
get_col_lb,
get_col_ub,
get_obj_coef,
get_num_nz,
get_mat_row,
get_mat_col,
create_index,
find_row,
find_col,
delete_index,
set_rii,
set_sjj,
get_rii,
get_sjj,
scale_prob,
unscale_prob,
set_row_stat,
set_col_stat,
std_basis,
adv_basis,
cpx_basis,
simplex,
exact,
init_smcp,
get_status,
get_prim_stat,
get_dual_stat,
get_obj_val,
get_row_stat,
get_row_prim,
get_row_dual,
get_col_stat,
get_col_prim,
get_col_dual,
get_unbnd_ray,
interior,
init_iptcp,
ipt_status,
ipt_obj_val,
ipt_row_prim,
ipt_row_dual,
ipt_col_prim,
ipt_col_dual,
set_col_kind,
get_col_kind,
get_num_int,
get_num_bin,
intopt,
init_iocp,
mip_status,
mip_obj_val,
mip_row_val,
mip_col_val,
read_mps,
write_mps,
read_lp,
write_lp,
read_prob,
write_prob,
mpl_read_model,
mpl_read_data,
mpl_generate,
mpl_build_prob,
mpl_postsolve,
print_sol,
read_sol,
write_sol,
print_ipt,
read_ipt,
write_ipt,
print_mip,
read_mip,
write_mip,
print_ranges,
print_ranges,
bf_exists,
factorize,
bf_updated,
get_bfcp,
set_bfcp,
get_bhead,
get_row_bind,
get_col_bind,
ftran,
btran,
warm_up,
eval_tab_row,
eval_tab_col,
transform_row,
transform_col,
prim_rtest,
dual_rtest,
analyze_bound,
analyze_coef,
init_env,
free_env,
term_out,
open_tee,
close_tee,
malloc,
calloc,
free,
mem_usage,
mem_limit,
time,
difftime,
sdf_open_file,
sdf_read_int,
sdf_read_num,
sdf_read_item,
sdf_read_text,
sdf_line,
sdf_close_file,
read_cnfsat,
check_cnfsat,
write_cnfsat,
minisat1,
intfeas1
import Base.pointer, Base.assign, Base.ref
## Shared library interface setup
#{{{
include("$JULIA_HOME/../share/julia/extras/glpk_h.jl")
macro glpk_ccall(f, args...)
:(ccall(($"glp_$(f)", :libglpk), $(args...)))
end
macro glpkw_ccall(f, args...)
:(ccall(($"jl_glpkw__$(f)", :libglpk_wrapper), $(args...)))
end
# We need to define GLPK.version as first thing
# in order to perform a sanity check
# (since we import structs from the header,
# we must ensure that the binary is the correct
# one)
function version()
csp = @glpk_ccall version Ptr{Uint8} ()
str = Array(Uint8, 100)
strp = pointer(str)
k = 0
for i = 1 : 100
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, Int), strp, csp, sizeof(Uint8))
if str[i] == '\0'
k = i
break
end
strp += sizeof(Uint8)
csp += sizeof(Uint8)
end
if k == 0
throw(GLPError("error reading version"))
end
vstr = ASCIIString(str[1:k - 1])
return tuple(map(x->int32(parse_int(x)), split(vstr, '.'))...)
end
if version() != (MAJOR_VERSION, MINOR_VERSION)
bv = version()
hv = (MAJOR_VERSION, MINOR_VERSION)
error("GLPK error: mismatched versions: header=$(hv[1]).$(hv[2]) binary=$(bv[1]).$(bv[2])")
end
#}}}
## Preliminary definitions
#{{{
# General structure for the parameters types
abstract Param
pointer(param::Param) = param.struct
typealias ParamFieldDescriptor (ASCIIString, BitsKind)
type ParamDescriptor
struct_name::String
field_names::Vector{ASCIIString}
field_types::Vector{BitsKind}
function ParamDescriptor(cstr::String, struct_desc)
struct_name = cstr
c_struct_desc = convert(Vector{ParamFieldDescriptor}, struct_desc)
field_names = [ x[1]::ASCIIString for x = c_struct_desc ]
field_types = [ x[2]::BitsKind for x = c_struct_desc ]
new(struct_name, field_names, field_types)
end
end
function assign{T}(param::Param, val::T, field_name::String)
if pointer(param) == C_NULL
error("param is not allocated")
end
for i = 1 : length(param.desc.field_names)
if field_name == param.desc.field_names[i]
if pointer(param) == C_NULL
throw(Error("invalid struct"))
end
t = param.desc.field_types[i]
csf = strcat("jl_glpkw__", param.desc.struct_name, "_set_", field_name)
ccs = :(ccall(($csf, :libglpk_wrapper), Void, (Ptr{Void}, $t), pointer($param), $val))
eval(ccs)
return
end
end
error("field '$field_name' not found in struct '$(param.desc.struct_name)'")
end
function ref(param::Param, field_name::String)
if pointer(param) == C_NULL
error("param is not allocated")
end
for i = 1 : length(param.desc.field_names)
if field_name == param.desc.field_names[i]
if pointer(param) == C_NULL
throw(Error("invalid struct"))
end
t = param.desc.field_types[i]
cgf = strcat("jl_glpkw__", param.desc.struct_name, "_get_", field_name)
ccg = :(ccall(($cgf, :libglpk_wrapper), $t, (Ptr{Void},), pointer($param)))
return eval(ccg)
end
end
error("field '$field_name' not found in struct '$(param.desc.struct_name)'")
end
# We define some types which allow to pass optional agruments
# to the function.
# In this framework, optional arguments can be passed either
# as an empty vector [] or as the 'nothing' constant
typealias VecOrNothing Union(Vector, Nothing)
function convert_vecornothing{T}(::Type{T}, a::VecOrNothing)
if isequal(a, nothing) || isa(a, Array{None})
return T[]
elseif T <: Integer
if !(eltype(a) <: Integer)
throw(Error("integer-valued array required, or [] or nothing"))
end
elseif T <: Real
if !(eltype(a) <: Real)
throw(Error("real-valued array required, or [] or nothing"))
end
end
convert(Array{T}, a)
end
vecornothing_length(a::VecOrNothing) = is(a, nothing) ? 0 : length(a)
# General exception: all GLP functions
# throw this in case of errors
type Error <: Exception
msg::String
end
#}}}
## Main types definitions
#{{{
# All structs in original glpk are wrapped up in
# composite types, which initialize and destroy themselves
# as needed, and expose pointers when asked to by
# ccall's.
#
# Therefore, the original C glp API
#
# int glp_simplex(prob * lp, glp_smpc * param)
#
# becomes
#
# GLPK.simplex(lp::GLPK.Prob, param::GLPK.SimplexParam)
#
#
# The map between names is as follows:
#
# +-------------+--------------------------+
# | C | Julia |
# +-------------+--------------------------+
# | glp_prob | GLPK.Prob |
# | glp_smcp | GLPK.SimplexParam |
# | glp_iptcp | GLPK.InteriorParam |
# | glp_iocp | GLPK.IntoptParam |
# | glp_bfcp | GLPK.BasisFactParam |
# | glp_tran | GLPK.MathProgWorkspace |
# | glp_data | GLPK.Data |
# +-------------+--------------------------+
#
# In order to get/set the value of a cstruct field, you can
# use vector-like referncing with the field name as an argument,
# e.g.:
#
# lps_opts = GLPK.SimplexParam()
# lps_opts["msg_lev"] = GLPK.MSG_ERR
# lps_opts["presolve"] = GLPK.ON
#
type Prob
p::Ptr{Void}
function Prob()
p = @glpk_ccall create_prob Ptr{Void} ()
prob = new(p)
finalizer(prob, delete_prob)
return prob
end
end
function delete_prob(prob::Prob)
if prob.p == C_NULL
return
end
@glpk_ccall delete_prob Void (Ptr{Void},) prob.p
prob.p = C_NULL
return
end
simplex_param_struct_desc = ParamDescriptor("smcp",
[("msg_lev", Int32), ("meth", Int32), ("pricing", Int32),
("r_test", Int32), ("tol_bnd", Float64), ("tol_dj", Float64),
("tol_piv", Float64), ("obj_ll", Float64), ("obj_ul", Float64),
("it_lim", Int32), ("tm_lim", Int32), ("out_frq", Int32),
("out_dly", Int32), ("presolve", Int32)])
type SimplexParam <: Param
struct::Ptr{Void}
desc::ParamDescriptor
function SimplexParam()
struct = @glpkw_ccall smcp_init Ptr{Void} ()
param = new(struct, simplex_param_struct_desc)
finalizer(param, smcp_delete)
return param
end
end
function smcp_delete(param::SimplexParam)
@glpkw_ccall smcp_delete Void (Ptr{Void},) pointer(param)
param.struct = C_NULL
end
interior_param_struct_desc = ParamDescriptor("iptcp",
[("msg_lev", Int32), ("ord_alg", Int32)])
type InteriorParam <: Param
struct::Ptr{Void}
desc::ParamDescriptor
function InteriorParam()
struct = @glpkw_ccall iptcp_init Ptr{Void} ()
param = new(struct, interior_param_struct_desc)
finalizer(param, iptcp_delete)
return param
end
end
function iptcp_delete(param::InteriorParam)
@glpkw_ccall iptcp_delete Void (Ptr{Void},) pointer(param)
param.struct = C_NULL
end
intopt_param_struct_desc = ParamDescriptor("iocp",
[("msg_lev", Int32), ("br_tech", Int32), ("bt_tech", Int32),
("pp_tech", Int32), ("fp_heur", Int32), ("gmi_cuts", Int32),
("mir_cuts", Int32), ("cov_cuts", Int32), ("clq_cuts", Int32),
("tol_int", Float64), ("tol_obj", Float64), ("mip_gap", Float64),
("tm_lim", Int32), ("out_frq", Int32), ("out_dly", Int32),
("cb_func", Ptr{Void}), ("cb_info", Ptr{Void}), ("cb_size", Int32),
("presolve", Int32), ("binarize", Int32)])
type IntoptParam <: Param
struct::Ptr{Void}
desc::ParamDescriptor
function IntoptParam()
struct = @glpkw_ccall iocp_init Ptr{Void} ()
param = new(struct, intopt_param_struct_desc)
finalizer(param, iocp_delete)
return param
end
end
function iocp_delete(param::IntoptParam)
@glpkw_ccall iocp_delete Void (Ptr{Void},) pointer(param)
param.struct = C_NULL
end
basisfact_param_struct_desc = ParamDescriptor("bfcp",
[("type", Int32), ("lu_size", Int32), ("piv_tol", Float64),
("piv_lim", Int32), ("suhl", Int32), ("eps_tol", Float64),
("max_gro", Float64), ("nfs_max", Int32), ("upd_tol", Float64),
("nrs_max", Int32), ("rs_size", Int32)])
type BasisFactParam <: Param
struct::Ptr{Void}
desc::ParamDescriptor
function BasisFactParam()
struct = @glpkw_ccall bfcp_init Ptr{Void} ()
param = new(struct, basisfact_param_struct_desc)
finalizer(param, bfcp_delete)
return param
end
end
function bfcp_delete(param::BasisFactParam)
@glpkw_ccall bfcp_delete Void (Ptr{Void},) pointer(param)
param.struct = C_NULL
end
type Data
p::Ptr{Void}
end
pointer(data::Data) = data.p
type MathProgWorkspace
p::Ptr{Void}
function MathProgWorkspace()
tran = @glpk_ccall mpl_alloc_wksp Ptr{Void} ()
wksp = new(tran)
finalizer(wksp, GLPK.mpl_free_wksp)
return wksp
end
end
function mpl_free_wksp(tran::MathProgWorkspace)
if tran.p == C_NULL
return
end
@glpk_ccall mpl_free_wksp Void (Ptr{Void},) tran.p
tran.p = C_NULL
return
end
#}}}
## Check functions for internal use
#{{{
# Functions which perform all sorts of
# sanity checks on input parameters and
# throw exceptions in case of errors.
# Ideally, it should never be possible
# to pass an invalid parameter to the
# underlying glp API.
function check_prob(prob::Prob)
if prob.p == C_NULL
throw(Error("Invalid GLPK.Prob"))
end
return true
end
function check_string_length(s::String, minl::Integer, maxl::Integer)
l = length(s)
if !(minl <= l <= maxl)
throw(Error("Invalid string length $l (must be $minl <= length <= $maxl)"))
end
return true
end
function check_row_is_valid(prob::Prob, row::Integer)
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
if (row < 1 || row > rows)
throw(Error("Invalid row $row (must be 1 <= row <= $rows)"))
end
return true
end
function check_col_is_valid(prob::Prob, col::Integer)
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
if (col < 1 || col > cols)
throw(Error("Invalid col $col (must be 1 <= col <= $cols)"))
end
return true
end
function check_col_is_valid_w0(prob::Prob, col::Integer)
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
if (col < 0 || col > cols)
throw(Error("Invalid col $col (must be 0 <= col <= $cols)"))
end
return true
end
function check_obj_dir_is_valid(dir::Integer)
if !(dir == MIN || dir == MAX)
throw(Error("Invalid obj_dir $dir (use MIN or MAX)"))
end
return true
end
function check_bounds_type_is_valid(bounds_type::Integer)
if !(bounds_type == FR ||
bounds_type == LO ||
bounds_type == UP ||
bounds_type == DB ||
bounds_type == FX)
throw(Error("Invalid bounds_type $bounds_type (allowed values: GLPK.FR, GLPK.LO, GLPK.UP, GLPK.DB, GLPK.FX)"))
end
return true
end
function check_bounds_are_valid(bounds_type::Integer, lb::Real, ub::Real)
if bounds_type == DB && lb > ub
throw(Error("Invalid bounds for double-bounded variable: $lb > $ub"))
elseif bounds_type == FX && lb != ub
throw(Error("Invalid bounds for fixed variable: $lb != $ub"))
end
return true
end
function check_vectors_size(numel::Integer, vecs...)
if numel < 0
throw(Error("Invalid numer of elements: $numel"))
end
if numel > 0
for v = vecs
if isempty(v)
throw(Error("Number of elements is $numel but vector is empty or nothing"))
elseif length(v) < numel
throw(Error("Wrong vector size: $(length(v)) (numel declared as $numel)"))
end
end
end
return true
end
function check_vectors_all_same_size(vec0::VecOrNothing, vecs::VecOrNothing...)
l0 = vecornothing_length(vec0)
for v in vecs
l = vecornothing_length(v)
if l != l0
throw(Error("incosistent vector lengths: $l0 and $l"))
end
end
return true
end
function check_indices_vectors_dup(prob::Prob, numel::Integer, ia::Vector{Int32}, ja::Vector{Int32})
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
#numel = length(ia)
off32 = sizeof(Int32)
iap = pointer(ia) - off32
jap = pointer(ja) - off32
k = @glpk_ccall check_dup Int32 (Int32, Int32, Int32, Ptr{Int32}, Ptr{Int32}) rows cols numel iap jap
if k < 0
throw(Error("indices out of bounds: $(ia[-k]),$(ja[-k]) (bounds are (1,1) <= (ia,ja) <= ($rows,$cols))"))
elseif k > 0
throw(Error("duplicate index entry: $(ia[k]),$(ja[k])"))
end
return true
end
function check_rows_and_cols(rows::Integer, cols::Integer)
if (rows < 0)
throw(Error("rows < 0 : $rows"))
end
if (cols < 0)
throw(Error("cols < 0 : $rows"))
end
end
function check_rows_ids(prob::Prob, min_size::Integer, num_rows::Integer, rows_ids::Vector{Int32})
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
if num_rows < min_size || num_rows > rows
throw(Error("invalid vector size: $num_rows (min=$min_size max=$rows)"))
end
if num_rows == 0
return true
end
if length(rows_ids) < num_rows
throw(Error("invalid vector size: declared>=$num_rows actual=$(length(rows_ids))"))
end
ind_set = IntSet()
add_each(ind_set, rows_ids[1 : num_rows])
if min(ind_set) < 1 || max(ind_set) > rows
throw(Error("index out of bounds (min=1 max=$rows)"))
elseif length(ind_set) != length(rows_ids)
throw(Error("one or more duplicate index(es) found"))
end
return true
end
function check_cols_ids(prob::Prob, min_size::Integer, num_cols::Integer, cols_ids::Vector{Int32})
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
if num_cols < min_size || num_cols > cols
throw(Error("invalid vector size: $num_cols (min=$min_size max=$cols)"))
end
if num_cols == 0
return 0
end
if length(cols_ids) < num_cols
throw(Error("invalid vector size: declared>=$num_cols actual=$(length(cols_ids))"))
end
ind_set = IntSet()
add_each(ind_set, cols_ids[1 : num_cols])
if min(ind_set) < 1 || max(ind_set) > cols
throw(Error("index out of bounds (min=1 max=$cols)"))
elseif length(ind_set) != length(cols_ids)
throw(Error("one or more duplicate index(es) found"))
end
return true
end
function check_list_ids(prob::Prob, len::Integer, list_ids::Vector{Int32})
if len == 0
return true
end
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
# note1 the documentation does not mention forbidding duplicates in this case
# note2 the size should already be checked as this function is only called
# by GLPK.print_ranges
#if len < 0 #|| len > rows + cols
##throw(Error("invalid vector size: $len (min=0 max=$(rows + cols))"))
#throw(Error("invalid vector size: $len < 0"))
#end
#if length(list_ids) < len
#throw(Error("invalid vector size: declared>=$len actual=$(length(list_ids))"))
#end
if min(list_ids[1:len]) < 1 || max(list_ids[1:len]) > rows + cols
throw(Error("index out of bounds (min=1 max=$(rows + cols))"))
end
return true
end
function check_status_is_optimal(prob::Prob)
ret = @glpk_ccall get_status Int32 (Ptr{Void},) prob.p
if ret == OPT
throw(Error("current basic solution is not optimal"))
end
return true
end
function check_bf_exists(prob::Prob)
ret = @glpk_ccall bf_exists Int32 (Ptr{Void},) prob.p
if ret == 0
throw(Error("no bf solution found (use GLPK.factorize)"))
end
return true
end
function check_var_is_basic(prob::Prob, ind::Integer)
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
if ind <= rows
j = @glpk_ccall get_row_stat Int32 (Ptr{Void}, Int32) prob.p ind
if j != BS
throw(Error("variable $ind is non-basic"))
end
else
j = @glpk_ccall get_col_stat Int32 (Ptr{Void}, Int32) prob.p ind-rows
if j != BS
throw(Error("variable $ind is non-basic"))
end
end
end
function check_var_is_non_basic(prob::Prob, ind::Integer)
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
if ind <= rows
j = @glpk_ccall get_row_stat Int32 (Ptr{Void}, Int32) prob.p ind
if j == BS
throw(Error("variable $ind is basic"))
end
else
j = @glpk_ccall get_col_stat Int32 (Ptr{Void}, Int32) prob.p ind-rows
if j == BS
throw(Error("variable $ind is basic"))
end
end
end
function check_is_prim_feasible(prob::Prob)
if FEAS != @glpk_ccall get_prim_stat Int32 (Ptr{Void},) prob.p
throw(Error("problem is not primal feasible"))
end
return true
end
function check_is_dual_feasible(prob::Prob)
if FEAS != @glpk_ccall get_dual_stat Int32 (Ptr{Void},) prob.p
throw(Error("problem is not dual feasible"))
end
return true
end
function check_copy_names_flag(names::Integer)
if names != ON && names != OFF
throw(Error("invalid copy_names flag $names (use GLPK.ON or GLPK.OFF)"))
end
return true
end
function check_scale_flags(flags::Integer)
all = (SF_GM | SF_EQ | SF_2N | SF_SKIP)
if (flags | all) != all && flags != SF_AUTO
throw(Error("invalid scale flags $flags"))
end
return true
end
function check_stat_is_valid(stat::Integer)
if (stat != BS &&
stat != NL &&
stat != NU &&
stat != NF &&
stat != NS)
throw(Error("invalid status $stat (use GLPK.BS or GLPK.NL or GLPK.NU or GLPK.NF or GLPK.NS)"))
end
end
function check_adv_basis_flags(flags::Integer)
if flags != 0
throw(Error("adv_basis flags must be set to 0 (found $flags instead)"))
end
return true
end
function check_simplex_param(param::SimplexParam)
if pointer(param) == C_NULL
throw(Error("param = NULL"))
end
return true
end
function check_interior_param(param::InteriorParam)
if pointer(param) == C_NULL
throw(Error("param = NULL"))
end
return true
end
function check_kind_is_valid(kind::Integer)
if (kind != CV &&
kind != IV &&
kind != BV)
throw(Error("invalid kind $kind (use GLPK.CV or GLPK.IV or GLPK.BV)"))
end
return true
end
function check_intopt_param(param::IntoptParam)
if pointer(param) == C_NULL
throw(Error("param = NULL"))
end
return true
end
function check_file_is_readable(filename::String)
try
f = open(filename, "r")
close(f)
catch err
throw(Error("file $filename not readable"))
end
return true
end
function check_file_is_writable(filename::String)
try
f = open(filename, "w")
close(f)
catch err
throw(Error("file $filename not writable"))
end
return true
end
function check_mps_format(format::Integer)
if (format != MPS_DECK &&
format != MPS_FILE)
throw(Error("invalid MPS format $format (use GLPK.MPS_DECK or GLPK.MPS_FILE)"))
end
return true
end
function check_mps_param(param)
if param != C_NULL
throw(Error("MPS param must be C_NULL"))
end
return true
end
function check_lp_param(param)
if param != C_NULL
throw(Error("LP param must be C_NULL"))
end
return true
end
function check_read_prob_flags(flags::Integer)
if flags != 0
throw(Error("read_prob flags must be 0"))
end
return true
end
function check_write_prob_flags(flags::Integer)
if flags != 0
throw(Error("write_prob flags must be 0"))
end
return true
end
function check_print_ranges_flags(flags::Integer)
if flags != 0
throw(Error("print_ranges flags must be set to 0 (found $flags instead)"))
end
return true
end
function check_bfcp(param::BasisFactParam)
if pointer(param) == C_NULL
throw(Error("Invalid GLPK.BasisFactParam"))
end
return true
end
function check_data(data::Data)
if pointer(data) == C_NULL
throw(Error("Invalid GLPK.Data"))
end
return true
end
function check_mpl_workspace(tran::MathProgWorkspace)
if tran.p == C_NULL
throw(Error("Invalid GLPK.MathProgWorkspace"))
end
return true
end
function check_rowcol_is_valid(prob::Prob, k::Integer)
rows = @glpk_ccall get_num_rows Int32 (Ptr{Void},) prob.p
cols = @glpk_ccall get_num_cols Int32 (Ptr{Void},) prob.p
k_max = rows + cols
if !(1 <= k <= k_max)
throw(Error("index out of bounds: $k (bounds are 1 <= k <= $k_max"))
end
return true
end
function check_dir_is_valid(dir::Integer)
if !(dir == 1 || dir == -1)
throw(Error("invalid direction $dir (must be 1 or -1)"))
end
return true
end
function check_eps_is_valid(eps::Real)
if (eps < 0)
throw(Error("invalid eps $eps (must be >= 0)"))
end
return true
end
function check_init_env_succeeded(ret::Integer)
if !(0 <= ret <= 1)
throw(Error("initialization failed"))
end
return true
end
function check_term_out_flag(flag::Integer)
if !(flag == ON || flag == OFF)
throw(Error("invalid flag $flag (use GLPK.ON or GLPK.OFF)"))
end
return true
end
function check_open_tee_succeeded(ret::Integer)
if !(0 <= ret <= 1)
throw(Error("GLPK.open_tee failed"))
end
return true
end
function check_alloc_size(n::Integer)
if n <= 0
throw(Error("invalid alloc size $n"))
end
return true
end
function check_pointer_is_valid(ptr::Ptr)
if ptr == C_NULL
throw(Error("invalid pointer"))
end
return true
end
function check_sdf_file_opened(data_p::Ptr)
if data_p == C_NULL
throw(Error("GLPK.sdf_open_file failed"))
end
return true
end
#}}}
## GLP functions
#{{{
# The API interface is as close as possible to the original
# one.
# The general translation rules are:
#
# * function names tranlsate like this: glp_func -> GLPK.func
# * constant names tranlsate like this: GLPK_CONST -> GLPK.CONST
# * whenever the C library accepts NULL as argument,
# the Julia one will accept the nothing constant.
# * vectors do not need to have an extra element at the
# beginning to accomodate to the 1-based GLP indexing.
# * most functions will accept any kind of vectors as inputs,
# provided they can be converted to be C-compatible
# (i.e. to either Int32 (int) or Float64 (double) elements).
# * the exceptions to the above are those functions which write
# their output in a vector, in which case the vector type must
# be strictly C-compatible.
# * all char[] strings become Strings, both in inputs and in output.
#
# A single exception to the strict compatibility is GLPK.version(),
# which returns a tuple of integers in the form (major, minor)
# rather than a string.
function set_prob_name(prob::Prob, name::Union(String,Nothing))
check_prob(prob)
if is(name, nothing)
name = ""
end
check_string_length(name, 0, 255)
@glpk_ccall set_prob_name Void (Ptr{Void}, Ptr{Uint8}) prob.p bytestring(name)
end
function set_obj_name(prob::Prob, name::Union(String,Nothing))
check_prob(prob)
if is(name, nothing)
name = ""
end
check_string_length(name, 0, 255)
@glpk_ccall set_obj_name Void (Ptr{Void}, Ptr{Uint8}) prob.p bytestring(name)
end