How can I get around this?
It is odd to me as well that the prob.Variables property is read-only. However, I notice things are pretty easy if you haven't discarded the OptimizationVariable objects used to build prob. The optimvars are handles, so you can just change those directly and the changes will be reflected in the prob object as well. For example,
>> x=optimvar('x',1,2); prob.Objective=sum(x);
>> prob.Variables.x.LowerBound
ans =
-Inf -Inf
>> x.LowerBound=[2,3]; prob.Variables.x.LowerBound
ans =
2 3
If you have discarded the original OptimizationVariable objects, then you can recover them from the prob object and do likewise,
>> x=prob.Variables.x; x.LowerBound=[-7,-9]; prob.Variables.x.LowerBound
ans =
-7 -9
There are ways to wrap these operations in functions so that you can do them in a single command, of course.
Another issue I found was that I could not add variables to the problem, for the same reason.
Didn't understand this part. You mean you want to add variables to the problem that don't explicitly appear in either the objective or constraints? If so, why?
Best Answer