"""
This code probides a basic skeleton for the
stationary bandit code. It should be adapted
y the students for their work.
"""
import csv, random
class BanditSet(object):
"""
This object represents a set of arms for a stationary multi-armed
bandit problem it will store a fixed set of arms from a set and
will then maintain them over multiple iterations.
"""
def __init__(self, DataRows, ArmNames, ExpRate,
XXXXXXXXXXDistribParam, DecayRate, RewardWeight):
"""
XXXXXXXXXXThis initializes the set of choices by acting as a factory
XXXXXXXXXXclass to create one arm instance for each of the choices.
XXXXXXXXXXThe names and the rows will come from the file that
XXXXXXXXXXis read in.
"""
# Store the Data for later use.
XXXXXXXXXXself.Data = DataRows
# Initialize the parameters.
XXXXXXXXXXself.ExplorationRate = ExpRate
XXXXXXXXXXself.DistributionParameter = DistribParam
XXXXXXXXXXself.DecayRate = DecayRate
XXXXXXXXXXself.RewardWeight = RewardWeight
# Store items for each of the arms.
XXXXXXXXXXself.Names = ArmNames
# Store a list for the weights.
XXXXXXXXXXself.Weights = [-1 for I in range(len(ArmNames))]
# Calculate the starting probability and add it.
XXXXXXXXXXStartProb = 1 / float(len(ArmNames))
XXXXXXXXXXself.Probabilities = [StartProb for I in range(len(ArmNames))]
# And store the Cumulative Reward
XXXXXXXXXXself.CumulativeReward = 0
def handleRows(self):
"""
XXXXXXXXXXProcess each of the rows and update our running reward
XXXXXXXXXXand the basic probabilies for each one.
"""
# We initialize the cumulative
# Reward to be 0
XXXXXXXXXXself.CumulativeReward = 0
# Now iterate over the rows and make each
# of the choices.
XXXXXXXXXXfor Cu
Row in self.Rows:
# Now pick one from the list of probabilities.
# Get the reward value from the row.
# Update the reward weight.
# And update the probabilities.
# Return the cumulative reward.
XXXXXXXXXXreturn(self.CumulativeReward)
def pickArmIndex(self):
"""
XXXXXXXXXXPick an index based upon the probabilities
XXXXXXXXXXusing the cumulative score approach based
XXXXXXXXXXupon a random value.
"""
XXXXXXXXXXpass
def getReward(self, Index):
"""
XXXXXXXXXXUse the Armnames to get the reward for the
XXXXXXXXXXchosen arm.
"""
XXXXXXXXXXpass
def updateWeight(self, Index, Reward):
"""
XXXXXXXXXXUpdate the weight for the chosen index using
XXXXXXXXXXthe parameters.
"""
XXXXXXXXXXpass
def updateProbability(self, Index):
"""
XXXXXXXXXXUpdate the probability for the index from its weight.
"""
XXXXXXXXXXpass
def normalizeProbabilities(self, Index, Reward):
"""
XXXXXXXXXXNormalize the probability values.
"""
XXXXXXXXXXpass
"Sample A","Sample B","Sample C","Sample D"
0,0,0,0
1,0,0,0
0,0,1,0
0,0,0,0
0,0,0,1
0,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,0,0,1
0,1,0,1
0,0,0,0
0,1,0,0
0,0,0,1
0,0,0,0
1,1,0,0
0,0,0,0
1,1,0,0
0,0,0,0
0,0,0,0
0,0,0,0
1,1,0,0
1,0,0,0
0,1,1,0
0,0,0,0
0,1,0,0
0,1,0,1
0,1,0,0
0,0,0,0
0,0,1,0
0,0,1,0
0,0,0,1
0,1,0,0
0,0,0,0
1,0,0,0
0,0,0,0
0,0,0,0
0,1,0,0
1,0,1,0
1,1,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,0,0,1
1,0,0,0
0,1,0,1
0,1,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,0,1,0
1,0,0,1
0,0,0,0
0,1,0,0
0,1,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,1,0,0
0,0,0,0
0,0,0,0
0,0,1,0
0,0,1,0
0,0,0,0
1,1,0,1
0,0,0,0
0,1,1,0
1,0,0,0
0,1,0,0
1,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,0,1,0
0,1,1,1
1,0,0,0
1,1,1,0
0,0,0,1
0,0,1,0
0,0,1,0
0,0,0,0
0,1,0,0
0,0,0,0
0,0,0,0
1,1,0,0
0,0,0,0
0,0,0,0
0,0,0,0
0,1,0,0
0,1,1,0
0,0,0,0
0,0,0,0
0,0,1,0
0,1,0,0
0,0,0,0
0,0,0,0
0,1,0,1
0,0,0,0
0,0,0,0
0,1,0,0
0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