2017-04-06 62 views
0

所以我寫完代碼,以顯示紐約市的地圖的圖像看起來應該像這樣建模和顯示地圖紐約市的:病毒模擬

enter image description here

但與當前的代碼,我有:

import random 
import string 
import math 
from matplotlib import pyplot as plt 

def normpdf(x, mean, sd): 
""" 
Return the value of the normal distribution 
with the specified mean and standard deviation (sd) at 
position x. 
You do not have to understand how this function works exactly. 
""" 
var = float(sd)**2 
denom = (2*math.pi*var)**.5 
num = math.exp(-(float(x)-float(mean))**2/(2*var)) 
return num/denom 


recovery_time = 4 # recovery time in time-steps 
virality = 0.2 # probability that a neighbor cell is infected in 
       # each time step             

class Cell(object): 

    def __init__(self,x, y): 
     self.x = x 
     self.y = y 
     self.state = "S" # can be "S" (susceptible), "R" (resistant = dead), or 
        # "I" (infected) 

    def infect(self): 
    pass 

class Map(object): 
    cells_list = [] 

    def __init__(self): 
     self.height = 150 
     self.width = 150   
     self.cells = {} 

    def add_cell(self, cell): 
     self.cells_list.append((cell.x, cell.y)) 
     self.cells_list.append(cell.state) 



    def display(self): 
     colors = [] 
     for y in range(150): 
      for x in range(150): 
       if (x, y) in self.cells: 
        if self.cells[(x,y)] in "S": 
         colors.append((0.0,1.0, 0.0)) 
        elif self.cells[(x, y)] in "R": 
         colors.append((0.5, 0.5, 0.5)) 
        else: 
         colors.append((1.0, 0.0, 0.0)) 
       else: 
        colors.append((0.0,0.0,0.0)) 
     plt.imshow(colors) 

def adjacent_cells(self, x,y): 
    pass 


def read_map(filename): 
    m = Map() 
    coordinates = open(filename, 'r') 
    coordinates_list = coordinates.readlines() 
    for l in coordinates_list: 
     line = l.strip() 
     split_coords = line.split(',') 
     c = Cell(split_coords[0], split_coords[1]) 
     m.add_cell(c) 

    # ... Write this function 

    return m 

read_map('nyc_map.txt').display() 

我得到這樣的圖片來代替:

enter image description here

順便說一句,我們的地圖是一個150 x 150的網格;創建圖像我必須使用列表

回答

0

有很多你的代碼錯誤列表:

  • 幾個縮進錯誤
  • colors的長度是3的150級* 150的元組的列表,但輸入到imshow應該是一個大小的數組(150,150,3)。
  • self.cells[(x,y)] in "S"檢查self.cells[(x,y)]的內容是否包含在字符串"S"中,只有在self.cells[(x,y)] == "S"的情況下才能爲真。 self.cells使用{}進行初始化,並且從未在別處設置,因此條件始終爲假。
  • self.cells[(x,y)]預計xy是整數,但Cell(split_coords[0], split_coords[1])創建單元格時使用字符串。

這裏是一個固定的版本刪除了所有代碼是不必要的例子:

from matplotlib import pyplot as plt 
import numpy as np 

class Cell(object): 

    def __init__(self,x, y): 
     self.x = x 
     self.y = y 
     self.state = "S" # can be "S" (susceptible), "R" (resistant = dead), or 
        # "I" (infected) 

class Map(object): 

    def __init__(self): 
     self.cells = {} 

    def add_cell(self, cell): 
     self.cells[(cell.x, cell.y)] = cell.state 

    def display(self): 
     colors = np.zeros((150,150,3)) 
     for y in range(150): 
      for x in range(150): 
       if (x, y) in self.cells: 
        if self.cells[(x,y)] == "S": 
         colors[x,y,:] = (0.0,1.0, 0.0) 
       else: 
        colors[x, y, :] = (0.0,0.0,0.0) 

     plt.imshow(colors) 
     plt.show() 

def read_map(filename): 
    m = Map() 
    coordinates = open(filename, 'r') 
    coordinates_list = coordinates.readlines() 
    for l in coordinates_list: 
     line = l.strip() 
     split_coords = line.split(',') 
     c = Cell(int(split_coords[0]), int(split_coords[1])) 
     m.add_cell(c) 

    # ... Write this function 

    return m 

read_map('nyc_map.txt').display() 
+0

感謝您抽出時間來幫我找出我哪裏錯了! –