optimproblem object: Change variable bounds
3 views (last 30 days)
Show older comments
"OptimizationProblem property 'Variables' is read-only". That is the message I get after trying to change something like:
prob.Variables.x.LowerBound = lb
where prob is an optimproblem object.
How can I get around this? Another issue I found was that I could not add variables to the problem, for the same reason. I got around this by adding dummy constraints with all zero coefficients. Overall I like this new tool, it is very convenient, but there should be a way to manipulate variables easily. I am using 2017b.
5 Comments
Matt J
on 23 Jan 2018
If you can get it to work for you with other solvers, great, but it is outside of the current design scope to do so.
That seems to throw into question the purpose of offering a prob2struct() method in the first place.
On the other hand, I don't think it would be difficult for the user to reconstruct the connection between the problem-based names and the solver form, assuming TMW guarantees that the ordering of the variables/constraints in both forms will always be the same. I think this is undocumented, currently.
Accepted Answer
Matt J
on 23 Jan 2018
Edited: Matt J
on 23 Jan 2018
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?
2 Comments
Matt J
on 23 Jan 2018
Edited: Matt J
on 23 Jan 2018
OK, but then it seems to me that all you should have to do is modify prob.Objective or prob.Constraints. The new variables participating in them will automatically be added to the list, for example,
>> prob.Variables
ans =
struct with fields:
x: [1×2 optim.problemdef.OptimizationVariable]
>> y=optimvar('y'); prob.Objective=sum(prob.Variables.x)+y; prob.Variables
ans =
struct with fields:
x: [1×2 optim.problemdef.OptimizationVariable]
y: [1×1 optim.problemdef.OptimizationVariable]
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!