找到给定半径的最少量设施

时间:2014-04-02 22:22:50

标签: python

我正在尝试编码的是我要求找到最少数量的设施,每个设施都位于数据集中的一个城市。
数据集在4个子列表的列表中设置如下。第一个列表是城市和州,下一个列表是坐标,下一个是人口,最后是里程。我只担心城市和州以及每个城市之间的里程数。数据的示例可能如下所示: (这些数字按顺序排列。)

data = [['Miami FL', 'Fort Meyers FL', 'Oakland CA', 'New York City NY'],
[[213, 124],[643,234],[754,652],[453,734]],[25345,56782,74563,234755],
[[0, 200, 2000, 1000],[200, 0, 1900, 950],[2000,1900,0,2500],[1000,950,2500]]

设定里程数,即城市的指数是彼此之间的距离。

data[3][0] = [0, 200, 2000, 1000] #Miami's distances from other cities
data[3][0][0] = 0 #how far away miami is from it self
data[3][0][1] = 200 #how far away Miami is from Fort Meyers
data[3][2][1] = 1900 how far away Oakland is from Fort Meyers

您获得半径为r(以英里为单位)并要求找到最少的数字 设施,每个设施位于数据集中的128个城市之一, 这样数据集中的128个城市中的每一个都距离某个设施不到r里程。返回的设施应按字母顺序排列。

我必须使用布尔列表来检查城市是否已被服务。

使用此算法

Greedy Algorithm for facility location
1. Initially all cities are unserved.
2. while there are cities that are unserved:
3. Pick a city c that "serves" the most unserved cities.
4. Mark the city c and all cities within r miles of c as served

这是我到目前为止所做的:

def locateFacilites(data, r):
    print data
    #creating list served list that has values of all False
    served = [False for x in range(128)]
    #all True list 
    complete = [True for x in range(128)]

    finalListOfFacilities = []
    beenServed = 0
    while served != complete:


        #looping over names of cities
        for index, c in enumerate(data[0]):

            if served[index] == False:
                #if unserved find the list of nearby cities
                listOfCities = nearbyCities(data[0][index], r, data)
                unservedList = []
                #condenses list to all false
                unservedList = cityUnservedList(listOfCities, data, served, unservedList)
                print len(unservedList)
                #Looping through cities to change them to be served
                if len(unservedList) > beenServed:
                    served = FalseToTrueList(unservedList, data, served)

                if len(unservedList) > beenServed:

                    finalListOfFacilities.append(c)
                    beenServed = len(unservedList)
    return finalListOfFacilities

#this function was given.  getDistance returns the distance from one city to another
def nearbyCities(city, r, data):
    listOfCities = []
    for i in data[0]:
        if getDistance(city, i, data) <=r:
            listOfCities.append(i)

    return sorted(listOfCities)

def cityUnservedList(listOfCities, data, served, unservedList):
    for city in listOfCities:
        i = data[0].index(city)
        if served[i] == False:
            unservedList.append(city)
    return unservedList

#converts cities that are false to true if unservedList is greater than beenServed
def FalseToTrueList(unservedList, data, served):        
    for city in unservedList:
        i = data[0].index(city)
        served[i] = True    
    return served

我相信我已经接近了,但是我遇到了运行时错误(无限循环)。有人可以指出我做错了什么或者建议去哪儿去?

0 个答案:

没有答案