Пример #1
0
 def __isValid__(self,g = None):
     if g != None:
         for s in self.sli:    
             r = a_star(s, g, self)
             if r == -1:
                 return False
     else:
         for g in self.gli:
             for s in self.sli:
                 r = a_star(s, g, self)
                 if r == -1:
                     return False
     return True
Пример #2
0
def fitness(individual, para):
    """
    Determine the fitness of an individual. Higher is better.

    individual: the individual to evaluate
    """

    score = 0
    tempMap = copy.deepcopy(para.mapData)
    # tempMap.mmap = copy.deepcopy(mapData.mmap)
    count = 0
    for i in xrange(len(individual)):
        if individual[i] == 1:
            count += 1
            pos = para.recLi[i]
            tempMap.mmap[pos[0]][pos[1]] = "t"
    for s in tempMap.sli:
        r = a_star(s, para.g, tempMap)
        if r == -1:
            # print 'No Way',individual
            score += -100
        else:
            score += len(getPath(r))
            score -= count / 2.0

    if para.gbInd == None or score > para.gbScore:
        para.gbInd = copy.deepcopy(individual)
        para.gbScore = score
    return score
Пример #3
0
 def __f__(self,g = None):
     if g != None:
         score = 0
         for s in self.sli:    
             r = a_star(s, g, self)
             if r == -1:
                 return -9999
             score += len(getPath(r))
         return score
     else:
         raise Exception("NO Implemented Yet!")
Пример #4
0
 def __SimpleSetUp__(self):
     # close
     g = self.getMaxG()
     gliTemp = copy.deepcopy(self.gli)
     gliTemp.remove(g)
     pathli = []
     for gt in gliTemp:
         self.closeG(gt)
     # getLayout
     
     # getPath
     for s in self.sli:    
         r = a_star(s, g, self)
         if r == -1:
             print "ERROR: NO TO GET G!!!"
             exit(-1)
         path = getPath(r)
         pathli.add(path)
     
     # getTower
     countMap = [[0 for column in range(self.w)] for row in range(self.h)]
     for path in pathli:
         for pos in path:
             countMap[pos[0],pos[1]] = 'X'
     att_range = 2
     #@attention: heavy code, try to improve, maybe use algorithms from comp graphics
     for path in pathli:
         for pos in path:
             for i in xrange(self.h):
                 for j in xrange(self.w):
                     if self.mmap[i][j] == '0' and countMap[i,j] != 'X':
                         if self.__getDist__([i,j], pos) <= 2: 
                             countMap[i][j] += 10
                         if self.__getDist__([i,j], pos) <= 3:
                             countMap[i][j] += 7
                         if self.__getDist__([i,j], pos) <= 4:
                             countMap[i][j] += 5
                         if self.__getDist__([i,j], pos) <= 5:
                             countMap[i][j] += 2
                         if self.__getDist__([i,j], pos) <= 6:
                             countMap[i][j] += 1
Пример #5
0
 def getEva(self,display):
     from vs_astar import a_star
     from vs_astar import getPath
     havePath = False
     pathli = []
     for g in self.cvsMap.gli:
         for s in self.cvsMap.sli:
             r = a_star(s, g, cvsMap)
             if r == -1:
                 continue
             havePath = True
             pathli.append(getPath(r))
         # end of for
     # end of for
     if not havePath:
         display.set("ERROR: NO PATH")
     else:
         display.set(reduce(add, (len(x) for x in pathli)))
         # show the path on map
         for path in pathli:
             for pos in path:
                 self.btnTexts[pos[0]][pos[1]].set('  P  ')