-
Notifications
You must be signed in to change notification settings - Fork 29
/
MosekLPQCQPInterface.jl
1253 lines (1055 loc) · 46.8 KB
/
MosekLPQCQPInterface.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
type MosekLinearQuadraticModel <: MathProgBase.AbstractLinearQuadraticModel
task :: Mosek.MSKtask
binvarflags:: Array{Bool,1}
# NOTE: bkx/blx/bux are the bound on the variable in the
# continuous problem. Setting a variable to :Bin, will not change
# these. Setting :Cont or :Int on a :Bin variable will effectively
# revert to the original bounds.
numvar :: Int
numcon :: Int
bkx :: Array{Int32,1}
blx :: Array{Float64,1}
bux :: Array{Float64,1}
bkc :: Array{Int32,1}
blc :: Array{Float64,1}
buc :: Array{Float64,1}
lincon :: Array{Int32,1}
quadcon :: Array{Int32,1}
options
end
type MosekNonlinearModel <: MathProgBase.AbstractLinearQuadraticModel
m :: MosekLinearQuadraticModel
end
MathProgBase.LinearQuadraticModel(s::MosekSolver) =
MosekLinearQuadraticModel(Mosek.maketask(),
Array(Bool,0),
0,
0,
Array(Int32,0),
Array(Float64,0),
Array(Float64,0),
Array(Int32,0),
Array(Float64,0),
Array(Float64,0),
Array(Int32,0),
Array(Int32,0),
s.options)
##############################################################
## Linear
##############################################################
function MathProgBase.loadproblem!{T1,T2,T3,T4,T5}(m::MosekLinearQuadraticModel,
A,
collb :: Array{T1,1},
colub :: Array{T2,1},
obj :: Array{T3,1},
rowlb :: Array{T4,1},
rowub :: Array{T5,1},
sense:: Symbol)
MathProgBase.loadproblem!(m,
convert(SparseMatrixCSC{Float64,Int},A),
convert(Array{Float64,1},collb),
convert(Array{Float64,1},colub),
convert(Array{Float64,1},obj),
convert(Array{Float64,1},rowlb),
convert(Array{Float64,1},rowub),
sense)
end
function MathProgBase.loadproblem!(m::MosekLinearQuadraticModel,
A:: SparseMatrixCSC{Float64,Int},
collb:: Array{Float64,1},
colub:: Array{Float64,1},
obj:: Array{Float64,1},
rowlb:: Array{Float64,1},
rowub:: Array{Float64,1},
sense:: Symbol)
Mosek.resizetask(m.task,0,0,0,0,0);
nrows,ncols = size(A)
if ncols != length(collb) ||
ncols != length(colub) ||
ncols != size(obj,1) ||
nrows != length(rowlb) ||
nrows != length(rowub) ||
ncols != length(obj)
throw(MosekMathProgModelError("Inconsistent data dimensions"))
end
Mosek.appendvars(m.task,ncols)
Mosek.appendcons(m.task,nrows)
(m.bkx,m.blx,m.bux) = makebounds(collb,colub)
(m.bkc,m.blc,m.buc) = makebounds(rowlb,rowub)
m.numvar = length(m.bkx)
m.numcon = length(m.bkc)
m.lincon = Int32[1:nrows;]
m.quadcon = Array(Int32,0)
m.binvarflags = fill(false,m.numvar)
# input coefficients
Mosek.putclist(m.task, Int32[1:ncols;], obj)
Mosek.putacolslice(m.task, 1, ncols+1, A.colptr[1:ncols], A.colptr[2:ncols+1], A.rowval, A.nzval)
MathProgBase.setsense!(m, sense)
# input bounds
Mosek.putvarboundslice(m.task, 1, ncols+1, m.bkx, m.blx, m.bux)
Mosek.putconboundslice(m.task, 1, nrows+1, m.bkc, m.blc, m.buc)
m
end
function MathProgBase.loadproblem!(m::MosekLinearQuadraticModel,
filename:: AbstractString)
tmptask = Mosek.maketask()
try
readdata(tmptask, filename)
if Mosek.getnumcone(tmptask) > 0 ||
Mosek.getnumbarvar(tmptask) > 0
throw(MosekMathProgModelError("Not a linear/quadratic model"))
end
numcon = Mosek.getnumcon(tmptask)
numvar = Mosek.getnumvar(tmptask)
bkx,blx,bux = Mosek.getvarboundslice(tmptask,1,numvar+1)
bkc,blc,buc = Mosek.getconboundslice(tmptask,1,numcon+1)
vts = Mosek.getvartypelist(tmptask,Int32[1:numvar])
binflags = Bool[ (vts[i] == Mosek.MSK_VARIABLE_TYPE_INT &&
bkx[i] == Mosek.MSK_BK_RA &&
abs(blx[i]) < 1e-8 &&
abs(bux[i]-1.0) < 1e-8)
for i in 1:numvar ]
lincon = find(i -> Mosek.getnumqconknz(m.task,i) == 0, 1:numcon)
quadcon = find(i -> Mosek.getnumqconknz(m.task,i) > 0, 1:numcon)
Mosek.deletetask(m.task)
m.task = tmptask
m.binvaflags = binflags
m.bkx = bkx
m.blx = blx
m.bux = bux
m.bkc = bkc
m.blc = blc
m.buc = buc
m.numvar = lenght(bkx)
m.numcon = length(bkc)
m.lincon = lincon
m.quadcon = quadcon
catch
Mosek.deletetask(tmptask)
rethrow()
end
m
end
function loadoptions!(m::MosekLinearQuadraticModel)
loadoptions_internal!(m.task, m.options)
end
function MathProgBase.writeproblem(m::MosekLinearQuadraticModel, filename::AbstractString)
Mosek.writedata(m.task,filename)
end
function MathProgBase.getvarLB(m::MosekLinearQuadraticModel)
bk,bu,bl = Mosek.getvarboundslice(m.task,1,m.numvar+1)
for i in 1:length(bk)
if bk == Mosek.MSK_BK_FR || bk == Mosek.MSK_BK_UP
bl[i] = -Inf
end
end
bl
end
function MathProgBase.getvarUB(m::MosekLinearQuadraticModel)
bk,bu,bl = Mosek.getvarboundslice(m.task,1,m.numvar+1)
for i in 1:length(bk)
if bk == Mosek.MSK_BK_FR || bk == Mosek.MSK_BK_LO
bu[i] = Inf
end
end
bu
end
function MathProgBase.getconstrLB(m::MosekLinearQuadraticModel)
bk,bu,bl = Mosek.getconboundslice(m.task,1,m.numcon+1)
for i in 1:length(bk)
if bk == Mosek.MSK_BK_FR || bk == Mosek.MSK_BK_UP
bl[i] = -Inf
end
end
bl
end
function MathProgBase.getconstrUB(m::MosekLinearQuadraticModel)
bk,bu,bl = Mosek.getconboundslice(m.task,1,m.numcon+1)
for i in 1:length(bk)
if bk == Mosek.MSK_BK_FR || bk == Mosek.MSK_BK_LO
bu[i] = Inf
end
end
bu
end
MathProgBase.setvarLB!{T}(m::MosekLinearQuadraticModel, bnd::Array{T,1}) = MathProgBase.setvarLB!(m,convert(Array{Float64,1},bnd))
function MathProgBase.setvarLB!(m::MosekLinearQuadraticModel, bnd::Array{Float64,1})
n = min(length(bnd),m.numvar)
for i in 1:n
if bnd[i] > -Inf
if m.bux[i] < Inf
if abs(bnd[i]-m.bux[i]) < 1e-8
m.bkx[i] = Mosek.MSK_BK_FX
m.blx[i] = m.bux[i]
m.bux[i] = m.bux[i]
else
m.bkx[i] = Mosek.MSK_BK_RA
m.blx[i] = bnd[i]
end
else # bux[i] == Inf
m.bkx[i] = Mosek.MSK_BK_LO
m.blx[i] = bnd[i]
end
else # bnd[i] == -Inf
if m.bux[i] < Inf
m.bkx[i] = Mosek.MSK_BK_UP
else
m.bkx[i] = Mosek.MSK_BK_FR
end
m.blx[i] = -Inf
end
end
Mosek.putvarboundslice(m.task,1,n+1,m.bkx,m.blx,m.bux)
if any(m.binvarflags)
idxs = convert(Array{Int32,1},find(v->v, m.binvarflags))
bkx = Int32[ Mosek.MSK_BK_RA for i in 1:length(idxs)]
blx = Float64[ max(m.blx[i],0.0) for i in idxs ]
bux = Float64[ min(m.bux[i],1.0) for i in idxs ]
Mosek.putvarboundlist(m.task,idxs, bkx,blx,bux)
end
nothing
end
MathProgBase.setvarUB!{T}(m::MosekLinearQuadraticModel, bnd::Array{T,1}) = MathProgBase.setvarUB!(m,convert(Array{Float64,1},bnd))
function MathProgBase.setvarUB!(m::MosekLinearQuadraticModel, bnd::Array{Float64,1})
n = min(length(bnd),m.numvar)
for i in 1:n
if bnd[i] < Inf
if m.blx[i] > -Inf
if abs(bnd[i]-m.blx[i]) < 1e-8
m.bkx[i] = Mosek.MSK_BK_FX
m.blx[i] = m.blx[i]
m.bux[i] = m.blx[i]
else
m.bkx[i] = Mosek.MSK_BK_RA
m.bux[i] = bnd[i]
end
else # blx[i] == -Inf
m.bkx[i] = Mosek.MSK_BK_UP
m.bux[i] = bnd[i]
end
else # bnd[i] == Inf
if m.blx[i] > -Inf
m.bkx[i] = Mosek.MSK_BK_LO
else
m.bkx[i] = Mosek.MSK_BK_FR
end
m.bux[i] = Inf
end
end
Mosek.putvarboundslice(m.task,1,n+1,m.bkx,m.blx,m.bux)
if any(m.binvarflags)
idxs = convert(Array{Int32,1},find(v->v, m.binvarflags))
bkx = Int32[ Mosek.MSK_BK_RA for i in 1:length(idxs)]
blx = Float64[ max(m.blx[i],0.0) for i in idxs ]
bux = Float64[ min(m.bux[i],1.0) for i in idxs ]
Mosek.putvarboundlist(m.task,idxs, bkx,blx,bux)
end
nothing
end
MathProgBase.setconstrLB!{T}(m::MosekLinearQuadraticModel, bnd::Array{T,1}) = MathProgBase.setconstrLB!(m,convert(Array{Float64,1},bnd))
function MathProgBase.setconstrLB!(m::MosekLinearQuadraticModel, bnd::Array{Float64,1})
n = min(length(bnd),length(m.lincon))
for i in 1:n
if bnd[i] > -Inf
if m.buc[i] < Inf
if abs(bnd[i]-m.buc[i]) < 1e-8
m.bkc[i] = Mosek.MSK_BK_FX
m.blc[i] = m.buc[i]
m.buc[i] = m.buc[i]
else
m.bkc[i] = Mosek.MSK_BK_RA
m.blc[i] = bnd[i]
end
else # buc[i] == Inf
m.bkc[i] = Mosek.MSK_BK_LO
m.blc[i] = bnd[i]
end
else # bnd[i] == -Inf
if m.buc[i] < Inf
m.bkc[i] = Mosek.MSK_BK_UP
else
m.bkc[i] = Mosek.MSK_BK_FR
end
m.blc[i] = -Inf
end
end
Mosek.putconboundslice(m.task,1,n+1,m.bkc,m.blc,m.buc)
nothing
end
MathProgBase.setconstrUB!{T}(m::MosekLinearQuadraticModel, bnd::Array{T,1}) = MathProgBase.setconstrUB!(m,convert(Array{Float64,1},bnd))
function MathProgBase.setconstrUB!(m::MosekLinearQuadraticModel, bnd::Array{Float64,1})
n = min(length(bnd),m.numcon)
for i in 1:n
if bnd[i] < Inf
if m.blc[m.lincon[i]] > -Inf
if abs(bnd[i]-m.blc[m.lincon[i]]) < 1e-8
m.bkc[m.lincon[i]] = Mosek.MSK_BK_FX
m.blc[m.lincon[i]] = m.blc[i]
m.buc[m.lincon[i]] = m.blc[i]
else
m.bkc[m.lincon[i]] = Mosek.MSK_BK_RA
m.buc[m.lincon[i]] = bnd[i]
end
else # blc[i] == -Inf
m.bkc[m.lincon[i]] = Mosek.MSK_BK_UP
m.buc[m.lincon[i]] = bnd[i]
end
else # bnd[i] == Inf
if m.blc[m.lincon[i]] > -Inf
m.bkc[m.lincon[i]] = Mosek.MSK_BK_LO
else
m.bkc[m.lincon[i]] = Mosek.MSK_BK_FR
end
m.blc[m.lincon[i]] = -Inf
end
end
Mosek.putconboundlist(m.task,m.lincon[1:n],m.bkc,m.blc,m.buc)
nothing
end
function MathProgBase.getobj(m::MosekLinearQuadraticModel)
Mosek.getcslice(m.task,1,m.numvar+1)
end
MathProgBase.setobj!{T}(m::MosekLinearQuadraticModel, c::Array{T,1}) = MathProgBase.setobj!(m,convert(Array{Float64,1},c))
function MathProgBase.setobj!(m::MosekLinearQuadraticModel, c :: Array{Float64,1})
n = min(length(c),m.numvar)
Mosek.putclist(m.task,Int32[1:n;],c[1:n])
end
function MathProgBase.getconstrmatrix(m::MosekLinearQuadraticModel)
numnz = sum(Int[ Mosek.getarownumnz(m.task,i) for i in 1:m.numcon ])
asubi = Array(Int32,numnz)
asubj = Array(Int32,numnz)
aval = Array(Float64,numnz)
let ptr = 1
for i in 1:m.numcon
subj,valj = Mosek.getarow(m.task,i)
n = length(subj)
asubi[ptr:ptr+n-1] = i
asubj[ptr:ptr+n-1] = subj
aval[ptr:ptr+n-1] = valj
ptr += n
end
end
sparse(asubi,asubj,aval,length(m.lincon),m.numvar)
end
MathProgBase.addvar!(m::MosekLinearQuadraticModel, bl, bu, c) = MathProgBase.addvar!(m,convert(Float64,bl),convert(Float64,bu),convert(Float64,c))
function MathProgBase.addvar!(m::MosekLinearQuadraticModel,
bl ::Float64,
bu ::Float64,
c ::Float64)
m.numvar += 1
if bl > -Inf
if bu < Inf
if abs(bl-bu) < 1e-8
push!(m.bkx,Mosek.MSK_BK_FX)
push!(m.blx,bl)
push!(m.bux,bl)
else
push!(m.bkx,Mosek.MSK_BK_RA)
push!(m.blx,bl)
push!(m.bux,bu)
end
else
push!(m.bkx,Mosek.MSK_BK_LO)
push!(m.blx,bl)
push!(m.bux,Inf)
end
else
if bu < Inf
push!(m.bkx,Mosek.MSK_BK_UP)
push!(m.blx,-Inf)
push!(m.bux,bu)
else
push!(m.bkx,Mosek.MSK_BK_FR)
push!(m.blx,-Inf)
push!(m.bux,Inf)
end
end
Mosek.appendvars(m.task,1);
Mosek.putvarbound(m.task,m.numvar,m.bkx[m.numvar],m.blx[m.numvar],m.bux[m.numvar])
push!(m.binvarflags,false)
end
MathProgBase.addvar!{T1,T2,T3,T4,T5}(m::MosekLinearQuadraticModel,
subi::Array{T1,1},
val ::Array{T2,1},
bl ::T3,
bu ::T4,
c ::T5) = MathProgBase.addvar!(m,convert(Array{Int32,1},subi),convert(Array{Float64,1},val),convert(Float64,bl),convert(Float64,bu),convert(Float64,c))
function MathProgBase.addvar!(m::MosekLinearQuadraticModel,
subi::Array{Int32,1},
val ::Array{Float64,1},
bl ::Float64,
bu ::Float64,
c ::Float64)
MathProgBase.addvar!(m,bl,bu,c)
Mosek.putacol(m.task,m.numvar,subi,val)
end
MathProgBase.addconstr!{T1,T2,T3,T4}(m::MosekLinearQuadraticModel,
subj::Array{T1,1},
val ::Array{T2,1},
bl ::T3,
bu ::T4) = MathProgBase.addconstr!(m,convert(Array{Int32,1},subj),convert(Array{Float64,1},val),convert(Float64,bl),convert(Float64,bu))
function MathProgBase.addconstr!(m::MosekLinearQuadraticModel,
subj::Array{Int32,1},
val ::Array{Float64,1},
bl ::Float64,
bu ::Float64)
m.numcon += 1
push!(m.lincon,m.numcon)
if bl > -Inf
if bu < Inf
if abs(bl-bu) < 1e-8
push!(m.bkc,Mosek.MSK_BK_FX)
push!(m.blc,bl)
push!(m.buc,bl)
else
push!(m.bkc,Mosek.MSK_BK_RA)
push!(m.blc,bl)
push!(m.buc,bu)
end
else
push!(m.bkc,Mosek.MSK_BK_LO)
push!(m.blc,bl)
push!(m.buc,Inf)
end
else
if bu < Inf
push!(m.bkc,Mosek.MSK_BK_UP)
push!(m.blc,-Inf)
push!(m.buc,bu)
else
push!(m.bkc,Mosek.MSK_BK_FR)
push!(m.blc,-Inf)
push!(m.buc,Inf)
end
end
Mosek.appendcons(m.task,1);
Mosek.putconbound(m.task,m.numcon,m.bkc[m.numcon],m.blc[m.numcon],m.buc[m.numcon])
Mosek.putarow(m.task,m.numcon,subj,val)
end
MathProgBase.updatemodel!(m::MosekLinearQuadraticModel) = nothing
MathProgBase.numlinconstr(m::MosekLinearQuadraticModel) = length(m.lincon)
MathProgBase.getobjval(m::MosekLinearQuadraticModel) = getobjval(m.task)
function MathProgBase.getsolution(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0
throw(MosekMathProgModelError("No solution available"))
end
solsta = Mosek.getsolsta(m.task,sol)
if solsta in [Mosek.MSK_SOL_STA_OPTIMAL,
Mosek.MSK_SOL_STA_PRIM_FEAS,
Mosek.MSK_SOL_STA_PRIM_AND_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_OPTIMAL,
Mosek.MSK_SOL_STA_NEAR_PRIM_FEAS,
Mosek.MSK_SOL_STA_NEAR_PRIM_AND_DUAL_FEAS,
Mosek.MSK_SOL_STA_INTEGER_OPTIMAL,
Mosek.MSK_SOL_STA_NEAR_INTEGER_OPTIMAL ]
Mosek.getxx(m.task,sol)
else
throw(MosekMathProgModelError("No solution available"))
end
end
function MathProgBase.getconstrsolution(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0
throw(Mosek.MosekMathProgModelError("No solution available"))
end
Mosek.getxc(m.task,sol)[m.lincon]
end
function MathProgBase.getreducedcosts(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0 || sol == Mosek.MSK_SOL_ITG
throw(Mosek.MosekMathProgModelError("Solution not available"))
end
solsta = Mosek.getsolsta(m.task,sol)
if solsta in [Mosek.MSK_SOL_STA_OPTIMAL,
Mosek.MSK_SOL_STA_DUAL_FEAS,
Mosek.MSK_SOL_STA_PRIM_AND_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_OPTIMAL,
Mosek.MSK_SOL_STA_NEAR_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_PRIM_AND_DUAL_FEAS ]
Mosek.getslx(m.task,sol) - Mosek.getsux(m.task,sol)
else
throw(MosekMathProgModelError("No solution available"))
end
end
function MathProgBase.getconstrduals(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0 || sol == Mosek.MSK_SOL_ITG
throw(Mosek.MosekMathProgModelError("Solution not available"))
end
solsta = Mosek.getsolsta(m.task,sol)
if solsta in [Mosek.MSK_SOL_STA_OPTIMAL,
Mosek.MSK_SOL_STA_DUAL_FEAS,
Mosek.MSK_SOL_STA_PRIM_AND_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_OPTIMAL,
Mosek.MSK_SOL_STA_NEAR_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_PRIM_AND_DUAL_FEAS ]
Mosek.gety(m.task,sol)[m.lincon]
else
throw(MosekMathProgModelError("Solution not available"))
end
end
function MathProgBase.getbasis(m::MosekLinearQuadraticModel)
if ! Mosek.solutiondef(m.task,Mosek.MSK_SOL_BAS)
throw(Mosek.MosekMathProgModelError("Basis not available"))
end
sol = Mosek.MSK_SOL_BAS
skx = Mosek.getskx(m.task,sol)
skc = Mosek.getskc(m.task,sol)
cbasis = [if skx[i] == Mosek.MSK_SK_BAS :Basic
elseif skx[i] == Mosek.MSK_SK_LO :NonBasicAtLower
elseif skx[i] == Mosek.MSK_SK_UP :NonBasicAtUpper
elseif skx[i] == Mosek.MSK_SK_FX :NonBasicAtLower # or upper. Doesn't matter.
else :SuperBasic
end
for i in 1:m.numvar ]
rbasis = [if skc[i] == Mosek.MSK_SK_BAS :Basic
elseif skc[i] == Mosek.MSK_SK_LO :NonBasicAtLower
elseif skc[i] == Mosek.MSK_SK_UP :NonBasicAtUpper
elseif skc[i] == Mosek.MSK_SK_FX :NonBasicAtLower # or upper. Doesn't matter.
else :SuperBasic
end
for i in m.lincon]
cbasis,rbasis
end
function MathProgBase.getunboundedray(m::MosekLinearQuadraticModel)
soldef = getsoldef(m.task)
if soldef < 0 throw(MosekMathProgModelError("No solution available")) end
solsta = Mosek.getsolsta(m.task,soldef)
if solsta in [ Mosek.MSK_SOL_STA_DUAL_INFEAS_CER, Mosek.MSK_SOL_STA_NEAR_DUAL_INFEAS_CER ]
Mosek.getxx(m.task,soldef)
else
throw(MosekMathProgModelError("No ray available"))
end
end
function MathProgBase.getinfeasibilityray(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0 throw(MosekMathProgModelError("No solution available")) end
solsta = Mosek.getsolsta(m.task,sol)
if solsta in [ Mosek.MSK_SOL_STA_PRIM_INFEAS_CER, Mosek.MSK_SOL_STA_NEAR_PRIM_INFEAS_CER ]
Mosek.getsux(m.task,sol) - Mosek.getslx(m.task,sol)
else
throw(MosekMathProgModelError("No ray available"))
end
end
MathProgBase.getrawsolver(m::MosekLinearQuadraticModel) = m.task
MathProgBase.getsimplexiter(m::MosekLinearQuadraticModel) = Mosek.getintinf(m.task,Mosek.MSK_IINFITEM_SIM_PRIMAL_ITER)+Mosek.getintinf(m.task,Mosek.MSK_IINFITEM_SIM_DUAL_ITER)+Mosek.getintinf(m.task,Mosek.MSK_IINFITEM_SIM_PRIMAL_DUAL_ITER)
MathProgBase.getbarrieriter(m::MosekLinearQuadraticModel) = Mosek.getintinf(m.task,Mosek.MSK_IINFITEM_SIM_INTPNT_ITER)
MathProgBase.setwarmstart!{T}(m::MosekLinearQuadraticModel, v::Array{T,1}) = MathProgBase.setwarmstart!(m,convert(Array{Float64,1},v))
function MathProgBase.setwarmstart!(m::MosekLinearQuadraticModel, v::Array{Float64,1})
n = min(m.numvar,length(v))
vals = Array(Float64, n)
vals[:] = v[1:n]
nanidxs = find(isnan,vals)
vals[nanidxs] = 0.0
skx = Int32[ if isnan(vals[i]) Mosek.MSK_SK_UNK else Mosek.MSK_SK_BAS end for i in 1:n ]
Mosek.putxxslice(m.task,Mosek.MSK_SOL_BAS,1,n+1,vals);
Mosek.putskxslice(m.task,Mosek.MSK_SOL_BAS,1,n+1,skx);
end
MathProgBase.optimize!(m::MosekLinearQuadraticModel) = Mosek.optimize(m.task)
MathProgBase.status(m::MosekLinearQuadraticModel) = status(m.task)
MathProgBase.getobjbound(m::MosekLinearQuadraticModel) = Mosek.getdouinf(m.task,Mosek.MSK_DINF_MIO_OBJ_BOUND)
MathProgBase.getobjgap(m::MosekLinearQuadraticModel) = getobjgap(m.task)
MathProgBase.getsolvetime(m::MosekLinearQuadraticModel) = Mosek.getdouinf(m.task,Mosek.MSK_DINF_OPTIMIZER_TIME)
MathProgBase.getrawsolver(m::MosekLinearQuadraticModel) = m.task
MathProgBase.getsense(m::MosekLinearQuadraticModel) = getsense(m.task)
MathProgBase.setsense!(m::MosekLinearQuadraticModel,sense) = setsense!(m.task,sense)
function MathProgBase.freemodel!(m::MosekLinearQuadraticModel)
Mosek.deletetask(m.task)
m.task = C_NULL
end
MathProgBase.numvar(m::MosekLinearQuadraticModel) = m.numvar
MathProgBase.numconstr(m::MosekLinearQuadraticModel) = length(m.lincon)
function MathProgBase.setvartype!(m::MosekLinearQuadraticModel,vtvec::Vector{Symbol})
n = min(m.numvar,length(vtvec))
if n > 0
vts = Int32[if vt == :Cont Mosek.MSK_VAR_TYPE_CONT
elseif vt == :Int Mosek.MSK_VAR_TYPE_INT
elseif vt == :Bin Mosek.MSK_VAR_TYPE_INT
else Mosek.MSK_VAR_TYPE_CONT
end
for vt in vtvec[1:n]]
Mosek.putvartypelist(m.task,Int32[1:n;],vts)
for i in find(vt -> vt == :Bin, vtvec[1:n])
bl = max(m.blx[i],0.0)
bu = min(m.bux[i],1.0)
Mosek.putvarbound(m.task,i,Mosek.MSK_BK_RA,bl,bu)
end
# for all :Bin vars being changed to :Int or :Cont, restore original bounds
for i in find(i -> (vtvec[i] == :Cont || vtvec[i] == :Int) && m.binvarflags[i], 1:n)
Mosek.putvarbound(m.task,i,m.bkx[i],m.blx[i],m.bux[i])
end
for i in 1:n
m.binvarflags[i] = vtvec[i] == :Bin
end
end
end
function MathProgBase.getvartype(m::MosekLinearQuadraticModel)
mskvt = Mosek.getvartypelist(m.task,Int32[1:m.numvar;])
[if mskvt[i] == Mosek.MSK_VAR_TYPE_INT
if m.binvarflags[i]
:Bin
else
:Int
end
else
:Cont
end
for i in 1:m.numvar]
end
##############################################################
## Integer Programming
##############################################################
MathProgBase.getnodecount(m::MosekLinearQuadraticModel) = 0
##############################################################
## Quadratic
##############################################################
MathProgBase.numquadconstr(m::MosekLinearQuadraticModel) = length(m.quadcon)
MathProgBase.setquadobj!(m::MosekLinearQuadraticModel,subi,subj,valij) = MathProgBase.setquadobj!(m,convert(Array{Int32,1},subi),convert(Array{Int32,1},subj),convert(Array{Float64,1},valij))
# NOTE on data format: The matrix is specified by inputting only lower
# or upper triangular part. This means that whenever element (i,j) is
# added, (j,i) is implicitly added. Duplicates are added together
function MathProgBase.setquadobj!(m::MosekLinearQuadraticModel,
subi :: Array{Int32,1},
subj :: Array{Int32,1},
valij :: Array{Float64,1})
n = length(subi)
let qsubi = subi[:],
qsubj = subj[:]
for i in 1:n
if qsubi[i] < qsubj[i]
tmp = qsubi[i]
qsubi[i] = qsubj[i]
qsubj[i] = tmp
end
end
Mosek.putqobj(m.task,qsubi,qsubj,valij)
end
end
function MathProgBase.addquadconstr!(m :: MosekLinearQuadraticModel,
subj,
valj,
qsubi,
qsubj,
qvalij,
sense :: Char,
bnd)
MathProgBase.addquadconstr!(m,
convert(Array{Int32,1},subj),
convert(Array{Float64,1},valj),
convert(Array{Int32,1},qsubi),
convert(Array{Int32,1},qsubj),
convert(Array{Float64,1},qvalij),
sense,
convert(Float64,bnd))
end
function MathProgBase.addquadconstr!(m :: MosekLinearQuadraticModel,
subj :: Array{Int32,1},
valj :: Array{Float64,1},
qsubi :: Array{Int32,1},
qsubj :: Array{Int32,1},
qvalij :: Array{Float64,1},
sense :: Char,
bnd :: Float64)
if sense == '<'
push!(m.bkc,Mosek.MSK_BK_UP)
elseif sense == '>'
push!(m.bkc,Mosek.MSK_BK_LO)
else
throw(MosekMathProgSolverInterface.MosekMathProgModelError("Invalid sense"))
end
m.numcon += 1
push!(m.quadcon,m.numcon)
push!(m.blc,bnd)
push!(m.buc,bnd)
Mosek.appendcons(m.task,1)
Mosek.putconbound(m.task,m.numcon,m.bkc[m.numcon],m.blc[m.numcon],m.buc[m.numcon])
let qsubi = qsubi[:],
qsubj = qsubj[:],
qval = qvalij[:]
for i in 1:length(qsubi)
if qsubi[i] < qsubj[i]
t = qsubi[i]
qsubi[i] = qsubj[i]
qsubj[i] = t
elseif qsubi[i] == qsubj[i]
qval[i] *= 2
end
end
Mosek.putqconk(m.task,m.numcon,qsubi,qsubj,qval)
end
Mosek.putarow(m.task,m.numcon,subj,valj)
end
function MathProgBase.getquadconstrsolution(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0
throw(Mosek.MosekMathProgModelError("No solution available"))
end
Mosek.getxc(sol)[m.quadcon]
end
function MathProgBase.getquadconstrduals(m::MosekLinearQuadraticModel)
sol = getsoldef(m.task)
if sol < 0 || sol == Mosek.MSK_SOL_ITG
throw(Mosek.MosekMathProgModelError("Solution not available"))
end
solsta = Mosek.getsolsta(m.task,sol)
if solsta in [Mosek.MSK_SOL_STA_OPTIMAL,
Mosek.MSK_SOL_STA_DUAL_FEAS,
Mosek.MSK_SOL_STA_PRIM_AND_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_OPTIMAL,
Mosek.MSK_SOL_STA_NEAR_DUAL_FEAS,
Mosek.MSK_SOL_STA_NEAR_PRIM_AND_DUAL_FEAS ]
Mosek.gety(m.task)[m.quadcon]
else
throw(MosekMathProgModelError("Solution not available"))
end
end
function MathProgBase.getquadinfeasibilityray(m::MosekLinearQuadraticModel)
sol = getsoldef(m)
if sol < 0 throw(MosekMathProgModelError("No solution available")) end
solsta = getsolsta(m.task,sol)
s = Mosek.getsux(m.task,sol) - Mosek.getslx(m.task,sol)
if solsta in [ Mosek.MSK_SOL_STA_PRIM_INFEAS_CER, Mosek.MSK_SOL_STA_NEAR_PRIM_INFEAS_CER ]
-s[m.quadcon]
else
throw(MosekMathProgModelError("No solution available"))
end
end
function MathProgBase.getquadconstrRHS(m::MosekLinearQuadraticModel)
m.blc[m.quadcon]
end
MathProgBase.setquadconstrRHS!(m::MosekLinearQuadraticModel, bnd) = MathProgBase.setquadconstrRHS!(m,convert(Array{Float64,1},bnd))
function MathProgBase.setquadconstrRHS!(m::MosekLinearQuadraticModel, bnd::Array{Float64,1})
n = min(length(bnd),length(m.quadcon))
m.blc[m.quadcon[1:n]] = bnd[1:n]
Mosek.putconboundlist(m.task,m.quadcon[1:n],m.bkc,m.blc,m.buc)
end
##############################################################
## Nonlinear
#############################################################
MathProgBase.NonlinearModel(s::MosekSolver) = MosekNonlinearModel(MathProgBase.LinearQuadraticModel(s))
type CallbackData
d::MathProgBase.AbstractNLPEvaluator
numVar::Int
numConstr::Int
Ihess::Vector{Int32}
Jhess::Vector{Int32}
jac_colval::Vector{Int32} # Compressed sparse row form of Jacobian sparsity
jac_rowstarts::Vector{Int32}
jac_nzval::Vector{Float64} # storage for full jacobian (mosek may ask for a subset)
jac_nnz_original::Int # nnz in NLP evaluator jacobian == length(Ijac)
jac_idxmap::Vector{Int} # map from indices in NLP evaluator jacobian to mosek jacobian
J_tmp::Vector{Float64} # storage for NLP evaluator jacobian
g_tmp::Vector{Float64} # storage for constraint values
end
function msk_nl_getsp_wrapper_mpb(nlhandle:: Ptr{Void},
numgrdobjnz:: Ptr{Int32}, # number of nonzeros in gradient of objective
grdobjsub:: Ptr{Int32}, # subscripts of nonzeros in gradient of objective
i_:: Int32, # constraint index
convali:: Ptr{Bool}, # 0/1 indicating whether constraint i is non-linear
grdconinz:: Ptr{Int32}, # number of nonzeros in gradient of constraint i
grdconisub:: Ptr{Int32}, # subscripts of nonzeros in gradient of constraint i
yo:: Int32, # 0/1 include objective in computation of hessian pattern
numycnz:: Int32, # number of constraints to include in computation of the hessian pattern
ycsub:: Ptr{Int32}, # indexes of constraints to include in computation of the hessian pattern
maxnumhesnz:: Int32, # lengths of hessubi and hessubj
numhesnz_:: Ptr{Int32}, # number of hessian nonzeros
hessubi:: Ptr{Int32}, # column subscrips of hessian non-zeros
hessubj:: Ptr{Int32}) # row subscripts of hessian non-zeros
cb = unsafe_pointer_to_objref(nlhandle)::CallbackData
i = i_+1
if numgrdobjnz != C_NULL
unsafe_store!(numgrdobjnz, convert(Int32,cb.numVar))
end
if grdobjsub != C_NULL
grdobjsub_a = pointer_to_array(grdobjsub,(cb.numVar,))
for i in 1:cb.numVar
grdobjsub_a[i] = i-1
end
end
if i <= cb.numConstr
con_nnz = cb.jac_rowstarts[i+1] - cb.jac_rowstarts[i]
if convali != C_NULL
# can we only say that constraint is linear if we've added the linear part separately?
if con_nnz > 0
unsafe_store!(convali, convert(Int32,1))
else
unsafe_store!(convali, convert(Int32,0))
end
end
if grdconinz != C_NULL
unsafe_store!(grdconinz, convert(Int32, con_nnz))
end
if grdconisub != C_NULL
if con_nnz > 0
grdconisub_a = pointer_to_array(grdconisub,(con_nnz,))
grdconisub_a[1:con_nnz] = cb.jac_colval[cb.jac_rowstarts[i]:(cb.jac_rowstarts[i+1]-1)] - 1
end
end
end
hess_nnz = length(cb.Ihess)
if numhesnz_ != C_NULL
unsafe_store!(numhesnz_, convert(Int32, hess_nnz))
end
if hessubi != C_NULL && hessubj != C_NULL && maxnumhesnz >= hess_nnz
hessubi_a = pointer_to_array(hessubi,(hess_nnz,))
hessubj_a = pointer_to_array(hessubj,(hess_nnz,))
for i in 1:hess_nnz
hessubi_a[i] = cb.Ihess[i] - 1
hessubj_a[i] = cb.Jhess[i] - 1
end
end
return Int32(0)::Int32
end
function msk_nl_getva_wrapper_mpb(nlhandle :: Ptr{Void},
xx_ :: Ptr{Float64}, # input
yo :: Float64,
yc_ :: Ptr{Float64}, # input, length = numcon
objval :: Ptr{Float64},
numgrdobjnz :: Ptr{Int32},
grdobjsub :: Ptr{Int32},
grdobjval :: Ptr{Float64},
numi_ :: Int32,
subi_ :: Ptr{Int32}, # input
conval :: Ptr{Float64},
grdconptrb_ :: Ptr{Int32}, # input
grdconptre_ :: Ptr{Int32}, # input
grdconsub_ :: Ptr{Int32}, # input
grdconval_ :: Ptr{Float64},
grdlag :: Ptr{Float64},
maxnumhesnz :: Int32,
numhesnz :: Ptr{Int32},
hessubi :: Ptr{Int32},
hessubj :: Ptr{Int32},
hesval :: Ptr{Float64})
cb = unsafe_pointer_to_objref(nlhandle)::CallbackData
numi = convert(Int,numi_)
xx = pointer_to_array(xx_,(cb.numVar,))
yc = pointer_to_array(yc_,(cb.numConstr,))
subi = pointer_to_array(subi_,(numi,))
if objval != C_NULL
unsafe_store!(objval, MathProgBase.eval_f(cb.d, xx))
end
if numgrdobjnz != C_NULL
unsafe_store!(numgrdobjnz, convert(Int32,cb.numVar))
end
if grdobjsub != C_NULL && grdobjval != C_NULL
grdobjval_a = pointer_to_array(grdobjval,(cb.numVar,))
grdobjsub_a = pointer_to_array(grdobjsub,(cb.numVar,))
MathProgBase.eval_grad_f(cb.d, grdobjval_a, xx)
for i in 1:cb.numVar