class Supervisor:
    # _map includes
    #    method for moving a car( e.g., map.moveCar(car, from, to) )
    # _cars includes the cars and it type, priority and speed
    def __init__(self, _numberOfCars,_mapWidth,_mapHeight,_AIName):
        self.numberOfCars = _numberOfCars

        self.state = State(_mapWidth, _mapHeight)
        self.state.generateCars(_numberOfCars)
        self.map = self.state.getMap()
        self.cars = self.state.getCars()


        # AI
        self.ai = AI(_numberOfCars,_AIName)

        # Initializing the remain moves of cars
        self.carMovesRemain = []
        for carID in range(numberOfCars):
            self.carMovesRemain.append(self.cars[carID].speed)


        # carList : a 'list' of 'car'
        # car :
        #    car.type : 'vehicle', 'taxi', 'ambulance'
        #    car.location : (x,y)
        #    car.start : (x,y)
        #    car.destination : (x,y)
        #    car.priority : 'high', 'medium', 'low'
        #    car.speed : 1, 2, 3
        #
        #    car.getNextLocation(gameState) : return next location of the car
    def setAI(self,_AIName):
        self.ai = AI(self.numberOfCars,_AIName)
    def start(self):

        # for car in cars:
        #     nextAction = AI.nextStep(car.id, state)
        #     state = state.getStateByAction(car.id, nextAction)
        #     Drawer.draw(state)


        # Start
        currCarIdx = 0
        nextAction = AI.nextStep(currCarIdx, state)
        state = state.getStateByAction(currCarIdx, nextAction)
        Drawer.draw(state)

        while(not state.isGoal()):
            nextCarIdx = self.getNextCarIdx(currCarIdx)
            nextAction = AI.nextStep(currCarIdx, state)
            state = state.getStateByAction(currCarIdx, nextAction)
            Drawer.draw(state)

    def printArrivedRatio(self,_mState):
        goal_counter = 0
        for carID in range(self.numberOfCars):
            if(_mState.isGoalState(carID)):
                goal_counter = goal_counter+1
        
        print 'Arrived Ratio : ',goal_counter , ' / ', self.numberOfCars


    def isGoal(self,_mState):
        goal_counter = 0
        for carID in range(self.numberOfCars):
            if(_mState.isGoalState(carID)):
                goal_counter = goal_counter+1
        
        #print goal_counter
        if  goal_counter == self.numberOfCars:
            return True
        else:
            return False

    def getNextCarIdx(self, currCarIdx):
        if(self.carMovesRemain[currCarIdx] > 0):
            nextCarIdx = currCarIdx
            self.carMovesRemain[nextCarIdx] = (self.carMovesRemain[nextCarIdx]
                                               - 1)
            return nextCarIdx
        else:
            self.carMovesRemain[currCarIdx] = self.carList[currCarIdx].speed
            nextCarIdx = (currCarIdx + 1) % len(self.carList)
            self.carMovesRemain[nextCarIdx] = (self.carMovesRemain[nextCarIdx]
                                               - 1)
            return nextCarIdx

    def getNextCarWithPriority():
        pass


    def showCarsInfo(self):
        if(len(self.cars) > 0):
            for car in self.cars:
                print '---------------'
                print 'ID: ', car.id_
                print 'type: ', car.type_
                print 'location: ', car.location
                print 'start: ', car.start
                print 'destination: ', car.destination
                print 'priority: ', car.priority
                print 'speed: ', car.speed
        else:
            print 'In showCarsInfo(): There is no any car!'
Exemple #2
0
    f=True
    for (v,u) in Edic:
        cap=0
        for (p,e) in v.p:
            if p!=u: cap=cap+e-len(Edic[(v,p)])
        for (p,e) in u.p:
            if p!=v: cap=cap+e-len(Edic[(u,p)])
        if cap<=len(Edic[(v,u)]): f=False
    return f

if __name__ == '__main__':
    n_car=120
    w=30; h=30
    a = State(30,30)
    ai = AI(n_car,"KGreedyAStar")
    a.generateCars(n_car)
    #a.printMap()
    
    """
    for x in range(h):
        for y in range(w):
            if a.currentMap[x][y]==-2:
                sys.stdout.write("\219\219")
            elif a.currentMap[x][y]==-1:
                sys.stdout.write("  ")
            elif a.currentMap[x][y]>=0:
                sys.stdout.write("OO")
            #sys.stdout.write( "%2d"%(int(a.currentMap[x][y])) )
        sys.stdout.write("\n")
    """
    (G,Vdic) = MapToGraph(a.currentMap)