# Solve it, and Solve it Right!

Now for something a little different. As some of you may know, most of the free time that I spend in front of a computer is absorbed by MATLAB Answers. So this week I am going to highlight one of my favorite questions and some of the answers to it.

Sean's pick this week is Seth's Answer to Sven's Question about numerical packing.

### Background on The Question (Personal Narrative)

I started working at MathWorks in late November, 2011. When Sven asked this question a few weeks later, my wife had not yet moved to Massachusetts. It was December in my temporary not-so-energy efficient house so I was saving costs by keeping the house at 50F (10C) and staying late at work.

This question came in at the end of the work day and immediately caught my attention. I spent the next five hours coming up with an answer.

A year or so later, Seth, our Optimization Toolbox Product Marketing Manager, was showcasing some new technology to the engineers where he debuted the Sudoku Solver that Alan wrote about a few weeks ago and the solution to the Traveling Sales Man problem. This actually interested me more since it brought back memories of Transportation Engineering classes in college. This new technology was still a few releases away from being ready but the potential was already obvious. I sent Seth this question to see if it would be a good candidate and he replied with the optimal answer in about an hour.

When R2014a was release earlier this Month, he agreed to post his answer.

### Background on the Question (Technical)

So what is the question? The goal of the question is to optimally group some elements into bins to minimize both the number of bins and the size of the biggest bin. Each bin has a maximum size and the elements can not be rearranged from their original order. This is a type of optimization problem known as a knapsack problem (thanks Walter!). The first answer to roll in came from Elige Grant. He posted a way to do this with brute force. For small problems, this will be quick enough and the optimal solution will obviously be found and guaranteed.

As the problem size grows, however, brute force starts to become cumbersome in terms of computational resources. My solution was to use the only solver at the time that we had for mixed integer optimization, ga, the genetic algorithm solver. This solver has an option, 'IntCon', to constrain variables to integers, a requirement. I'd also only been able to have access to the Global Optimization Toolbox for the last few weeks, so this was a chance to play with something new. It did work too (!), for small problems. Once again, as the problem size grew, this approach fell apart and there's no way to prove optimality (or lack thereof) from it.

Enter intlinprog.

In R2014a we introduced intlinprog, a mixed-integer linear programming solver. This solver will solve any mixed-integer linear programming problem, be it a minimization or a feasibility study. If requested it will also prove that the solution is optimal for a minimization problem.

Let's examine how it's done:

% The call to |intlinprog| looks like this:
[xopt,fval,eflag,output] = intlinprog(f, intIdxs, A, b, Aeq, beq, lb, ub);
LP:                Optimal objective value is 765.000000.

Cut Generation:    Applied 10 implication cuts.
Lower bound is 765.000000.

Heuristics:        Found 1 solution using rss.
Upper bound is 939.000000.
Relative gap is 18.50%.

Cut Generation:    Applied 2 gomory cuts,
and 7 implication cuts.
Lower bound is 785.000000.
Relative gap is 16.37%.

Branch and Bound:

nodes     total   num int        integer       relative
explored  time (s)  solution           fval        gap (%)
9      0.01         2   9.220000e+02   1.044609e+01
12      0.01         2   9.220000e+02   0.000000e+00

Optimal solution found.

Intlinprog stopped because the objective value is within a gap tolerance of the
optimal value; options.TolGapAbs = 0 (the default value). The intcon variables
are integer within tolerance, options.TolInteger = 1e-05 (the default value).


• f, objective function coefficients, zeros
• intIdxs, the integer constraints, are the integers between one and 52 meaning every variable is integer constrained
• A, contribution of each element to a bin, and that it must be in a bin greater than or equal to the previous element
• b, is the upper limit on the bin sizes
• Aeq, each element for each of the bins
• beq is all ones, each element is in only one bin
• lb, the lower bound, is all zeros
• ub, the upper bound, is all ones

We can see the sparsity patterns of A and Aeq the linear and equality constraints using spy()

subplot(2,1,1)
spy(A)
title('Linear Constraints')
subplot(2,1,2)
spy(Aeq)
title('Equality Constraints') Now let's look at output

output
output =
relativegap: 0
absolutegap: 0
numfeaspoints: 2
numnodes: 12
constrviolation: 1.7764e-15
message: 'Optimal solution found.



Since absolutegap is 0, this is the best solution.

If we were curious on time, that took about 0.037s to run!

Do you have a problem that could be solved with mixed-integer linear programming, or one that you're not sure about and would like our thoughts on? Let us know about it here.

Also, if you liked this post, and you're still reading at this point (which probably biases the data): Would you like me to write another out of model Pick Of The Week post that highlights an intlinprog practical example?

Published with MATLAB® R2014a

|
{"ft_lang_label":"__label__en","ft_lang_prob":0.93792474,"math_prob":0.8637254,"size":5450,"snap":"2022-05-2022-21","text_gpt3_token_len":1291,"char_repetition_ratio":0.10246052,"word_repetition_ratio":0.0021668472,"special_character_ratio":0.23779817,"punctuation_ratio":0.12956205,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9716395,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-26T17:31:16Z\",\"WARC-Record-ID\":\"<urn:uuid:573dffb7-0f24-4326-8dca-91b938ed055e>\",\"Content-Length\":\"167297\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5d0ba78d-2e1f-443a-80d9-c895ab1b96e8>\",\"WARC-Concurrent-To\":\"<urn:uuid:d2c44aed-bb6f-404d-9722-f0ea40e54121>\",\"WARC-IP-Address\":\"23.1.9.244\",\"WARC-Target-URI\":\"https://blogs.mathworks.com/pick/2014/03/28/solve-it-and-solve-it-right/\",\"WARC-Payload-Digest\":\"sha1:TGJ2Q7BTDXPSSOW3BOD2HV7W6WRZEIVL\",\"WARC-Block-Digest\":\"sha1:6RKYHDWIO63E7SERNCX2G4SGHGAINZ3L\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662619221.81_warc_CC-MAIN-20220526162749-20220526192749-00073.warc.gz\"}"}