# Optimizing user-defined objective functions with no direct expression

**URL:** <https://discourse.julialang.org/t/optimizing-user-defined-objective-functions-with-no-direct-expression/60096>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [April 27, 2021, 10:53am UTC](https://discourse.julialang.org/t/optimizing-user-defined-objective-functions-with-no-direct-expression/60096 "2021-04-27T10:53:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fathya.Salih](https://avatars.discourse-cdn.com/v4/letter/f/47e85d/32.png) [@Fathya.Salih](https://discourse.julialang.org/u/Fathya.Salih)\
**Post date:** [April 27, 2021, 10:53am UTC](https://discourse.julialang.org/t/optimizing-user-defined-objective-functions-with-no-direct-expression/60096/1 "2021-04-27T10:53:27Z")

</div>

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.

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [April 27, 2021, 6:48pm UTC](https://discourse.julialang.org/t/optimizing-user-defined-objective-functions-with-no-direct-expression/60096/2 "2021-04-27T18:48:40Z")

</div>

Try [GitHub - bbopt/NOMAD.jl: Julia interface to the NOMAD blackbox optimization software](https://github.com/bbopt/NOMAD.jl)
