-
Notifications
You must be signed in to change notification settings - Fork 119
/
Convex.jl
258 lines (220 loc) · 7.06 KB
/
Convex.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
__precompile__()
module Convex
using OrderedCollections: OrderedDict
using LinearAlgebra
using SparseArrays
using LDLFactorizations
using AbstractTrees: AbstractTrees, children
# using DataStructures
import MathOptInterface as MOI
# Functions
export conv,
dotsort, entropy, exp, geomean, hinge_loss, huber, inner_product, invpos
export log_perspective,
logisticloss,
logsumexp,
matrixfrac,
neg,
norm2,
norm_1,
norm_inf,
nuclearnorm
export partialtrace,
partialtranspose, pos, qol_elementwise, quadform, quadoverlin, rationalnorm
export relative_entropy,
sigmamax, square, sumlargest, sumlargesteigs, sumsmallest, sumsquares
export GeomMeanHypoCone,
GeomMeanEpiCone,
RelativeEntropyEpiCone,
quantum_relative_entropy,
quantum_entropy
export trace_logm, trace_mpower, lieb_ando
export DCPViolationError
# rexports from LinearAlgebra
export diag, diagm, Diagonal, dot, eigmax, eigmin, kron, logdet, norm, tr
# Constraints
export Constraint
export isposdef, ⪰, ⪯ # PSD constraints
export socp
export Constraint # useful for making abstractly-typed vectors via `Constraint[]`
# Variables
export constant, ComplexVariable, HermitianSemidefinite, Semidefinite, Variable
export curvature,
evaluate, fix!, free!, monotonicity, sign, vexity, problem_vexity
export BinVar, IntVar, ContVar, vartype, vartype!
export get_constraints, add_constraint!, set_value!, evaluate
# Signs
export Positive, Negative, ComplexSign, NoSign
# Problems
export add_constraints!, maximize, minimize, Problem, satisfy, solve!
export write_to_file
# Module level globals
"""
MAXDEPTH
Controls depth of tree printing globally for Convex.jl; defaults to 3. Set via
Convex.MAXDEPTH[] = 5
"""
const MAXDEPTH = Ref(3)
"""
MAXWIDTH
Controls width of tree printing globally for Convex.jl; defaults to 15. Set via
Convex.MAXWIDTH[] = 15
"""
const MAXWIDTH = Ref(15)
"""
MAXDIGITS
When priting IDs of variables, only show the initial and final digits
if the full ID has more than double the number of digits specified
here. So, with the default setting MAXDIGITS=3, any ID longer than 7
digits would be shortened; for example, ID `14656210999710729289`
would be printed as `146…289`.
This setting controls tree printing globally for Convex.jl; defaults to 3.
Set via:
Convex.MAXDIGITS[] = 3
"""
const MAXDIGITS = Ref(3)
# where do these go?
# used so far only in `Constant`
vectorize(v::AbstractVector) = v
vectorize(v::Number) = [v]
vectorize(v::AbstractMatrix) = vec(v)
# where should these go?
function vec_triu(M)
L = LinearIndices(size(M))
n, m = size(M)
inds = [L[i, j] for i in 1:n for j in i:m]
return M[inds]
end
function vec_tril(M)
L = LinearIndices(size(M))
n, m = size(M)
inds = [L[i, j] for i in 1:n for j in 1:i]
return M[inds]
end
# using SuiteSparseGraphBLAS
#
# vec(x) = Base.vec(x)
# function vec(x::GBMatrix)
# # Hacks to try to get `vec` to work
# x = reshape(x, length(x), 1)
# return x[:, 1]
# end
# blockdiag(xs...) = SparseArrays.blockdiag(xs...)::SPARSE_MATRIX
# function blockdiag(xs::GBMatrix{T,T}...) where {T}
# N = length(xs)
# entries = Matrix{GBMatrix{T,T}}(undef, N, N)
# heights = size.(xs, 1)
# for (i, x) in enumerate(xs)
# entries[i, i] = x
# m = size(x, 2)
# for j in 1:(i-1)
# entries[j, i] = GBMatrix{T,T}(heights[j], m)
# end
# for j in (i+1):lastindex(entries, 1)
# entries[j, i] = GBMatrix{T,T}(heights[j], m)
# end
# end
# return cat(entries)
# end
#
# const SPARSE_VECTOR{T} = GBVector{T,T}
# const SPARSE_MATRIX{T} = GBMatrix{T,T}
# spzeros(T, d) = GBVector{T,T}(d)
# spzeros(T, n, m) = GBMatrix{T,T}(n, m)
# spidentity(T, d) = GBMatrix{T,T}(Diagonal(ones(T, d)))
# create_sparse(T, args...) = GBMatrix{T,T}(args...)
const SPARSE_VECTOR{T} = Vector{T}
const SPARSE_MATRIX{T} = SparseMatrixCSC{T,Int}
spzeros(T, d) = zeros(T, d)
spzeros(T, n, m) = SparseArrays.spzeros(T, n, m)
spidentity(T, d) = sparse(one(T) * I, d, d)
function create_sparse(::Type{T}, args...) where {T}
local result::SPARSE_MATRIX{T}
result = SparseArrays.sparse(args...)
return result
end
include("Context.jl")
### modeling framework
include("dcp.jl")
include("expressions.jl")
include("variable.jl")
include("variable_template.jl")
include("constant.jl")
include("constraints/constraints.jl")
include("constraints/soc_constraints.jl")
include("constraints/exp_constraints.jl")
include("constraints/sdp_constraints.jl")
include("problems.jl")
include("SparseTape.jl")
include("VectorAffineFunctionAsMatrix.jl")
include("ComplexTape.jl")
include("operate.jl")
include("complex_operate.jl")
include("real_operate.jl")
include("solution.jl")
include("MOI_wrapper.jl")
### affine atoms
include("atoms/affine/add_subtract.jl")
include("atoms/affine/multiply_divide.jl")
include("atoms/affine/sum.jl")
include("atoms/affine/transpose.jl")
include("atoms/affine/index.jl")
include("atoms/affine/diag.jl")
include("atoms/affine/diagm.jl")
include("atoms/affine/stack.jl")
include("atoms/affine/dot.jl")
include("atoms/affine/reshape.jl")
include("atoms/affine/trace.jl")
include("atoms/affine/partialtrace.jl")
include("atoms/affine/partialtranspose.jl")
include("atoms/affine/conv.jl")
include("atoms/affine/real_imag.jl")
include("atoms/affine/inner_product.jl")
include("atoms/affine/conjugate.jl")
include("atoms/affine/kron.jl")
### lp atoms
include("atoms/lp_cone/abs.jl")
include("atoms/lp_cone/maximum.jl")
include("atoms/lp_cone/minimum.jl")
include("atoms/lp_cone/max.jl")
include("atoms/lp_cone/min.jl")
include("atoms/lp_cone/sumlargest.jl")
include("atoms/lp_cone/dotsort.jl")
### SOC atoms
include("atoms/second_order_cone/norm.jl") # also includes some lp atoms
include("atoms/second_order_cone/norm2.jl")
include("atoms/second_order_cone/quadoverlin.jl")
include("atoms/second_order_cone/qol_elementwise.jl")
include("atoms/second_order_cone/geomean.jl")
include("atoms/second_order_cone/quadform.jl")
include("atoms/second_order_cone/rationalnorm.jl")
include("atoms/second_order_cone/huber.jl")
### SDP atoms
include("atoms/sdp_cone/nuclearnorm.jl")
include("atoms/sdp_cone/operatornorm.jl")
include("atoms/sdp_cone/eig_min_max.jl")
include("atoms/sdp_cone/matrixfrac.jl")
include("atoms/sdp_cone/sumlargesteigs.jl")
include("atoms/sdp_cone/geom_mean_hypocone.jl")
include("atoms/sdp_cone/geom_mean_epicone.jl")
include("atoms/sdp_cone/relative_entropy_epicone.jl")
include("atoms/sdp_cone/quantum_relative_entropy.jl")
include("atoms/sdp_cone/quantum_entropy.jl")
include("atoms/sdp_cone/trace_logm.jl")
include("atoms/sdp_cone/trace_mpower.jl")
include("atoms/sdp_cone/lieb_ando.jl")
### exponential atoms
include("atoms/exp_cone/exp.jl")
include("atoms/exp_cone/log.jl")
include("atoms/exp_cone/logsumexp.jl")
include("atoms/exp_cone/entropy.jl")
include("atoms/exp_cone/relative_entropy.jl")
### exp + sdp atoms
include("atoms/exp_+_sdp_cone/logdet.jl")
### utilities
include("utilities/tree_print.jl")
include("utilities/tree_interface.jl")
include("utilities/show.jl")
include("utilities/iteration.jl")
include("problem_depot/problem_depot.jl")
end