
clc;
clear all;
disp('Mountaineering Team-Based Optimization ');

Max_iteration=1000;      % Maximum numbef of iterations
nPop = 30;             % the number of population


for ttt=1:4
   
global Pd dat

Pd1=[2400 2500 2600 2700];
Pd=Pd1(ttt)
Function_name='F1';
[lb,ub,fobj,dim]=Get_Functions_details(Function_name); 
Fun=fobj;



N=nPop;

% disp('MTBO: Mountaineering Team Based Optimization')

CostFunction = @(x) fobj(x);

nVar = dim;          % Number of Variables
VarSize = [1 nVar]; 
VarMin = lb;       %Variables Lower Bound
VarMax =ub ;       %Variables Upper Bound

%% MTBO Parameters

MaxIt = Max_iteration;        % Maximum Number of Iterations

nPop = N;           % Population Size

%% Initialization 

% Empty Structure for Individuals
empty_individual.Position = [];
empty_individual.Cost = [];

% Initialize Population Array
pop = repmat(empty_individual, nPop, 1);

% Initialize Best Solution
BestSol.Cost = inf;

% Initialize Population Members
for i=1:nPop
    pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
    pop(i).Cost = CostFunction(pop(i).Position);
    
    if pop(i).Cost < BestSol.Cost
        BestSol = pop(i);
    end
end

% Initialize Best Cost Record
BestCosts = zeros(MaxIt,1);

%% MTBO Main Loop

for it=1:MaxIt
    
    % Calculate Population Mean
    Mean = 0;
    for i=1:nPop
        Mean = Mean + pop(i).Position;
    end
    Mean = Mean/nPop;
    
    % Select Leader
    Leader = pop(1);
 
    for i=2:nPop
        if pop(i).Cost < Leader.Cost
            Leader = pop(i);
               end
    end
    
     
    for i=1:nPop
        % Create Empty Solution
        newsol = empty_individual;
      
     
        ii=i+1;
        if ii>nPop
            ii=1;
        end
       Li=(0.25+0.25*rand);
        Ai=(0.75+0.25*rand);
        Mi=(0.75+0.25*rand);
        if rand<Li
        newsol.Position = pop(i).Position ...
            +rand(VarSize).*(pop(ii).Position-pop(i).Position) + rand(VarSize).*(Leader.Position-pop(ii).Position);
        elseif rand<Ai
              newsol.Position = pop(i).Position ...
            + 1*rand(VarSize).*(pop(i).Position-pop(nPop).Position); 
         elseif rand<Mi
              newsol.Position = pop(i).Position ...
            + 1*rand(VarSize).*(Mean-pop(i).Position); 
        else

            newsol.Position = unifrnd(VarMin, VarMax, VarSize); 
       
    end
          
        newsol.Position = max(newsol.Position, VarMin);
        newsol.Position = min(newsol.Position, VarMax);

        newsol.Cost = CostFunction(newsol.Position);
   
        if newsol.Cost<pop(i).Cost
            pop(i) = newsol;
             if pop(i).Cost < Leader.Cost
            Leader = pop(i);
                      end
        end
    end
% Sort Population
    [uuu, SortOrder]=sort([pop.Cost]);
    pop = pop(SortOrder);
   
    % Store Record for Current Iteration
 Convergence_curve(it) =pop(1).Cost;
    
    % Show Iteration Information
    
    if mod(it,50)==0
    disp(['Iteration ' num2str(it) ': Best Cost = ' num2str( Convergence_curve(it))]);
             
    end
            
end
   
%% Results
Destination_fitness=pop(1).Cost;
Destination_position=pop(1).Position;

Best_score=Destination_fitness;
Best_pos=Destination_position;
mtbo_cg_curve1=Convergence_curve;

 [Fa, PT, Pg, Pl]=economicgeneration(Best_pos);
 %disp(sprintf( 'the emission cost is  %4.4f', F1))
 powergeneration= Pg
 powerloss=Pl
 totalpowergeneratio=PT
 disp(sprintf( 'the fuel cost is  %4.4f', Fa))

Function_name='F2';
[lb,ub,fobj,dim]=Get_Functions_details(Function_name); 
Fun=fobj;



N=nPop;

% disp('MTBO: Mountaineering Team Based Optimization')

CostFunction = @(x) fobj(x);

nVar = dim;          % Number of Variables
VarSize = [1 nVar]; 
VarMin = lb;       %Variables Lower Bound
VarMax =ub ;       %Variables Upper Bound

%% MTBO Parameters

MaxIt = Max_iteration;        % Maximum Number of Iterations

nPop = N;           % Population Size

%% Initialization 

% Empty Structure for Individuals
empty_individual.Position = [];
empty_individual.Cost = [];

% Initialize Population Array
pop = repmat(empty_individual, nPop, 1);

% Initialize Best Solution
BestSol.Cost = inf;

% Initialize Population Members
for i=1:nPop
    pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
    pop(i).Cost = CostFunction(pop(i).Position);
    
    if pop(i).Cost < BestSol.Cost
        BestSol = pop(i);
    end
end

% Initialize Best Cost Record
BestCosts = zeros(MaxIt,1);

%% MTBO Main Loop

for it=1:MaxIt
    
    % Calculate Population Mean
    Mean = 0;
    for i=1:nPop
        Mean = Mean + pop(i).Position;
    end
    Mean = Mean/nPop;
    
    % Select Leader
    Leader = pop(1);
 
    for i=2:nPop
        if pop(i).Cost < Leader.Cost
            Leader = pop(i);
               end
    end
    
     
    for i=1:nPop
        % Create Empty Solution
        newsol = empty_individual;
      
     
        ii=i+1;
        if ii>nPop
            ii=1;
        end
       Li=(0.25+0.25*rand);
        Ai=(0.75+0.25*rand);
        Mi=(0.75+0.25*rand);
        if rand<Li
        newsol.Position = pop(i).Position ...
            +rand(VarSize).*(pop(ii).Position-pop(i).Position) + rand(VarSize).*(Leader.Position-pop(ii).Position);
        elseif rand<Ai
              newsol.Position = pop(i).Position ...
            + 1*rand(VarSize).*(pop(i).Position-pop(nPop).Position); 
         elseif rand<Mi
              newsol.Position = pop(i).Position ...
            + 1*rand(VarSize).*(Mean-pop(i).Position); 
        else

            newsol.Position = unifrnd(VarMin, VarMax, VarSize); 
       
    end
          
        newsol.Position = max(newsol.Position, VarMin);
        newsol.Position = min(newsol.Position, VarMax);

        newsol.Cost = CostFunction(newsol.Position);
   
        if newsol.Cost<pop(i).Cost
            pop(i) = newsol;
             if pop(i).Cost < Leader.Cost
            Leader = pop(i);
                      end
        end
    end
% Sort Population
    [uuu, SortOrder]=sort([pop.Cost]);
    pop = pop(SortOrder);
   
    % Store Record for Current Iteration
 Convergence_curve(it) =pop(1).Cost;
    
    % Show Iteration Information
    
    if mod(it,50)==0
    disp(['Iteration ' num2str(it) ': Best Cost = ' num2str( Convergence_curve(it))]);
             
    end
            
end
   
%% Results
Destination_fitness=pop(1).Cost;
Destination_position=pop(1).Position;

Best_score=Destination_fitness;
Best_pos=Destination_position;
mtbo_cg_curve2=Convergence_curve;


  [Fa, PT, Pg, Pl]=economicgen(Best_pos);
 %disp(sprintf( 'the emission cost is  %4.4f', F1))
 powergeneration= Pg
 powerloss=Pl
 totalpowergeneratio=PT
 disp(sprintf( 'the fuel cost is  %4.4f', Fa))
 
 for i=1:dim
  if Pg(i)>dat(i,1)&&Pg(i)<dat(i,2)
     
     disp('the   fuel type is 1' )
 elseif Pg(i)>dat(i,3)&&Pg(i)<dat(i,4)
    
     disp('the   fuel type is 2')
  else
     
     disp('the   fuel type is 3')
  end

semilogy(mtbo_cg_curve1,'Color','b','LineWidth',3,'Linestyle','-.')
hold on
semilogy(mtbo_cg_curve2,'Color','r','LineWidth',3)
legend('Wtihout losses','with losses')
xlabel('Iteration');
ylabel('Fuel cost ($/hr)');
axis tight
hold on
grid off
box on

 end
end






