{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Part 1 Code\n", "import tkinter\n", "\n", "BLOCKED = 0 # site is blocked \n", "OPEN = 1 #...

1 answer below »
Hi,I need help with problems in python programming. Would appreciate the help.Thank you


{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Part 1 Code\n", "import tkinter\n", "\n", "BLOCKED = 0 # site is blocked \n", "OPEN = 1 # site is open and empty\n", "FULL = 2 # site is open and full\n", "\n", "# drawing scale \n", "SCALE = 30 \n", "\n", "\"\"\"Draws the initial configuration of the grid.\n", " (You do not need to call this function in any new places)\"\"\"\n", "def drawGrid(grid, canvas): \n", " rows = len(grid)\n", " columns = len(grid[0])\n", " for row in range(rows):\n", " for col in range(columns):\n", " if grid[row][col] == BLOCKED:\n", " drawSquare((row, col), 'black', grid, canvas)\n", " else:\n", " drawSquare((row, col), 'white', grid, canvas)\n", " \n", "\"\"\" Draws one square in the given color at the\n", " given position in the grid.\n", " (You will call this in order to \"fill-in\" squares )\n", " \n", " Parameters: \n", " pos: a (row, column) tuple\n", " color: a color string\n", " canvas: a Tkinter canvas object\n", " \n", " Return value: None (void function)\"\"\"\n", "def drawSquare(pos, color, grid, canvas): #anytime you visit a cell, you should be calling this function\n", " n = len(grid)\n", " m = len(grid[0])\n", " outline = \"white\"\n", " if color == \"white\":\n", " outline = \"black\"\n", " (row, col) = pos\n", " scaleRow = SCALE*row + 10\n", " scaleCol = SCALE*col + 10\n", " canvas.create_rectangle(scaleCol, scaleRow, scaleCol + SCALE, scaleRow + SCALE,\n", " outline=outline, fill=color, width=1)\n", " if row == 0:\n", " canvas.create_line(scaleCol, scaleRow, scaleCol + SCALE, scaleRow, fill=\"black\")\n", " if col == 0:\n", " canvas.create_line(scaleCol, scaleRow, scaleCol, scaleRow + SCALE, fill=\"black\")\n", " if row == n-1:\n", " canvas.create_line(scaleCol, scaleRow+SCALE, scaleCol+SCALE, scaleRow + SCALE, fill=\"black\")\n", " if col == m-1: \n", " canvas.create_line(scaleCol+SCALE, scaleRow, scaleCol+SCALE, scaleRow + SCALE, fill=\"black\")\n", " \n", "\"\"\"Do a depth first search on grid starting at site \n", " (row, col). If draw == True, visualize the \n", " percolation using tortoise.\"\"\"\n", "def dfs(grid, row, col, canvas, draw):\n", " # given the below function, change it to fit the above function. \n", "def dfs(grid, source, dest): # source is a tuple basically the same thing as row and col except your adding two parameter\n", "#dest is destination \n", " if source == dest: # base case, return true if we are at destination \n", " return True\n", " \n", " #define variable based on the grid\n", " (row, col) = square\n", " numberRow = len(grid)\n", " numCols = len(grid[0])\n", " \n", " #base case: return False if source is invalid\n", " if (row < 0)="" or="" (row="">= numRow) \\\n", " or (col < 0)="" or="" (col="">= numCols)\\\n", " or grid[row][col] == BLOCKED \\\n", " or grid[row][col] == VISITED \\\n", " return false\n", " grid[row][col] = VISTIED # mark that we have visited the location\n", " \n", " #Recursive case: do DFS on each adjacent position (above, below, left, right)\n", " above = (row - 1, col)\n", " below = (row + 1, col)\n", " left = (row, col - 1)\n", " right = = (row, col + 1)\n", " \n", " if dfs (grid, above, dest) == True: #dfs above\n", " return True\n", " if dfs (grid, below, dest) == True: # dfs below\n", " return True\n", " if dfs (grid, left, dest) == True: # dfs left\n", " return True\n", " if dfs (grid, right, dest) == True: #def rigth\n", " return True\n", " # no dfs was able to reach destination so return False\n", " return False\n", "\n", "\"\"\" Decide whether a grid percolates.\n", " If draw == True, then visualize the percolation with \n", " in a graphics window.\"\"\" \n", "def percolates(grid, draw):\n", "\n", " numRows = len(grid)\n", " numCols = len(grid[0])\n", " canvas = None\n", " \n", " # if boolean draw is set to be true...\n", " if draw == True: # set of the the graphics window\n", " tkObject = tkinter.Tk()\n", " canvas_width = numCols*SCALE + 20\n", " canvas_height = numRows*SCALE + 20\n", " canvas = tkinter.Canvas(tkObject, width = canvas_width, height = canvas_height)\n", " drawGrid(grid, canvas)\n", " canvas.pack()\n", " \n", " # *** \"POUR THE FLUID\" ***\n", " # (add your code here)\n", " \n", " # final update to graphics window \n", " if draw == True:\n", " tkObject.update()\n", " tkObject.mainloop()\n", " \n", " # return true or false \n", " pass \n", "\n", "def main(): \n", " exampleGrid = [[OPEN,OPEN,OPEN,OPEN,OPEN],\n", " [BLOCKED,BLOCKED,BLOCKED,BLOCKED,OPEN],\n", " [OPEN,OPEN,OPEN,OPEN
Answered Same DayMay 10, 2021

Answer To: { "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [],...

Sandeep Kumar answered on May 11 2021
138 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Percolation on a square grid - Monte Carlo simulation\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot
as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"**Percolation**"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def generate_damage_grid(height, width, probability):\n",
" return np.random.choice([True, False], p=[probability, 1 - probability], size=(height, width))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def find_route_to_bottom(damage_grid, fulfillment_grid, row, col):\n",
" if row == damage_grid.shape[0] - 1:\n",
" return True\n",
"\n",
" fulfillment_grid[row][col] = True\n",
"\n",
" for next_row, next_col in [(row - 1, col), (row, col - 1), (row, col + 1), (row + 1, col)]:\n",
" if (next_row >= 0) and (next_col >= 0) and (next_col < damage_grid.shape[1]):\n",
" if (damage_grid[next_row][next_col]) and (not fulfillment_grid[next_row][next_col]):\n",
" if find_route_to_bottom(damage_grid, fulfillment_grid, next_row, next_col):\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def check_if_percolating(damage_grid):\n",
" fulfillment_grid = np.full_like(damage_grid, False)\n",
"\n",
" for col in range(damage_grid.shape[1]):\n",
" if (damage_grid[0][col]) and (not fulfillment_grid[0][col]):\n",
" if find_route_to_bottom(damage_grid, fulfillment_grid, 0, col):\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"**Monte Carlo simulation**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"GRID_HEIGHT = 16\n",
"GRID_WIDTH = 16\n",
"\n",
"TRIALS_NUMBER = 1000"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"probability_list = np.linspace(0.1, 0.9, 17)\n",
"percolating_percentage_list = []\n",
"\n",
"for damage_probability in probability_list:\n",
" percolating_counter = 0\n",
"\n",
" for trial in range(TRIALS_NUMBER):\n",
" damage_grid = generate_damage_grid(GRID_HEIGHT, GRID_WIDTH, damage_probability)\n",
"\n",
" if check_if_percolating(damage_grid):\n",
" percolating_counter += 1\n",
"\n",
" percolating_percentage_list.append(100.0 * percolating_counter / TRIALS_NUMBER)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"**Visualization**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here