/
GLPK.jl
2333 lines (2016 loc) · 65.1 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
## Exports
#{{{
export
# Types
Param,
GLPKError,
Prob,
SimplexParam,
InteriorParam,
IntoptParam,
BasisFactParam,
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,
check_kkt,
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,
ios_reason,
ios_get_prob,
ios_row_attr,
ios_mip_gap,
ios_node_data,
ios_select_node,
ios_heur_sol,
ios_can_branch,
ios_branch_upon,
ios_terminate,
ios_tree_size,
ios_curr_node,
ios_next_node,
ios_prev_node,
ios_up_node,
ios_node_level,
ios_node_bound,
ios_best_node,
ios_pool_size,
ios_add_row,
ios_del_row,
ios_clear_pool,
init_env,
free_env,
term_out,
open_tee,
close_tee,
malloc,
calloc,
free,
mem_usage,
mem_limit,
read_cnfsat,
check_cnfsat,
write_cnfsat,
minisat1,
intfeas1
#}}}
import Base.pointer, Base.setindex!, Base.getindex
## Shared library interface setup
#{{{
using BinDeps
@BinDeps.load_dependencies
include("GLPK_constants.jl")
# General recoverable exception: all GLPK functions
# throw this in case of recoverable errors
type GLPKError <: Exception
msg::String
end
# Fatal exception: when this is thrown, all GLPK
# objects are no longer valid
type GLPKFatalError <: Exception
msg::String
end
# Error hook, used to catch internal errors when calling
# GLPK functions
function _err_hook(info::Ptr{Void})
ccall((:glp_error_hook, libglpk), Void, (Ptr{Void}, Ptr{Void}), C_NULL, C_NULL)
ccall((:glp_free_env, libglpk), Void, ())
_del_all_objs()
throw(GLPKFatalError("GLPK call failed. All GLPK objects you defined so far are now invalidated."))
end
macro glpk_ccall(f, args...)
quote
ccall((:glp_error_hook, libglpk), Void, (Ptr{Void}, Ptr{Void}), cfunction(_err_hook, Void, (Ptr{Void},)), C_NULL)
ret = ccall(($"glp_$(f)", libglpk), $(args...))
ccall((:glp_error_hook, libglpk), Void, (Ptr{Void}, Ptr{Void}), C_NULL, C_NULL)
ret
end
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}, Csize_t), strp, csp, sizeof(Uint8))
if str[i] == '\0'
k = i
break
end
strp += sizeof(Uint8)
csp += sizeof(Uint8)
end
if k == 0
throw(GLPKError("error reading version"))
end
vstr = ASCIIString(str[1:k - 1])
return tuple(map(x->parseint(x), split(vstr, '.'))...)
end
if version() != (MAJOR_VERSION, MINOR_VERSION)
bv = version()
hv = (MAJOR_VERSION, MINOR_VERSION)
error("GLPK: mismatched versions: header=$(hv[1]).$(hv[2]) binary=$(bv[1]).$(bv[2])")
end
#}}}
## Preliminary definitions
#{{{
# General structure for the parameters types
abstract Param
function setindex!{T<:Param}(param::T, val, field_name::String)
s = symbol(field_name)
i = findfirst(x->x==s, T.names)
if i == 0
error("Parameter type $T has no field $field_name")
end
t = T.types[i]
param.(s) = convert(t,val)
end
function getindex(param::Param, field_name::String)
param.(symbol(field_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(GLPKError("integer-valued array required, or [] or nothing"))
end
elseif T <: Real
if !(eltype(a) <: Real)
throw(GLPKError("real-valued array required, or [] or nothing"))
end
end
convert(Array{T}, a)
end
vecornothing_length(a::VecOrNothing) = is(a, nothing) ? 0 : length(a)
#}}}
## 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_attr | GLPK.Attr |
# +-------------+--------------------------+
#
# In order to get/set the value of a Param field, no
# special syntax is required:
#
# lps_opts = GLPK.SimplexParam()
# lps_opts.msg_lev = GLPK.MSG_ERR
# lps_opts.presolve = GLPK.ON
#
# However, as a special case, the "type" field in
# glp_bfcp is renamed as "bftype" in GLPK.BasisFactParam:
#
# bf_opts = GLPK.BasisFactParam()
# bf_opts.bftype = ...
#
type Prob
p::Ptr{Void}
function Prob(p::Ptr{Void})
create = (p == C_NULL)
if create
p = @glpk_ccall create_prob Ptr{Void} ()
end
prob = new(p)
_add_obj(prob)
if create
finalizer(prob, delete_prob)
else
finalizer(prob, _del_obj)
end
return prob
end
end
Prob() = Prob(C_NULL)
function delete_prob(prob::Prob)
if prob.p == C_NULL
return
end
if jl_obj_is_valid(prob)
@glpk_ccall delete_prob Void (Ptr{Void},) prob.p
_del_obj(prob)
end
prob.p = C_NULL
return
end
type SimplexParam <: Param
msg_lev::Cint
meth::Cint
pricing::Cint
r_test::Cint
tol_bnd::Cdouble
tol_dj::Cdouble
tol_piv::Cdouble
obj_ll::Cdouble
obj_ul::Cdouble
it_lim::Cint
tm_lim::Cint
out_frq::Cint
out_dly::Cint
presolve::Cint
_reserved01::Cdouble
_reserved02::Cdouble
_reserved03::Cdouble
_reserved04::Cdouble
_reserved05::Cdouble
_reserved06::Cdouble
_reserved07::Cdouble
_reserved08::Cdouble
_reserved09::Cdouble
_reserved10::Cdouble
_reserved11::Cdouble
_reserved12::Cdouble
_reserved13::Cdouble
_reserved14::Cdouble
_reserved15::Cdouble
_reserved16::Cdouble
_reserved17::Cdouble
_reserved18::Cdouble
_reserved19::Cdouble
_reserved20::Cdouble
_reserved21::Cdouble
_reserved22::Cdouble
_reserved23::Cdouble
_reserved24::Cdouble
_reserved25::Cdouble
_reserved26::Cdouble
_reserved27::Cdouble
_reserved28::Cdouble
_reserved29::Cdouble
_reserved30::Cdouble
_reserved31::Cdouble
_reserved32::Cdouble
_reserved33::Cdouble
_reserved34::Cdouble
_reserved35::Cdouble
_reserved36::Cdouble
function SimplexParam()
p = new()
@glpk_ccall "init_smcp" Void (Ptr{SimplexParam},) &p
return p
end
end
type InteriorParam <: Param
msg_lev::Cint
ord_alg::Cint
_reserved01::Cdouble
_reserved02::Cdouble
_reserved03::Cdouble
_reserved04::Cdouble
_reserved05::Cdouble
_reserved06::Cdouble
_reserved07::Cdouble
_reserved08::Cdouble
_reserved09::Cdouble
_reserved10::Cdouble
_reserved11::Cdouble
_reserved12::Cdouble
_reserved13::Cdouble
_reserved14::Cdouble
_reserved15::Cdouble
_reserved16::Cdouble
_reserved17::Cdouble
_reserved18::Cdouble
_reserved19::Cdouble
_reserved20::Cdouble
_reserved21::Cdouble
_reserved22::Cdouble
_reserved23::Cdouble
_reserved24::Cdouble
_reserved25::Cdouble
_reserved26::Cdouble
_reserved27::Cdouble
_reserved28::Cdouble
_reserved29::Cdouble
_reserved30::Cdouble
_reserved31::Cdouble
_reserved32::Cdouble
_reserved33::Cdouble
_reserved34::Cdouble
_reserved35::Cdouble
_reserved36::Cdouble
_reserved37::Cdouble
_reserved38::Cdouble
_reserved39::Cdouble
_reserved40::Cdouble
_reserved41::Cdouble
_reserved42::Cdouble
_reserved43::Cdouble
_reserved44::Cdouble
_reserved45::Cdouble
_reserved46::Cdouble
_reserved47::Cdouble
_reserved48::Cdouble
function InteriorParam()
p = new()
@glpk_ccall "init_iptcp" Void (Ptr{InteriorParam},) &p
return p
end
end
type IntoptParam <: Param
msg_lev::Cint
br_tech::Cint
bt_tech::Cint
tol_int::Cdouble
tol_obj::Cdouble
tm_lim::Cint
out_frq::Cint
out_dly::Cint
cb_func::Ptr{Void}
cb_info::Ptr{Void}
cb_size::Cint
pp_tech::Cint
mip_gap::Cdouble
mir_cuts::Cint
gmi_cuts::Cint
cov_cuts::Cint
clq_cuts::Cint
presolve::Cint
binarize::Cint
fp_heur::Cint
ps_heur::Cint
ps_tm_lim::Cint
use_sol::Cint
save_sol::Ptr{Cchar}
alien::Cint
_reserved01::Cdouble
_reserved02::Cdouble
_reserved03::Cdouble
_reserved04::Cdouble
_reserved05::Cdouble
_reserved06::Cdouble
_reserved07::Cdouble
_reserved08::Cdouble
_reserved09::Cdouble
_reserved10::Cdouble
_reserved11::Cdouble
_reserved12::Cdouble
_reserved13::Cdouble
_reserved14::Cdouble
_reserved15::Cdouble
_reserved16::Cdouble
_reserved17::Cdouble
_reserved18::Cdouble
_reserved19::Cdouble
_reserved20::Cdouble
_reserved21::Cdouble
_reserved22::Cdouble
_reserved23::Cdouble
_reserved24::Cdouble
_reserved25::Cdouble
function IntoptParam()
p = new()
@glpk_ccall "init_iocp" Void (Ptr{IntoptParam},) &p
return p
end
end
type BasisFactParam <: Param
msg_lev::Cint
bftype::Cint # NOTE: changed type->bftype
lu_size::Cint
piv_tol::Cdouble
piv_lim::Cint
suhl::Cint
eps_tol::Cdouble
max_gro::Cdouble
nfs_max::Cint
upd_tol::Cdouble
nrs_max::Cint
rs_size::Cint
_reserved01::Cdouble
_reserved02::Cdouble
_reserved03::Cdouble
_reserved04::Cdouble
_reserved05::Cdouble
_reserved06::Cdouble
_reserved07::Cdouble
_reserved08::Cdouble
_reserved09::Cdouble
_reserved10::Cdouble
_reserved11::Cdouble
_reserved12::Cdouble
_reserved13::Cdouble
_reserved14::Cdouble
_reserved15::Cdouble
_reserved16::Cdouble
_reserved17::Cdouble
_reserved18::Cdouble
_reserved19::Cdouble
_reserved20::Cdouble
_reserved21::Cdouble
_reserved22::Cdouble
_reserved23::Cdouble
_reserved24::Cdouble
_reserved25::Cdouble
_reserved26::Cdouble
_reserved27::Cdouble
_reserved28::Cdouble
_reserved29::Cdouble
_reserved30::Cdouble
_reserved31::Cdouble
_reserved32::Cdouble
_reserved33::Cdouble
_reserved34::Cdouble
_reserved35::Cdouble
_reserved36::Cdouble
_reserved37::Cdouble
_reserved38::Cdouble
function BasisFactParam()
return new(0, 0, 0, 0.0, 0, 0, 0.0, 0.0, 0, 0.0, 0, 0)
end
end
type MathProgWorkspace
p::Ptr{Void}
function MathProgWorkspace()
tran = @glpk_ccall mpl_alloc_wksp Ptr{Void} ()
wksp = new(tran)
_add_obj(wksp)
finalizer(wksp, GLPK.mpl_free_wksp)
return wksp
end
end
function mpl_free_wksp(tran::MathProgWorkspace)
if tran.p == C_NULL
return
end
if jl_obj_is_valid(tran)
@glpk_ccall mpl_free_wksp Void (Ptr{Void},) tran.p
_del_obj(tran)
end
tran.p = C_NULL
return
end
type Attr
level::Cint
origin::Cint
klass::Cint
_reserved01::Cdouble
_reserved02::Cdouble
_reserved03::Cdouble
_reserved04::Cdouble
_reserved05::Cdouble
_reserved06::Cdouble
_reserved07::Cdouble
function Attr()
new(0, 0, 0)
end
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 Cint or Cdouble 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.
include("GLPK_checks.jl")
function set_prob_name(prob::Prob, name::Union(String,Nothing))
@check! _prob(prob)
if is(name, nothing)
name = ""
end
@check _string_length(name, 255)
@glpk_ccall set_prob_name Void (Ptr{Void}, Ptr{Cchar}) 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, 255)
@glpk_ccall set_obj_name Void (Ptr{Void}, Ptr{Cchar}) prob.p bytestring(name)
end
function set_row_name(prob::Prob, row::Integer, name::Union(String,Nothing))
@check! _prob(prob)
@check _row_is_valid(prob, row)
if is(name, nothing)
name = ""
end
@check _string_length(name, 255)
@glpk_ccall set_row_name Void (Ptr{Void}, Cint, Ptr{Cchar}) prob.p row bytestring(name)
end
function set_col_name(prob::Prob, col::Integer, name::Union(String,Nothing))
@check! _prob(prob)
@check _col_is_valid(prob, col)
if is(name, nothing)
name = ""
end
@check _string_length(name, 255)
@glpk_ccall set_col_name Void (Ptr{Void}, Cint, Ptr{Cchar}) prob.p col bytestring(name)
end
function set_obj_dir(prob::Prob, dir::Integer)
@check! _prob(prob)
@check _obj_dir_is_valid(dir)
@glpk_ccall set_obj_dir Void (Ptr{Void}, Cint) prob.p dir
end
function add_rows(prob::Prob, rows::Integer)
@check! _prob(prob)
@glpk_ccall add_rows Cint (Ptr{Void}, Cint) prob.p rows
end
function add_cols(prob::Prob, cols::Integer)
@check! _prob(prob)
@glpk_ccall add_cols Cint (Ptr{Void}, Cint) prob.p cols
end
function set_row_bnds(prob::Prob, row::Integer, bounds_type::Integer, lb::Real, ub::Real)
@check! _prob(prob)
@check _row_is_valid(prob, row)
@check _bounds_type_is_valid(bounds_type)
@check _bounds_are_valid(bounds_type, lb, ub)
@glpk_ccall set_row_bnds Void (Ptr{Void}, Cint, Cint, Cdouble, Cdouble) prob.p row bounds_type lb ub
end
function set_col_bnds(prob::Prob, col::Integer, bounds_type::Integer, lb::Real, ub::Real)
@check! _prob(prob)
@check _col_is_valid(prob, col)
@check _bounds_type_is_valid(bounds_type)
@check _bounds_are_valid(bounds_type, lb, ub)
@glpk_ccall set_col_bnds Void (Ptr{Void}, Cint, Cint, Cdouble, Cdouble) prob.p col bounds_type lb ub
end
function set_obj_coef(prob::Prob, col::Integer, coef::Real)
@check! _prob(prob)
@check _col_is_valid_w0(prob, col)
@glpk_ccall set_obj_coef Void (Ptr{Void}, Cint, Cdouble) prob.p col coef
end
function set_mat_row{Ti<:Integer, Tv<:Real}(prob::Prob, row::Integer, len::Integer, ind::Vector{Ti}, val::Vector{Tv})
@check! _prob(prob)
@check! _vectors_size(len, ind, val)
@check _row_is_valid(prob, row)
if len > 0
ind32 = convert(Vector{Cint}, ind)
val64 = convert(Vector{Cdouble}, val)
off32 = sizeof(Cint)
off64 = sizeof(Cdouble)
ind32p = pointer(ind32) - off32
val64p = pointer(val64) - off64
@check! _cols_ids_size(prob, 0, len, ind32)
@check _cols_ids_content(prob, len, ind32)
else
ind32p = C_NULL
val64p = C_NULL
end
@glpk_ccall set_mat_row Void (Ptr{Void}, Cint, Cint, Ptr{Cint}, Ptr{Cdouble}) prob.p row len ind32p val64p
end
function set_mat_row(prob::Prob, row::Integer, len::Integer, ind::VecOrNothing, val::VecOrNothing)
ind = convert_vecornothing(Cint, ind)
val = convert_vecornothing(Cdouble, ar)
set_mat_row(prob, row, len, ind, val)
end
function set_mat_row(prob::Prob, row::Integer, ind::VecOrNothing, val::VecOrNothing)
@check! _vectors_all_same_size(ind, val)
l = vecornothing_length(ind)
set_mat_row(prob, row, l, ind, val)
end
function set_mat_col{Ti<:Integer, Tv<:Real}(prob::Prob, col::Integer, len::Integer, ind::Vector{Ti}, val::Vector{Tv})
@check! _prob(prob)
@check! _vectors_size(len, ind, val)
@check _col_is_valid(prob, col)
if len > 0
ind32 = convert(Vector{Cint}, ind)
val64 = convert(Vector{Cdouble}, val)
off32 = sizeof(Cint)
off64 = sizeof(Cdouble)
ind32p = pointer(ind32) - off32
val64p = pointer(val64) - off64
@check! _rows_ids_size(prob, 0, len, ind32)
@check _rows_ids_content(prob, len, ind32)
else
ind32p = C_NULL
val64p = C_NULL
end
@glpk_ccall set_mat_col Void (Ptr{Void}, Cint, Cint, Ptr{Cint}, Ptr{Cdouble}) prob.p col len ind32p val64p
end
function set_mat_col(prob::Prob, col::Integer, len::Integer, ind::VecOrNothing, val::VecOrNothing)
ind = convert_vecornothing(Cint, ind)
val = convert_vecornothing(Cdouble, ar)
set_mat_col(prob, col, len, ind, val)
end
function set_mat_col(prob::Prob, col::Integer, ind::VecOrNothing, val::VecOrNothing)
@check! _vectors_all_same_size(ind, val)
l = vecornothing_length(ind)
set_mat_col(prob, col, l, ind, val)
end
function load_matrix{Ti<:Integer, Tv<:Real}(prob::Prob, numel::Integer, ia::Vector{Ti}, ja::Vector{Ti}, ar::Vector{Tv})
@check! _prob(prob)
@check! _vectors_size(numel, ia, ja, ar)
if numel == 0
return
end
ia32 = convert(Vector{Cint}, ia)
ja32 = convert(Vector{Cint}, ja)
ar64 = convert(Vector{Cdouble}, ar)
@check _indices_vectors_dup(prob, numel, ia32, ja32)
off32 = sizeof(Cint)
off64 = sizeof(Cdouble)
ia32p = pointer(ia32) - off32
ja32p = pointer(ja32) - off32
ar64p = pointer(ar64) - off64
@glpk_ccall load_matrix Void (Ptr{Void}, Cint, Ptr{Cint}, Ptr{Cint}, Ptr{Cdouble}) prob.p numel ia32p ja32p ar64p
end
function load_matrix(prob::Prob, numel::Integer, ia::VecOrNothing, ja::VecOrNothing, ar::VecOrNothing)
ia = convert_vecornothing(Cint, ia)
ja = convert_vecornothing(Cint, ja)
ar = convert_vecornothing(Cdouble, ar)
load_matrix(prob, numel, ia, ja, ar)
end
function load_matrix(prob::Prob, ia::VecOrNothing, ja::VecOrNothing, ar::VecOrNothing)
@check! _vectors_all_same_size(ia, ja, ar)
l = vecornothing_length(ar)
load_matrix(prob, l, ia, ja, ar)
end
function load_matrix{Ti<:Integer, Tv<:Real}(prob::Prob, a::SparseMatrixCSC{Tv, Ti})
(ia, ja, ar) = findnz(a)
load_matrix(prob, ia, ja, ar)
end
function check_dup{Ti<:Integer}(rows::Integer, cols::Integer, numel::Integer, ia::Vector{Ti}, ja::Vector{Ti})
@check _rows_and_cols(rows, cols)
@check! _vectors_size(numel, ia, ja)
ia32 = convert(Vector{Cint}, ia)
ja32 = convert(Vector{Cint}, ja)
off32 = sizeof(Cint)
ia32p = pointer(ia32) - off32
ja32p = pointer(ja32) - off32
@glpk_ccall check_dup Cint (Cint, Cint, Cint, Ptr{Cint}, Ptr{Cint}) rows cols numel ia32p ja32p
end
function check_dup(rows::Integer, cols::Integer, numel::Integer, ia::VecOrNothing, ja::VecOrNothing)
ia = convert_vecornothing(Cint, ia)
ja = convert_vecornothing(Cint, ja)
check_dup(rows, cols, numel, ia, ja)
end
function check_dup(rows::Integer, cols::Integer, ia::VecOrNothing, ja::VecOrNothing)
@check! _vectors_all_same_size(ia, ja)
l = vecornothing_length(ia)
check_dup(rows, cols, l, ia, ja)
end
function sort_matrix(prob::Prob)
@check! _prob(prob)
@glpk_ccall sort_matrix Void (Ptr{Void},) prob.p
end
function del_rows{Ti<:Integer}(prob::Prob, num_rows::Integer, rows_ids::AbstractVector{Ti})
@check! _prob(prob)
rows_ids32 = convert(Vector{Cint}, rows_ids)
@check! _rows_ids_size(prob, 1, num_rows, rows_ids32)
@check _rows_ids_content(prob, num_rows, rows_ids32)
off32 = sizeof(Cint)
rows_ids32p = pointer(rows_ids32) - off32
@glpk_ccall del_rows Void (Ptr{Void}, Cint, Ptr{Cint}) prob.p num_rows rows_ids32p
end
del_rows{Ti<:Integer}(prob::Prob, rows_ids::AbstractVector{Ti}) =
del_rows(prob, length(rows_ids), rows_ids)
function del_cols{Ti<:Integer}(prob::Prob, num_cols::Integer, cols_ids::AbstractVector{Ti})
@check! _prob(prob)
cols_ids32 = convert(Vector{Cint}, cols_ids)
@check! _cols_ids_size(prob, 1, num_cols, cols_ids32)
@check _cols_ids_content(prob, num_cols, cols_ids32)
off32 = sizeof(Cint)
cols_ids32p = pointer(cols_ids32) - off32
@glpk_ccall del_cols Void (Ptr{Void}, Cint, Ptr{Cint}) prob.p num_cols cols_ids32p
end
del_cols{Ti<:Integer}(prob::Prob, cols_ids::AbstractVector{Ti}) =
del_cols(prob, length(cols_ids), cols_ids)
function copy_prob(prob_dest::Prob, prob::Prob, copy_names::Integer)
@check! _prob(prob)
@check _copy_names_flag(copy_names)
@glpk_ccall copy_prob Void (Ptr{Void}, Ptr{Void}, Cint) prob_dest.p prob.p copy_names
end
function erase_prob(prob::Prob)
@check! _prob(prob)
@glpk_ccall erase_prob Void (Ptr{Void},) prob.p
end
function get_prob_name(prob::Prob)
@check! _prob(prob)
name_cstr = @glpk_ccall get_prob_name Ptr{Cchar} (Ptr{Void},) prob.p
if name_cstr == C_NULL
return ""
else
return bytestring(name_cstr)
end
end
function get_obj_name(prob::Prob)
@check! _prob(prob)
name_cstr = @glpk_ccall get_obj_name Ptr{Cchar} (Ptr{Void},) prob.p
if name_cstr == C_NULL
return ""
else
return bytestring(name_cstr)
end
end
function get_obj_dir(prob::Prob)
@check! _prob(prob)
@glpk_ccall get_obj_dir Cint (Ptr{Void},) prob.p
end
function get_num_rows(prob::Prob)
@check! _prob(prob)
@glpk_ccall get_num_rows Cint (Ptr{Void},) prob.p
end
function get_num_cols(prob::Prob)
@check! _prob(prob)
@glpk_ccall get_num_cols Cint (Ptr{Void},) prob.p
end
function get_row_name(prob::Prob, row::Integer)
@check! _prob(prob)
@check _row_is_valid(prob, row)
name_cstr = @glpk_ccall get_row_name Ptr{Cchar} (Ptr{Void}, Cint) prob.p row
if name_cstr == C_NULL
return ""
else
return bytestring(name_cstr)
end
end
function get_col_name(prob::Prob, col::Integer)
@check! _prob(prob)
@check _col_is_valid(prob, col)
name_cstr = @glpk_ccall get_col_name Ptr{Cchar} (Ptr{Void}, Cint) prob.p col
if name_cstr == C_NULL
return ""
else
return bytestring(name_cstr)
end
end
function get_row_type(prob::Prob, row::Integer)
@check! _prob(prob)
@check _row_is_valid(prob, row)
@glpk_ccall get_row_type Cint (Ptr{Void}, Cint) prob.p row
end
function get_row_lb(prob::Prob, row::Integer)
@check! _prob(prob)
@check _row_is_valid(prob, row)
@glpk_ccall get_row_lb Cdouble (Ptr{Void}, Cint) prob.p row
end
function get_row_ub(prob::Prob, row::Integer)
@check! _prob(prob)
@check _row_is_valid(prob, row)
@glpk_ccall get_row_ub Cdouble (Ptr{Void}, Cint) prob.p row
end
function get_col_type(prob::Prob, col::Integer)