%% Script file to run the N-armed bandit using the softmax strategy %Initializations are Here: NumMachines=10; %Number of machines ActQ=randn(NumMachines,1); %This creates our actual payouts NumPlay=1000; %Number times to play. %The variable tau controls the "temp"- Hot means more randomness, cold %means lock into the highest paying machine. Initialtau=10; Endingtau=0.5; tau=10; %Some other variables: NumPlayed=zeros(NumMachines,1); %Keep a running sum of the number of times each action is selected ValPlayed=zeros(NumMachines,1); %Keep a running sum of the total reward for each action EstQ=zeros(NumMachines,1); %Storage space for our estimated payouts PayoffHistory=zeros(NumPlay,1); %Keep a record of our payoffs %% Main loop below! for i=1:NumPlay %Pick a machine to play: a=softmax(EstQ,tau); %Play the machine and update EstQ, tau Payoff=randn+ActQ(a); NumPlayed(a)=NumPlayed(a)+1; ValPlayed(a)=ValPlayed(a)+Payoff; %Update estimates and store payoff EstQ(a)=ValPlayed(a)/NumPlayed(a); PayoffHistory(i)=Payoff; %Update tau for the next round. tau=Initialtau*(Endingtau/Initialtau)^(i/NumPlay); end %% Take a look at the results [v,winningmachine]=max(ActQ); winningmachine NumPlayed plot(1:10,ActQ,'k',1:10,EstQ,'r')