MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Peter
on 12 Dec 2012

The user inputs 10 length values (1-10) and inputs another value length A which is a total length. Values 1-10 can range from 0 to infinity which are the quantities of lengths and input A is the maximum length which is larger then the largest length from value 1-10.

The optimization comes in where I need to figure how many values of A are need to cover all the lengths but keeping the scrap value of A to a minimum. Example A=25 and value 2= 3(qty) and value 5 = 3(qty) the sum of values 2 and 5 = 21 so the left over of A is 4.

I could have time were only two inputs of values 1-10 being used up to having all ten values being used.

I can't add the left overs of A together to eliminate more values. I need to output the combinations of values 1-10, the number of Value A and the total left overs of A added together. The combinations of values 1-10 can be placed into an array and the number of rows will tell me how many Values of A will be needed.

I've started to use a while loop with a counter to keep track of the numbers of A's required but I'm not sure how to write the proper code to make the proper optimization of the values into A keep A and the left over a minimum.

Any help would be greatly appreciated and if any clarification is need just ask.

Thank you very much.

Answer by Walter Roberson
on 12 Dec 2012

Accepted answer

A small number of people have asked about the same kind of problem in the past. I am not sure how they solved it. I think some of them used a PSO (particle swarm) approach.

There *is* a MATLAB routine in the optimization toolbox that could in theory be used. See Binary Integer Programming

To get an idea of how to implement this using bintprog(), see http://www.mathworks.com/help/optim/ug/binary-integer-programming-example.html

## 2 Comments

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/56361#comment_116665

This is a "knapsack with repeated elements" problem. It is known to be non-polynomial to solve.

## Peter (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/56361#comment_116668

Is there any prebuilt functions in Matlab that deal with something like this?