Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: `*` has no method matching *(::Variable)

I wrote the following code:

using JuMP

m = Model()

const A = 
[ :a0 ,
  :a1 , 
  :a2 ]

const T = [1:5]

const U = 
[
  :a0 =>    [9  9  9  9  999],
  :a1 =>    [11 11 11 11 11],
  :a2 =>    [1  1  1  1  1]
]

@defVar(m, x[A,T], Bin)

@setObjective(m, Max, sum{sum{x[i,j] * U[i,j], i=A}, j=T} )

print(m)

status = solve(m)

println("Objective value: ", getObjectiveValue(m))
println("x = ", getValue(x))

When I run it I get the following error

ERROR: `*` has no method matching *(::Variable)
 in anonymous at /home/username/.julia/v0.3/JuMP/src/macros.jl:71
 in include at ./boot.jl:245
 in include_from_node1 at loading.jl:128
 in process_options at ./client.jl:285
 in _start at ./client.jl:354
while loading /programs/julia-0.2.1/models/a003.jl, in expression starting on line 21

What's the correct way of doing this?

like image 811
HAL9000 Avatar asked Jul 30 '26 08:07

HAL9000


2 Answers

As the manual says:

There is one key restriction on the form of the expression in the second case: if there is a product between coefficients and variables, the variables must appear last. That is, Coefficient times Variable is good, but Variable times Coefficient is bad

Let me know if there is another place I could put this that would have helped you out.

This situation isn't desirable but unfortunately we haven't got a good solution yet that retains the fast model construction capabilities of JuMP.

I believe the problem with U is that it is a dictionary of arrays, thus you first need to index into the dictionary to return the correct array, then index into the array. JuMP's variables have more powerful indexing, so allow you to do it in one set of [].

like image 144
IainDunning Avatar answered Aug 02 '26 19:08

IainDunning


I resolved my problem: constants must preceed variables as I read somewhere, moreover it seems that an array of constants must be used as an array of arrays while variables can be used as matrices.

Here's the correct line:

@setObjective(m, Max, sum{sum{U[i][j]*x[i,j], i=A}, j=T} )
like image 29
HAL9000 Avatar answered Aug 02 '26 18:08

HAL9000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!