 # Optimizing user-defined objective functions with no direct expression

I have a Monte Carlo simulation that takes in a set of variables `x1, x2, etc.` and returns a number of counters that can be used to calculate the actual objective function `f()`

I wanted to use JuMP to optimize the dimension variables, but is it possible to use the non-linear solver for a problem where there’s no expression to auto-differentiate?

Below is an example of what I thought would work.

``````function MonteCarlo(data, x, args)
for i = 1:length(data)
R = rand()*x  # x is the optimization variable
a = round(R*data[i]) # some processing based on data and random numbers
if a%2 == 0
counter1 = counter1 + 1
else
counter2 = counter2 + 1
end
return counter1, counter2
end

# Function to calculate the optimization objective
function f(x, data, args)

counter1, counter2  = MonteCarlo(data, x, args)

η = counter1/counter2
return η
end

model = Model()
@variable(model, 0 <= x <= 10)
@NLobjective(model, Max, f(data, x, args))
optimize!(model)
``````
2 Likes