code: from flask import Flask, redirect, render_template, url_for
  import numpy as np
  
  app = Flask( __name__ )
  
  @app.route( '/home' )
  def index():
      # retrieve the agent
      agent = app.config['AGENT']
  
      print( 'Episode: {}/{}'.format( agent.get_episode(), agent.get_episodes() ) )
      print( 'Trial: {}/{}'.format( agent.get_trial(), agent.get_trials() ) )
      if agent.get_episode() > agent.get_episodes():
          # episodes are over
          # compute the final prob
          prob_reward_array = agent.get_prob_reward_array()
          prob_01 = 100*np.round( prob_reward_array[0] / agent.get_episodes(), 2 )
          prob_02 = 100*np.round( prob_reward_array[1] / agent.get_episodes(), 2 )
  
          # avg the accumulated reward
          avg_accumulated_reward = agent.get_avg_accumulated_reward_array()
  
          # print the final 
          print( '\nProb Bandit 01:{}% - Prob Bandit 02:{}%'.format( prob_01, prob_02 ) )
          print( '\n Avg accumulated reward: {}\n'.format( np.mean( avg_accumulated_reward ) ) )
  
          # reset the episodes
          agent.reset_episode()
  
      elif agent.get_trial() > agent.get_trials():
          # trials are over
          # increase the episode
          agent.set_episode()
  
          # compute the partial results
          agent.set_prob_reward_array()
  
          # append the accumualted reward
          agent.set_append_accumulated_reward()
  
          # append the avg accumulated reward
          agent.set_append_avg_accumulated_reward()
  
          # reset the trial and initial variables
          agent.set_trial( reset=1 )
  
          # get the partial results
          partial_result = agent.get_prob_reward_array()
          prob_01 = partial_result[0] / agent.get_episode()
          prob_02 = partial_result[1] / agent.get_episode()
  
          # print the partial results
          print( '\n Prob Bandit 01:{} - Prob Bandit 02:{}\n'.format( prob_01, prob_02 ) )
          return redirect( url_for( 'index' ) )
  
      else:
          # trials are not over
          # code the omniscient agent
          bandit_machine = np.argmax( agent.get_prob_list() )
  
          # set the current bandit machine
          agent.set_current_bandit( bandit_machine )
  
          # pick up the web page
          if bandit_machine == 0: # red Yes button
              return render_template( 'layout_red.html' )
          else:
              return render_template( 'layout_blue.html' )
  
  @app.route( '/yes', methods=['POST'] )
  def yes_event():
      agent = app.config['AGENT']
  
      # set the reward
      reward = 1
  
      # get the current bandit machine
      bandit_machine = agent.get_current_bandit()
  
      # add a reward to the bandit machine
      agent.set_reward_array( bandit_machine, reward )
  
      # increase how many times the bandit machine gets the lever pulled
      agent.set_bandit_array( bandit_machine )
  
      # sum the accumulated reward
      agent.set_accumulated_reward( reward )
  
      # increase the number of trial
      agent.set_trial( reset=0 )
  
      return redirect( url_for( 'index' ) )
  
  @app.route( '/no', methods=['POST'] )
  def no_event():
      agent = app.config['AGENT']
  
      # set the reward
      reward = 0
  
      # get the current bandit machine
      bandit_machine = agent.get_current_bandit()
  
      # add a reward to the bandit machine
      agent.set_reward_array( bandit_machine, reward )
  
      # increase how many times the bandit machine gets the lever pulled
      agent.set_bandit_array( bandit_machine )
  
      # sum the accumulated reward
      agent.set_accumulated_reward( reward )
  
      # increase the number of trial
      agent.set_trial( reset=0 )
      return redirect( url_for( 'index' ) )
  
  if __name__ == "__main__":
      trials = 100
      episodes = 20
  
      prob_list = [0.3, 0.8]
  
      agent = OmniscientAgent( prob_list, trials, episodes )
  
      app.config['AGENT'] = agent
      app.run()
  
   #import the libraries
  import numpy as np
  import random
  
  # class of random agent
  class RandomAgent( object ):
      def __init__( self, prob_list ):
          self.prob_list = prob_list
  
      def pull( self, bandit_machine ):
          if np.random.random() < self.prob_list[ bandit_machine ]:
              reward = 1
          else:
              reward = 0
  
          return reward
  
  # prob list
  prob_list = [0.3, 0.8 ]
  
  # define the variable of the episode
  trials = 1000
  episodes = 200
  
  prob_reward_array = np.zeros( len( prob_list ) )
  accumulated_reward_array = list()
  avg_accumulated_reward_array = list()
  
  bandit = RandomAgent( prob_list )
  
  for episode in range( episodes ):
      if episode % 10 == 0:
          print( 'Episode: {} / {}'.format( episode, episodes ) )
  
      # initialize the variable
      reward_array = np.zeros( len( prob_list ) )
      bandit_array = np.full( len( prob_list ), 1.0e-5 )
      accumulated_reward = 0
  
      for trial in range( trials ):
          # define the random strategy
          bandit_machine = np.random.randint( low=0, high=2, size=1 )[0]
  
          # get the reward
          reward = bandit.pull( bandit_machine )
  
          # save the partial results
          reward_array[ bandit_machine ] += reward
          bandit_array[ bandit_machine ] += 1
          accumulated_reward += reward
  
      # compute the partial results
      prob_reward_array += reward_array / bandit_array
      accumulated_reward_array.append( accumulated_reward )
      avg_accumulated_reward_array.append( np.mean( accumulated_reward_array ) )
  
  # compute the final results
  prob_01 = 100*np.round( prob_reward_array[0] / episodes, 2 )
  prob_02 = 100*np.round( prob_reward_array[1] / episodes, 2 )
  
  # print the final results
  print( '\n Prob Bandit 01:{}% - Prob Bandit 02:{}%'.format( prob_01, prob_02 ) )
  print( '\n Avg accumulated reward: {}\n'.format( np.mean( avg_accumulated_reward_array ) ) )
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
apis: numpy.mean
apis: numpy.round numpy.random.randint numpy.mean numpy.random.random