# How to avoid repmat in problem-based optimization?

2 views (last 30 days)
I am using the problem-based workflow of the Optimization toolbox for solving a MILP problem. Variables x's size is 1xn.
x = optimvar ('x', [1,n]);
Matrix D's size is mxn. I want to create efficient problem-based optimization problem (described here) by avoiding for loops and defining equations in a vectorized fashion. I used either of the following:
Eq1 = D .* x <= 1;
and
Eq1 = x .* D <= 1;
And got the following error.
% Argument dimensions 20-by-3 and 1-by-3 must agree.
It seems like the problem-based optimization setup does not support automatic expansion of optimization variables, and I had to manually expand x for the program to work:
Eq1 = D .* repmat(x,[m,1]) <= 1;
My question is that, although I was able to solve the problem, is there a way to do define such equations without explicit expansion? Like what MATLAB does for doubles.

Matt J on 28 Aug 2020
Edited: Matt J on 28 Aug 2020
You can do things like,
Eq1 = D.*(ones(m,1)*x)<= 1;
I don't think it will matter much, though. The use of the problem-based framework is itself inefficient, and the use of repmat will probably just be an extra drop in the bucket.
If you really care about maximizing efficiency, you would set up the optimization directly in solver form. The problem-based framework is really just a convenient conversion tool, allowing you to set-up the problem with more intuitive syntax. Ultimately, everything gets translated into solver-based form, so it will be more but efficeint if you just set the problem up directly in the solver-based domain yourself.
Abdolkarim Mohammadi on 28 Aug 2020
Thanks Matt. You are right. I have been using the solver-based in the past years. I have been recently into the problem-based approach and I found it quite useful, especially for colleagues with less experience to have all of our works right in MATLAB. GAMS' gdxmrw is very useful but problem-based optimization can help have many functionalities here. I thought I am doing something wrong by using repmat since I expected the regular automatic array expansion to work here.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!