Summation formulation

Hello, Apologies if this is not the right category for positing this question. I am not sure whether all linear algebra operations such as cumulative sum can be used with JuMP. I have given a working generic example to give some context and a JuMP example on what I am aiming to achieve.

I have monthly data in a vector and I want to create another vector with cumulative sums. So year 1 will be simple sum of the first 12, year 2 will be sum of first 24, and so on. I would also like to divide each element of this vector by another vector C[12i] where i is reference to the relevant entry in C.

I do get the expected result however, I wonder whether I really need to use two loops to achieve this or my formulation is unusual. At least it looks odd to me!

The second part of my question is really all about whether we could use vectors or arrays containing a variable x in the example that is being solved in optimisation? So essentially I am doing the same operation as in the first part. I have not progressed this part much as I suspect that it may require defining some other expressions than matrices.

Would appreciate advice from experienced members

A = rand(100.0:200.0,36)
B = Vector{eltype(A)}(undef,Int(length(A)/12))
C = rand(0.5:1.0,length(A))
D = similar(B)

# Calculate cumulative sum in blocks of 12, 24 and 36

for i in 1:length(B)
    B[i] += A[12i]
end

# Divide each element of B by each index of C that is a multiple of 12

for i in 1:length(B)
        D[i] = B[i]/C[12i]
end

# Do I really have to use this loop? Could this not be merged with the first? I don't get sensible result if I combine with the first

 # JuMP part

m= Model(SomeSolver.Optimizer)
@variable(m,x[1:4]>=0)
A = [rand(36,4)]
A = A*x

B = Vector{eltype(A)}(undef,Int(length(A)/12))
C = rand(0.5:1.0,length(A))
D = similar(B)

for i in 1:length(B)
    B[i] += A[12i]
end

for i in 1:length(B)
        D[i] = B[i]/C[12i]
end

for i in 1:lenght(B)
    @constraint(m,D[i]>= some_value)  # some_value could be just a number
end

Hi @Maria95, I’ve moved your question to the “Optimization (Mathematical)” section.

Here’s how I could write your first part, although I don’t understand why C has length(A) instead of N elements:

A = rand(100.0:200.0, 36)
N = Int(length(A) / 12)
# C = rand(0.5:1.0, length(A))  # I don't think this does what you think it does.
C = 0.5 + rand(length(A)) / 2
D = [sum(A[1:(12 * i)]) / C[12 * i] for i = 1:N]

The second part is:

A = rand(36, 4)
C = 0.5 * rand(length(A)) / 2
model = Model()
@variable(model, x[1:4] >= 0)
Ax = A * x
N = Int(length(Ax) / 12)
some_value = 0.0
@constraint(model, [i = 1:N], sum(Ax[1:(12 * i)]) / C[12 * i] >= some_value)

I’m not sure I fully understand your question, but hopefully this provides some insight on where to go next.

1 Like

Thank you @odow for the great answer.

I am sorry, I should have done homework first before asking this question. The first part of the question was to build the picture for the type of constraint I was looking to define and I hadn’t progressed as far as putting a toy example.

C has same length as A and hence the reason I need to extract values in each index that is a multiple of 12. It would have been simpler if it had the same length as N.

Most platforms support a limited set of operations on optimization variables and expressions, e.g. MATLAB provides a list: Supported Operations on Optimization Variables and Expressions - MATLAB & Simulink - MathWorks United Kingdom

I was trying to understand whether constraints need to be defined algebraically, i.e. coefficients of a constraint in one matrix and optimization variables in a separate array. If this not required, what operations (on matrix AX) like multiplication with other matrices I can do. You have effectively answered my specific question, so I will mark it as a solution.

I have been sketching how this constraint will operate in the type of examples I have in mind, and I will come back with questions once I have put together a toy example. I will scan through this forum to see if I can find a similar question, to make it easier to get help!

1 Like