2017-04-06 7 views
0

は、だから私はこのモデリングとNYCの地図を表示:ウイルスシミュレーション

enter image description here

しかし、私が持っている現在のコードを持つようになるはずNYCのマップの画像を表示するようにコードを書き終えました。

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{}で初期化され、他の場所に設定されることはないため、条件は常にfalseです。
  • 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

は私が間違っていたところ、私が把握助けるために時間を割いていただき、ありがとうございます! –

関連する問題