Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MOI.UserDefinedFunction #95

Merged
merged 3 commits into from
Nov 11, 2023
Merged

Add support for MOI.UserDefinedFunction #95

merged 3 commits into from
Nov 11, 2023

Conversation

odow
Copy link
Collaborator

@odow odow commented Nov 11, 2023

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

Base automatically changed from od/re-org to master November 11, 2023 01:45
Copy link

codecov bot commented Nov 11, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@            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 Coverage Δ
src/MOI_wrapper.jl 98.04% <100.00%> (+0.61%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@odow odow merged commit 9e4c3df into master Nov 11, 2023
9 checks passed
@odow odow deleted the od/udf branch November 11, 2023 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support MOI.UserDefinedFunction
1 participant