Example #1
0
    def evalIndividual(self, individual):
        ''' Evaluate an individual in the population. Based on how close the
        average bus request time is to the actual bus trip time.

        @param an individual in the population
        @return a summation of the difference between past past requests'
        average trip starting time and actual start time
        according to the evolving timetable.
        Lower values are better.
        '''

        self.runOnce()

        # DONE Store the date on mongo as datetime 
        # Store the requests of the previous day into a JSON file order them by date and KEEP IT during the whole iteration on memory
        # DONE Group by request query from the file to reduce the number of elements being processed

        # Use map function instead of LOOP
        # Multi thread the MAP functions

        # First, the randomly-generated starting times are sorted in order to check sequentially the number of requests for that particular trip

        individual = sorted(individual, key=itemgetter(2))
        # Second, we loop trough the number of genes in order to retrieve the number of requests for that particular trip
        # DB calls can ve avoided by querying the whole Request Collection for a particular day
        # For the 1st trip, the starting time has to be selected
        db = DB()
        # Replace the dates here from yesterday's date
        request = []
        dif = []
        cnt = []
        intialTripTime = "00:00"
        # TODO: Change to timedelta(1)
        yesterday = date.today() - timedelta(2)
        # The result here should be added into a file: the order is by hour, minute and initialBusStop
        # request = db.getTravelRequestSummary(datetime.combine(yesterday, datetime.strptime(Fitness.firstMinute, Fitness.formatTime).time()),datetime.combine(yesterday, datetime.strptime(Fitness.lastMinute, Fitness.formatTime).time()))
        for i in range(len(individual)):
            tripTimeTable = []
            tripTimeTable = db.generateFitnessTripTimeTable(individual[i][0], individual[i][2])
            # For each gene, the corresponding requests are returned
            for j in range(len(tripTimeTable)):
                request = []
                if j==0:
                    request = db.getTravelRequestSummary2(datetime.combine(yesterday, datetime.strptime(intialTripTime, Fitness.formatTime).time()),datetime.combine(yesterday, datetime.strptime(tripTimeTable[j][1], Fitness.formatTime).time()), tripTimeTable[j][0])
                    intialTripTime = tripTimeTable[j][1]
                else:
                    request = db.getTravelRequestSummary2(datetime.combine(yesterday, datetime.strptime(tripTimeTable[j-1][1], Fitness.formatTime).time()),datetime.combine(yesterday, datetime.strptime(tripTimeTable[j][1], Fitness.formatTime).time()), tripTimeTable[j][0])
                if len(request)>0: 
                    diff = 0
                    count = 0
                    for k in range(len(request)):
                        diff = diff + self.getMinutes(self.timeDiff(tripTimeTable[j][1],str(int(request[k]["hour"])) + ":" + str(int(request[k]["minute"]))))*int(request[k]["count"])
                        count = count + int(request[k]["count"])
                    dif.append(diff)
                    cnt.append(count)

        return sum(dif)/sum(cnt),