A series of bandit algorithms in Swift, built with functional programing and immutable data structures. Inspired by johnmyleswhite/BanditsBook.
To run on the command line:
$ swift build// requires you to be in the./Swiperdirectory$ ./build/debug/Swiper// builds aresults_swift.tsvfile in your~/Documents/directory
The epsilon in the Epsilon-greedy strategy controls the proportion of explorations vs exploitations.
Example usage:
letepsilonGreedy=EpsilonGreedy(epsilon:0.1, nArms:2)letselectedArm= epsilonGreedy.selectArm()somethingWithCallback(color: selectedArm){(reward)inletupdatedEpsilonGreedy= epsilonGreedy.update(selectedArm, reward: reward)}The Annealing Softmax object selects arms based on a softmax function. This object does not require a temperature—the algorithm automatically manages it via simulated annealing.
Example usage:
letsoftmax=Softmax(nArms:4)letselectedArm= softmax.selectArm()somethingWithCallback(copy: selectedArm){(reward)inletupdatedSoftmax= softmax.update(selectedArm, reward: reward)}The UCB strategy uses context to select its next arm. The UCB1 assumes that your max reward is a value of 1.
Example useage:
letucb=UCB1(nArms:3)letselectedArm= ucb.selectArm()somethingWithCallback(displayPopup: selectedArm){(reward)inletupdatedUcb= ucb.update(selectedArm, reward: reward)}