Hi! I am trying to optimize this section of code. Basically, there are two functions. The first one solves a linear programming problem over a convex set. The second function computes a Monte Carlo experiment over S trials of the first function. I can see how the second function can be easy to parallelize, however, this tidbit is part of a larger loop which I am parallelizing. Any help would be very much appreciated!!
using JuMP, LinearAlgebra, Random, Distributions, BenchmarkTools, MosekTools
const Ι = 250
const J = 50
const σ = 5
const α = 1.0
const S= 500
#Create a vector of productivities
const z=exp.(rand(Normal(0,1),Ι,S))
const β=exp.(rand(Normal(0,1),J))./10
C = ones(Ι).*10
# Create a Matrix of Distances
function distmat(J,Ι)
Distances = zeros(J,Ι)
Random.seed!(7777)
coordinates_market = 100*rand(J,2)
coordinates_plant = 100*rand(Ι,2)
for j = 1:J, l=1:Ι
Distances[j,l] = sqrt((coordinates_market[j,1]-coordinates_plant[l,1])^2+(coordinates_market[j,2]-coordinates_plant[l,2])^2)
end
return 1 .+ Distances./100
end
const τ = distmat(J,Ι)
function f1(C,β,τ,z)
(J,Ι) = size(τ)
opt = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))
set_silent(opt)
a = ((σ-1)/σ)^(σ-1)/(σ).*β.^(σ)
@variable(opt, x[1:Ι] >= 0)
@variable(opt, y[1:J]>= 0)
@variable(opt, t[1:J]>= 0)
@objective(opt, Min, x'*C .+ a'*y)
for i=1:Ι, j=1:J
@constraint(opt, τ[j,i]/z[i] + τ[j,i]*x[i] - t[j] >= 0)
end
for j = 1 : J
@constraint(opt, [y[j],t[j],1] in MOI.PowerCone(1/σ))
end
optimize!(opt)
return objective_value(opt), value.(x), value.(t)
end
function f2(Cap,β,τ,z)
(J,Ι) = size(τ)
Simul = size(z)[2]
Eπ = 0.0
Eν = zeros(Ι)
for s = 1 : Simul
temp = f1(Cap,β,τ,z[:,s])./Simul
Eπ += temp[1]
Eν += temp[2]
end
func = Eπ .- α*sum(Cap)
foc = Eν .- α
return func, foc
end
Test_Vec = 100 .*rand(Ι)
@btime f1(Test_Vec,β,τ,z)
@btime f2(Test_Vec,β,τ,z)