• Gurobi Staff

Hi Jonathan,

Can you try calling grbModel.update() before changing the coefficient? You can read more about this in the "Lazy Updates" section here. Essentially, if you want to immediately access a variable/constraint attribute or change some part of the model, you should first update the model.

If this doesn't work, could you post a small working code example that reproduces the problem? Thanks!

Eli

• Yes, I did update the model first.  I'll try to post a self-contained example soon.

• Here is a 12-line program that produces the same error.  Possibly I am doing something really dumb, but it is my first time with this API.  Optimizing the model instead of updating doesn't change the error.

Thanks!  -- Jonatha

import numpy
import gurobipy as gp
from gurobipy import GRB

n = 10

m = gp.Model()
grbx = m.addMVar(shape=n, lb=-1.0, ub=1.0, vtype=GRB.CONTINUOUS, name="grbx")
randomMatrix = numpy.random.rand(20,10)
Q = numpy.transpose(randomMatrix) @ randomMatrix
m.setObjective(0.5*(grbx @ Q @ grbx))
constr = m.addConstr(grbx + grbx + grbx <= 1.0)
m.update()

m.chgCoeff(constr,grbx,-2.0)

• Gurobi Staff

Hi Jonathan,

Ah, yes, you're right that this is because you are passing an $$\texttt{MVar}$$ object instead of a $$\texttt{Var}$$ object. I missed that in my first (hasty) reading of your post. I don't believe there is an analogous method for $$\texttt{MVar}$$ objects. I'll look into this and get back to you.

Thanks,

Eli

• OK, thanks!  Please let me know what you find out.

Setting up my objective function with only scalar variables would be hellish.  I suppose that I could make big pile of scalar variables, each with an equality constraint to an element of the matrix variable.  That would not be pretty.  Or I guess I could simply delete all my constraints and then add in new ones.

I have to solve a bunch of QP problems, each of which some different variable bounds and linear constraint coefficients.

• Gurobi Staff

Hello Jonathan,

as written by Eli there currently is no way to change coefficents w.r.t. an Mvar object, but I have registered this in our backlog of future improvements to the matrix-friendly API.  Your suggestion to delete the old constraints, modify the coefficients on the ndarray level, and add them back to the model seems to be the most practical workaround to me.

Robert

• OK, thanks for checking.  I will just delete the constraints and add back modified ones.  I suspect that will be slow, but this is a prototype, proof-of-concept implementation, so its speed is not absolutely critical.

• Dear Robert and Eli,

does this feature (change coefficients w.r.t. an Mvar object) already exist in the latest version of Gurobi (9.1)?

Thank you!

Chris.

• Gurobi Staff

Unfortunately not. However, there is an easy workaround available in Gurobi 9.1. Namely, you can use MVar.tolist() to convert MVar objects to Var objects for use in Model.chgCoeff(). Similarly, you can convert MConstr objects to Constr objects with MConstr.tolist(). For example:

import numpy as np
import gurobipy as gp

m = gp.Model()
a = np.array([1.0, 2.0, 3.0])

# Add constraint x + 2 x + 3 x <= 1
c = m.addConstr(a @ x <= 1)
m.write('model1.lp')

# Change to 5 x + 2 x + 3 x <= 1
m.chgCoeff(c.tolist(), x.tolist(), 5)
m.write('model2.lp')
• Gurobi Staff

That said, the workaround should no longer be necessary starting in the next feature release after 9.1, where you will be able to just do

m.chgCoeff(c, x, 5)

This works because indexing an MVar/MConstr will yield a Var/Constr object instead of a (1,) slice.

• Thank you both! I am looking forward to the new release but for the time being Eli's suggested work-around will do it too.

• Gurobi Staff

Hi Chris,

Gurobi 9.5 was recently released. Included in this release is a new feature where indexing an MVar/MConstr returns a Var/Constr object.

We hope this fix works well for you. Please let us know if you see any other issues using this.

Best regards,

Maliheh
{"ft_lang_label":"__label__en","ft_lang_prob":0.8619441,"math_prob":0.8017045,"size":5031,"snap":"2022-05-2022-21","text_gpt3_token_len":1358,"char_repetition_ratio":0.104038194,"word_repetition_ratio":0.0121654505,"special_character_ratio":0.2574041,"punctuation_ratio":0.15595463,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9612322,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-21T09:05:29Z\",\"WARC-Record-ID\":\"<urn:uuid:61aa7d51-c73c-447a-96f9-380e6278d4da>\",\"Content-Length\":\"88285\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4a319c9a-34c5-47fc-aa4d-1f06aafe5cff>\",\"WARC-Concurrent-To\":\"<urn:uuid:f29f03bf-86d8-458a-8f57-90a92edc00bd>\",\"WARC-IP-Address\":\"104.16.53.111\",\"WARC-Target-URI\":\"https://support.gurobi.com/hc/en-us/community/posts/360068782212-Python-call-to-chgCoef-yields-attribute-error-for-cindex-?page=1#community_comment_360011055592\",\"WARC-Payload-Digest\":\"sha1:W2FJPGT26XJY3VH5QXO2TUORUD5GBO7T\",\"WARC-Block-Digest\":\"sha1:M3S6H2AW7AYVRLA2GFOV5ME64UN4IXGT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320302740.94_warc_CC-MAIN-20220121071203-20220121101203-00505.warc.gz\"}"}