% Example 1
% Application  of a factorized distribution algorithm (FDA) to the
% hydrophobic-polar (HP) protein model

global HPInitConf;   % This is the HP protein instance 
 	 	     % defined as a sequence of zeros and ones 
HPInitConf =   [zeros(1,12),1,0,1,0,1,1,0,0,1,1,0,0,1,1,0,1,1,0,0,1,1,0,0,1,1,0,1,1,0,0,1,1,0,0,1,1,0,1,0,1,zeros(1,12)]; 
PopSize = 800;  
NumbVar = size(HPInitConf,2); 
cache  = [0,0,0,0,0]; 
Card = 3*ones(1,NumbVar); 
maxgen = 300; 
% The Markov chain model (Cliques) is constructed specifying the number of 
% conditioned (previous) variables. In the example below this number is 
% 1., i.e. p(x) = p(x_0)p(x_1|x_0) ... p(x_n|x_{n-1}) 
Cliques = CreateMarkovModel(NumbVar,1); 
F = 'EvaluateEnergy'; % HP protein evaluation function 
edaparams{1} = {'learning_method','LearnFDA',{Cliques}};
edaparams{2} = {'sampling_method','SampleFDA',{PopSize}};
% Repairing method used to guarantee that solutions do not self-intersect 
edaparams{3} = {'repairing_method','HP_repairing',{}}; 
edaparams{4} = {'stop_cond_method','max_gen',{maxgen}}; 
[AllStat,Cache]=RunEDA(PopSize,NumbVar,F,Card,cache,edaparams) 
% to draw the resulting solution, use function PrintProtein(vector),
% where vector is the best solution found.
vector = AllStat{maxgen,2};
PrintProtein(vector);


% Example 2
% Application of the ViewPCStruct method that allows a
% parallel coordinate visualization of the structures learned by a Bayesian-based EDA

viewparams{1} = [14]; % Font size 
viewparams{2} = []; % The edges will be found by the algorithm 
viewparams{3} = 60; % Those edges that appear at least 60 times 
viewparams{4} = 2;  % Structures that have at least two edges 
viewparams{5} = 'ClusterUsingDist'; % Variables are clustered using affinity propagation 
viewparams{6} = 'correlation'; % Similarity measure used is the inverse of correlation 
[run_structures,maxgen,nruns] = ReadStructures('ProteinStructsExR.txt',20); 
[results] = ViewStructures(run_structures,20,maxgen,nruns,'viewmatrix_method', 'ViewPCStruct',viewparams);


% Example 3
% Application of the ViewStructures method that allows a
% dendrogram visualization of the structures learned by a Bayesian-based EDA

 viewparams{1} = [14]; % Font size 
 viewparams{2} = []; % The edges will be found by the algorithm 
 viewparams{3} = 60; % Those edges appearing at least 60 times 
 viewparams{4} = 2;  % Structures that have at least two edges 
 viewparams{5} = 'correlation'; % Similarity measure used to group edges is correlation 
 [run_structures,maxgen,nruns] = ReadStructures('ProteinStructsExR.txt',20); 
 [results] = ViewStructures(run_structures,20,maxgen,nruns,  'viewmatrix_method', 'ViewDendroStruct',viewparams);


% Example 4
% Application of a Bayesian network based  EDA implemented in Mateda-2.0  combined with a classification algorithm to 
% find a subset of features that maximizes the correct classification rate of a given classifier.
% We use the lung cancer data set \citep{Hong_and_Yang:1991} from the UCI machine learning 
% repository\footnote{http://archive.ics.uci.edu/ml/index.html}.
% The clasifier is implented by the function FindClassifier.m 

filenamedata = 'lungcancer.dat'; % Dataset name
global AttributesMatrix 
global ProblemClasses	 
AuxMatrix = load(filenamedata); % The dataset is read
NumbVar = size(AuxMatrix,2)-1; 
% Since there are missing values an imputation method is applied
AttributesMatrix = knnimpute(AuxMatrix(:,2:NumbVar+1));
ProblemClasses = AuxMatrix(:,1);
F = 'FindClassifier';	  % FSS evaluation function 
PopSize = 250;         
cache  = [1,1,1,1,1];
maxgen =  30;		   
selparams(1:2) = {0.5,'ParetoRank_ordering'};
Card = 2*ones(1,NumbVar);  
edaparams{3} = {'selection_method','truncation_selection',selparams};
edaparams{4} = {'replacement_method','best_elitism',{'ParetoRank_ordering'}};
edaparams{5} = {'stop_cond_method','max_gen',{maxgen}}; 
for Exp=1:30,
  filename = ['NewLungResults_Alg1_Exp_',num2str(Exp),'.mat'];% EBNA 
  BN_params(1:7) = {'k2',5,0.05,'pearson','bayesian','no',Card};
  edaparams{1} = {'learning_method','LearnBN',BN_params};
  edaparams{2} = {'sampling_method','SampleBN',{PopSize,1}};
  [AllStat,Cache]=RunEDA(PopSize,NumbVar,F,Card,cache,edaparams) 
  eval(['save ', filename,  ' AllStat Cache']);
  filename = ['NewLungResults_Alg2_Exp_',num2str(Exp),'.mat']; % GA  
  crossover_params = {fix(PopSize/2) + 1}; % Number of parents pairs 
  edaparams{1} = {'learning_method','LearnOnePointCrossover',crossover_params};
  edaparams{2} = {'sampling_method','SampleOnePointCrossoverPlusMut',{PopSize,1}};
  [AllStat,Cache]=RunEDA(PopSize,NumbVar,F,Card,cache,edaparams) 
  eval(['save ', filename,  ' AllStat Cache']);
end,
 
