Borrar filtros
Borrar filtros

how to sort the elements dependent on one another

1 visualización (últimos 30 días)
PRANAY DISHAN
PRANAY DISHAN el 23 de Abr. de 2018
Dear sir, I have a multi level inventory optimization problem in which the total cost of 2 items is to be minimized by generating random solutions to the demand and calculating the corresponding cost.
Item1:
External Demand is given as: Element.Item0.demand=[40 0 100 0 90 10]
A random solution is developed Element.Item1.Sol=(1 0 1 1 1 0)
The demand 1 is generated based on Element.Item1.Demand=[40 0 100 0 100 0]
The cost calculated for Item1 is $1320
The obtained result is optimum one from which demand for second item should be developed
Item2:
Element.Item2.Sol=[1 0 0 0 0 0]
The corresponding demand for item 2 is Element.Item2.Demand=(240 0 0 0 0 0)
The cost calculated should be $860
The above procedure is carried out in a loop in which 50 items are executed simultaneoulsy. The problem am facing is that am not able to get the optimum cost of item 2 dependent on item 1. Rather the optimum cost of item 2 is getting generated independently.
The following is the program:
clc; clear; close all; %% [DSE,KSE,LSE,OSE]=SEDemand();
%% Parameters of HSA HMS=50; NHM=20; maxit=10; HMCR=0.95; PAR=0.2;
%% Empty templates to hold the solutions, demand and cost of the harmonies empty_Element.Item1.Sol = []; empty_Element.Item2.Sol = [];
empty_Element.Item0.Demand = []; empty_Element.Item1.Demand = []; empty_Element.Item2.Demand = [];
empty_Element.Item1.Cost = []; empty_Element.Item2.Cost = [];
%% Repeat matrix to hold the values of each iteration Element.Item0=repmat(empty_Element.Item0,HMS,1); Element.Item1=repmat(empty_Element.Item1,HMS,1); Element.Item2=repmat(empty_Element.Item2,HMS,1);
%% %HARMONY MEMORY for i=1:HMS
%corresponding sol of the elements
Element.Item1(i).Sol=SESoln();
Element.Item2(i).Sol=SESoln();
%demand of 0th element
Element.Item0(i).Demand=zeros(1,6) ;
Element.Item0(i).Demand=SEDemand;
%1st demand
Element.Item1(i).Demand = zeros(1,6) ;
Element.Item1(i).Demand(Element.Item1(i).Sol==1) = 1*(accumarray(cumsum(Element.Item1(i).Sol(:)), Element.Item0(i).Demand ));
%2nd demand
Element.Item2(i).Demand = zeros(1,6) ;
Element.Item2(i).Demand(Element.Item2(i).Sol==1) = 5*(accumarray(cumsum(Element.Item2(i).Sol(:)), Element.Item1(i).Demand ));
%cost calculations for 1st element
HC1=0;
G1=0;
H1=0;
OC1=400;
X1=12;
for k=1:6
if Element.Item1(i).Sol(1,k)==0
G1=G1+1;
else
G1 =0;
H1=H1+1;
end
HC1 = (HC1 + G1*(X1*( Element.Item0(i).Demand(1,k))));
end
P1=find(Element.Item1(i).Demand);
A1=size(P1,2);
TC1 =HC1+(A1*OC1);
Element.Item1(i).Cost=TC1;
%cost calculations for 2nd element
HC2=0;
G2=0;
H2=0;
OC2=500;
X2=0.6;
for k=1:6
if Element.Item2(i).Sol(1,k)==0
G2=G2+1;
else
G2 =0;
H2=H2+1;
end
HC2 = (HC2 + G2*(X2*( Element.Item1(i).Demand(1,k))));
end
P2=find(Element.Item2(i).Demand);
A2=size(P2,2);
TC2 =HC2+(A2*OC2);
Element.Item2(i).Cost=TC2;
end
BestCosts=zeros(maxit,1);
for it=1:maxit
%Repeat matrix to Elementold new solutions
NewElement.Item0=repmat(empty_Element.Item0,NHM,1);
NewElement.Item1=repmat(empty_Element.Item1,NHM,1);
NewElement.Item2=repmat(empty_Element.Item2,NHM,1);
for i=1:NHM
%New solution wrt HMCR
if rand<=HMCR
RVHMS = vertcat(Element.Item1(i).Sol);
nrow = size(RVHMS,1);
ncol = size(RVHMS,2);
RS = randi(nrow, 1, ncol);
NewElement.Item1(i).Sol = RVHMS((0:ncol-1)*nrow + RS);
%Improvising the new solution wrt PAR
if rand < PAR
N1=randi([2,5],1,1);
N2=N1+1;
NewElement.Item1(i).Sol([N1 N2])= NewElement.Item1(i).Sol([N2 N1]);
end
else
%Developing a new solution randomly
NewElement.Item1(i).Sol=SESoln();
end
%New solution wrt HMCR
if rand<=HMCR
RVHMS = vertcat(Element.Item2(i).Sol);
nrow = size(RVHMS,1);
ncol = size(RVHMS,2);
RS = randi(nrow, 1, ncol);
NewElement.Item2(i).Sol = RVHMS((0:ncol-1)*nrow + RS);
%Improvising the new solution wrt PAR
if rand < PAR
N1=randi([2,5],1,1);
N2=N1+1;
NewElement.Item2(i).Sol([N1 N2])= NewElement.Item2(i).Sol([N2 N1]);
end
else
%Developing a new solution randomly
NewElement.Item2(i).Sol=SESoln();
end
%corresponding sol of the NewElements
NewElement.Item1(i).Sol=SESoln();
NewElement.Item2(i).Sol=SESoln();
%demand of 0th NewElement
NewElement.Item0(i).Demand=zeros(1,6) ;
NewElement.Item0(i).Demand=SEDemand;
%1st demand
NewElement.Item1(i).Demand = zeros(1,6) ;
NewElement.Item1(i).Demand(NewElement.Item1(i).Sol==1) = 1*(accumarray(cumsum(NewElement.Item1(i).Sol(:)), NewElement.Item0(i).Demand ));
%2nd demand
NewElement.Item2(i).Demand = zeros(1,6) ;
NewElement.Item2(i).Demand(NewElement.Item2(i).Sol==1) = 5*(accumarray(cumsum(NewElement.Item2(i).Sol(:)), NewElement.Item1(i).Demand ));
%cost calculations for 1st NewElement
HC1=0;
G1=0;
H1=0;
OC1=400;
X1=12;
for k=1:6
if NewElement.Item1(i).Sol(1,k)==0
G1=G1+1;
else
G1 =0;
H1=H1+1;
end
HC1 = (HC1 + G1*(X1*( NewElement.Item0(i).Demand(1,k))));
end
P1=find(NewElement.Item1(i).Demand);
A1=size(P1,2);
TC1 =HC1+(A1*OC1);
NewElement.Item1(i).Cost=TC1;
%cost calculations for 2nd NewElement
HC2=0;
G2=0;
H2=0;
OC2=500;
X2=0.6;
for k=1:6
if NewElement.Item2(i).Sol(1,k)==0
G2=G2+1;
else
G2 =0;
H2=H2+1;
end
HC2 = (HC2 + G2*(X2*( NewElement.Item1(i).Demand(1,k))));
end
P2=find(NewElement.Item2(i).Demand);
A2=size(P2,2);
TC2 =HC2+(A2*OC2);
NewElement.Item2(i).Cost=TC2;
end
CombMat1=[Element.Item1
NewElement.Item1];
CombMat2=[Element.Item2
NewElement.Item2];
[~, Sorting]=sort([CombMat1.Cost]);
CombMat1 = CombMat1(Sorting(1:end));
[~, Sorting]=sort([CombMat2.Cost]);
CombMat2 = CombMat2(Sorting(1:end));
Element.Item1=CombMat1(1:end-NHM);
Element.Item2=CombMat2(1:end-NHM);
BestHarmony.Item1=Element.Item1(1);
BestHarmony.Item2=Element.Item2(1);
C1=BestHarmony.Item1.Cost;
C2=BestHarmony.Item2.Cost;
BestCosts(it)=C1+C2;
disp(['Iteration No: ' num2str(it) '- Best Cost = ' num2str(BestCosts(it))]);
end

Respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by