Thursday, July 28, 2016
T.s.road Summary notes: Packet knapsack problem MATLAB Implementation--The basic backpack expansion
Project Source: Https://github.com/Tsroad/KnapsackProblemSeries
Author's description:
Whenrunning Thisprogramme, the author ' s PC setting is:
MicrosoftWindows 7 (SP1) + Matlab r2010b +cpu i5-4590 + RAM 4.0GB.
(Lab SX309; Check by Keung Charteris or T.S.ROADCZQ)
Topic
The number of n is divided into two groups, and the difference between the two sets of numbers is minimal.
Basic Ideas
This is a knapsack problem (the last blog introduced the basic knapsack problem) of the deformation, backpack capacity C for SUM/2. The value and weight of number I are equal to the size of the number.
% problem Description:% 20 number, divided into two groups, the number of two sets of numbers and the difference is minimal. % problem analysis% 1. To judge the first number to be put or not;% 2. To judge the next number is release; M[i,c]=m[i-1,c] or m[i,c]= m[i-1,c-w (i)]+v (i);% 3. Repeat 2;% 4.
Find these numbers. CLC
% clears all clear all;% the variable close all;% closes the picture firstgroup=[];
Secondgroup=[];
BAGNUMBERPOINTS=[38,82,54,36,94,88,56,63,59,21,31,48,24,85,20,23,18,23,44,32];
Capacity=ceil (SUM (bagnumberpoints)/2) +1;% the capacity of the backpack weight=bagnumberpoints;% the number of weights value=bagnumberpoints;% the number corresponding to the price. Numberofobject =length (Weight); Number of% numbers transfermatrix=[];% define state transfer matrix objectstate=[];% the status of the number in the backpack is%1. To judge the first digit to be put or not; flagtemp=1:capacity if Weight (numberofobject) <flagtemp Transfermatrix (numberofobject,flagtemp) =
Value (Numberofobject);
else Transfermatrix (numberofobject,flagtemp) = 0;
End of%%2. To determine whether the next number is release; when: F[i,v]=f[i-1,v]; put: f[i,v]= f[i-1,v-c_i]+w_i%%3. Repeat 2. For flagtempexternal=numberofobject-1:-1:1 for flagtemp=1:capacity if Flagtemp<=weight (FlagTemp
External) Transfermatrix (flagtempexternal,flagtemp) =transfermatrix (flagtempexternal+1,flagtemp); else if Transfermatrix (flagtempexternal+1,flagtemp) >transfermatrix (flagtempexternal+1,flagtemp- Weight (flagtempexternal)) +value (flagtempexternal) Transfermatrix (flagtempexternal,flagtemp
) =transfermatrix (flagtempexternal+1,flagtemp); else Transfermatrix (flagtempexternal,flagtemp) =transfermatrix (flagtempexternal+1,flagtemp-
Weight (flagtempexternal)) +value (flagtempexternal);
End-end-end-end% Transfermatrix%4. Find these numbers.
flagtempexternal=capacity; For flagtemp=1:numberofobject-1 if Transfermatrix (flagtemp,flagtempexternal) ==transfermatrix (FlagTemp+1,FlagTempE
xternal) objectstate (flagtemp) = 0;
else Objectstate (flagtemp) = 1; Flagtempexternal=flagtempexternal-weight (FlaGTEMP);
End-If Transfermatrix (numberofobject,flagtempexternal) ==0 objectstate (numberofobject) = 0;
else Objectstate (numberofobject) = 1; End for Flagtemp=1:numberofobject if Objectstate (flagtemp) ==1 firstgroup=[firstgroup,bag
Numberpoints (flagtemp)];
else Secondgroup=[secondgroup,bagnumberpoints (flagtemp)];
End-End disp (' two sets of numbers and respectively: ');
[Sum (FirstGroup); sum (Secondgroup)] Disp (' first group is ');
FirstGroup disp (' The second group is ');
Secondgroup
Run Result: