-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsa.m
116 lines (105 loc) · 3.83 KB
/
dsa.m
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
111
112
113
114
115
116
PopSize=10; %size of superorganism
MaxGen=10; %epoch/total generation/iteration
CompRun=10;
p1=0.3*rand;
p2=0.3*rand;
fileID=fopen('DataAnalysis.txt','w');
IDfile=fopen('Results.txt','w');
TimeFile=fopen('ElapsedTime.txt','w');
ScoringMatrix=csvread('x60189_4.csv');
Dimension=size(ScoringMatrix,1); %size of artificial-organism/one clan
for i=1:CompRun
IniTime=tic;
fprintf('\nNo of Run %d\n',i);
fprintf(fileID,'-------------------------------------------------------');
fprintf(fileID,'\nNo of Run %d\n',i);
fprintf(fileID,'-----------------------------------------------------\n');
%% Initial population (generate artificial-organism/solution)
Chrom=InitPop(PopSize,Dimension);
ChromList=Chrom;
%% Initial artificial-organism (solution) with scoring value (fitness function)
InitialSolution=OutputSolution(Chrom(1,:))
InitialScoringValue=CalculateScore(ScoringMatrix,Chrom(1,:))
%% Evaluation of scoring value for each artificial-organism (solution) - fitness function: maximum score
ObjV=CalculateScore(ScoringMatrix,Chrom);
MaxObjV=max(ObjV);
%% Optimization
for Gen=1:MaxGen
% fprintf('\nIteration: %d\n',Gen);
Donor=Chrom(randperm(PopSize),:);
map=zeros(PopSize,Dimension);
if rand<rand
if rand<p1
% Random-mutation #1st strategy
fprintf(fileID,'Random-mutation #1st strategy');
% disp('Random-mutation #1st strategy');
for i=1:PopSize
map(i,:)=rand(1,Dimension)<rand;
end
else
% Differential-mutation strategy
fprintf(fileID,'Differential-mutation strategy');
% disp('Differential-mutation strategy');
for i=1:PopSize
map(i,randi(Dimension))=1;
end
end
else
% Random-mutation #2nd strategy
fprintf(fileID,'Random-mutation #2st strategy');
% disp('Random-mutation #2st strategy');
for i=1:PopSize
map(i,randi(Dimension,1,ceil(p2*Dimension)))=1;
end
end
Scale=4*randg;
StopOver=PopSize+(Scale.*map).*(Donor-PopSize);
StopOver=UpdateStopOver(StopOver,Dimension);
StopOverObjV=CalculateScore(ScoringMatrix,StopOver);
MaxStopOverObjV=max(StopOverObjV);
ind=MaxStopOverObjV>MaxObjV;
MaxObjV(ind)=MaxStopOverObjV(ind);
Chrom(ind,:)=StopOver(ind,:);
[globalmaximum,indexbest]=max(MaxObjV);
globalmaximizer=Chrom(indexbest,:);
globalmax(Gen)= globalmaximum; % save globalminimum value in each iteration
iteration(Gen)= Gen; % save number of iteration
% export results
assignin('base','globalmaximum',globalmaximum);
assignin('base','globalmaximizer',globalmaximizer);
% fprintf('%5.0f ---> %5d\n',Gen,globalmaximum);
fprintf(fileID,'%5d ---> %5d ---> %5d ---> %5d\n',Gen,MaxStopOverObjV,MaxObjV,globalmaximum);
end
plot(iteration,globalmax);
xlabel('Iteration');
ylabel('Best Score');
hold on;
grid on;
OverallGlobalMax=max(globalmaximum);
fprintf(fileID,'\nOverallGlobalMax ---> %5d\n',OverallGlobalMax);
fprintf(IDfile,'%5d\n',OverallGlobalMax);
FinTime=toc(IniTime);
fprintf(TimeFile,'%5d\n',FinTime);
end
fclose(fileID);
fclose(IDfile);
fclose(TimeFile);
IDfile=fopen('Results.txt','r');
data=cell2mat(textscan(IDfile,'%5d'));
data=dlmread('results.txt')
TimeFile=fopen('ElapsedTime.txt','r');
ElapsedTime=cell2mat(textscan(TimeFile,'%5d'));
ElapsedTime=dlmread('elapsedtime.txt')
highestScore=max(data);
lowestScore=min(data);
avg=mean(data);
stdDev=std(data);
totalTime=sum(ElapsedTime);
avgElapsedTime=mean(ElapsedTime);
disp(['Best Overlap Score = ' num2str(highestScore)]);
disp(['Worst Overlap Score = ' num2str(lowestScore)]);
disp(['Average Overlap Score = ' num2str(avg)]);
disp(['Standard Deviation = ' num2str(stdDev)]);
disp(['Average Elapsed Time = ' num2str(avgElapsedTime)]);
fclose(IDfile);
fclose(TimeFile);