Clear Filters
Clear Filters

optimproblem object: Change variable bounds

3 views (last 30 days)
"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
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.
Opt User
Opt User on 24 Jan 2018
The way I see optimproblem is as an optimization programming language within Matlab (like YALMIP). Otherwise I do not see its application beyond small problems, considering the understandable limitations of Matlab solvers. Another way to implement this would be for the method optimproblem.writeproblem to output into a standard mathematical programming format such .mip and .nl (what is the current output for anyway?).

Sign in to comment.

Accepted Answer

Matt J
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
Opt User
Opt User on 23 Jan 2018
Thanks for your solution, I will try it as soon as I can and then accept. Regarding the second comment: I want to add variables, pass the problem object to a function, and then add the constraints/objective containing those variables. It is simply for the sake of clarity.
Matt J
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]

Sign in to comment.

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations 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!