Thursday, February 5, 2009

Ch6, Ex17

Following are the changes:
%Add  a variable for threhold number of users on one site
declare N=10000 M=500000 T=200 Threshold=100

%Add methods for incrementing/decrementing hits
%Procedures for incrementing/decrementing by 1 user on site
proc {IncrementHits S}
C = 1.0
in
Sites.S.hits := Sites.S.hits + 1
if Sites.S.hits>Threshold then
Sites.S.performance := Sites.S.performance - C
end
end
proc {DecrementHits S}
C = 1.0
in
if Sites.S.hits>Threshold then
Sites.S.performance := Sites.S.performance + C
end
Sites.S.hits := Sites.S.hits - 1
end

%Never increment/decrement hits manualy but use above methods
declare
Users={MakeTuple users M}
for I in 1..M do
S={UniformI 1 N}
in
Users.I={Record.toDictionary o(currentSite:S)}
{IncrementHits S}
end
proc {UserStep I}
U = Users.I
% Ask three users for their performance information
L = {List.map [{UniformI 1 M} {UniformI 1 M} {UniformI 1 M}]
fun{$ X}
(Users.X.currentSite) #
Sites.(Users.X.currentSite).performance
+ {Gauss}*{IntToFloat N}
end}
% Calculate the best site
MS#MP = {List.foldL L
fun {$ X1 X2} if X2.2>X1.2 then X2 else X1 end end
U.currentSite #
Sites.(U.currentSite).performance
+ {Abs {Gauss}*{IntToFloat N}}
}
in
if MS\=U.currentSite then
{DecrementHits U.currentSite}
U.currentSite := MS
{IncrementHits MS}
end
end

No comments:

Post a Comment