Hi,
I am trying to solve a maximization problem using Gurobi/JuMP and I would really appreciate it if you could help me speed things up. Some background on the problem, essentially I am trying to maximize the expectation of a function by sampling it. I am going to find the solution for every sampled outcome, and then I am going to force the solutions to be the same (nonanticipativity constraint).
I am sampling K different outcomes of the random variable which in the example is price and solving
\max_{I(\omega^{k})\forall k} \frac{1}{K} \sum_{k=1}^{K} F(I(\omega^{k}),\omega^{k})
subject to
I(\omega^{1})=I(\omega^{2})=I(\omega^{3})=\cdots=I(\omega^{K})
This is a MWE of the problem:
using DelimitedFiles,DataFrames,GLM,FixedEffects, ExcelReaders, LinearAlgebra, Random
using Distributions, BitOperations
using Discretizers,Plots
using JuMP, Gurobi, BenchmarkTools
###############################################################
# AUX FUNCTIONS
###############################################################
###############################################################
# Generating Data
###############################################################
#Model Parameters
J=67 #number of nodes#
K = 500
price=rand(J,K)
F=rand(J)
function problem_iter(price,σ,K,F)
price = price.^(1-σ)
model = Model(Gurobi.Optimizer)
@variable(model, I[1:J,1:K], Bin)
@variable(model, Ipost[1:J,1:K], Bin)
set_silent(model)
# Objective Function
@objective(model, Max, 1/K*(sum( (price[:,k]'*Ipost[:,k]) - F'*I[:,k] for k=1:K)))
# Constraint 1 and 2 - non-anticipativity constraint and ex-post choice
for k=1:K
@constraint(model, I[:,k] .== sum(I,dims=2)/K)
@constraint(model, Ipost[:,k]'*Ipost[:,k] == 1)
@constraint(model, sum(Ipost[:,k].*I[:,k] ) == 1)
end
optimize!(model)
Iᵒ = value.(I)
Iᵒ = trunc.(Int,Iᵒ)
Ipost = value.(Ipost)
Ipost = trunc.(Int,Ipost)
return Iᵒ, Ipost
end
TEST=@time problem_iter(price,2,K,F)
I am getting the following warning that for sure is diminishing performance, but I am not sure on how to handle
┌ Warning: The addition operator has been used on JuMP expressions a large number of times. This warning is safe to ignore but may indicate that model generation is slower than necessary. For performance reasons, you should not add expressions in a loop. Instead of x += y, use add_to_expression!(x,y) to modify x in place. If y is a single variable, you may also
use add_to_expression!(x, coef, y) for x += coef*y.