# Optimization of a decision vector of arbitrary dimension

**URL:** <https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846>\
**Category:** Optimization (Mathematical)\
**Created:** [August 23, 2021, 2:15pm UTC](https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846 "2021-08-23T14:15:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![etas](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@etas](https://discourse.julialang.org/u/etas)\
**Post date:** [August 23, 2021, 2:15pm UTC](https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846/1 "2021-08-23T14:15:00Z")

</div>

Hi,  
For an optimization problem having time dependent variables (i.e. large vectors), I’m trying to define the problem like below, with variables constrained by vectors, and optimize one variable.

```julia
using JuMP
using CPLEX

p_DCL = [160, 180]

data_length = length(p_DCL)

lb_c1 = 0
ub_c1 = 180
lb_c2 = -800
ub_c2 = 200

model = Model(CPLEX.Optimizer)

@variables(model, begin
    lb_ICE[i] <= p_ICE[i = length(data_length)] <= ub_ICE[i]
    lb_Batt[i] <= p_Batt[i = 1:length(data_length)] <= ub_Batt[i]
end)

@constraint(model, [i = 1:length(data_length)], p_ICE[i]+p_Batt[i]==p_DCL[i])

@objective(model, Min, p_Batt)

optimize!(model)

```

But I have an error of type `The objective function `VariableRef[p\_Batt[1]]` is not supported by JuMP.` I’m quite sure this is related to how I define the variables and constrain them, but I’m not familiar with JuMP and could not find an alternative.  
Thanks for your help !

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [August 23, 2021, 2:43pm UTC](https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846/2 "2021-08-23T14:43:40Z")

</div>

I cannot run the code because the lower and upper bounds in variables are undefined. Also, according to the code, why is `p_Batt` defined as a vector of length 1? The value in `data_length` tells us how long the vector `p_DCL` is, so it should be a number. Or did you mean `p_Batt[i = 1:data_length]`?

Basically, this runs:

```julia

using JuMP
using Ipopt

p_DCL = [160, 180]

data_length = length(p_DCL)

lb_c1 = 0
ub_c1 = 180
lb_c2 = -800
ub_c2 = 200

model = Model(Ipopt.Optimizer)

@variables(model, begin
    0.0 <= p_ICE[i = length(data_length)] <= 1000.0
    0.0 <= p_Batt[i = 1:length(data_length)] <= 1000.0
end)

@constraint(model, [i = 1:length(data_length)], p_ICE[i]+p_Batt[i]==p_DCL[i])

@objective(model, Min, p_Batt[1]) ####Changes here

optimize!(model)

```

This is with vectors:

```julia
using JuMP
using Ipopt ###Had to use a different solver, I don't have CPLEX

p_DCL = [160, 180]

data_length = length(p_DCL)

lb_c1 = 0
ub_c1 = 180
lb_c2 = -800
ub_c2 = 200

model = Model(Ipopt.Optimizer)

@variables(model, begin
    0.0 <= p_ICE[i = 1:data_length] <= 1000.0 ####Main changes here
    0.0 <= p_Batt[i = 1:data_length] <= 1000.0 ###And here
end)

@constraint(model, [i = 1:data_length], p_ICE[i]+p_Batt[i]==p_DCL[i]) ###and a bit here

@objective(model, Min, p_Batt[1]) ####If p_Batt is a vector I need to explicitly tell which element to minimise

optimize!(model)

```

---

<div class="post-metadata">

**Author:** ![etas](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@etas](https://discourse.julialang.org/u/etas)\
**Post date:** [August 24, 2021, 7:23am UTC](https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846/3 "2021-08-24T07:23:00Z")

</div>

Hi,

Yes sorry, it was a bad copy/paste for symplifying my boundaries.  
Your simple solution in the objective definition, made me see that I didn’t quite understood how JuMP worked. I will write the objective function as c’x and all should be ok.  
Thanks !

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 24, 2021, 7:31am UTC](https://discourse.julialang.org/t/optimization-of-a-decision-vector-of-arbitrary-dimension/66846/4 "2021-08-24T07:31:46Z")

</div>

You can also use `sum(p_Batt)` if you meant that instead.
