Crear una matrix 3D a partir de vectores
Publicado por Ricardo (42 intervenciones) el 05/06/2017 23:24:21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
clear
clc
% 1. DATOS DEL PROBLEMA
% Número de particulas, C1, C2, y W
NP=20;
C1=0.1;
C2=0.1;
W=0.1;
% Número de variables
NV=1;
% Valores mínimos y máximos Posición
Val_minP=[0];
Val_maxP=[20];
% Valores mínimos y máximos velocidad
Val_minV=[0];
Val_maxV=[0.5];
% Número máximo de iteraciones
NMI=40;
%Iteraciones totales máximas
ITM=40;
% 2. INICIACIÓN DE VARIABLES
Mz_Pos=zeros(NP,NV);
Mz_Vel=zeros(NP,NV);
Mz_Pbest=zeros(NP,NV);
Vt_Costo=zeros(1,NP);
Vt_Gbest=zeros(1,NV);
H=zeros(1,NV);
Vt_CostoBest=zeros(1,1+NMI);
Vt_CostoPbest=zeros(1,NP);
% 3. CÁLCULO DE VELOCIDADES MÍNIMAS Y MÁXIMAS
for i=1:NV
H(i)=(Val_maxV(i)-Val_minV(i))/5;
end
% 4. GENERACIÓN DE VELOCIDADES Y POSICIONES INCIALES
% Posiciones Iniciales
for i=1:NV
Mz_Pos(:,i)=random('unif',Val_minP(i),Val_maxP(i),NP,1);
end
% Velocidades Iniciales
for i=1:NV
Mz_Vel(:,i)=random('unif',-H(i),H(i),NP,1);
end
% 5. EVALUACIÓN DE LA FUNCIÓN OBJETIVO
for i=1:NP
Vt_Costo(i)=evalcosto(Mz_Pos(i,1));
end
% 6. PROCESO ITERATIVO
Mz_Pbest=Mz_Pos;
Vt_CostoPbest=Vt_Costo;
[Cost_Best,PosBest]=min(Vt_Costo);
Vt_CostoBest(1)=Cost_Best;
Vt_Gbest=Mz_Pos(PosBest,:);
Mz_Vt_Gbest=zeros(NV,ITM+1);
r=2;
Dist(:,1)=Vt_Costo;
Dist(:,2)=Mz_Pos;
[M_dist,Pos_dist]=min(Dist(:,1));
Max_Pos=Dist(Pos_dist,2);
Dist(:,3)=sqrt((Max_Pos-Dist(:,2)).^2);
it=1;
S=find(Dist(:,3)<=r)
STOP=NP-size(S,1);
tams(it,1)=size(S,1);
%ITERACIONES DE PROCESO
while STOP>0
%ELIMINACION IGUALES
for i=1:1:size(S,1)
Mz_Pos(S(i,1),1)=0;
end
%ACTUALIZAR VELOCIDADES
for i=1:1:NP
Mz_Vel(i,1)=(W*Mz_Vel(i,1))+(C1*(Mz_Pbest(i,1)-Mz_Pos(i,1)))+(C2*(Vt_Gbest-Mz_Pos(i,1)));
end
for i=1:1:size(S,1)
Mz_Vel(S(i,1),1)=0;
end
%ACTUALIZAR POSICIONES
for i=1:1:NP
Mz_Pos(i,1)=Mz_Pos(i,1)+Mz_Vel(i,1);
end
%EVALUACIÓN DE LA FUNCIÓN OBJETIVO
for i=1:NP
Vt_Costo(i)=evalcosto(Mz_Pos(i,1));
end
for i=1:1:size(S,1)
Vt_Costo(1,S(i,1))=0;
end
%PROCESO ITERATIVO
Mz_Pbest=Mz_Pos;
Vt_CostoPbest=Vt_Costo;
[Cost_Best,PosBest]=min(Vt_Costo);
Vt_CostoBest(1)=Cost_Best;
Vt_Gbest=Mz_Pos(PosBest,:);
Mz_Vt_Gbest=zeros(NV,ITM+1);
r=2;
Dist(:,1)=Vt_Costo;
Dist(:,2)=Mz_Pos;
[M_dist,Pos_dist]=min(Dist(:,1));
Max_Pos=Dist(Pos_dist,2);
Dist(:,3)=sqrt((Max_Pos-Dist(:,2)).^2);
for i=1:1:size(S,1)
Dist(S(i,1),3)=0;
end
it=it+1;
S=find(Dist(:,3)<=r)
tams(it,1)=size(S,1);
difs=tams(it,1)-tams(it-1,1);
%PARAMETRO DE PARADA
STOP=STOP-difs;
end
Buenas tardes programadores/as tengo el siguiente codigo y tengo la siguiente duda
tengo que imprimir el vector S pero con las siguientes condiciones
1. En cada iteracion me guarda un resultado diferente, debo hacer un setdiff entre 2 iteraciones seguidas
2. Guardar esos vectores en una matrix tridimensional debido a que no tienen el mismo tamaño
Agradezco su colaboracion
Valora esta pregunta


0