# Optimization of two parameters

**URL:** https://forum.mimiframework.org/t/optimization-of-two-parameters/62
**Category:** Uncategorized
**Created:** [June 5, 2019, 9:06pm UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62 "2019-06-05T21:06:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![BerBastien](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/berbastien/32/26_2.png) [@BerBastien](https://forum.mimiframework.org/u/BerBastien)
#### Post date: [June 5, 2019, 9:06pm UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/1 "2019-06-05T21:06:06Z")

</div>

Hi all,  
I’m trying to optimize MIU and S (mitigation and savings) for a small variation of MimiDICE2013. But I only know how to optimize one parameter, **in the following way:**

```
using BlackBoxOptim
m_opt = GreenDICE
function eval_dice(x)
    set_param!(m_opt,:emissions,:MIU,x)
    
    run(m_opt)
    
    return -m_opt[:welfare, :UTILITY]
end
res = bboptimize(eval_dice;SearchRange=(0.,1.), NumDimensions=60, Method=:adaptive_de_rand_1_bin_radiuslimited,MaxSteps=99999)
best_candidate(res) # optimal vector of miu emissions trajectories

```

**When I try to add the second parameter bboptimize won’t work:**

```
function eval_dice(x)
    m = x[:,1]
    s = x[:,2]
    set_param!(m_opt,:emissions,:MIU,m)
    set_param!(m_opt,:neteconomy,:S,s)
    run(m_opt)
    
    return -m_opt[:welfare, :UTILITY]
end
res = bboptimize(eval_dice;SearchRange=(0.,1.), NumDimensions=[2,60], Method=:adaptive_de_rand_1_bin_radiuslimited,MaxSteps=99999)

```

**I get this error:**

```
ERROR: MethodError: no method matching fill(::Tuple{Float64,Float64}, ::Array{Int64,1})
Closest candidates are:
  fill(::Any, ::Union{Integer, AbstractUnitRange}...) at array.jl:401
  fill(::Any, ::Tuple{}) at array.jl:404
  fill(::Any, ::Tuple{Vararg{Integer,N}}) where N at array.jl:403
  ...
Stacktrace:
 [1] symmetric_search_space(::Array{Int64,1}, ::Tuple{Float64,Float64}) at C:\Users\bastien\.julia\packages\BlackBoxOptim\RgNEa\src\search_space.jl:109
 [2] check_and_create_search_space(::DictChain{Symbol,Any}) at C:\Users\bastien\.julia\packages\BlackBoxOptim\RgNEa\src\default_parameters.jl:64
 [3] setup_problem(::Function, ::DictChain{Symbol,Any}) at C:\Users\bastien\.julia\packages\BlackBoxOptim\RgNEa\src\bboptimize.jl:27
 [4] #bbsetup#73(::Base.Iterators.Pairs{Symbol,Any,NTuple{4,Symbol},NamedTuple{(:SearchRange, :NumDimensions, :Method, :MaxSteps),Tuple{Tuple{Float64,Float64},Array{Int64,1},Symbol,Int64}}}, ::Function, ::Function, ::Dict{Symbol,Any}) at C:\Users\bastien\.julia\packkages\BlackBoxOptim\RgNEa\src\bboptimize.jl:86
 [5] #bbsetup at .\none:0 [inlined]
 [6] #bboptimize#72(::Base.Iterators.Pairs{Symbol,Any,NTuple{4,Symbol},NamedTuple{(:SearchRange, :NumDimensions, :Method, :MaxSteps),,Tuple{Tuple{Float64,Float64},Array{Int64,1},Symbol,Int64}}}, ::Function, ::Function, ::Dict{Symbol,Any}) at C:\Users\bastien\.juliap\packages\BlackBoxOptim\RgNEa\src\bboptimize.jl:69
 [7] #bboptimize at .\none:0 [inlined] (repeats 2 times)
 [8] top-level scope at none:0

```

Thanks in advance for your help!

---

<div class="post-metadata">

### Author: ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)
#### Post date: [June 6, 2019, 5:04am UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/2 "2019-06-06T05:04:30Z")

</div>

Hi there,

I’m not sure if this is the only solution, but I think I recall that `BlackBoxOptim` only optimizes on a single vector of choice values ie. your `NumDimensions` can only be an integer, not an array. The following should work, I tried it out and it runs! @davidanthoff @FrankErrickson is this response correct or is there a nuance I’m missing?

```auto
using Mimi
using MimiDICE2013
using BlackBoxOptim

m_opt = MimiDICE2013.get_model()
run(m_opt)

function eval_dice(x)
    m = x[1:60]
    s = x[61:end]
    set_param!(m_opt,:emissions,:MIU,m)
    set_param!(m_opt,:neteconomy,:S,s)
    run(m_opt)
    
    return -m_opt[:welfare, :UTILITY]
end
res = bboptimize(eval_dice;SearchRange=(0.,1.), NumDimensions=120, Method=:adaptive_de_rand_1_bin_radiuslimited,MaxSteps=99999)

```

---

<div class="post-metadata">

### Author: ![davidanthoff](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/davidanthoff/32/5_2.png) [@davidanthoff](https://forum.mimiframework.org/u/davidanthoff)
#### Post date: [June 6, 2019, 1:34pm UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/3 "2019-06-06T13:34:02Z")

</div>

Yes, that is exactly right. One small twist, you can avoid a bunch of unnecessary allocations by using the following code:

```auto
m = @view x[1:60]
s = @view x[61:end]

```

---

<div class="post-metadata">

### Author: ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)
#### Post date: [June 7, 2019, 1:22am UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/4 "2019-06-07T01:22:55Z")

</div>

@lrennels @BerBastien I’ve used `NLopt` for all of my standard IAM optimizations, and only used `BlackBoxOptim` for more complicated multi-objective stuff that doesn’t really apply here… so take my advice with a grain of salt.

At least with `NLopt`, you need to pass in a single vector for your choice variables, and then split them up to different parameters within your objective function if you’re optimizing on more than two parameters. So it seems like the approach @lrennels took is correct.

A few other minor points. (1) If you end up wanting to optimize RICE then it’d be easier to optimize the carbon tax rather than the mitigation rates (assuming a globally harmonized tax). Then you just need to optimize a single tax for each period rather than regional mitigation rates for every period. You’ll have to adjust your upper values for the optimization to correspond to the backstop prices. Happy to share some code if you end up taking this approach, (2) I’ve generally found the savings rates have very little impact on the results, so including them may just be making it harder for the optimization to converge to a solution, and (3) @davidanthoff I have no clue what the `@view` code is doing. Can you provide some intuition for why this is better? Is this a Mimi thing or a Julia thing?

---

<div class="post-metadata">

### Author: ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)
#### Post date: [June 7, 2019, 1:41am UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/5 "2019-06-07T01:41:53Z")

</div>

@view is a Julia thing, it’s an optimization to prevent unnecessary allocation of new arrays, I’ll let @davidanthoff elaborate

---

<div class="post-metadata">

### Author: ![BerBastien](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/berbastien/32/26_2.png) [@BerBastien](https://forum.mimiframework.org/u/BerBastien)
#### Post date: [June 7, 2019, 6:15am UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/6 "2019-06-07T06:15:30Z")

</div>

Thanks for your help!

---

<div class="post-metadata">

### Author: ![davidanthoff](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/davidanthoff/32/5_2.png) [@davidanthoff](https://forum.mimiframework.org/u/davidanthoff)
#### Post date: [June 7, 2019, 7:32pm UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/7 "2019-06-07T19:32:21Z")

</div>

Here is the story about `@view`. If you have an array `A = [1,2,3,4]`, and you write `x = A[2:3]`, then julia will under the hood create a new array and copy the subset of data from `A` that you referenced in the brackets into that new array, and then `x` points to this new array. At that point, `A` and `x` just point to different arrays that don’t have anything to do with each other.

`x = @view A[2:3]` (or equivalently `x = view(A, 2:3)`) on the other hand does _not_ create an new array. Instead it returns something that behaves like an array, but actually is an alternative “view” into the original array `A`. So no data is copied, `x` now just is a way to access a subset of `A` with slightly different indexing offsets.

There are two major differences between these two approaches: 1) views allocate no new memory, so generally speaking you put a lot less pressure on the memory management side of julia which can help a lot with performance. 2) if you edit a value in the view (e.g. you write `x[1]=10`), then you are actually modifying that value in `A`, so if you then look at the content of `A`, you will see that edit there as well. In the first example that didn’t use views, though, any edit you make to `x` won’t show up in `A` because those are just different arrays.

---

<div class="post-metadata">

### Author: ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)
#### Post date: [June 7, 2019, 9:06pm UTC](https://forum.mimiframework.org/t/optimization-of-two-parameters/62/8 "2019-06-07T21:06:40Z")

</div>

@davidanthoff Thanks, this is super useful to know!
