2017-04-06 2 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{}으로 초기화되고 다른 곳에서는 절대로 설정되지 않으므로 조건은 항상 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

내가 잘못 어디로 갔는지 저를 알아내는 데 도움이 시간을내어 주셔서 감사합니다! –