Optimization of a decision vector of arbitrary dimension

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.

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 !

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:


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:

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)

2 Likes

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 !

1 Like

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