grid3.py - .

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
import random import math import os import sys #Function to print the Matrix def printMatrix(testMatrix): #print '\t', #for i in range(len(testMatrix[1])): # print i, #print for i, element in enumerate(testMatrix): print ',\t'.join(element) #Fucntion for random sampling with replacement(if sample is greater than population) def sample_wr(population, k): n = len(population) _random, _int = random.random, int # speed hack result = [None] * k for i in xrange(k): j = _int(_random() * n) result[i] = population[j] return result def highway_continuation(Matrix,Current_Matrix,movement_direction,x_start_prob,y_sta rt_prob,highway_num): #highway_num = str(int(highway_num)+1) x_fordist_start, y_fordist_start, x_fordist_end, y_fordist_end = 0, 0, 0, 0 x_fordist_start = x_start_prob y_fordist_start = y_start_prob while x_start_prob > -1 and x_start_prob < w and y_start_prob > -1 and y_start_prob < h: #print x_start_prob,y_start_prob if movement_direction == "down": random_number_dir = random.randint(1,5) #random_number_dir = 1 #move in the same dir if 1 <= random_number_dir <=3: i=0 while i<20: y_start_prob+=1 x_start_prob=x_start_prob #make the change in the matrix if x_start_prob != -1 and x_start_prob != w and y_start_prob != -1 and y_start_prob != h: if Matrix[y_start_prob][x_start_prob] == '1': Matrix[y_start_prob] [x_start_prob] = 'a'+highway_num i+=1 elif Matrix[y_start_prob] [x_start_prob] == '2': Matrix[y_start_prob] [x_start_prob] = 'b'+highway_num i+=1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
else: #collision with either a or b, build again #try: Matrix = Current_Matrix #movement_direction = "down" #x_start_prob , y_start_prob = 0, 0 #print "hi" generate_highways(Matrix,highway_num) break #except RuntimeError as r: # os.system("python grid3.py") else: break #move right elif random_number_dir == 4: i=0 while i<20: y_start_prob=y_start_prob x_start_prob-=1 movement_direction = "left" #make the change in the matrix if x_start_prob != -1 and x_start_prob != w and y_start_prob != -1 and y_start_prob != h: if Matrix[y_start_prob][x_start_prob] == '1': Matrix[y_start_prob] [x_start_prob] = 'a'+highway_num i+=1 elif Matrix[y_start_prob] [x_start_prob] == '2': Matrix[y_start_prob] [x_start_prob] = 'b'+highway_num i+=1 else: #collision with either a or b, build again #try: Matrix = Current_Matrix #movement_direction = "down" #x_start_prob , y_start_prob = 0, 0 #print "hi" generate_highways(Matrix,highway_num) break #except RuntimeError as r: # os.system("python grid3.py") else: break #move left else: i=0
Image of page 2
while i<20: y_start_prob=y_start_prob x_start_prob+=1 movement_direction = "right" #make the change in the matrix if x_start_prob != -1 and x_start_prob != w and y_start_prob != -1 and y_start_prob != h: if Matrix[y_start_prob][x_start_prob] == '1': Matrix[y_start_prob] [x_start_prob] = 'a'+highway_num i+=1 elif Matrix[y_start_prob] [x_start_prob] == '2': Matrix[y_start_prob] [x_start_prob] = 'b'+highway_num i+=1 else: #collision with either a or b, build again #try: Matrix = Current_Matrix #movement_direction = "down" #x_start_prob , y_start_prob = 0, 0 #print "hi" generate_highways(Matrix,highway_num) break #except RuntimeError as r: # os.system("python grid3.py") else: break elif movement_direction == "left": random_number_dir = random.randint(1,5) #move in the same dir if 1 <= random_number_dir <=3: i=0 while i<20: x_start_prob-=1 y_start_prob=y_start_prob if x_start_prob != -1 and x_start_prob != w
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern