Example for the C++ interface

Here, we outline the MAXCUT example, although we don't implement all the custom triangle functionality (though it would certainly be possible).

julia> using ConicBundle
julia> import MutableArithmetics as MA
julia> # warning: we have to take care of garbage collection by ourselves! garbage = [];
julia> add_gc(x) = (push!(garbage, x); x);
julia> STDIN = open("samplegraph.dat", "r")IOStream(<file samplegraph.dat>)
julia> # read the graph into (indi,indj,val) triples (replace val by 1.) nnodes = parse(Int, readuntil(STDIN, " "))50
julia> medges = parse(Int, readuntil(STDIN, "\n"))800
julia> indi = add_gc(CBIndexmatrix(medges, 1, 0))ConicBundle.CBIndexmatrix(Ptr{Nothing} @0x0000000002dd2820)
julia> indj = add_gc(CBIndexmatrix(medges, 1, 0))ConicBundle.CBIndexmatrix(Ptr{Nothing} @0x0000000002f15e40)
julia> val = add_gc(CBMatrix(medges, 1, 1.))ConicBundle.CBMatrix(Ptr{Nothing} @0x0000000002b08850)
julia> for i in 0:medges-1 head = parse(Int, readuntil(STDIN, " ")) tail = parse(Int, readuntil(STDIN, " ")) d = parse(Float64, readuntil(STDIN, "\n")) indi[i] = tail -1 indj[i] = head -1 # val[i] = d # edge weight d could be plugged in here end
julia> close(STDIN)
julia> L = add_gc(CBSparsesym(nnodes, medges, indi, indj, val))ConicBundle.CBSparsesym(Ptr{Nothing} @0x00000000027b5250)
julia> Ldiag = add_gc(CBMatrix(cb_diag(L)))ConicBundle.CBMatrix(Ptr{Nothing} @0x00000000032fbd80)
julia> L = MA.operate!(-, L, add_gc(cb_sparseDiag(Ldiag, 1e-60)))ConicBundle.CBSparsesym(Ptr{Nothing} @0x00000000027b5250)
julia> cb_init!(Ldiag, nnodes, 1, cb_sum(L) / nnodes)ConicBundle.CBMatrix(Ptr{Nothing} @0x00000000032fbd80)
julia> L = MA.operate!(*, L, -1)ConicBundle.CBSparsesym(Ptr{Nothing} @0x00000000027b5250)
julia> L = MA.operate!(+, L, add_gc(cb_sparseDiag(Ldiag, 1e-60)))ConicBundle.CBSparsesym(Ptr{Nothing} @0x00000000027b5250)
julia> L = MA.operate!(/, L, 4.)ConicBundle.CBSparsesym(Ptr{Nothing} @0x00000000027b5250)
julia> bestcut = add_gc(CBMatrix(nnodes, 1, 1.))ConicBundle.CBMatrix(Ptr{Nothing} @0x0000000002dc0650)
julia> bestcut_val = cb_ip(bestcut, L * bestcut)8.881784197001252e-15
julia> # form the Affine Matrix Function Oracle Xdim = add_gc(CBIndexmatrix(1, 1, nnodes))ConicBundle.CBIndexmatrix(Ptr{Nothing} @0x0000000002da7f40)
julia> C = add_gc(CBSparseCoeffmatMatrix(Xdim, 1))ConicBundle.CBSparseCoeffmatMatrix(Ptr{Nothing} @0x000000000262d940)
julia> cb_set!(C, 0, 0, CBCMsymsparse(L)) # by default, ownership is managed by the coeffmat0
julia> opAt = add_gc(CBSparseCoeffmatMatrix(Xdim, nnodes))ConicBundle.CBSparseCoeffmatMatrix(Ptr{Nothing} @0x0000000002c3c720)
julia> for i in 0:nnodes-1 cb_set!(opAt, 0, i, CBCMsingleton(nnodes, i, i, -1.)) # by default, the singletons are deleted by the coeffmat end
julia> mc = add_gc(CBPSCAffineFunction(C, opAt, CBGramSparsePSCPrimal(L))) # ownership of third argument passes to the functionConicBundle.CBPSCAffineFunction(Ptr{Nothing} @0x000000000371e500)
julia> cb_set_out!(mc, 0)
julia> # initialize the solver and the problem cbsolver = add_gc(CBMatrixCBSolver(1))ConicBundle.CBMatrixCBSolver(Ptr{Nothing} @0x0000000002756a80)
julia> lb = add_gc(CBMatrix(nnodes, 1, cb_minus_infinity))ConicBundle.CBMatrix(Ptr{Nothing} @0x0000000003716000)
julia> ub = add_gc(CBMatrix(nnodes, 1, cb_plus_infinity))ConicBundle.CBMatrix(Ptr{Nothing} @0x0000000002eb75d0)
julia> rhs = add_gc(CBMatrix(nnodes, 1, 1.))ConicBundle.CBMatrix(Ptr{Nothing} @0x0000000002ec76d0)
julia> cb_init_problem!(cbsolver, nnodes, lb, ub, nothing, rhs)0
julia> cb_add_function!(cbsolver, mc, nnodes, cbft_objective_function, nothing, true)0
julia> # call the solver cb_set_active_bounds_fixing!(cbsolver, true)
julia> cb_solve!(cbsolver) # here we could do all the rounding from the example00:00:00.02 enditd 1 1 1 8.39 1.73 460.4510619 461.1189502 00:00:00.03 enditd 2 2 2 8.39 1.27 458.8471559 459.6518768 00:00:00.03 enditd 3 3 3 5.95 1.25 457.5849765 458.7480685 00:00:00.03 enditd 4 5 5 5.95 0.880 457.0934129 457.7354977 00:00:00.03 enditd 5 7 7 5.95 0.613 457.1158445 457.3003017 00:00:00.04 enditd 6 8 8 2.97 0.769 456.5516702 456.8952777 00:00:00.04 enditd 7 11 11 2.73 0.606 456.2833095 456.5530380 00:00:00.04 enditd 8 13 13 2.73 0.219 456.2637578 456.4956584 00:00:00.05 enditd 9 15 15 2.73 0.178 456.2971020 456.4555383 00:00:00.05 enditd 10 17 17 2.73 0.162 456.3372847 456.4070153 00:00:00.06 enditd 11 18 18 2.73 0.130 456.3339646 456.3876297 00:00:00.06 enditd 12 21 21 2.73 0.0831 456.3519313 456.3622658 00:00:00.07 enditd 13 23 23 2.73 0.0694 456.3493528 456.3597937 00:00:00.07 enditd 14 26 26 2.73 0.0353 456.3525845 456.3580168 00:00:00.08 enditd 15 27 27 2.73 0.0344 456.3519836 456.3546421 00:00:00.08 enditd 16 30 30 2.40 0.0208 456.3526147 456.3541001 00:00:00.09 endit_ 16 31 31 2.40 0.0168 456.3526338 456.3541001 0
julia> cb_print_termination_code(cbsolver)termination status: 1, relative precision criterion satisfied
julia> cb_destroy!(@view(garbage[end:-1:1])...)