Performance issues with matrix multiplication in JuMP

I am trying to do linear constraint optimisation as follows:

model = Model(Tulip.Optimizer)
    # Set up variables
    @variable(model, x[1:length(VecA)+length(VecB)])
    # set up linear constraints
    workingweights = x' * Mat' + VecC'
    # Equalities
    for i in 1:length(VecA)
        @constraint(model, workingweights * Mat[:,i] == VecA[i])
    # Inequalities
    for i in 1:length(VecB)
        xi = i+length(VecB)
        @constraint(model, workingweights * Mat[:,i] <= VecB[i])


Here, Mat is a matrix, and VecA, VecB, and VecC are vectors. The problem is I am getting the “addition operator has been used on JuMP expressions a large number of times…” I imagine the issue is in either the definition of workingweights or the @constraints declarations, but I am unsure how to fix this. This code is central in my program, and optimising this would help a lot.

It would help a lot if you could provide a complete, executable program so that other people can replicate your problem.


This is covered in the docs (but could probably be improved):

Always construct expressions inside a JuMP macro:

@expression(model, workingweights, x' * Mat' + VecC')

Can you give the types of the vectors involved in workingweights = x' * Mat' + VecC', even if it is outside a macro, we also try to make it fast but it’s harder so we may be missing a method in MutableArithmetics.jl