<jupyter_start><jupyter_text>Approximating Year 1's reward Ditribution using Bayesian OptimizationIn this notebook we demonstrate the efficiency of Bayesian Optimization in approximating year 1's reward distribution.<jupyter_code>from bayes_opt import BayesianOptimization
from bayes_opt.util import UtilityFunction
import numpy as np

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import mlab
from matplotlib import gridspec
%matplotlib inline
## challenge import
from netsapi.challenge import *
 
from sys import exit, exc_info, argv
from multiprocessing import Pool, current_process
import random as rand
import json
import requests
import numpy as np
import pandas as pd


import statistics
from IPython.display import clear_output

from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout():
    with open(os.devnull, "w") as devnull:
        old_stdout = sys.stdout
        sys.stdout = devnull
        try:  
            yield
        finally:
            sys.stdout = old_stdout

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
print("done")
envSeqDec = ChallengeSeqDecEnvironment()  # here you can change the environment (environment 1 or 2 )
x_start = 0.0
x_end = 1.0

VAL_Max = 1.2 # fel heat map

def target1(x, y):
    x = np.asscalar(x)
    y = np.asscalar(y)
    envSeqDec.reset()
    action = [x , y]
    print("action",action)
    s,r,d,_ = envSeqDec.evaluateAction(action)
    return r/90.0    

def target(x,y):
    if type(x) is np.ndarray:
        result = []
        for a,b in zip(x,y):
            reward = target1(a,b)
            result.append( reward )
            print((len(result) % 30 ) )
            if ((len(result) % 30 ) > 25) : 
                global envSeqDec
                envSeqDec = ChallengeSeqDecEnvironment()
        return result
    else:
        return target1(x,y)

# let's get an idea about the target  function

x = y = np.linspace(x_start, x_end, 40)# was 300
X, Y = np.meshgrid(x, y)
x = X.ravel()
y = Y.ravel()
X = np.vstack([x, y]).T[:, [1, 0]]
z = target(x, y)


fig, axis = plt.subplots(1, 1, figsize=(14, 10))
gridsize=29

im = axis.hexbin(x, y, C=z, gridsize=gridsize, cmap=cm.jet, bins=None, vmin=-VAL_Max, vmax=VAL_Max)
axis.axis([x.min(), x.max(), y.min(), y.max()])

cb = fig.colorbar(im, )
cb.set_label('Value')

f = np.array(z)    
f = f.reshape(40,40)  
    
    
fig, ax = plt.subplots(1, 1, figsize=(7, 5))
im = ax.imshow(f, clim=(-VAL_Max, VAL_Max),interpolation="nearest", origin="lower") # origin="upper"
nbOfVal = 40
title = "y1  "+" min="+str(round(np.amin(f),2))+ " max="+str(round(np.amax(f),2))
longTitle = title+" "+str(nbOfVal)+"by"+str(nbOfVal)
ax.set_title(title)
fig.colorbar(im, ax=ax)

np.savetxt(longTitle+'.out', f, delimiter=',')   # f is an array # use numpy.genfromtxt
print(longTitle+'.out')
print(f)
#print(np.matrix.sum(f))

fig.canvas.draw()
fig.canvas.flush_events()<jupyter_output>y1   min=-1.21 max=1.25 40by40.out
[[2.75671645e-02 2.83477984e-02 2.65722459e-02 ... 1.05763021e+00
  9.75040382e-01 1.01270948e+00]
 [5.47577064e-02 4.92837952e-02 4.63862897e-02 ... 7.76182199e-01
  8.94415416e-01 1.01907086e+00]
 [1.04018183e-01 9.63074168e-02 8.23448295e-02 ... 5.43901368e-01
  6.11510700e-01 8.03641191e-01]
 ...
 [9.35459110e-01 1.05445769e+00 9.25383607e-01 ... 9.86251934e-03
  6.30137545e-03 8.47104308e-04]
 [1.12482987e+00 1.09105475e+00 1.00530704e+00 ... 1.75914785e-01
  1.41913758e-01 9.15880632e-02]
 [1.07490652e+00 1.15915414e+00 9.17745964e-01 ... 3.16792578e-01
  2.38726952e-01 1.49886083e-01]]
{'fraction_non_alphanumeric': 0.1361358574610245, 'fraction_numerical': 0.1272271714922049, 'mean_word_length': 3.452292441140025, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '419263', 'license': 'MIT', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1401, 'n_words': 327, 'path': '2 Bayesian Optimisation based solutions/Misc/KDDCup_2Dplot_Env01_year01_only_40x40.ipynb', 'repo_name': 'DorraElMekki/Reinforcement-Learning-Malaria-Control-KDDCup-2019', 'size': '3592'}