Click here to Skip to main content
15,894,362 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
cp98765-Apr-07 16:23
cp98765-Apr-07 16:23 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
David Crow6-Apr-07 2:39
David Crow6-Apr-07 2:39 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
cp98766-Apr-07 3:01
cp98766-Apr-07 3:01 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
David Crow6-Apr-07 3:23
David Crow6-Apr-07 3:23 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
cp98766-Apr-07 3:47
cp98766-Apr-07 3:47 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
Bassam Abdul-Baki6-Apr-07 5:52
professionalBassam Abdul-Baki6-Apr-07 5:52 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
samv986-Apr-07 6:09
samv986-Apr-07 6:09 
GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
cp98768-Apr-07 21:49
cp98768-Apr-07 21:49 
Have a look at this algorithm
http://www.netlib.org/toms/632[^]

(I hope fortran is within your definition of 'any language except assembly').

I believe this code is public domain, and is documented in TOMS (detailed ref in comments).

It actually solves a more general problem,

      SUBROUTINE MKP(N,M,P,W,K,BCK,XSTAR,VSTAR)<br />
C<br />
C SUBROUTINE TO SOLVE A 0-1 MULTIPLE KNAPSACK PROBLEM OF  N<br />
C ITEMS (WITH  N .GE. 2) AND  M  KNAPSACKS (WITH  M .GE. 1 ,<br />
C I.E. ALSO SUITABLE FOR A 0-1 SINGLE KNAPSACK PROBLEM).<br />
C THE PROBLEM TO BE SOLVED IS:<br />
C<br />
C MAXIMIZE  VSTAR = P(1)*(X(1,1) + ... + X(M,1)) + ...<br />
C                   ... + P(N)*(X(1,N) + ... + X(M,N))<br />
C SUBJECT TO<br />
C W(1)*X(I,1) + ... + W(N)*X(I,N) .LE. K(I)   FOR  I=1,...,M<br />
C X(1,J) + ... + X(M,J) .LE. 1   FOR  J=1,...,N<br />
C X(I,J) = 0 OR 1   FOR  I=1,...,M ,  J=1,...,N ,<br />
C<br />
C WITH ALL  P(J) ,  W(J)  AND  K(I)  POSITIVE INTEGERS.


i.e. if you have all your partial pallettes {a1, a2, .., aN}
set W(1) = P(1) = a1, ..., W(N) = P(N) = aN

so it will actually optimally pack M orders at a time with quantities K(1) ... K(M). (This is why it is called a multiple knapsack problem.) It works with M=1 (your problem), and the runtimes apparently increase significantly as M increases.

I tried it out, using g77 on my 700MHz linux box (the only fortran compiler I have). I just wrote a simple loop to randomly generate the partial pallettes, each with a value 1 - 100, with single knapsack packing goal of 100, and ran it for 1000 problems. If you want this code let me know. The runtimes were (M=1):
<br />
N	Runtime(1000 problems)<br />
10	50ms<br />
20	80ms<br />
30	100ms<br />
50	150ms


Although the runtime is not bounded (some sets of numbers may take much longer), you could play with the BCK parameter which limits how many times the algorithm can backtrack to find the best answer, I just left it set to -1 with is supposed to find the best fit. Note that as N increased it almost always managed to find a perfect match.








Peter

"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

GeneralRe: Find Closest Sum of Numbers to a Given Number Pin
samv989-Apr-07 9:06
samv989-Apr-07 9:06 
QuestionMath (statistics) problem that I just can't seem to get a grasp on Pin
David Crow4-Apr-07 4:00
David Crow4-Apr-07 4:00 
AnswerRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
CPallini4-Apr-07 4:08
mveCPallini4-Apr-07 4:08 
AnswerRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
Dan Neely4-Apr-07 4:13
Dan Neely4-Apr-07 4:13 
AnswerRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
Rob Graham4-Apr-07 4:19
Rob Graham4-Apr-07 4:19 
GeneralRe: Math (statistics) problem that I just can't seem to get a grasp on [modified] Pin
cp98764-Apr-07 17:59
cp98764-Apr-07 17:59 
GeneralRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
Rob Graham5-Apr-07 13:22
Rob Graham5-Apr-07 13:22 
GeneralRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
cp98765-Apr-07 16:07
cp98765-Apr-07 16:07 
AnswerRe: Math (statistics) problem that I just can't seem to get a grasp on Pin
Frank Kerrigan2-May-07 6:33
Frank Kerrigan2-May-07 6:33 
QuestionAngle Algorithm Pin
Kushi Bobby4-Apr-07 1:23
Kushi Bobby4-Apr-07 1:23 
AnswerRe: Angle Algorithm Pin
CPallini4-Apr-07 1:46
mveCPallini4-Apr-07 1:46 
GeneralRe: Angle Algorithm Pin
cp98764-Apr-07 2:06
cp98764-Apr-07 2:06 
GeneralRe: Angle Algorithm Pin
Dan Neely4-Apr-07 2:11
Dan Neely4-Apr-07 2:11 
GeneralRe: Angle Algorithm Pin
CPallini4-Apr-07 4:04
mveCPallini4-Apr-07 4:04 
GeneralRe: Angle Algorithm Pin
cp98764-Apr-07 18:09
cp98764-Apr-07 18:09 
GeneralRe: Angle Algorithm Pin
CPallini4-Apr-07 21:05
mveCPallini4-Apr-07 21:05 
GeneralRe: Angle Algorithm Pin
cp98764-Apr-07 21:53
cp98764-Apr-07 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.