XiongPengNUS / rsome

Robust Stochastic Optimization Made Easy
GNU General Public License v3.0
282 stars 54 forks source link

Compile Error #14

Closed xihaizhang closed 2 years ago

xihaizhang commented 2 years ago

when I compile the file, it gives a message that" Traceback (most recent call last): File "C:\Users\zhang\Documents\Pycharm\rsome\test_mv.py", line 44, in model.solve(grb) # solve the model by Gurobi File "E:\Python\lib\site-packages\rsome\dro.py", line 656, in solve solution = solver.solve(self.do_math(), display, export, params) File "E:\Python\lib\site-packages\rsome\grb_solver.py", line 39, in solve x[index_right] @ x[index_right]) TypeError: unsupported operand type(s) for @: 'Var' and 'Var'"

My code is a demo from the DRO

"from rsome import dro from rsome import norm from rsome import E from rsome import grb_solver as grb import numpy as np import numpy.random as rd

model and ambiguity set parameters

I = 2 S = 50 c = np.ones(I) d = 50 I p = 1 + 4rd.rand(I) zbar = 100 rd.rand(I) zhat = zbar rd.rand(S, I) theta = 0.01 * zbar.min()

modeling with RSOME

model = dro.Model(S) # create a DRO model with S scenarios z = model.rvar(I) # random demand z u = model.rvar() # auxiliary random variable

fset = model.ambiguity() # create an ambiguity set for s in range(S): fset[s].suppset(0 <= z, z <= zbar, norm(z - zhat[s]) <= u) # define the support for each scenario fset.exptset(E(u) <= theta) # the Wasserstein metric constraint pr = model.p # an array of scenario probabilities fset.probset(pr == 1/S) # support of scenario probabilities

x = model.dvar(I) # define first-stage decisions y = model.dvar(I) # define decision rule variables y.adapt(z) # y affinely adapts to z y.adapt(u) # y affinely adapts to u for s in range(S): y.adapt(s) # y adapts to each scenario s

model.minsup(-p@x + E(p@y), fset) # worst-case expectation over fset model.st(y >= 0) # constraints model.st(y >= x - z) # constraints model.st(x >= 0) # constraints model.st(c@x == d) # constraints

model.solve(grb) # solve the model by Gurobi "

DanieleGioia commented 2 years ago

Hi, I'm dealing with the same issue.

I'm working on a two-stage DRO model with a bound on the variances. Once I noticed the issue I tried the given DRO example on portfolio optimization in the DRO guide. That one doesn't work as well due to the same error ( unsupported operand type(s) for @: 'Var' and 'Var'" ) on the squared constraints on the support.

(Nb: that example has some other errors easy to solve like bounds defined as z then used as d and pandas .std() called as .sigma() )

Looking into the code, the problem could also be a Gurobi related one. It is generated when the conic constraints are added to the model. The critic function (and constraint) is rsome.square() and the squared norm too. No issues with 1-norm (that does not involve SOC).

I was not able to fix it so far. Hopefully, the authors will give us a solution.

COMMENT UPDATE: Actually, it turns out to be a Gurobi communication problem. WIth MOSEK everything is fine.

XiongPengNUS commented 2 years ago

The bug is caused by the new Gurobi 9.5 solver. One solution is to downgrade to lower version of Gurobi solvers. Another solution is to upgrade RSOME, as we have fixed the bug in the RSOME 0.1.6.