When a problem has integer constraints,
intlinprog to obtain the solution. For suggestions on obtaining a faster
solution or more integer-feasible points, see Tuning Integer Linear Programming.
Before you start solving the problem, sometimes you can improve the formulation of your problem constraints or objective. Usually, it is faster for the software to create expressions for objective function or constraints in a vectorized fashion rather than in a loop. Suppose that your objective function is
x is an optimization variable, and
c are constants. Two general ways to formulate this objective
function are as follows:
for loop. In this case,
expr = optimexpr; for i = 1:30 for j = 1:30 for k = 1:10 expr = expr + x(i,j,k)*b(k)*c(i,j); end end end
expr contains the objective function expression.
While this method is straightforward, it can take excessive time to loop through
many levels of
Use a vectorized statement. Vectorized statements generally run faster than a
for loop. You can create a vectorized statement in
c. To enable
term-wise multiplication, create constants that are the same size as
bigb = reshape(b,1,1,10); bigb = repmat(bigb,30,30,1); bigc = repmat(c,1,1,10); expr = sum(sum(sum(x.*bigb.*bigc)));
Loop once over
expr = optimexpr; for k = 1:10 expr = expr + sum(sum(x(:,:,k).*c))*b(k); end
Create an expression differently by looping over
and then summing terms after the loop.
expr = optimexpr(30,30,10); for k = 1:10 expr(:,:,k) = x(:,:,k).*c*b(k); end expr = sum(expr(:));