{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install imutils -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-28T13:10:48.091847Z","iopub.execute_input":"2022-07-28T13:10:48.092951Z","iopub.status.idle":"2022-07-28T13:10:57.615385Z","shell.execute_reply.started":"2022-07-28T13:10:48.092896Z","shell.execute_reply":"2022-07-28T13:10:57.614239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel = 10","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.622176Z","iopub.execute_input":"2022-07-28T13:10:57.622549Z","iopub.status.idle":"2022-07-28T13:10:57.628347Z","shell.execute_reply.started":"2022-07-28T13:10:57.622507Z","shell.execute_reply":"2022-07-28T13:10:57.626779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import argparse\nimport time\nimport cv2\nimport imutils\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom termcolor import colored\n\nfrom imutils import contours\nfrom PIL import Image as Img\nfrom IPython.display import Image\nfrom skimage import measure\n\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm_notebook as tqdm\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.629908Z","iopub.execute_input":"2022-07-28T13:10:57.630785Z","iopub.status.idle":"2022-07-28T13:10:57.645896Z","shell.execute_reply.started":"2022-07-28T13:10:57.630695Z","shell.execute_reply":"2022-07-28T13:10:57.644884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exp","metadata":{}},{"cell_type":"code","source":"# Noise\nfrom sklearn.datasets import make_blobs\ndef CreateNoiseImage(input_image):\n    global size\n    global noise_matrix\n    data = make_blobs(n_samples= 2800, n_features= 13, centers= 4, cluster_std =3, random_state=1)\n    #plt.figure(figsize=(8,8))\n    #plt.scatter(data[0][:,0],data[0][:,1], c = data[1], cmap='rainbow')\n\n    if size==1000:\n      data1=(((data[0]-np.min(data[0]))/(np.max(data[0])-np.min(data[0])))*860).astype(int)\n    elif size== 500:\n      data1=(((data[0]-np.min(data[0]))/(np.max(data[0])-np.min(data[0])))*460).astype(int)\n    data1[:,1] += 10\n    \n    noise_matrix = -1*np.ones((size,size,3))\n    for i,j in data1[:,:2]:\n        noise_matrix[j,i,:] = -5\n    \n    import matplotlib.pyplot as plt\n    \"\"\"\n    im = plt.imread('../Images/img4.jpg')[:size,:size,:] #[:1000,:1000,:]  # img4.jpg\n    plt.figure(figsize=(10,10))\n    implot = plt.imshow(im)\n\n    plt.scatter(data1[:,0],data1[:,1], c = data[1], cmap='rainbow')\n    plt.axis('off')\n    plt.savefig(\"../Images/img4_noise.jpg\",bbox_inches='tight',transparent=True, pad_inches=0)\n    plt.show()\n    \"\"\"\n    fig = plt.figure()\n    im = input_image\n    plt.figure(figsize=(10,10))\n    implot = plt.imshow(im)\n\n    \n    plt.scatter(data1[:,0],data1[:,1], c = data[1], cmap='rainbow')\n    plt.axis('off')\n    plt.savefig(\"../Images/img4_noise.jpg\",bbox_inches='tight',transparent=True, pad_inches=0)\n    plt.close()\n    out_image = plt.imread(\"../Images/img4_noise.jpg\")\n    #plt.show()\n    return out_image\n#t=CreateNoiseImage(plt.imread('../Images/img4.jpg')[:size,:size,:])","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.647746Z","iopub.execute_input":"2022-07-28T13:10:57.648377Z","iopub.status.idle":"2022-07-28T13:10:57.662923Z","shell.execute_reply.started":"2022-07-28T13:10:57.648335Z","shell.execute_reply":"2022-07-28T13:10:57.661762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Calculate_noise(img):\n    global width\n    global size\n    mean_img = img.copy()\n    img_transition_value = np.zeros((size//width,size//width))\n    for i in range(0,size,width):\n        for j in range(0,size,width):\n            # got all the points\n            t = img[i:i+width,j:j+width,:]\n            no_5 = (t==-5).sum()\n            #pixel_value = np.max(img[i:i+width,j:j+width,:])\n            #if pixel_value==-5:\n               # print(pixel_value)\n            if no_5 <= 3:              #200 didn't work\n                mean_img[i:i+width,j:j+width,:] = 250\n            else:\n                mean_img[i:i+width,j:j+width,:] = 0 #pixel_value\n    #kernel = np.ones((5,5), np.uint8)\n    #img_dilation = cv2.dilate(mean_img, kernel, iterations=1) \n    img_dilation = mean_img.copy()\n    img_transition_value=np.where(img_dilation<230,0,255) # 230\n    #cv2.imwrite(\"dilated_5_2.jpg\",img_transition_value)  # final image look BGR looks black and white\n    img_transition_value=img_transition_value[::width,::width,0]\n    img_transition_value=np.where(img_transition_value<120,-7,-1) # 230\n    return img_transition_value","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.665170Z","iopub.execute_input":"2022-07-28T13:10:57.665577Z","iopub.status.idle":"2022-07-28T13:10:57.681284Z","shell.execute_reply.started":"2022-07-28T13:10:57.665544Z","shell.execute_reply":"2022-07-28T13:10:57.680513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img = cv2.imread(\"../Images/img4.jpg\")[:size,:size,:]    #[:1000,:1000,:]\ndef Calculate_transition_matrix(img):\n    global noise_matrix\n    global width\n    global size\n    global noise\n    mean_img = img.copy()\n    img_transition_value = np.zeros((size//width,size//width))\n    for i in range(0,size,width):\n        for j in range(0,size,width):\n            # got all the points\n            pixel_value = np.mean(img[i:i+width,j:j+width,:])\n            if pixel_value >210:              #200 didn't work\n                mean_img[i:i+width,j:j+width,:] = 250\n            else:\n                mean_img[i:i+width,j:j+width,:] = 0 #pixel_value\n    #kernel = np.ones((5,5), np.uint8)\n    #img_dilation = cv2.dilate(mean_img, kernel, iterations=1) \n    img_dilation = mean_img.copy()\n    img_transition_value=np.where(img_dilation<230,0,255) # 230\n    #cv2.imwrite(\"dilated_5_2.jpg\",img_transition_value)  # final image look BGR looks black and white\n    img_transition_value=img_transition_value[::width,::width,0]\n    img_transition_value=np.where(img_transition_value<120,-80,-1) # 230 \n    \n    ## noise\n    #i1 = cv2.imread(\"noise1.PNG\")\n    #i1=cv2.resize(i1,(size,size))\n    #img_transition_value = np.minimum(i2,img_transition_value)\n    if noise == True:\n      i2=Calculate_noise(noise_matrix)\n      img_transition_value = np.minimum(img_transition_value,i2 )\n    ##\n    #print(\"final reward\")\n    #sns.heatmap(img_transition_value)\n    #plt.show()\n    return img_transition_value","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.682862Z","iopub.execute_input":"2022-07-28T13:10:57.683455Z","iopub.status.idle":"2022-07-28T13:10:57.701575Z","shell.execute_reply.started":"2022-07-28T13:10:57.683413Z","shell.execute_reply":"2022-07-28T13:10:57.700699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Calculate_transition_matrix(img):\n    temp_img = -10*np.ones(img.shape) # -80\n    temp_img[img>20] = -1 \n    return temp_img\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.703223Z","iopub.execute_input":"2022-07-28T13:10:57.703686Z","iopub.status.idle":"2022-07-28T13:10:57.719353Z","shell.execute_reply.started":"2022-07-28T13:10:57.703619Z","shell.execute_reply":"2022-07-28T13:10:57.718584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Sweep(Value, Policy,terminators,transition_value):\n    # create transition matrix globally that is not possible since we will create dynamic wall\n    New_Value = Value.copy()\n    delta = 0\n    x,y = Value.shape\n    for i in range(x):  # x\n        left = 0\n        right = 0\n        top = 0\n        bottom = 0\n        for j in range(y):  # y\n            # (i,j)  \n            if (i,j) in terminators:\n                pass\n            else:\n                same= Value[i,j]\n                if i==0:\n                    left = same\n                else:\n                    left = Value[i-1,j]\n                if i==x-1:\n                    right= same\n                else:\n                    right = Value[i+1,j]\n                if j==0:\n                    top= same\n                else:\n                    top = Value[i,j-1]\n                if j==y-1:\n                    bottom= same\n                else:\n                    bottom = Value[i ,j+1]\n#                 if (i,j) in red:\n#                     transition_reward = -10\n#                 else:\n#                     transition_reward = -1\n                transition_reward = transition_value[i,j]\n                total_value = Policy[(y)*i+j,0]*(transition_reward+left) + Policy[(y)*i+j,1]*(transition_reward+right) + Policy[(y)*i+j,2]*(transition_reward+top) + Policy[(y)*i+j,3]*(transition_reward+bottom)\n                #print(total_value, New_Value[i,j])\n                delta = max(delta, np.abs(total_value- Value[i,j]))\n                New_Value[i,j] = total_value    \n    #print(\"Sweep\")\n    return New_Value, delta","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.720743Z","iopub.execute_input":"2022-07-28T13:10:57.721201Z","iopub.status.idle":"2022-07-28T13:10:57.735846Z","shell.execute_reply.started":"2022-07-28T13:10:57.721150Z","shell.execute_reply":"2022-07-28T13:10:57.735008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Improve_Policy(Value, Policy):\n    New_Policy = Policy.copy()\n    #terminators = (0,1), (0,2),(0,3), (2,0),(2,1),(2,2)\n    x,y = Value.shape\n    for i in range(x):\n        for j in range(y):  # y\n            # (i,j)  \n            left = 0\n            right = 0\n            top = 0\n            bottom = 0\n            same= Value[i,j]\n            if i==0:\n                left = same\n            else:\n                left = Value[i-1,j]\n            if i==x-1:\n                right= same\n            else:\n                right = Value[i+1,j]\n            if j==0:\n                top= same\n            else:\n                top = Value[i,j-1]\n            if j==y-1:\n                bottom= same\n            else:\n                bottom = Value[i ,j+1]\n            my_list = [left,right, top,bottom]\n            max_val = max(my_list)\n            my_list = np.array(my_list)\n            my_list = (my_list==max_val).astype(int)\n            my_list = my_list/np.sum(my_list)\n            New_Policy[(y)*i+j,:] = my_list\n    #print(\"Improve_Policy\")\n    return New_Policy","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.737462Z","iopub.execute_input":"2022-07-28T13:10:57.737726Z","iopub.status.idle":"2022-07-28T13:10:57.754749Z","shell.execute_reply.started":"2022-07-28T13:10:57.737692Z","shell.execute_reply":"2022-07-28T13:10:57.753838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call =0\ndef Evaluate_value(Value, Policy,theta,terminators,transition_value):\n    Policy = Policy.copy()\n    theta = theta\n    terminators = terminators.copy()\n    Policy = Policy.copy()\n    global call\n    call += 1\n    #terminators = (0,1), (0,2),(0,3), (2,0),(2,1),(2,2)\n    delta = float(np.inf)\n    while delta> theta:\n        delta = 0\n        Value, delta = Sweep(Value, Policy, terminators,transition_value)\n        #print(delta,theta, delta>theta)\n    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n    New_Policy = Improve_Policy(Value, Policy)\n    if np.all(Policy==New_Policy):\n        #print(\"Evaluate_value_\")\n        return Value, New_Policy, theta, terminators\n    else:\n        # repeat\n        #print(\"Evaluate_value\")\n        return Evaluate_value(Value, New_Policy,theta, terminators,transition_value)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.756685Z","iopub.execute_input":"2022-07-28T13:10:57.757323Z","iopub.status.idle":"2022-07-28T13:10:57.772985Z","shell.execute_reply.started":"2022-07-28T13:10:57.757266Z","shell.execute_reply":"2022-07-28T13:10:57.771812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Calculate_optimal(Grid_shape,no_actions, terminators,img_mask):\n    global verbose\n    global img\n    global find_optimal_CS\n    global Charging_time_CS\n    global static_quality_score\n    global dynamic_quality_score\n    global transition_value\n    global actual_dynamic_overhead\n    global actual_static_overhead # we can't add charging time before we calculate rem overhead\n    x,y = Grid_shape\n    State_action = np.ones((x*y,no_actions))*(1/no_actions)  #######  L     R     T     B\n    Value = np.zeros((x,y))\n    theta = 0.1 #0.001\n    \n    transition_value = Calculate_transition_matrix(img_mask)\n#     for i,j in HOME:\n#         transition_value[i,j] = 0  \n    for no, (i,j) in enumerate(HOME):\n        transition_value[i,j] = 0\n        if static_quality_score == True or dynamic_quality_score == True:\n          if i != 0:\n            # not touching left edge\n            transition_value[i,j-1] = min(transition_value[i,j-1], -1*(actual_static_overhead[no]+actual_dynamic_overhead[no])-1)\n          if i != 49:\n            # not touching right edge\n            transition_value[i,j+1] = min(transition_value[i,j+1], -1*(actual_static_overhead[no]+actual_dynamic_overhead[no])-1)\n          if j != 0:\n            # not touching top edge\n            transition_value[i-1,j] = min(transition_value[i-1,j], -1*(actual_static_overhead[no]+actual_dynamic_overhead[no])-1)\n          if j != 49:\n            # not touching bottom edge\n            transition_value[i+1,j] = min(transition_value[i+1,j], -1*(actual_static_overhead[no]+actual_dynamic_overhead[no])-1)\n          \n#     print(\"REWARD of each state:-\")\n#     sns.heatmap(transition_value)\n#     plt.show()\n#     Value[17,24] = 30\n#     terminators.append((17,24))\n#     print(\"terminators\",terminators)\n    \n    #transition_value[25,25] = 1\n    #img[25*width-10-70:25*width+10-70,25*width-10:25*width+10,:] = 0\n    #Value[HOME] = 0 # No matter how low you make it (-4000) this will become optimal point because everywhere else value is calculated\n\n    l,m,th,tr =Evaluate_value(Value,State_action,79, terminators, transition_value) #l:- value function m:- optimal action\n    \n    if find_optimal_CS == False and dynamic_quality_score== False:\n      print('-'*40)\n      print(\"calculating FUTURE REWARD of each state...\")\n      print(\"-\"*40)\n      print(l)\n      print('FUTURE REWARD/Value Functions:-')\n#     g2=sns.heatmap(l)\n#     g2.tick_params(left=False)  # remove the ticks  #tick_params(left=False)  # remove the ticks\n#     plt.tight_layout()\n#     plt.show()\n    \n\n    if dynamic_quality_score == False and verbose != 0:\n      f,(ax1,ax2) = plt.subplots(1,2,sharey=True,figsize=(18,6))\n      g1 = sns.heatmap(transition_value,ax=ax1)\n      g1.set_ylabel('')\n      g1.set_xlabel('')\n      g2 = sns.heatmap(l,ax=ax2)\n      g2.set_ylabel('')\n      g2.set_xlabel('')\n      plt.show()\n    \n    \n#     layout1 = cv.Layout(\n#     height=500,\n#     width=500\n#     )\n    if static_quality_score == True and dynamic_quality_score == False:\n      # 3d plot\n      new_value = (l-np.min(l))/(np.max(l)-np.min(l))\n      pd.DataFrame(new_value).iplot(kind='surface',) # layout= layout1)\n    \n    #print(\"Calculate_optimal\")         # 80 for -100 , 9.5 for -10 , 100 for -1000  60 for -80\n    return l,m              ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.774452Z","iopub.execute_input":"2022-07-28T13:10:57.775279Z","iopub.status.idle":"2022-07-28T13:10:57.797679Z","shell.execute_reply.started":"2022-07-28T13:10:57.775235Z","shell.execute_reply":"2022-07-28T13:10:57.796904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def recheck(x,y):\n  global HOME\n  global optimal_value\n  global AGENT\n  global dynamic_quality_score\n  global actual_dynamic_overhead\n  global actual_static_overhead\n  global terminators\n  global state\n  global battery\n  global Overhead_time_CS\n  global Charging_time_CS\n  global no_steps\n  global stats_df\n  \n  index_no = HOME.index((x,y))\n  time_taken = -1* optimal_value[AGENT[0],AGENT[1]]\n  rem_overhead = max(0,Overhead_time_CS[index_no] - time_taken)\n  stats_df.loc[index_no,\"CS_pos\"] = str((x,y))\n  stats_df.loc[index_no,\"travel_time\"] = time_taken\n  stats_df.loc[index_no, \"rem_overhead\"] = rem_overhead\n  stats_df.loc[index_no, \"total time\"] = time_taken + rem_overhead + Charging_time_CS[index_no]\n  if rem_overhead <= 0:\n    # means none can be better than this cs.\n    # stop\n    #print(f\"Overhead on the CS is {Overhead_time_CS[index_no]}\")\n    #print(f\"Time taken to reach the CS is {time_taken}\")\n    #print(f\"No of steps is {no_steps}\")\n    #print(f\"Overhead on reaching CS {(x,y)} is: {rem_overhead} thus Total Time: {time_taken + rem_overhead}\")\n    print(stats_df)\n    if battery == True:\n      #print(f\"REACHED_DEST [{x},{y}] in {no_steps} steps, time taken {-1*optimal_value[AGENT[0],AGENT[1]]} minutes\")\n      print(f\"REACHED_DEST [{x},{y}] in {no_steps} steps, time taken {time_taken + rem_overhead + Charging_time_CS[index_no]} minutes\")\n    else:\n      print(f\"REACHED_DEST [{x},{y}] in {no_steps} steps, with reward of {optimal_value[AGENT[0],AGENT[1]]-rem_overhead-Charging_time_CS[index_no]}\")\n    print('-'*40)\n    return\n  else:\n    # change transition matrix\n    #print(f\"Overhead on the CS is {Overhead_time_CS[index_no]}\")\n    #print(f\"Time taken to reach the CS is {time_taken}\")\n    #print(f\"No of steps is {no_steps}\")\n    #print(f\"Overhead on reaching CS {(x,y)} is: {rem_overhead} thus Total Time: {time_taken + rem_overhead}\")\n    #print(\"Calculating...\")\n    print(stats_df)\n    actual_dynamic_overhead[index_no] = rem_overhead\n    actual_static_overhead[index_no]  = Charging_time_CS[index_no]\n    state = 112\n    \"\"\"\n    Grid_shape = transition_matrix.shape\n    no_actions = 4\n    \n    optimal_value,optimal_policy = Calculate_optimal(Grid_shape,no_actions, terminators,img_mask)\n    \"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.799033Z","iopub.execute_input":"2022-07-28T13:10:57.799735Z","iopub.status.idle":"2022-07-28T13:10:57.819575Z","shell.execute_reply.started":"2022-07-28T13:10:57.799696Z","shell.execute_reply":"2022-07-28T13:10:57.818534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimal_path = np.zeros((50,50))\ncollect_pts = []\ndef trace_path(optimal_value, optimal_policy,initial_pt):\n    global optimal_path\n    global width \n    global collect_pts\n    global dynamic_quality_score\n    global Last_CS \n    x,y = initial_pt  # 48,25\n    if (x,y) in HOME: #== HOME:\n        if dynamic_quality_score == True and (Last_CS != (x,y) or (x,y)== HOME[-1]):\n          # we will check once again\n          Last_CS = (x,y)\n          recheck(x,y)\n        else:\n          # stop\n          print(\"REACHED HOME\")\n          return\n    if x<0 or x>= 50 or y<0 or y>= 50:\n        # stop execution\n        return\n    # Plot\n    a1 = width*y\n    b1 = width*x\n    a2 = width*y + width\n    b2 = width*x + width\n    \n    #\n    policy_index = x*50 + y\n    l,r,t,b = optimal_policy[policy_index,:]\n    max_index = (l,r,t,b).index(max(l,r,t,b)) # pick the first optimal index\n    indexes = [i for i,j in enumerate((l,r,t,b)) if j==max(l,r,t,b)]\n    max_index = random.choice(indexes)\n    if max_index==0:\n        # left\n        #cv2.circle(img, ((a1+a2)//2,(b1+b2)//2),3,(0,250,0),2)\n        #cv2.line(img,((a1+a2)//2,b1),((a1+a2)//2,b2), (0,0,0),3)\n        trace_path(optimal_value, optimal_policy,(x-1,y))\n        optimal_path[x-1:x,y:y+1] += 1\n    elif max_index==1:\n        # right\n        #cv2.circle(img, ((a1+a2)//2,(b1+b2)//2),3,(0,250,0),2)\n        #cv2.line(img,((a1+a2)//2,b1),((a1+a2)//2,b2), (0,0,0),3)\n        trace_path(optimal_value, optimal_policy,(x,y+1))\n        optimal_path[x:x+1,y+1:y+2] += 1\n    elif max_index==2:\n        # top\n        #cv2.circle(img, ((a1+a2)//2,(b1+b2)//2),3,(0,250,0),2)\n        #cv2.line(img,(a1,(b1+b2)//2),(a2,(b1+b2)//2), (0,0,0),3)\n        trace_path(optimal_value, optimal_policy,(x,y-1))\n        optimal_path[x:x+1,y-1:y] += 1\n    elif max_index==3:\n        # bottom \n        #cv2.circle(img, ((a1+a2)//2,(b1+b2)//2),3,(0,250,0),2)\n        #cv2.line(img,(a1,(b1+b2)//2),(a2,(b1+b2)//2), (0,0,0),3)\n        trace_path(optimal_value, optimal_policy,(x,y+1))\n        optimal_path[x:x+1,y+1:y+2] += 1\n    else:\n        print(\"ERROR\")","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.821533Z","iopub.execute_input":"2022-07-28T13:10:57.822130Z","iopub.status.idle":"2022-07-28T13:10:57.842092Z","shell.execute_reply.started":"2022-07-28T13:10:57.822092Z","shell.execute_reply":"2022-07-28T13:10:57.841065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_path():\n    global optimal_path\n    global img\n    global width\n    global size\n    print('calculating')\n    Full_matrix = (np.repeat(np.repeat(optimal_path,width,axis=0),width,axis=1)*500).astype(int)\n    img = ((np.clip((img - Full_matrix.reshape(size,size,1)).astype(int),0,255)).astype(int)).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.845367Z","iopub.execute_input":"2022-07-28T13:10:57.845619Z","iopub.status.idle":"2022-07-28T13:10:57.861592Z","shell.execute_reply.started":"2022-07-28T13:10:57.845589Z","shell.execute_reply":"2022-07-28T13:10:57.860592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def smart_trace():\n#     #print('tracing path..')\n#     print(\"=\"*80)\n#     global optimal_value\n#     global optimal_policy\n#     global width \n#     global size\n#     global battery\n    \n#     global dynamic_quality_score\n#     global actual_dynamic_overhead\n#     global Last_CS\n#     global no_checked_CS\n#     global no_steps\n#     global stats_df\n#     global Overhead_time_CS\n#     global HOME\n#     global Charging_time_CS\n    \n#     if width == 20:\n#       w = 10\n#     elif width == 10:\n#       w = 10\n#     T =np.argmax(optimal_policy, axis=1)\n#     x_dict = {0:-1,1:1,2:0,3:0}\n#     y_dict = {0:0,1:0,2:-1,3:1}\n#     x_axis=np.vectorize(x_dict.get)(T)\n#     y_axis=np.vectorize(y_dict.get)(T)\n#     #f_x ,f_y = HOME\n#     in_x, in_y = AGENT\n#     no_steps = 0\n#     if width == 20:\n#       r = 2\n#     elif width == 10:\n#       r = 3\n#     while (in_x,in_y) not in HOME: #in_x != f_x or in_y != f_y:\n#         no_steps += 1\n#         m = w*in_x\n#         l = w*in_y\n#         cv2.circle(img, ((2*l+w)//2,(2*m+w)//2),r,(0,255,0),5)\n#         new_loc = 50*in_x + in_y\n#         in_x += x_axis[new_loc]\n#         in_y += y_axis[new_loc]\n#         #print(\"action\",x_axis[new_loc],y_axis[new_loc])\n#         #print(\"reached\",in_x,in_y)\n#         if l<0 or l>999 or m <0 or m>999:\n#             print('Not found',(2*l + w),(2*m + w))\n#             return\n\n \n#     x,y = in_x, in_y  # 48,25\n#     if (x,y) in HOME: #== HOME:\n#         if dynamic_quality_score == True and (Last_CS != (x,y)) and no_checked_CS != len(actual_dynamic_overhead):\n#           # we will check once again\n#           no_checked_CS += 1\n#           Last_CS = (x,y)\n#           recheck(x,y)\n#         else:\n#           # stop\n#           rem_overhead = 0\n#           Charge_t = 0\n#           if dynamic_quality_score == True:\n#             index_no = HOME.index((x,y))\n#             time_taken = -1* optimal_value[AGENT[0],AGENT[1]]\n#             rem_overhead = max(0,Overhead_time_CS[index_no] - time_taken)\n#             stats_df.loc[index_no,\"CS_pos\"] = str((x,y))\n#             stats_df.loc[index_no,\"travel_time\"] = time_taken\n#             stats_df.loc[index_no, \"rem_overhead\"] = rem_overhead\n#             stats_df.loc[index_no, \"total time\"] = time_taken + rem_overhead + Charging_time_CS[index_no]\n#             Charge_t = Charging_time_CS[index_no]\n#             print(stats_df)\n#           if battery == True:\n#             print(f\"REACHED_DEST [{in_x},{in_y}] in {no_steps} steps, time taken {-1*optimal_value[AGENT[0],AGENT[1]]+rem_overhead+Charge_t} minutes\")\n#           else:\n#             print(f\"REACHED_DEST [{in_x},{in_y}] in {no_steps} steps, with reward of {optimal_value[AGENT[0],AGENT[1]]-rem_overhead-Charge_t}\")\n#           print('-'*40)\n#           return\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.863203Z","iopub.execute_input":"2022-07-28T13:10:57.863769Z","iopub.status.idle":"2022-07-28T13:10:57.883983Z","shell.execute_reply.started":"2022-07-28T13:10:57.863728Z","shell.execute_reply":"2022-07-28T13:10:57.883112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def smart_trace():\n    #print('tracing path..')\n    print(\"=\"*80)\n    global optimal_value\n    global optimal_policy\n    global width \n    global size\n    global battery\n    \n    global dynamic_quality_score\n    global actual_dynamic_overhead\n    global Last_CS\n    global no_checked_CS\n    global no_steps\n    global stats_df\n    global Overhead_time_CS\n    global HOME\n    global Charging_time_CS\n    \n    if width == 20:\n      w = 10\n    elif width == 10:\n      w = 10\n    T =np.argmax(optimal_policy, axis=1)\n    x_dict = {0:-1,1:1,2:0,3:0}\n    y_dict = {0:0,1:0,2:-1,3:1}\n    x_axis=np.vectorize(x_dict.get)(T)\n    y_axis=np.vectorize(y_dict.get)(T)\n    #f_x ,f_y = HOME\n    in_x, in_y = AGENT\n    no_steps = 0\n    if width == 20:\n      r = 2\n    elif width == 10:\n      r = 3\n    while (in_x,in_y) not in HOME: #in_x != f_x or in_y != f_y:\n        no_steps += 1\n        m = w*in_x\n        l = w*in_y\n        #-#cv2.circle(img, ((2*l+w)//2,(2*m+w)//2),r,(0,255,0),5)\n        new_loc = 50*in_x + in_y\n        in_x += x_axis[new_loc]\n        in_y += y_axis[new_loc]\n        #print(\"action\",x_axis[new_loc],y_axis[new_loc])\n        #print(\"reached\",in_x,in_y)\n        if l<0 or l>999 or m <0 or m>999:\n            print('Not found',(2*l + w),(2*m + w))\n            return\n\n \n    x,y = in_x, in_y  # 48,25\n    if (x,y) in HOME: #== HOME:\n        if dynamic_quality_score == True and (Last_CS != (x,y)) and no_checked_CS != len(actual_dynamic_overhead):\n          # we will check once again\n          no_checked_CS += 1\n          Last_CS = (x,y)\n          recheck(x,y)\n        else:\n          # stop\n          rem_overhead = 0\n          Charge_t = 0\n          if dynamic_quality_score == True:\n            index_no = HOME.index((x,y))\n            time_taken = -1* optimal_value[AGENT[0],AGENT[1]]\n            rem_overhead = max(0,Overhead_time_CS[index_no] - time_taken)\n            stats_df.loc[index_no,\"CS_pos\"] = str((x,y))\n            stats_df.loc[index_no,\"travel_time\"] = time_taken\n            stats_df.loc[index_no, \"rem_overhead\"] = rem_overhead\n            stats_df.loc[index_no, \"total time\"] = time_taken + rem_overhead + Charging_time_CS[index_no]\n            Charge_t = Charging_time_CS[index_no]\n            print(stats_df)\n          if battery == True:\n            print(f\"REACHED_DEST [{in_x},{in_y}] in {no_steps} steps, time taken {-1*optimal_value[AGENT[0],AGENT[1]]+rem_overhead+Charge_t} minutes\")\n          else:\n            print(f\"REACHED_DEST [{in_x},{in_y}] in {no_steps} steps, with reward of {optimal_value[AGENT[0],AGENT[1]]-rem_overhead-Charge_t}\")\n          print('-'*40)\n          return\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.885394Z","iopub.execute_input":"2022-07-28T13:10:57.885808Z","iopub.status.idle":"2022-07-28T13:10:57.905515Z","shell.execute_reply.started":"2022-07-28T13:10:57.885758Z","shell.execute_reply":"2022-07-28T13:10:57.904332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Calculate_Cost(index):\n    global drag\n    global state\n    global HOME\n    global AGENT\n    global optimal_value\n    global img\n    global Grid_shape\n    global no_action\n    global terminators\n    global optimal_path\n    global width\n    global size\n    global optimal_policy\n    global time_limit\n    \n    global dj\n    \n    img_mask = dj #-#cv2.imread(\"img4.jpg\")[:size,:size,:]\n    terminators = [(index[0],index[1])]\n    HOME = terminators\n    v,p= Calculate_optimal(Grid_shape,no_actions, terminators,img_mask)\n    return v,p","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.906908Z","iopub.execute_input":"2022-07-28T13:10:57.907178Z","iopub.status.idle":"2022-07-28T13:10:57.922368Z","shell.execute_reply.started":"2022-07-28T13:10:57.907143Z","shell.execute_reply":"2022-07-28T13:10:57.921275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# called_No = 0\n# x1 = 0\n# y1 = 0\n# def Optimize_CS(image):\n#     global called_No \n#     global non_path_reward  #-80\n#     global tile_size\n#     global x1, y1\n#     List_indices = []\n#     List_costs = []\n    \n#     img_mask = image #-#cv2.imread(\"img4.jpg\")[:size,:size,:]\n#     transition_value =image #-# Calculate_transition_matrix(img_mask)\n#     \"\"\"\n#     HOME = [(3,3)]\n#     for i,j in HOME:\n#         transition_value[i,j] = 0\n#     \"\"\"\n#     mask = transition_value> non_path_reward+2\n#     #print(np.where(mask==1))\n#     # pick tile_size which can divide 50 like 10/50/2 etc\n#     no_subcell = 2\n\n#     #tile_size = 16\n#     if called_No == 0:\n#         # first time     \n#         x1 = 0\n#         y1 = 0\n#     else:\n#         tile_size = 8\n        \n        \n#     for i in range(0,no_subcell):\n#         for j in range(0,no_subcell):\n#             # (i,j)\n#             # pick su\n#             x,y = np.where(mask[x1+ i*tile_size: x1+ i*tile_size+tile_size, y1+ j*tile_size:y1 + j*tile_size+tile_size])\n#             x =  (i*tile_size) + x\n#             y =  (j*tile_size) + y\n#             my_list = list(zip(x,y))\n#             if my_list == []:\n#                 print(\"no road here\")\n#             else:\n#                 print(my_list)\n#                 length = len(my_list)\n#                 if length %2 != 0:\n#                     # odd no of terms\n#                     middle = my_list[(length-1)//2]\n#                 else:\n#                     # even no of terms\n#                     middle = my_list[(length//2) -1]\n#                 temp_transition_value = (transition_value[i*tile_size: i*tile_size+tile_size, j*tile_size: j*tile_size+tile_size]).copy()\n#                 print(f\"Calculating cost for {middle}\")\n#                 temp_transition_value[middle[0]-(i*tile_size),middle[1]-(j*tile_size)] = 10\n#                 plt.figure(figsize=(3,2))\n#                 sns.heatmap(temp_transition_value)\n#                 plt.show()\n                \n#                 List_indices.append(middle)\n#                 v,p = Calculate_Cost(middle)\n#                 cost = np.sum(v[mask])\n#                 List_costs.append(cost)\n#                 #print(np.random.choice(my_list, size=8))\n#                 print(middle,\"=\",cost)\n#             print(\"#\"*80)\n#     print(List_indices)\n#     print(\"+\"*40)\n#     print(List_costs)\n#     print(\"+\"*40)\n#     print(\"Optimal CS location\")\n#     print(List_indices[List_costs.index(max(List_costs))], max(List_costs))\n#     Calculate_Cost(List_indices[List_costs.index(max(List_costs))])\n    \n#     #-#\n#     a,b = List_indices[List_costs.index(max(List_costs))] # indices of max cost\n#     if a> x1 + tile_size:\n#         x1 += tile_size \n#     if b > y1 + tile_size:\n#         y1 += tile_size\n#     called_No += 1\n#     if called_No == 2:\n#         return [List_indices[List_costs.index(max(List_costs))]]\n#     else:\n#         return Optimize_CS(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.924749Z","iopub.execute_input":"2022-07-28T13:10:57.925886Z","iopub.status.idle":"2022-07-28T13:10:57.941610Z","shell.execute_reply.started":"2022-07-28T13:10:57.925828Z","shell.execute_reply":"2022-07-28T13:10:57.940686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Optimize_CS(image):\n    global verbose\n    global called_No \n    global non_path_reward  #-80\n    global tile_size\n    global x1, y1\n    List_indices = []\n    List_costs = []\n    \n    img_mask = image #-#cv2.imread(\"img4.jpg\")[:size,:size,:]\n    transition_value =image #-# Calculate_transition_matrix(img_mask)\n    \"\"\"\n    HOME = [(3,3)]\n    for i,j in HOME:\n        transition_value[i,j] = 0\n    \"\"\"\n    mask = transition_value> non_path_reward+2\n    #print(np.where(mask==1))\n    # pick tile_size which can divide 50 like 10/50/2 etc\n    no_subcell = 2\n\n    tile_size = 16\n    if called_No == 0:\n        # first time     \n        x1 = 0\n        y1 = 0\n    else:\n        tile_size = 8\n    for i in range(0,no_subcell):\n        for j in range(0,no_subcell):\n            # (i,j)\n            # pick su\n            x,y = np.where(mask[x1+ i*tile_size: x1+ i*tile_size+tile_size, y1+ j*tile_size:y1 + j*tile_size+tile_size])\n            x =  (i*tile_size) + x # since we have 2d aray flattended to 1d\n            y =  (j*tile_size) + y\n            my_list = list(zip(x,y))\n            if my_list == []:\n                if verbose != 0:\n                    print(\"no road here\")\n            else:\n                if verbose != 0:\n                    print(\"this is road\")\n                    print([(x1+i,y1+j) for i,j in my_list])\n                length = len(my_list)\n                if length %2 != 0:\n                    # odd no of terms\n                    middle = my_list[(length-1)//2]\n                else:\n                    # even no of terms\n                    middle = my_list[(length//2) -1]\n                temp_transition_value = (transition_value[x1 + i*tile_size: x1 + i*tile_size+tile_size, y1 + j*tile_size: y1 + j*tile_size+tile_size]).copy() # picking a 1/4th for display\n                if verbose != 0:\n                    print(f\"Calculating cost for {middle[0]+x1, middle[1]+y1}\")\n                temp_transition_value[middle[0]-(i*tile_size),middle[1]-(j*tile_size)] = 10 #mapping 1d back to 2d\n                if verbose != 0:\n                    plt.figure(figsize=(3,2))\n                    sns.heatmap(temp_transition_value)\n                    plt.show()\n                \n                List_indices.append(middle)\n                v,p = Calculate_Cost(middle)\n                if verbose != 0:\n                    print(mask.shape, v.shape)\n                cost = np.sum(v[mask])\n                List_costs.append(cost)\n                #print(np.random.choice(my_list, size=8))\n                if verbose != 0:\n                    print(middle[0]+x1 , middle[1]+y1,\"=\",cost)\n            if verbose != 0:\n                print(\"#\"*80)\n    if verbose != 0:\n        print([(x1+i,y1+j) for i,j in List_indices])\n        print(\"+\"*40)\n        print(List_costs)\n        print(\"+\"*40)\n        print(\"Optimal CS location\")\n    t = List_indices[List_costs.index(max(List_costs))]\n    if verbose != 0:\n        print(t[0]+x1, t[1]+ y1,\":\", max(List_costs))\n    Calculate_Cost(List_indices[List_costs.index(max(List_costs))])\n    \n    #-#\n    a,b = List_indices[List_costs.index(max(List_costs))] # indices of max cost\n    if a> x1 + tile_size:\n        x1 += tile_size \n    if b > y1 + tile_size:\n        y1 += tile_size\n    called_No += 1\n    if called_No == 2:\n        return [List_indices[List_costs.index(max(List_costs))]]\n    else:\n        return Optimize_CS(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.943353Z","iopub.execute_input":"2022-07-28T13:10:57.943769Z","iopub.status.idle":"2022-07-28T13:10:57.968129Z","shell.execute_reply.started":"2022-07-28T13:10:57.943730Z","shell.execute_reply":"2022-07-28T13:10:57.967312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"non_path_reward = -80 #-80\ntile_size = 10\n#\nsize =  32 #1000 #1000 #500\nwidth= 1\nnoise = False\nbattery = False\nfind_optimal_CS = True\nstatic_quality_score = False\ndynamic_quality_score = False\ndemo = False\n##############################\ntile_size = 16 #-#\ndrag = False\nLast_CS = (-100,-100) # something which is not in 50*50\nHOME =  [] #0\nAGENT = 0\nGrid_shape = (32,32) #-#(20,20) #(50,50) #(100,100) # later set automatically\nno_actions = 4\nterminatos = []\noptimal_path = np.zeros((50,50))\nnon_path_reward = -80\n#################################\nif size== 1000:\n  width = 20\nelif size==500:\n  width = 10\nw = 10","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.969306Z","iopub.execute_input":"2022-07-28T13:10:57.969949Z","iopub.status.idle":"2022-07-28T13:10:57.988766Z","shell.execute_reply.started":"2022-07-28T13:10:57.969914Z","shell.execute_reply":"2022-07-28T13:10:57.987520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install plotly\n#!pip install cufflinks\nfrom plotly import __version__\nprint(__version__)\nimport cufflinks as cv\nfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot,iplot\ninit_notebook_mode(connected=True)\ncv.go_offline()\n#pd.DataFrame(new_value).iplot(kind='surface',)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:57.990632Z","iopub.execute_input":"2022-07-28T13:10:57.991952Z","iopub.status.idle":"2022-07-28T13:10:58.012465Z","shell.execute_reply.started":"2022-07-28T13:10:57.991906Z","shell.execute_reply":"2022-07-28T13:10:58.011401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# verbose= 0\n# for bbox in bbox_list:\n#     bbox_img = image[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]]\n#     bbox_resized = cv2.resize(bbox_img, (32,32), interpolation = cv2.INTER_AREA)\n    \n#     # bbox_resized is my Image \n#     dj = bbox_resized\n#     called_No = 0\n#     x1 = 0\n#     y1 = 0\n#     print(\"Got Value:\")\n#     HOME = Optimize_CS(bbox_resized)\n#     # Calculate path cost\n#     img_mask = bbox_resized\n#     optimal_value,optimal_policy = Calculate_optimal(Grid_shape,no_actions, HOME,img_mask)\n#     if verbose != 0:\n#         sns.heatmap(optimal_value)\n#         plt.show()\n#         new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n#         pd.DataFrame(new_value).iplot(kind='surface',)\n#     print(optimal_value[0,0], optimal_value[-1,0], optimal_value[0,-1], optimal_value[-1,-1], \"sum: \",optimal_value[0,0]+ optimal_value[-1,0]+optimal_value[0,-1]+ optimal_value[-1,-1] )\n#     #break\n#     \"\"\"\n#     #AGENT\n#     FROM = [(0,0),(31,0),(0,31),(31,31)]\n#     total_sum = 0\n#     for i in range(4):\n#         AGENT = FROM[i]\n\n#         #smart_trace()\n#         new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n#         pd.DataFrame(new_value).iplot(kind='surface',)\n#     print(\"Net\")\n#     print(total_sum)\n#     \"\"\"\n    \n        \n\n#     display(Img.fromarray((bbox_resized).astype(np.uint8)))\n#     print(\"=\"*40)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:58.014103Z","iopub.execute_input":"2022-07-28T13:10:58.014963Z","iopub.status.idle":"2022-07-28T13:10:58.030316Z","shell.execute_reply.started":"2022-07-28T13:10:58.014910Z","shell.execute_reply":"2022-07-28T13:10:58.029501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"called_No = 0\nverbose = 0\ndj = 0\noptimal_policy= 0\ndef get_pieces(img_path):\n    global called_No \n    global verbose \n    global dj\n    global optimal_policy\n    global AGENT\n    \n    image = cv2.imread(img_path, 0)\n    blurred = cv2.GaussianBlur(image, (11, 11), 0)\n    thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]\n    thresh = cv2.erode(thresh, None, iterations=2)\n    thresh = cv2.dilate(thresh, None, iterations=4)\n    labels = measure.label(thresh,background = 0)\n    mask = np.zeros(thresh.shape, dtype=\"uint8\")\n\n    for label in np.unique(labels):\n        if label == 0:\n            continue\n\n        labelMask = np.zeros(thresh.shape, dtype=\"uint8\")\n        labelMask[labels == label] = 255\n        numPixels = cv2.countNonZero(labelMask)\n        if numPixels > 300:\n            mask = cv2.add(mask, labelMask)\n    \n    bbox_list = []\n\n    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    cnts = imutils.grab_contours(cnts)\n    cnts = contours.sort_contours(cnts)[0]\n    print(f'Found {len(cnts)} contours / numbers')\n    backtorgb = cv2.cvtColor(image.astype('float32'), cv2.COLOR_GRAY2RGB)\n\n    for (i, c) in enumerate(cnts):\n        (x, y, w, h) = cv2.boundingRect(c)\n        bbox_list.append([x, y, w, h])\n        cv2.rectangle(backtorgb, (x,y), (x+w, y+h), (255,0,0), 5)\n\n    print(f'BBoxes coordinates: {bbox_list}')\n    \n    verbose= 0\n    for bbox in bbox_list:\n        bbox_img = image[bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]]\n        bbox_resized = cv2.resize(bbox_img, (32,32), interpolation = cv2.INTER_AREA)\n\n        # bbox_resized is my Image \n        dj = bbox_resized\n        called_No = 0\n        x1 = 0\n        y1 = 0\n        #print(\"Got Value:\")\n        HOME = Optimize_CS(bbox_resized)\n        # Calculate path cost\n        img_mask = bbox_resized\n        optimal_value,optimal_policy = Calculate_optimal(Grid_shape,no_actions, HOME,img_mask)\n        \"\"\"\n        for k in range(2):\n            for t in range(2):\n                t1= 0\n                t2 = 0\n                if k==1:\n                    t1 = -1 \n                if t == 1:\n                    t2 = -1\n                AGENT = (t1,t2)\n                smart_trace()\n        \"\"\"\n        if verbose != 0:\n            sns.heatmap(optimal_value)\n            plt.show()\n            new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n            pd.DataFrame(new_value).iplot(kind='surface',)\n        print(round(optimal_value[0,0],2), round(optimal_value[-1,0],2), round(optimal_value[0,-1],2), round(optimal_value[-1,-1],2), \"sum: \", round((optimal_value[0,0]+ optimal_value[-1,0]+optimal_value[0,-1]+ optimal_value[-1,-1]),2) )\n        #break\n        \"\"\"\n        #AGENT\n        FROM = [(0,0),(31,0),(0,31),(31,31)]\n        total_sum = 0\n        for i in range(4):\n            AGENT = FROM[i]\n\n            #smart_trace()\n            new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n            pd.DataFrame(new_value).iplot(kind='surface',)\n        print(\"Net\")\n        print(total_sum)\n        \"\"\"\n\n\n\n        display(Img.fromarray((bbox_resized).astype(np.uint8)))\n        print(\"=\"*40)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:58.032086Z","iopub.execute_input":"2022-07-28T13:10:58.032664Z","iopub.status.idle":"2022-07-28T13:10:58.059624Z","shell.execute_reply.started":"2022-07-28T13:10:58.032583Z","shell.execute_reply":"2022-07-28T13:10:58.058783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"called_No = 0\nverbose = 1 #0\ndj = 0\noptimal_policy= 0\ndef Image_array(image):\n    global called_No \n    global verbose \n    global dj\n    global optimal_policy\n    global AGENT\n    \n    # bbox_resized is my Image \n    dj = np.array(image.resize( (32,32)))\n    called_No = 0\n    x1 = 0\n    y1 = 0\n    #print(\"Got Value:\")\n    HOME = Optimize_CS(dj)\n    # Calculate path cost\n    img_mask = dj\n    optimal_value,optimal_policy = Calculate_optimal(Grid_shape,no_actions, HOME,img_mask)\n    \"\"\"\n    for k in range(2):\n        for t in range(2):\n            t1= 0\n            t2 = 0\n            if k==1:\n                t1 = -1 \n            if t == 1:\n                t2 = -1\n            AGENT = (t1,t2)\n            smart_trace()\n    \"\"\"\n    if verbose != 0:\n        sns.heatmap(optimal_value)\n        plt.show()\n        new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n        pd.DataFrame(new_value).iplot(kind='surface',)\n    print(round(optimal_value[0,0],2), round(optimal_value[-1,0],2), round(optimal_value[0,-1],2), round(optimal_value[-1,-1],2), \"sum: \", round((optimal_value[0,0]+ optimal_value[-1,0]+optimal_value[0,-1]+ optimal_value[-1,-1]),2) )\n    #break\n    \"\"\"\n    #AGENT\n    FROM = [(0,0),(31,0),(0,31),(31,31)]\n    total_sum = 0\n    for i in range(4):\n        AGENT = FROM[i]\n\n        #smart_trace()\n        new_value = (optimal_value-np.min(optimal_value))/(np.max(optimal_value)-np.min(optimal_value))\n        pd.DataFrame(new_value).iplot(kind='surface',)\n    print(\"Net\")\n    print(total_sum)\n    \"\"\"\n\n\n\n    display(Img.fromarray((dj).astype(np.uint8)))\n    print(\"=\"*40)\n    \n    return round(optimal_value[0,0],2), round(optimal_value[-1,0],2), round(optimal_value[0,-1],2), round(optimal_value[-1,-1],2),  round((optimal_value[0,0]+ optimal_value[-1,0]+optimal_value[0,-1]+ optimal_value[-1,-1]),2) ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:58.061222Z","iopub.execute_input":"2022-07-28T13:10:58.061789Z","iopub.status.idle":"2022-07-28T13:10:58.079075Z","shell.execute_reply.started":"2022-07-28T13:10:58.061735Z","shell.execute_reply":"2022-07-28T13:10:58.078287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nTest_Set = pd.read_csv(\"../input/digit-recognizer/test.csv\").iloc[sel*1000: (sel+1)*1000]","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:10:58.080432Z","iopub.execute_input":"2022-07-28T13:10:58.080918Z","iopub.status.idle":"2022-07-28T13:11:00.128350Z","shell.execute_reply.started":"2022-07-28T13:10:58.080869Z","shell.execute_reply":"2022-07-28T13:11:00.127193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pre = np.zeros((Test_Set.shape[0],5))\ntest_pre.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:11:00.129714Z","iopub.execute_input":"2022-07-28T13:11:00.130022Z","iopub.status.idle":"2022-07-28T13:11:00.138917Z","shell.execute_reply.started":"2022-07-28T13:11:00.129986Z","shell.execute_reply":"2022-07-28T13:11:00.137933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modifying Test Set","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfor i in range(5,15):\n    img=Test_Set.iloc[i,:].values#.as_matrix()\n    img=img.reshape((28,28))\n    j1= Image.fromarray(img.astype(np.uint8), 'L')\n    type(j1)\n    test_pre[i,:]= Image_array(j1)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:15:31.173940Z","iopub.execute_input":"2022-07-28T13:15:31.174286Z","iopub.status.idle":"2022-07-28T13:20:32.763553Z","shell.execute_reply.started":"2022-07-28T13:15:31.174250Z","shell.execute_reply":"2022-07-28T13:20:32.762622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"st= pd.DataFrame(test_pre,columns=['f1','f2','f3','f4','f5'])\nst.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:37:46.830357Z","iopub.execute_input":"2022-07-28T13:37:46.830732Z","iopub.status.idle":"2022-07-28T13:37:46.848512Z","shell.execute_reply.started":"2022-07-28T13:37:46.830693Z","shell.execute_reply":"2022-07-28T13:37:46.847636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"st.to_csv(f\"test_modified_{sel}.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T13:37:48.423513Z","iopub.execute_input":"2022-07-28T13:37:48.424069Z","iopub.status.idle":"2022-07-28T13:37:48.438889Z","shell.execute_reply.started":"2022-07-28T13:37:48.424023Z","shell.execute_reply":"2022-07-28T13:37:48.437716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}