For loop error

I’m still new to Julia, and am having problems with a for loop. I’m trying to create an array where each value is the former value minus another value. This is the code:

for i = 1:T 
     bal[i] = bal[i-1] - A[i]

where bal and A are arrays of the same size. The error I get is “MethodError: no method matching -(::VariableRef, ::Array{VariableRef,1})”

Please help, thanks!

It would be great if you could provide an MWE - the error is telling you that for the type of variables you’re using, a subtraction operation is not defined, but without more context it’s hard to say where these types come from and why (if at all) you’d need them.

1 Like

Oh alright, thanks for the link to the page as well. Here’s an MWE, I hope this would be enough.

using JuMP

m = Model()
T = 300

@variables(m, begin
    delta0 >= 0
    amort[i=1:T] >= 0
	bal[0:T] >= 0

amort = delta0/T
bal[0] = delta0

for i = 1:T
	bal[i] = bal[i-1] - amort

If there is a better way to do this please let me know.


Running your code seems to fail with a different error for me

for i = 1:T 
    bal[i] = bal[i-1] - amort

ERROR: MethodError: Cannot `convert` an object of type GenericAffExpr{Float64,VariableRef} to an object of type VariableRef
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:154
  VariableRef(::Any, ::Any) at C:\Users\####\.julia\packages\JuMP\ibcEh\src\variables.jl:99

So what’s happening here is:

julia> typeof(bal)

julia> typeof(bal[0])

julia> typeof(amort)

julia> typeof(bal[0] - amort)

Subtracting amort from an element in your bal array produces an object of type GenericAffExpr{Float64, VariableRef}, which then can’t be assigned to the JuMP DenseAxisArray which is expecting an object of type VariableRef

Unfortunately I’ve never really used JuMP so I’m not sure what a workaround might be. Maybe you could explain what you’re trying to do?

Oh alright, yeah I’m getting the same error too actually, and I sort of understand why it gives an error, but I’m not sure what else to do. I don’t think this particular issue is related to JuMP tho. So here’s basically what I want.

Let’s say I have an amount of $10 at time period t = 0, and at the beginning of each period I want to deduct $1. So the formula would be at time t, amount owned would be the amount at period (t-1) minus the $1, correct? How would I do this in Julia? In my code, bal is the amount and amort is the amount to be subtracted.

Would really appreciate any advice, thanks!

That’s simple enough, although I’m not sure how it relates to what you’re trying to do in JuMP - what you’re trying to do works if you do it with regular numbers (e.g. Float64) rather than references:

balance = zeros(10)
balance[1] = 10 # initial amount owed
amort = 1 # repayment per period

julia> for i in 2:length(balance)
           balance[i] = balance[i-1] - amort

julia> balance
10-element Array{Float64,1}:

Thank you! I completely forgot about trying regular numbers instead of JuMP variables.

1 Like

That is an impressive commitment to optimization - glad it works now!

1 Like