Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "./bandit"
  5. "fmt"
  6. "image/color"
  7. "math/rand"
  8. "time"
  9.  
  10. "code.google.com/p/plotinum/plot"
  11. "code.google.com/p/plotinum/plotter"
  12. "code.google.com/p/plotinum/vg"
  13. )
  14.  
  15. const (
  16. NUM_SIMS = 5000
  17. HORIZON = 250
  18. )
  19.  
  20. // algorithms
  21. const (
  22. EPSILON_GREEDY = iota
  23. EPSILON_GREEDY_ANNEAL
  24. SOFTMAX
  25. SOFTMAX_ANNEAL
  26. UCB1
  27. )
  28.  
  29. func main() {
  30. rand.Seed(time.Now().Unix())
  31.  
  32. // setup arms
  33. means := []float64{0.1, 0.1, 0.1, 0.1, 0.9}
  34. nArms := len(means)
  35. shuffleFloat64(means)
  36.  
  37. arms := []bandit.Arm{}
  38. for _, mu := range means {
  39. arms = append(arms, bandit.NewBernoulliArm(mu))
  40. }
  41. bestArm := bestArm(means)
  42.  
  43. // setup plot
  44. p, err := plot.New()
  45. if err != nil {
  46. panic(err)
  47. }
  48. p.Title.Text = "bandit"
  49. p.X.Label.Text = "time"
  50. p.X.Min = 0.0
  51. p.X.Max = 250.0
  52. p.Y.Label.Text = "prob"
  53. p.Y.Min = 0.0
  54. p.Y.Max = 1.0
  55. p.Add(plotter.NewGrid())
  56. colors := []color.RGBA{
  57. color.RGBA{R: 244, G: 67, B: 54, A: 255},
  58. color.RGBA{R: 233, G: 30, B: 99, A: 255},
  59. color.RGBA{R: 156, G: 39, B: 176, A: 255},
  60. color.RGBA{R: 103, G: 58, B: 183, A: 255},
  61. color.RGBA{R: 63, G: 81, B: 181, A: 255},
  62. }
  63. pts := make(plotter.XYs, HORIZON)
  64.  
  65. // simulation
  66. epsilons := []float64{0.1, 0.2, 0.3, 0.4, 0.5}
  67. for i, epsilon := range epsilons {
  68. algorithms := []bandit.Algorithm{
  69. bandit.NewEpsilonGreedy(epsilon, nArms),
  70. bandit.NewEpsilonGreedyAnneal(epsilon, nArms),
  71. bandit.NewSoftmax(epsilon, nArms),
  72. bandit.NewSoftmaxAnneal(epsilon, nArms),
  73. bandit.NewUCB1(nArms),
  74. }
  75.  
  76. //return value: simNums, times, chosenArms, rewards, comulativeRewards
  77. _, _, chosenArms, _, _ := bandit.Simulate(algorithms[EPSILON_GREEDY], arms, NUM_SIMS, HORIZON)
  78.  
  79. calculateProbPoint(pts, bestArm, chosenArms)
  80. plotLine(p, pts, colors[i], fmt.Sprintf("%v", epsilon))
  81. }
  82.  
  83. fileName := p.Title.Text + ".png"
  84. if err := p.Save(4, 4, fileName); err != nil {
  85. panic(err)
  86. }
  87. }
  88.  
  89. func shuffleFloat64(s []float64) {
  90. n := len(s)
  91. for i := 0; i < n; i++ {
  92. j := rand.Intn(n - 1)
  93. s[j], s[i] = s[i], s[j]
  94. }
  95. }
  96.  
  97. func bestArm(means []float64) (index int) {
  98. index, _ = bandit.Max(means)
  99. return
  100. }
  101.  
  102. func calculateProbPoint(pts plotter.XYs, bestArm int, chosenArms []int) {
  103. for i := 0; i < HORIZON; i++ {
  104. count := 0
  105. for j := 0; j < NUM_SIMS; j++ {
  106. index := HORIZON*j + i
  107. if chosenArms[index] == bestArm {
  108. count = count + 1
  109. }
  110. }
  111.  
  112. pts[i].X = float64(i)
  113. pts[i].Y = float64(count) / float64(NUM_SIMS)
  114. }
  115. }
  116.  
  117. func plotLine(p *plot.Plot, pts plotter.XYs, color color.RGBA, legend string) {
  118. l, err := plotter.NewLine(pts)
  119. if err != nil {
  120. panic(err)
  121. }
  122. l.LineStyle.Width = vg.Points(1)
  123. l.LineStyle.Color = color
  124. p.Add(l)
  125. p.Legend.Add(legend, l)
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement