Example #1
0
    def schedule_update(self):
        dis_matrix = self.map.matrix
        length_matrix = len(dis_matrix)
        for row in range(length_matrix):
            for column in range(length_matrix):
                if (dis_matrix[row][column] != 0
                        and dis_matrix[row][column] != 9999):
                    #print (row)
                    #print (column)
                    #print (dis_matrix[row][column])
                    road_id = max([
                        val for val in self.crosslist[row + 1].roads
                        if val in self.crosslist[column + 1].roads
                    ])
                    duplex = 0 if self.roadlist[road_id].begin == row + 1 else 1
                    rate_block_road = float(
                        np.sum(self.roadlist[road_id].car_queue[duplex] != 0)
                    ) / (float(
                        np.sum(self.roadlist[road_id].car_queue[duplex] != 0)
                    ) + float(
                        np.sum(self.roadlist[road_id].car_queue[duplex] == 0)))

                    dis_matrix[row][column] = rate_block_road * self.roadlist[
                        road_id].length * 4 + (
                            1 -
                            rate_block_road) * self.roadlist[road_id].length

        distance_matrix, path_matrix = floyd(dis_matrix)
        return path_matrix
Example #2
0
def main():

    name = input('What is your name? ')
    nameFile = open('name.txt', 'w')
    nameFile.write(name)
    nameFile.close()

    color = input('\nHi ' + name + "! What's your favorite color? ")

    # Color Condition
    if color == 'blue':
        print('\n' + color + "?! That's awesome " + name +
              "!! Blue's my favorite color too!!")
    else:
        print('\n' + color + "'s ok... i guess ")

    eye_color = input('\nWhat color are your eyes? ')

    # Eye Color Condition
    if eye_color != color:
        print('\nNo way,' + name + '! I would think that ' + eye_color +
              ' would be your favorite color then!')
    else:
        print('\nFigures')

    car_check = input('\nDo you have a car, yes or no? ')

    # Did the user have a car
    if car_check.startswith('y'):
        car_color = input('\nWell good for you.  What color is it? ')
        # Car Color Condition
        if car_color == color:
            print('\nOf course it is\n')
        else:
            print('\nuh...ok\n')
    else:
        print("\nThat's ok, " + name + ".  We'll skip that one for now\n")

    # Offer a game
    game_check = input('\nWould you like to play a game?\n')

    # Yes
    if game_check.startswith('y'):
        floyd()
    else:
        return
Example #3
0
 def test_floyd_negative(self):
     inf = float("inf")
     g = Graph(4)
     g.edges = [[0, inf, -2, inf],
                [4, 0, 3, inf],
                [inf, inf, 0, 2],
                [inf, -1, inf, 0]]
     expected = [[0, -1, -2, 0], [4, 0, 2, 4], [5, 1, 0, 2], [3, -1, 1, 0]]
     self.assertEqual(floyd(g), expected)
Example #4
0
 def test_floyd_algorithm(self):
     """ Test if the algorithm returns expected result
     """
     floyd_test_matrix = np.matrix([[-1, 9, -1, 7, 5, -1, -1, -1],
                                  [-1, -1, -1, -1, -1, -1, -1, -1],
                                  [1, -1, -1, -1, -1, 8, -1, -1],
                                  [-1, -1, -1, -1, -1, 9, -1, -1],
                                  [-1, -1, 1, -1, -1, -1, 8, -1],
                                  [-1, 6, -1, -1, -1, -1, 8, -1],
                                  [-1, -1, 7, -1, -1, -1, -1, -1],
                                  [-1, -1, -1, -1, -1, -1, -1, -1]
                                 ], dtype=float)
     floyd_solution_matrix = np.matrix([[-1, 9, 6, 7, 5, 14, 13, -1],
                                      [-1, -1, -1, -1, -1, -1, -1, -1],
                                      [1, 10, -1, 8, 6, 8, 14, -1],
                                      [25, 15, 24, -1, 30, 9, 17, -1],
                                      [2, 11, 1, 9, -1, 9, 8, -1],
                                      [16, 6, 15, 23, 21, -1, 8, -1],
                                      [8, 17, 7, 15, 13, 15, -1, -1],
                                      [-1, -1, -1, -1, -1, -1, -1, -1]
                                     ], dtype=float)
     algorithm_solution = floyd(floyd_test_matrix)
     compare = np.array_equal(algorithm_solution, floyd_solution_matrix)
     self.assertTrue(compare)
Example #5
0
def main():
    INFIN = 1000

    D = [[0, 4, 1, 5, 8, 10], [INFIN, 0, INFIN, INFIN, INFIN, INFIN],
         [INFIN, 2, 0, INFIN, INFIN,
          INFIN], [INFIN, INFIN, INFIN, 0, 2, INFIN],
         [INFIN, INFIN, INFIN, INFIN, 0, 1],
         [INFIN, INFIN, INFIN, INFIN, INFIN, 0]]

    expOp = [[0, 3, 1, 5, 7, 8], [INFIN, 0, INFIN, INFIN, INFIN, INFIN],
             [INFIN, 2, 0, INFIN, INFIN,
              INFIN], [INFIN, INFIN, INFIN, 0, 2, 3],
             [INFIN, INFIN, INFIN, INFIN, 0, 1],
             [INFIN, INFIN, INFIN, INFIN, INFIN, 0]]

    expP = [[0, 3, 0, 0, 4, 5], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

    print "Original chart, INFINITY = ", INFIN, ": "
    print ' ', '{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1', '2', '3', '4', '5',
                                                       '6')

    c = 1
    for n in D:
        sys.stdout.write(str(c))
        for i in n:
            sys.stdout.write('{:^8}'.format(str(i)))
        sys.stdout.write('\n')
        c = c + 1

    print '\n', "Expected:"
    print "Optimal chart: "
    print ' ', '{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1', '2', '3', '4', '5',
                                                       '6')

    c = 1
    for n in expOp:
        sys.stdout.write(str(c))
        for i in n:
            sys.stdout.write('{:^8}'.format(str(i)))
        sys.stdout.write('\n')
        c = c + 1

    print '\n', "P chart: "
    print ' ', '{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1', '2', '3', '4', '5',
                                                       '6')

    c = 1
    for n in expP:
        sys.stdout.write(str(c))
        for i in n:
            sys.stdout.write('{:^8}'.format(str(i)))
        sys.stdout.write('\n')
        c = c + 1

    print '\n', "Resultant:"

    B = floyd.floyd(D)
    print "Optimal chart: "
    print ' ', '{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1', '2', '3', '4', '5',
                                                       '6')

    c = 1
    for n in D:
        sys.stdout.write(str(c))
        for i in n:
            sys.stdout.write('{:^8}'.format(str(i)))
        sys.stdout.write('\n')
        c = c + 1

    print '\n', "P chart: "
    print ' ', '{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1', '2', '3', '4', '5',
                                                       '6')

    c = 1
    for n in B:
        sys.stdout.write(str(c))
        for i in n:
            sys.stdout.write('{:^8}'.format(str(i)))
        sys.stdout.write('\n')
        c = c + 1
Example #6
0
def main():
    if len(sys.argv) != 5:
        logging.info(
            'please input args: car_path, road_path, cross_path, answerPath')
        exit(1)

    car_path = sys.argv[1]
    road_path = sys.argv[2]
    cross_path = sys.argv[3]
    answer_path = sys.argv[4]

    logging.info("car_path is %s" % (car_path))
    logging.info("road_path is %s" % (road_path))
    logging.info("cross_path is %s" % (cross_path))
    logging.info("answer_path is %s" % (answer_path))

    # to read input file
    source = road_path  ## 源文件路径
    dest = '../config/road1.txt'  ## 去除括号后的文件路径
    f = open(dest, "w+")
    f.truncate()
    with open(source, 'r') as text:
        with open(dest, 'a+') as road_1:  ## 以追加写的方式打开目标文件
            for line in text.readlines():
                road_1.write(line.replace('(', '').replace(')',
                                                           ''))  ## 去除一行的左右括号
        roadMat = np.loadtxt(dest, dtype=int, skiprows=1,
                             delimiter=',')  ## 读取txt并转换为ndarray

    source2 = car_path  ## 源文件路径
    dest2 = '../config/car1.txt'  ## 去除括号后的文件路径
    f = open(dest2, "w+")
    f.truncate()
    with open(source2, 'r') as text:
        with open(dest2, 'a+') as car_1:  ## 以追加写的方式打开目标文件
            for line in text.readlines():
                car_1.write(line.replace('(', '').replace(')',
                                                          ''))  ## 去除一行的左右括号
        carMat = np.loadtxt(dest2, dtype=int, skiprows=1,
                            delimiter=',')  ## 读取txt并转换为ndarray
    maxSpeed = carMat[:, 3]
    from_car = carMat[:, 1]
    to_car = carMat[:, 2]
    planTime = carMat[:, 4]
    carId = carMat[:, 0]
    car_amount = len(from_car)
    '''
	初始化权重矩阵,并运行floyd最短路径寻找程序
	'''
    #	start = time.time() # 计算耗时,单位为s
    R_all = []
    planTime1 = 0
    from_ = roadMat[:, 4]
    to_ = roadMat[:, 5]
    roadId = (roadMat[:, 0])
    with open(answer_path, 'a+') as file:
        file = open(answer_path, 'w')
        file.truncate()

        weight = buildWeight.buildWeight(roadMat, 1e5)
        # print(weight.shape)
        D, path = floyd.floyd(weight)

        for i in range(0, car_amount):

            R = floyd.router(D, path, from_car[i], to_car[i])

            R1 = []  # 用R1存储道路编号carId
            # 实现节点到道路编号的索引查找
            for m in range(0, len(R) - 1):
                for l in range(0, len(roadId)):
                    #if from_.tolist().index(R[m])==to_.tolist().index(R[m+1]) or from_.tolist().index(R[m+1])==to_.tolist().index(R[m]):
                    if from_[l] + to_[l] == R[m] + R[m + 1] and from_[l] - R[
                            m] + to_[l] - R[m + 1] == 0:
                        R1.append(roadId[l])
                        break
            if planTime[i] < 3:
                planTime1 = planTime[i]
            else:
                planTime1 = planTime[i] + np.random.randint(1, 500)
            R1.insert(0, planTime1)
            R1.insert(0, carId[i])
            #R.insert(0,'(')
            #R.append('/n')

            file.write(str(tuple(R1)) + '\n')
Example #7
0
'''
初始化权重矩阵,并运行floyd最短路径寻找程序
'''
start = time.time()  # 计算耗时,单位为s
R_all = []
planTime1 = 0
from_ = roadMat[:, 4]
to_ = roadMat[:, 5]
roadId = (roadMat[:, 0])
with open('./config/answer.txt', 'a+') as file:
    file = open('./config/answer.txt', 'w')
    file.truncate()

    weight = buildWeight.buildWeight(roadMat, 1e5)
    # print(weight.shape)
    D, path = floyd.floyd(weight)

    for i in range(0, car_amount):

        R = floyd.router(D, path, from_car[i], to_car[i])

        R1 = []  # 用R1存储道路编号carId
        # 实现节点到道路编号的索引查找
        for m in range(0, len(R) - 1):
            for l in range(0, len(roadId)):
                #if from_.tolist().index(R[m])==to_.tolist().index(R[m+1]) or from_.tolist().index(R[m+1])==to_.tolist().index(R[m]):
                if from_[l] + to_[l] == R[m] + R[
                        m + 1] and from_[l] - R[m] + to_[l] - R[m + 1] == 0:
                    R1.append(roadId[l])
                    break
        if planTime[i] < 3:
Example #8
0
 def test_floyd(self):
     self.assertEqual(floyd(self.g)[0], [0, 7, 9, 20, 20, 11])
Example #9
0
def main():
	INFIN = 1000

	D=[[0,4,1,5,8,10],[INFIN,0,INFIN,INFIN,INFIN,INFIN],[INFIN,2,0,INFIN,INFIN,INFIN],[INFIN,INFIN,INFIN,0,2,INFIN],[INFIN,INFIN,INFIN,INFIN,0,1],[INFIN,INFIN,INFIN,INFIN,INFIN,0]]

	expOp =[[0,3,1,5,7,8],[INFIN,0,INFIN,INFIN,INFIN,INFIN],[INFIN,2,0,INFIN,INFIN,INFIN],[INFIN,INFIN,INFIN,0,2,3],[INFIN,INFIN,INFIN,INFIN,0,1],[INFIN,INFIN,INFIN,INFIN,INFIN,0]]

	expP =[[0,3,0,0,4,5],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,5],[0,0,0,0,0,0],[0,0,0,0,0,0]]

	print "Original chart, INFINITY = ", INFIN, ": "
	print ' ','{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1','2','3','4','5','6')

	c = 1
	for n in D:
		sys.stdout.write(str(c))
		for i in n:
			sys.stdout.write('{:^8}'.format(str(i)))
		sys.stdout.write('\n')
		c = c + 1

	print '\n',"Expected:"
	print "Optimal chart: "
	print ' ','{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1','2','3','4','5','6')

	c = 1
	for n in expOp:
		sys.stdout.write(str(c))
		for i in n:
			sys.stdout.write('{:^8}'.format(str(i)))
		sys.stdout.write('\n')
		c = c + 1

	print '\n',"P chart: "
	print ' ','{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1','2','3','4','5','6')

	c = 1
	for n in expP:
		sys.stdout.write(str(c))
		for i in n:
			sys.stdout.write('{:^8}'.format(str(i)))
		sys.stdout.write('\n')
		c = c + 1


	print '\n',"Resultant:"
	
	B = floyd.floyd(D)
	print "Optimal chart: "
	print ' ','{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1','2','3','4','5','6')
	
	c = 1
	for n in D:
		sys.stdout.write(str(c))
		for i in n:
			sys.stdout.write('{:^8}'.format(str(i)))
		sys.stdout.write('\n')
		c = c + 1

	print '\n',"P chart: "
	print ' ','{:^8}{:^8}{:^8}{:^8}{:^8}{:^8}'.format('1','2','3','4','5','6')

	c = 1
	for n in B:
		sys.stdout.write(str(c))
		for i in n:
			sys.stdout.write('{:^8}'.format(str(i)))
		sys.stdout.write('\n')
		c = c + 1