\فصل{پیاده سازی کد‌نویسی با نرم افزار متلب} 
\section{مقدمه}

در این فصل برنامه نویسی چهار استراتژی ارائه شده در فصل 2 بیان‌شده‌است.
برنامه‌ها بر مبنای استفاده از 4 استراتژی می‌باشند که عبارتند از:
\begin{itemize}
\item 
استراتژی  
\lr{Tit For Tat}
کلاسیک
\item
استراتژی  
\lr{Tit For Tat}
بخشنده
\item
استراتژی  
\lr{Tit For Tat}
بر اساس اعتبار
\item
استراتژی  
\lr{Tit For Tat}
بر اساس اعتبار گروه
\end{itemize}
\section{برنامه تاثیر سواری رایگان بر گروه}

\section{تابع استراتژی 
\lr{Tit For Tat}
کلاسیک}

\begin{verbatim}
% The ImportTrackData function written by VAFA KHALIGHI

% Define the function with one input which is the name of the track
% data file (InputFile) and outputs time (t), speed in m/s (v), 
% longitudinal G-force (FGt),latitudinal G-force (FGn), 
% tangential acceleartion (at), and  normal acceleration (an)

function [t,v,FGt,FGn,at,an] = ImportTrackData(InputFile)
%
% When we look at the file 'Track-P11-GroupA.txt', we see that it has
% the following formats:
%
% Time (Seconds): 0.00
% Ground Speed (km/h): 135.0286190
% G Force Long (G): 0.27
% G Force Lat (G): 0.1632125
%
% Time (Seconds): 0.02
% Ground Speed (km/h): 135.2315170
% G Force Long (G): 0.27
% G Force Lat (G): 0.1642262
%
% ...
% From this, we can see that : separates strings and numbers in each line
% and we should have four columns. So the importdata function, is the
% perfect choice
%
% Let's first make a variable which is the number of columns of our track
% data:
nocols=4; 
%
% Next, we use the importdata function to separate the columns and then
% automatically create a cell array of strings and a numeric array with
% all the data:
%
TrackDATA=importdata(InputFile,':');
%
% The TrackDATA.data now holds the numeric array; so we first reshape the 
% numeric array into four rows and automatic number of columns using the
% reshape function and then we take the transpose of the reshaped numeric
% array so that we would have automatic number of rows and four columns:
%
TrackMatrix=transpose(reshape(TrackDATA.data,nocols,[]));
%
% t is the first column of this matrix:
%
t=TrackMatrix(:,1);
%
% FGt isthethirdcolumnofthematrix:
% 
FGt=TrackMatrix(:,3);
%
% FGn is thefourthcolumnofthematrix:
%
FGn=TrackMatrix(:,4);
% 
% The second column of the matrix is speed in km/h but we want to have
% speed in m/s so we need to multiply the second column of the matrix
% by 1000/3600=1/3.6:
%
v=TrackMatrix(:,2)/3.6;
% 
% The tangential acceleartion is obtained by multiplying longitudinal
% G-force by 9.8:
%
at=9.8*FGt;
%
% The normal acceleartion is obtained by mutiplying latitudinal G-force
% by 9.8:
%
an=9.8*FGn;
%
% End the definition of the function
end
\end{verbatim}
\end{flushleft}















%\begin{flushleft}
%\begin{align*}
%\begin{array}
%\text{function} \left[  totalresult,finalHistory,average\right] =classicalTFT
%\left( coeff,iterations,\nonumber \\numberOfUsers, selfishPercentige \right)  ; \\
%numberOfSelfishes = round\left( \left( selfishPercentige * numberOfUsers\right) /100\right) ; \\
%%coeff=4;
%%iterations = 100000;
%p_a = 1/4; \\
%k = coeff*\left( 1/p_a\right) ;\nonumber \\
%nodes = ones\left( 1,numberOfUsers\right) ;\nonumber \\
%vector = ones\left( 1,\left(k+1\right) \right) ;\nonumber \\
%vector2 = zeros\left( 1,\left( numberOfUsers\right) \right) ;\nonumber \\
%vector3 = zeros\left(1,\left(numberOfUsers\right) \right) ;\nonumber \\
%history\left(1,1)=struct\left('history',vector\right) ;\nonumber \\
%for \     \ i=1:numberOfUsers\nonumber \\
%   for \     \ j=1:numberOfUsers\nonumber \\
%        history\left(i,j\right) =struct\left('history',vector\right) ;\nonumber \\
%          end\nonumber \\
%p_r = 1/12;\nonumber \\
%a = -1;\nonumber \\
%c = 0;\nonumber \\
%b = 1/p_r;\nonumber \\
%d = -1;\nonumber \\
%a_r = 1:iterations;\nonumber \\
%result = 0;\nonumber \\
%badhistories = 0;\nonumber \\
%endresult = zeros\left( iterations,1\right) ;\nonumber \\
%systemgain = zeros\left( iterations,1\right) ;\nonumber \\
%selfishendresult = zeros\left( iterations,1\right) ;\nonumber \\
%numberOfSelfishNodes = 0;\nonumber \\ %will increment to numberOfSelfishes
%% picking four selfish nodes randomly
%while numberOfSelfishNodes~=numberOfSelfishes\nonumber \\
%   randomNumber = randi\left( numberOfUsers,1,1\right) ;\nonumber \\
%   if nodes\left( randomNumber\right)  \sim = 0;\nonumber \\
%     nodes\left( randomNumber\right)  = 0;\nonumber \\
%      numberOfSelfishNodes = numberOfSelfishNodes+1;\nonumber \\
%    end\nonumber \\
%end\nonumber \\
%\end{array}
%\end{align*}
%\end{flushleft}
%
%
%
%
%
%
%$% The iteration starts from here$\\
%$testn = 0;$\\
%$for i=1:iterations$\\
%  $  i$\\
%    $    n=numberOfUsers;$\\
%    $    questions=rand(1,n)<(1/10);$\\
%   $     % ensure that there are requesting nodes in this iteration$\\
%    $    one = 1;$\\
%    $    while(~ismember(one,questions))$\\
%      $       questions=rand(1,n)<(1/10);$\\
%   $     end$\\
%   $     for q=1:numberOfUsers$\\
%   $     if(questions(q)>0)$\\
%   $     gain = 0;$\\
%   $     csgain = 0;$\\
%   $     n=numberOfUsers;$\\
%   $     resources=rand(1,n)<pa;$\\
%   $     gotAnswer = false;$\\
%   $     needAnswer = false;$\\
%     $   num = 0;$\\
%     $   reqNode=0;$\\
%    $    one = 1;$\\
%    $    result = 0;$\\
%    $    isSelfish = false;$\\
%    $   % ismember(two,resources)==0$\\
%     $  % ensure that there are requesting nodes in this iteration$\\
%     $   %while(~ismember(zero,resources))$\\
%      $  %     resources=rand(1,n)<pa;$\\
%    $    %end$\\
%    $    %ensure that the random choosen requesting does not have resources$\\
%     $  % while(~needAnswer)$\\
%     $   %     randomNumber = randi (numberOfUsers,1,1);$\\
%     $  %     if(resources(randomNumber)==0)$\\
%      $ %         needAnswer = true;$\\
%     $  %         reqNode=randomNumber; $  \\    
%    $  %      end$\\
%     $  % end$\\
%      $  reqNode = q;$\\
%      $   if(nodes(reqNode)==0)$\\
%         $      isSelfish = true;$\\
%       $  end$\\
%       $ vector2(reqNode)=vector2(reqNode)+1;$\\
%$% to give answer$\\
%$% 1 I have answer 2 I am coop 3 he has a good history with me$\\
%$totalgain = 0;$\\
%$bad = 0;$\\
%$if(isSelfish)$\\
%$selfishfinalgain = -1;$\\
%$finalgain = 0;$\\
%$else$\\
%  $  selfishfinalgain = 0;$\\
%  $  finalgain = -1;$\\
%$end$\\
%          $  for j=1:numberOfUsers$\\
%              $  gotAnswer = false;$\\
%             $   donothing = false;$\\
%             $   if j==reqNode$\\
%                  $  donothing = true;$\\
%               $ end$\\
%           $ if(resources(j)==1&&~donothing)$\\
%               $ if(nodes(j)==1)$\\
%                $  hist = history(reqNode,j).history;$\\
%                $  rpos = history(reqNode,j).history(k+1);$\\
%                $  pos = history(j,reqNode).history(k+1);$\\
%                  $posOne = 0;$\\
%                $  if(rpos==1)$\\
%                   $   posOne=1;$\\
%                $  end$\\
%                 $ historySum=sum(hist==1);$\\
%               $   if(i>25000&&historySum>15)$\\
%                 $     testn = testn+1;$\\
%                $  end$\\
%                $  if((historySum)>=((k/2)+posOne))$\\
%               $   gotAnswer = true;$\\
%                $  if(~isSelfish)$\\
%                  $    gain = a + b;$\\
%                      $finalgain = a+b;$\\
%                 $ else$\\
%                  $    selfishfinalgain = b; $\\
%                  $    gain = a;$\\
%                $  end$\\
%                 $ %result = pr*(((1-((1-pa)^7))*b)+((1-pa)^7)*d)+(1-pr)*(pa*a+(1-pa)*c);$\\
%                $  %history(randomNumber,reqNode)= history(randomNumber,reqNode)+1;$\\
%               $   if(i>30000)$\\
%               $   i$\\
%               $   histt = history(j,reqNode).history;$\\
%                $  end$\\
%              $    history(j,reqNode).history(pos)=1;$\\
%               $   history(j,reqNode).history(k+1)=pos+1;$\\
%              $    if(pos+1>k)$\\
%                  $     history(j,reqNode).history(k+1)=1;$\\
%               $   end$\\
%                $  else$\\
%                  $    gain = 0;$\\
%                    $  badhistories = badhistories+1;$\\
%                   $   bad = bad + 1;$\\
%                    $  if(bad>8)$\\
%                   $   end$\\
%                     $ %his = 'bad history'  $\\
%                $  end$\\
%              $  else$\\
%                   $ gain = -1;$\\
%                  $  %sel = 'selfish node'$\\
%               $ end$\\
%          $  end$\\
%          $  num = num +1;$\\
%          $  if(~gotAnswer&&~donothing)$\\
%              $  vector3(j) = vector3(j)+1;$\\
%              $  totalgain = totalgain+gain;$\\
%              $ pos = history(j,reqNode).history(k+1);$\\
%              $    history(j,reqNode).history(pos)=0;$\\
%             $     history(j,reqNode).history(k+1)=pos+1;$\\
%             $    if(pos+1>k)$\\
%                $       history(j,reqNode).history(k+1)=1;$\\
%             $     end$\\
%         $   elseif(~donothing)$\\
%               $  totalgain = totalgain+gain;$\\
%          $  end$\\
%          $  end$\\
%          $  if(finalgain<0)$\\
%             $   stop = 'stop';$\\
%          $  end$\\
%              $  if(i>1)$\\
%                 $   if(endresult(i)==0)$\\
%                      $  endresult(i)= endresult(i-1);$\\
%                   $ end$\\
%                   $  if(selfishendresult(i)==0)$\\
%                       $ selfishendresult(i)= selfishendresult(i-1);$\\
%                   $ end$\\
%                $ endresult(i)= endresult(i)+(finalgain);$\\
%                $ selfishendresult(i)= selfishendresult(i)+(selfishfinalgain);$\\
%             $   else$\\
%                 $endresult(i) = endresult(i)+finalgain;$\\
%                 $selfishendresult(i)= selfishfinalgain;$\\
%               $ end  $     $ % while ends$ \\
%            $end$\\
%        $end$\\
%$end$\\
%$average = (endresult(iterations)+selfishendresult(iterations))/iterations;$\\
%$totalresult = endresult;$\\
%$finalHistory = history;$\\
%$plot(1:iterations,endresult);$\\



\section{تابع استراتژی 
\lr{Tit For Tat}
بخشنده}

\section{تابع استراتژی 
\lr{Tit For Tat}
بر اساس اعتبار}

\section{برنامه استراتژی‌ها}







