Created
January 4, 2013 15:55
-
-
Save romanmt/4453642 to your computer and use it in GitHub Desktop.
epsilon greedy algo implemented as jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(global) { | |
| "use strict"; | |
| // Just some hardcoded stuff until i wire up the backend | |
| $(document).ready(function initializeTest() { | |
| var test = $('.epsilon-test') | |
| var name = test.attr('id') | |
| var levers = _.map(test.find('.epsilon-lever'), function (lever) { | |
| $(lever).hide(); | |
| return $(lever).attr('id'); | |
| }) | |
| console.log(name) | |
| console.log(levers) | |
| var options = [ | |
| {name: "blue", totalTrials: 10, totalRewards: 1}, | |
| {name: "orange", totalTrials: 10, totalRewards: 2}, | |
| {name: "green", totalTrials: 10, totalRewards: 5}, | |
| ] | |
| var eg = new EpsilonGreedy(options); | |
| var selected = $(test).find('#'+ eg.selectLever(Math.random(), Math.random())); | |
| $(selected).show(); | |
| /* | |
| $.get('/api/epsilon/'+name+'?levers='levers.join(','), function(data) { | |
| console.log(data); | |
| }) | |
| */ | |
| }) | |
| // Here's where the magic happens | |
| function EpsilonGreedy(options) { | |
| var self = this; | |
| this.selectRandom = function selectRandom(randomNumber) { | |
| console.log('select random') | |
| return options[Math.floor(randomNumber * options.length)]; | |
| } | |
| this.selectLever = function selectLever(randomNumber1, randomNumber2) { | |
| console.log('randomNumber1', randomNumber1) | |
| var lever = (randomNumber1 < 0.1) ? | |
| self.selectRandom(randomNumber2) : | |
| self.selectHighestProbability(); | |
| return lever.name; | |
| } | |
| this.selectHighestProbability = function selectHighestProbability() { | |
| return _(options).max(function(option) { | |
| return option.totalRewards / option.totalTrials; | |
| }); | |
| } | |
| } | |
| global.EpsilonGreedy = EpsilonGreedy; | |
| })(typeof window === 'undefined' ? this : window); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .epsilon-test#button-color | |
| a.epsilon-lever#orange.btn.btn-large.btn-warning.signup-button(href='/signup/wiz') Launch your jampaign! | |
| a.epsilon-lever#blue.btn.btn-large.btn-primary.signup-button(href='/signup/wiz') Launch your jampaign! | |
| a.epsilon-lever#green.btn.btn-large.btn-success.signup-button(href='/signup/wiz') Launch your jampaign! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment