chkwon / PATHSolver.jl

provides a Julia wrapper for the PATH Solver for solving mixed complementarity problems
http://pages.cs.wisc.edu/%7Eferris/path.html
MIT License
50 stars 15 forks source link

Add support for MOI.UserDefinedFunction #95

Closed odow closed 10 months ago

odow commented 10 months ago

Closes #93

julia> using JuMP

julia> import BitOperations

julia> import PATHSolver

julia> function possible_shock_combinations(J)
           return [BitOperations.bget(j, i) for j in 0:(2^J-1), i in 0:(J-1)]
       end
possible_shock_combinations (generic function with 1 method)

julia> function shock_probabilities(sequences, probs)
           @assert size(sequences, 2) == length(probs)
           return map(1:size(sequences, 1)) do i
               return prod(
                   s_i ? p_i : (1 - p_i) for (s_i, p_i) in zip(sequences[i, :], probs)
               )
           end
       end
shock_probabilities (generic function with 1 method)

julia> function solve_complementarity(param1, param2, shock, probs, w, i)
           model = Model(PATHSolver.Optimizer)
           @variable(model, x[1:J] >= 0, start = 0.5)
           A = 1 .- shock .* (1 - param2)
           function f(x_j, x...)
               j = round(Int, x_j)
               return sum(probs[s] * A[s, j] / sum(A[s, k] * x[k] for k in 1:J) for s in 1:2^J)
           end
           function 𝝯f(g, x_j, x...)
               j = round(Int, x_j)
               for s in 1:2^J
                   temp = -probs[s] * A[s, j] / sum(A[s, k] * x[k] for k in 1:J)^2
                   for k in 1:J
                       g[1+k] += temp * A[s, k]
                   end
               end
               return
           end
           @operator(model, op_f, 1 + J, f, 𝝯f)
           @constraint(model, c[j in 1:J], -(1 - param1) * w[i] * op_f(j, x...) + w[j] ⟂ x[j])
           optimize!(model)
           return value.(x)
       end
solve_complementarity (generic function with 1 method)

julia> begin
           J = 10
           param1 = 0.5
           param2 = 0.1
           prob = fill(0.5, J)
           shock = possible_shock_combinations(J)
           probs = shock_probabilities(shock, prob)
           w = ones(J)
           i = 1
           x = solve_complementarity(param1, param2, shock, probs, w, i)
       end
Path 5.0.03 (Fri Jun 26 09:58:07 2020)
Written by Todd Munson, Steven Dirkse, Youngdae Kim, and Michael Ferris

Crash Log
major  func  diff  size  residual    step       prox   (label)
    0     0             1.1714e+00             0.0e+00 (f[    1])
    1     2     0    10 2.8492e-01  8.0e-01    0.0e+00 (f[    5])
pn_search terminated: no basis change.

Major Iteration Log
major minor  func  grad  residual    step  type prox    inorm  (label)
    0     0     3     2 2.8492e-01           I 0.0e+00 9.0e-02 (f[    5])
    1     1     7     3 9.6246e-02  5.1e-01 SB 0.0e+00 3.0e-02 (f[   10])
    2     1     8     4 1.8330e-03  1.0e+00 SO 0.0e+00 5.8e-04 (f[    1])
    3     1     9     5 1.0492e-06  1.0e+00 SO 0.0e+00 3.3e-07 (f[   10])
    4     1    10     6 3.7354e-13  1.0e+00 SO 0.0e+00 1.2e-13 (f[    1])

Major Iterations. . . . 4
Minor Iterations. . . . 4
Restarts. . . . . . . . 0
Crash Iterations. . . . 1
Gradient Steps. . . . . 0
Function Evaluations. . 10
Gradient Evaluations. . 6
Basis Time. . . . . . . 0.000101
Total Time. . . . . . . 0.545882
Residual. . . . . . . . 3.735389e-13
10-element Vector{Float64}:
 0.049999999999993314
 0.04999999999999365
 0.049999999999993494
 0.049999999999993695
 0.04999999999999393
 0.049999999999993786
 0.04999999999999392
 0.0499999999999937
 0.049999999999993494
 0.05000000000000107
codecov[bot] commented 10 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (821efdb) 93.95% compared to head (e62c38b) 94.31%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #95 +/- ## ========================================== + Coverage 93.95% 94.31% +0.35% ========================================== Files 3 3 Lines 447 457 +10 ========================================== + Hits 420 431 +11 + Misses 27 26 -1 ``` | [Files](https://app.codecov.io/gh/chkwon/PATHSolver.jl/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Changhyun+Kwon) | Coverage Δ | | |---|---|---| | [src/MOI\_wrapper.jl](https://app.codecov.io/gh/chkwon/PATHSolver.jl/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Changhyun+Kwon#diff-c3JjL01PSV93cmFwcGVyLmps) | `98.04% <100.00%> (+0.61%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.