def getSavedIbmValues(self, IBMDictionary, songWord):
     ibmWordBeginTime = IBMDictionary[songWord][1]
     ibmWordBeginTimestamp = Timestamp(
         self.formatSubtitle(ibmWordBeginTime))
     ibmWordEndTime = IBMDictionary[songWord][2]
     ibmWordEndTimestamp = Timestamp(self.formatSubtitle(ibmWordEndTime))
     return ibmWordBeginTimestamp, ibmWordEndTimestamp
Beispiel #2
0
    def __init__(self, agent_id, total_agents, domain, constraints, cost_functions):
        # Solution CPA and Bound
        self._CPA_SOLUTION = None
        self._B = sys.maxsize

        # Agent information
        self._id = agent_id
        self._constraints = constraints
        self._domain = domain
        self._cost_functions = cost_functions
        self._total_agents = total_agents
        # self._timestamp = [0] * (total_agents + 1)
        self._timestamp = Timestamp(self._total_agents + 1)

        # Instance variables
        self._received_cpa = None
        self._current_cpa = None
        self._next_assignment = 0  # index of value in domain
        self._current_assignment = 0
        self._estimate_value = 0  # scalar value of this agents estimate
        self._queue = []  # A python list can be used a queue
        self._converged = False

        # Only for first Agent
        if self._id == 1:
            cpa = self._generate_cpa()

            # start events
            self._queue.append(Message('CPA_MSG', self._timestamp, 0, self._id, cpa))
Beispiel #3
0
 def extractTimestamp(self, timestamp):
     # 00: 00:40, 125 --> 00: 00:44, 170
     timestamp = timestamp.replace(" ", "")
     timestamp = timestamp.replace(",", ".")
     times = timestamp.split("-->")
     beginTime = times[0]
     endTime = times[1].rstrip()
     beginTS = Timestamp(beginTime)
     endTS = Timestamp(endTime)
     return beginTS, endTS
Beispiel #4
0
    def set_header(self, payment_status_bulk_payload, credentials):

        hash_string = credentials.api_key + payment_status_bulk_payload.request_id + credentials.api_token

        txn_hash = EncryptionConfig.sha512(hash_string)
        time_stamp = Timestamp()

        headers = {'Content-Type': 'application/json', 'MERCHANT_ID':credentials.merchant_id, 'API_KEY':credentials.api_key,
                   'REQUEST_ID':payment_status_bulk_payload.request_id, 'REQUEST_TS':time_stamp.dateTimeObj(dateTimeObj=datetime.now()),
                   'API_DETAILS_HASH': txn_hash}
        return headers
 def addSilence(self, silenceDuration, lyricItr):
     """ calculate duration of the pause between lyrics and trim the right amount of silence"""
     silenceTS = Timestamp()
     silenceEnd = silenceTS.convertTotalMsToString(silenceDuration)
     silenceVideoName = os.path.join(self.movieLocation, "silence",
                                     "silence" + str(lyricItr) + ".mp4")
     silenceVidInput = self.speaker.lower() + "swag" + ".mp4"
     os.system(
         "/usr/local/opt/ffmpeg/bin/ffmpeg -i /Users/edwardcox/Desktop/TomCruiseSing/silence/"
         + silenceVidInput + "\
          -ss 00:00:00.0" + " -to " + silenceEnd + " -y " +
         silenceVideoName)  # removed -c copy to re-encode
     return silenceVideoName
 def saveListAsSnippet(self, timeStampedSubtitle, movieName, songWord,
                       parentSnippet):
     """ save word as VideoSnippet, then save to a dictionary """
     songWordVideoSnippet = None
     print("timestamped subtitle is ", timeStampedSubtitle)
     for aiWordList in timeStampedSubtitle:
         word = Cleanser.Cleanser.cleaning_word(
             aiWordList[0])  #  get rid of apostrophes and other annoyances
         path = os.path.join(self.movieLocation, self.speaker, "wordAudio",
                             word + str(self.wordid) + ".mp3")
         beginSec, beginMs = self.splitUnformatTime(
             aiWordList[1])  # format: ["several":, 1.0, 1.51]
         endSec, endMs = self.splitUnformatTime(aiWordList[2])
         beginTS = "00:00:" + beginSec + "." + beginMs
         endTS = "00:00:" + endSec + "." + endMs
         beginTSObject = Timestamp(beginTS)
         endTSObject = Timestamp(endTS)
         videoSnippet = VideoSnippet(beginTSObject, endTSObject, word, path,
                                     movieName, parentSnippet.path,
                                     parentSnippet.beginTime,
                                     parentSnippet.endTime)
         """ save the file path of the video snippet containing word to reuse words in future songs """
         aiWordList.append(parentSnippet.path)
         if songWord == word:
             songWordVideoSnippet = videoSnippet
             continue  # dont add to IBM saved dictionary
         if word in self.IBM_Dictionary:
             wordIsDuplicate = videoSnippet.isIdentical(
                 self.duplicateIBM_Dictionary[word]
             )  # if so, then we have already rejected this IBM word
         else:
             wordIsDuplicate = False
         if not wordIsDuplicate:
             if word in self.IBM_Dictionary:
                 self.duplicateIBM_Dictionary[word].append(
                     videoSnippet
                 )  # will removing videosnippet from one list delete from other dictionary?
                 self.IBM_Dictionary[word].append(videoSnippet)
             else:
                 self.duplicateIBM_Dictionary[word] = [videoSnippet]
                 self.IBM_Dictionary[word] = [videoSnippet]
             FileCourier.writeDictionaryListOfObjects(
                 self.IBM_Filename, self.IBM_Dictionary)
             self.wordid += 1
         else:
             print("DOOP ALERT!. Word %s is a duplicate" %
                   word)  # NOT WORKING!!!
     return songWordVideoSnippet
Beispiel #7
0
def get_timer_interval_limit(session_id,begin,end):
    """
    use page instead of select all data at once avoid data too large to
    crash
    """
    global last_timestamp
    global last_sn
    query = (" SELECT log_tv_sec,log_tv_usec,log_content        "
             " FROM `{0}`                               "
             " WHERE session_id = {1} AND log_type = get_log_type_id('timer_enter') "
             " LIMIT {2},{3}".format(table_name,session_id,begin,end)
            )
    print("query {0},{1}".format(begin,end))
    cursor.execute(query)
    rows = cursor.fetchall()

    for sec,usec,sn in rows:
        timestamp = Timestamp(int(sec),int(usec))
        sn = int(sn,16)
        if sn - last_sn == 1:
            interval = timestamp - last_timestamp
            # if it's great than 100ms,tell me
            if interval > 100000:
                print(interval / 1000.0,end="")
            s = "{0},".format(interval / 1000.0)
            f.write(s)
            pass

        last_sn = sn
        last_timestamp = timestamp
    return
Beispiel #8
0
 def readDictionaryObjects(file):
     " get VideoSnippets from serialized pruned movie dictionary "
     if len(file) == 0:
         return {}
     snippetDictionary = eval(file)
     for key in snippetDictionary:
         serializedMovieEntry = snippetDictionary[key] # get list of arguments to VideoSnippet
         beginTime = Timestamp(serializedMovieEntry[0])
         endTime = Timestamp(serializedMovieEntry[1])
         word = serializedMovieEntry[2]
         path = serializedMovieEntry[3]
         movieName = serializedMovieEntry[4]
         parentFilePath = serializedMovieEntry[5]
         parentBeginTime = Timestamp(serializedMovieEntry[6])
         parentEndTime = Timestamp(serializedMovieEntry[7])
         snippetDictionary[key] = VideoSnippet(beginTime, endTime, word, path, movieName, parentFilePath, parentBeginTime, parentEndTime)
     return snippetDictionary
Beispiel #9
0
 def __init__(self,
              id=None,
              name=None,
              eng_name=None,
              typeid=None,
              timestamp=Timestamp.sec_now()):
     self.id = id
     self.name = name
     self.typeid = typeid
     self.timestamp = timestamp
     self.eng_name = eng_name
Beispiel #10
0
 def __init__(self,
              id=None,
              src=None,
              dest=None,
              typeid=None,
              timestamp=Timestamp.sec_now()):
     self.id = id
     self.src = src
     self.dest = dest
     self.typeid = typeid
     self.timestamp = timestamp
Beispiel #11
0
 def getWordFromSRT(self, subtitle_path, dirName, isSong=False):
     """ map words to Snippets (contain timestamps of sentences the word is contained in) """
     # read file line by line
     arrowCounter = 0  #  number of snippets
     subtitle_file = open(subtitle_path, "r", errors='ignore')
     lines = subtitle_file.readlines()
     song_snippet_word_array = [
     ]  #  store the words for multiple lines belonging to 1 subtitle
     subtitle_file.close()
     prevEndTimestamp = prevprevEndTimestamp = prevBeginTimestamp = None  # combine lyrics with the previous timestamp (not the next one)
     for l in range(
             len(lines)):  # iterate through the lines of the SRT file
         line = lines[l]
         line = Cleanser.Cleanser.cleanhtml(line)  # remove html tags
         if "-->" in line or l == len(
                 lines) - 1:  #  if last lines add last snippet to list
             if arrowCounter == 1:
                 prevBeginTimestamp = Timestamp("00:00:00.0")
             arrowCounter += 1
             if l == len(lines) - 1:
                 song_snippet_word_array += line.rstrip().split()
                 self.saveSubtitleWords(song_snippet_word_array, dirName,
                                        prevBeginTimestamp,
                                        prevEndTimestamp, isSong)
             if isSong:
                 if prevEndTimestamp is not None:  # len(song_snippet_word_array) > 0: # Cleanser.Cleanser.hasLetters(song_snippet_word_array)
                     clean_word_array = Cleanser.Cleanser.clean_sentence(
                         song_snippet_word_array)
                     # song SRT subtitle length to be used in VideoAssembler for stretching/shrinking clips
                     if prevprevEndTimestamp is not None:
                         self.songSnippetList.append(
                             SongSnippet(prevprevEndTimestamp,
                                         prevEndTimestamp, clean_word_array)
                         )  # include the pause between lyrics in a snippet
                     else:
                         self.songSnippetList.append(
                             SongSnippet(prevBeginTimestamp,
                                         prevEndTimestamp,
                                         clean_word_array))
                 prevprevEndTimestamp = prevEndTimestamp
             song_snippet_word_array = []
             if l != len(lines) - 1:
                 prevBeginTimestamp, prevEndTimestamp = self.extractTimestamp(
                     line)
             continue
         word_array = line.rstrip().split()  # remove \n at end of sentence
         song_snippet_word_array += word_array
         self.saveSubtitleWords(word_array, dirName, prevBeginTimestamp,
                                prevEndTimestamp, isSong)
Beispiel #12
0
 def toWebLogEntry(self, txt):
     match = re.match(WebLogEntry.WEB_LOG_ENTRY_PAT, txt)
     if match != None:
         trbytes  =  0
         if match.group(8) == '-':
             trbytes = 0
         else:
             trbytes = int(match.group(8))
         self.mIP = match.group(1)
         tm = Timestamp()
         self.mTimestamp = tm.toTimestamp(match.group(3))
         self.mMethod = match.group(4)
         self.mURL = match.group(5)
         self.mProtocol = match.group(6)
         self.mStatusCode = int(match.group(7))
         self.mTransBytes = trbytes
         return True
     else:
         match = re.match(WebLogEntry.WEB_LOG_ENTRY_PAT2, txt)
         if match != None:
             trbytes = 0
             if match.group(7) == '-':
                 trbytes = 0
             else:
                 trbytes = int(match.group(7))
             self.mIP = match.group(1)
             tm = Timestamp()
             self.mTimestamp = tm.toTimestamp(match.group(3))
             self.mMethod = match.group(4)
             self.mURL = match.group(5)
             self.mProtocol = 'UNKNOWN'
             self.mStatusCode = int(match.group(6))
             self.mTransBytes = trbytes
             return True
         else:
             return False
Beispiel #13
0
 def __init__(self,
              id=None,
              elid=None,
              typeid=None,
              value=None,
              eltype=None,
              eng_value=None,
              timestamp=Timestamp.sec_now()):
     self.id = id
     self.elid = elid
     self.typeid = typeid
     self.value = value
     self.eltype = eltype
     self.timestamp = timestamp
     self.eng_value = eng_value
    def assembleVideoClips(self, humanApprovedDictionary, songSnippetList):
        """ IBMDictionary contains movieEntry. key is word, value is a Snippet
            songDictionary contains Snippet. key is word, value is object with start, end times
        """
        id = 0
        lyricItr = 0
        prevLyricTimestamp = Timestamp("00:00:00.0")
        sentenceFileList = []
        totalsilence = 0  # silence between lyrics
        snippetidx = 0
        combinedClips = 0
        self.trimAllSongWord(
            humanApprovedDictionary)  # cut all the word videos
        for songSnippet in songSnippetList:
            if totalsilence == 0:
                longSilenceStartTime = prevLyricTimestamp
            snippetidx += 1
            songLine = songSnippet.subtitle
            lyricEndTime = songSnippet.endTime
            wordList = []
            """ the length of silence is measured as the gap between the last subtitle end time and the current start time """
            prevLyricTimestamp = lyricEndTime
            lyricItr += 1  # track the first word found in IBMDictionary for silence timestamp duration calculation
            if len(songLine) > 0:  #  takes care of empty song lines
                """ trim all song words belonging to a song line"""
                wordList = self.getSongWordLocations(wordList, songLine)
                if len(
                        wordList
                ) > 0:  # words added to wordList. If couldn't find any words in line, replace with silence clip
                    combinedClips += 1
                    sentenceFilePath = self.combineVideoClipsIntoSentence(
                        wordList, songSnippet, totalsilence,
                        longSilenceStartTime, songSnippet.beginTime, lyricItr,
                        id)
                    sentenceFileList.append(sentenceFilePath)
                    totalsilence = 0
                else:  # songline contains words, but we couldn't find any from our speaker
                    totalsilence += songSnippet.endTime.totalms - songSnippet.beginTime.totalms
                id += 1
            else:  # songline doesnt contain words
                totalsilence += songSnippet.endTime.totalms - songSnippet.beginTime.totalms

            # Delete combineClip.mp4 afterward
        parodyPath = os.path.join("finalVideo",
                                  self.speaker + "_" + self.songTitle + ".mp4")
        self.combineVideoClips(sentenceFileList, parodyPath)
Beispiel #15
0
                lastFilter = window.filterActive.get()
                elem = searchNextPatient(window, window.filterActive.get())
                break

            if window.previous.get():
                changed = True
                lastFilter = window.filterActive.get()
                elem = searchPreviousPatient(window, window.filterActive.get())
                break

            lastNotification = window.notificationActive.get()
            lastContinue = window.continueActive.get()

            sensorTime = dataset.iloc[window.currentIndex]["Timestamp"]

            actualTime = Timestamp(sensorTime)

            if window.currentIndex != 0 or window.currentIndex != dim_ciclo:
                sensorTimePrecedente = dataset.iloc[window.currentIndex -
                                                    1]["Timestamp"]

                sensorTimePrev = Timestamp(sensorTimePrecedente)

                timeDiff = sensorTimePrev.secondsConverter(
                ) - actualTime.secondsConverter()

                if timeDiff > 5000:
                    timeDiff = 1000

            window.root.after(
                int(800 * window.velocity),
Beispiel #16
0
def ReadTimestamps():
    mTimestamp = Timestamp()
    UsersTimestamps = mFirebaseData.child(Constants.Timestamps).get()
    print("Deleting relevant timestamps..")
    if (UsersTimestamps is not None):
        if (UsersTimestamps.each() is not None):
            for timestamp in UsersTimestamps.each():
                userId = timestamp.key()
                mTimestamp.setUserId(userId)
                print(userId)
                details = mFirebaseData.child(
                    Constants.Timestamps).child(userId).get()
                details = timestamp.val()
                details = dict(details)
                mTimestamp.setTimestamp(
                    details.get(Constants.Timestamp_Timestamp))
                mTimestamp.setBeachName(
                    details.get(Constants.Timestamp_BeachName))
                mTimestamp.setBeachId(details.get(Constants.Timestamp_BeachID))
                mTimestamp.setBeachListenerId(
                    details.get(Constants.Timestamp_BeachListenerId))
                mTimestamp.setBeachCountry(
                    details.get(Constants.Timestamp_Country))
                mTimestamp.print()
                date = datetime.datetime.utcfromtimestamp(
                    int(mTimestamp.Timestamp))
                dayOfMonth = date.day
                month = date.month
                currentDayOfMonth = datetime.datetime.utcnow().day
                currentMonth = datetime.datetime.utcnow().month
                print("received date: month: " + str(month) + " day " +
                      str(dayOfMonth))
                print("current date: month: " + str(currentMonth) + " day " +
                      str(currentDayOfMonth))
                checkInHour = datetime.datetime.utcfromtimestamp(
                    int(mTimestamp.Timestamp)).hour
                checkInMinutes = int(
                    datetime.datetime.fromtimestamp(int(
                        mTimestamp.Timestamp)).minute)
                print("check in minutes: " + str(checkInMinutes))
                print("check in hour: " + str(checkInHour))
                currentHour = datetime.datetime.utcnow().hour
                currentMinute = datetime.datetime.utcnow().minute
                print("current hour : " + str(currentHour))
                print("current minute: " + str(currentMinute))
                if ((checkInHour + Constants.Timestamp_Max_Hour) < currentHour
                        or (currentMonth > month)
                        or (currentDayOfMonth > dayOfMonth)):
                    print("deleting timestamp...")
                    mFirebaseData.child(Constants.Timestamps).child(
                        mTimestamp.UserID).remove()
                    mFirebaseData.child(Constants.Beaches).child(
                        mTimestamp.BeachID).child(Constants.Peoplelist).child(
                            mTimestamp.UserID).remove()
                    mFirebaseData.child(Constants.Users).child(
                        mTimestamp.UserID).child(
                            Constants.CurrentBeach).remove()
                    CurrentDevices = mFirebaseData.child(
                        Constants.BeachesListener).child(
                            mTimestamp.BeachListenerID).child(
                                Constants.CurrentDevices).get()
                    CurrentDevices = int(CurrentDevices.val())
                    CurrentDevices = CurrentDevices - 1
                    mFirebaseData.child(Constants.BeachesListener).child(
                        mTimestamp.BeachListenerID).child(
                            Constants.CurrentDevices).set(CurrentDevices)
                else:
                    print("Not deleteing from timestamps")
Beispiel #17
0
def timestamp(**kwds):
    from Timestamp import Timestamp
    return Timestamp(**kwds)
Beispiel #18
0
from Timestamp import Timestamp, generateTimestamps

t1 = Timestamp().createBasedOnKey("2015101523")
print("t1: " + str(t1))

t2 = Timestamp().createBasedOnKey("2013010101")
print("t2: " + str(t2))

t3 = Timestamp().createBasedOnKey("2013010101")
print("t3: " + str(t3))

print("t2 == t3: " + str(t2 == t3))

print("t1 == t3: " + str(t1 == t3))

timestamps = generateTimestamps(2013)
print(str(len(timestamps)))
Beispiel #19
0
# /usr/bin/python3

from DbConnect import cursor
import time
from Timestamp import Timestamp

log_file_name = "TimerInterval.txt"
f = open(log_file_name,"w+")
table_name = "log20140929"

last_timestamp = Timestamp()
last_sn = -1

def get_count(session_id):
    query = (" SELECT count(*)                                                      "
             " FROM `{0}`                                                           "
             " WHERE session_id = {1} AND log_type = get_log_type_id('timer_enter') "
             .format(table_name,session_id)
            )

    print("query data count(it will take a long time)...")
    cursor.execute(query)
    count = cursor.fetchone()
    print("count:",count)
    return int(count[0])

def get_timer_interval_limit(session_id,begin,end):
    """
    use page instead of select all data at once avoid data too large to
    crash
    """
tomorrow = (datetime.now() + timedelta(days=1))
tomorrowString = tomorrow.strftime('%Y%m%d')
tomorrowYear = int(tomorrow.strftime('%Y'))
tomorrowMonth = int(tomorrow.strftime('%m'))
tomorrowDay = int(tomorrow.strftime('%d'))
      
print("\tToday's day stamp: " + todayString)
print("\tTomorrow's day stamp: " + tomorrowString)
print("\tCurrent year, month, day, hour: " + str(currentYear) + "," + str(currentMonth) + "," + str(currentDay) + "," + str(currentHour))
print("\tTomorrow year, month, day: " + str(tomorrowYear) + "," + str(tomorrowMonth) + "," + str(tomorrowDay))
     
timestamps = []
historyTimestamps = []
forecastTimestamps = []
for hour in range(0, currentHour + 1):
    timestamp = Timestamp().createBasedOnOther(currentYear, currentMonth, currentDay, hour)
    historyTimestamps.append(timestamp)
     
for hour in range(0, 24):
    timestamp = Timestamp().createBasedOnOther(currentYear, currentMonth, currentDay, hour)
    timestamps.append(timestamp)
    if timestamp not in historyTimestamps:
        forecastTimestamps.append(timestamp) 
     
for hour in range(0, 24):
    timestamp = Timestamp().createBasedOnOther(tomorrowYear, tomorrowMonth, tomorrowDay, hour)
    timestamps.append(timestamp)
    forecastTimestamps.append(timestamp)
         
# print("History " + str(len(historyTimestamps)))
# for t in historyTimestamps:
Beispiel #21
0
class Agent:
    # Init Defined in the AFB DisCOP Paper
    def __init__(self, agent_id, total_agents, domain, constraints, cost_functions):
        # Solution CPA and Bound
        self._CPA_SOLUTION = None
        self._B = sys.maxsize

        # Agent information
        self._id = agent_id
        self._constraints = constraints
        self._domain = domain
        self._cost_functions = cost_functions
        self._total_agents = total_agents
        # self._timestamp = [0] * (total_agents + 1)
        self._timestamp = Timestamp(self._total_agents + 1)

        # Instance variables
        self._received_cpa = None
        self._current_cpa = None
        self._next_assignment = 0  # index of value in domain
        self._current_assignment = 0
        self._estimate_value = 0  # scalar value of this agents estimate
        self._queue = []  # A python list can be used a queue
        self._converged = False

        # Only for first Agent
        if self._id == 1:
            cpa = self._generate_cpa()

            # start events
            self._queue.append(Message('CPA_MSG', self._timestamp, 0, self._id, cpa))

    # Return Solution
    def solution(self):
        return self._CPA_SOLUTION

    # Run a sequence of actions once
    def step(self):
        return self._handle_message()

    # Incoming Message
    def receive(self, msg):
        # Check Timestamp
        if self._timestamp < msg.get_timestamp():
            # Add into queue
            self._queue.append(msg)
        #else:
        #    print('failed')

    # Handle message in queue
    def _handle_message(self):
        # Dequeue message from queue
        if len(self._queue) is not 0 and self._converged is not True:

            # Pop message
            msg = self._queue.pop()
            #print(msg.get_timestamp() != self._timestamp)

            # Ignore message if it's an outdated timestamp :)
            if msg.get_timestamp() < self._timestamp and msg.get_timestamp() != self._timestamp:
                #print(msg.get_timestamp())
                #print(self._timestamp)
                return []

            if msg.get_type() == 'FB_CPA':
                # print('Event: FB_CPA')
                return self._calculate_estimate(msg)

            elif msg.get_type() == 'CPA_MSG':
                # print('Event: CPA_MSG')
                return self._handle_cpa(msg)

            elif msg.get_type() == 'FB_ESTIMATE':
                # print('Event: FB_ESTIMATE')

                return self._handle_estimate(msg)

            elif msg.get_type() == 'NEW_SOLUTION':
                # print('Event: NEW_SOLUTION')
                # Update current CPA solution
                self._CPA_SOLUTION = msg.get_payload()
                # Update current Agent B
                self._B = self._CPA_SOLUTION.get_cost()
                return []

            elif msg.get_type() == 'TERMINATE':
                # print('Event: TERMINATE')
                # Status
                self._converged = True
                self._queue = []
                return []

    # Handle CPA
    def _handle_cpa(self, msg):
        # Store incoming CPA
        cpa = msg.get_payload()

        # If higher priority
        if msg.get_source() < self._id:
            self._current_assignment = 0
            self._next_assignment = 0
            self._received_cpa = copy.deepcopy(cpa)
            self._current_cpa = copy.deepcopy(cpa)
            self._timestamp = copy.deepcopy(msg.get_timestamp())

            # Reset future timestamp for lower priority agents
            # relate to this agent
            self._timestamp.reset_timestamp(self._id)

        return self._assign_cpa()

    # Handle creating an estimate value
    # Should return a message if estimate causes a backtrack
    def _handle_estimate(self, msg):
        # do we need to wait for all messages to be received
        # save estimate
        self._estimate_value += msg.get_payload()
        if (self._estimate_value + self._current_cpa.get_cost()) >= self._B:
            # print(self._estimate_value)
            # print(self._current_cpa)
            print('Backtracking due to estimate')
            return self._assign_cpa()

    # Talk about this one tomorrow!!!
    def _calculate_estimate(self, msg):
        # msg = Message('FB_ESTIMATE', msg.timestamp(), self._id, msg.source(), 30)
        # Find best estimate value based on received cpa and possible combinations of unresolved constraints.
        # msg = FB_CPA
        fb_cpa = msg.get_payload()

        min_cost = sys.maxsize  # cost difference
        est_value = None
        for x in range(0, len(self._domain[0])):
            # assignment_cost = self._current_cost(self._current_assignment)
            # f_v =  assignment_cost + self._future_cost(self._current_assignment)
            tmp = self._possible_cpa_assignment_cost(fb_cpa.get_cpa(), x) + self._future_cpa_cost(x)

            if tmp < min_cost:
                min_cost = tmp
                est_value = x

        # Generate a FB_ESTIMATE
        # print('[Agent-%d] Estimate: %d %s'%(self._id, min_cost, self._domain[0][est_value]))
        msg = Message('FB_ESTIMATE', msg.get_timestamp(), self._id, msg.get_source(), min_cost)
        return [msg]

    # Select next value for this agent
    # Currently using an iterative method
    # If we have exhausted all possible values then
    #   return None
    def _next_domain_assignment(self):

        if self._next_assignment == len(self._domain[0]):
            return None
        else:
            assn = self._next_assignment
            self._next_assignment += 1
            return assn

    # Clear Estimates
    def _clear_estimates(self):
        self._estimate_value = 0

    # Define the hashing key
    # Select the higher priority first
    def _key(self, priority1, priority2, val1, val2):
        if priority1 > priority2:
            return '%s,%s' % (val2, val1)
        else:
            return '%s,%s' % (val1, val2)

    # Check constraints and calculate cost in local constraint graph
    # local constraint graph - constrained agents to this agent...(duh, right?)
    def _possible_cpa_assignment_cost(self, cpa, value):
        assignment_cost = 0
        hash_id = 0
        for constraint in self._constraints:
            # Check if constraint is assigned... if not ignore.
            if cpa[constraint] is not None:

                # Get hash table key
                key = self._key(self._id, constraint, self._domain[0][value], cpa[int(constraint)])

                # Add cost to assignment cost
                assignment_cost += self._cost_functions[hash_id][key]

            # Hash Id
            hash_id += 1

        return assignment_cost

    # Get most recent CPA cost provided by higher priority agent
    def _past_cpa_cost(self):
        # Check if current is first
        if self._id == 1:
            # We are in the first agent
            return 0
        else:
            # Not first agent then return receive CPA_Cost
            return self._received_cpa.get_cost()

    # Estimate future cost based on constrained lower priority agents
    def _future_cpa_cost(self, value):
        # Try all possible domain values
        # Case 1: No Lower Priority Constraints
        lower_priority = False
        for x in self._constraints:
            if x > self._id:
                lower_priority = True

        if lower_priority is not True:
            return 0

        # Case 2: Constraints, but we only consider lower priority agents
        # Summation of h functions
        h_sum = 0

        # Iterate over all constraints
        for constraint in self._constraints:
            if constraint > self._id:
                h_sum += self._min_assignment(value, constraint)

        return h_sum

    # Find best assignment for constrained agent given possible assignment value
    def _min_assignment(self, value, j_id):
        min_cost = sys.maxsize
        # For each value in the jth domain
        j_hash_function_index = self._constraints.index(j_id)
        # index of jth hash
        j_hash_function = self._cost_functions[j_hash_function_index]
        j_domain_index = j_hash_function_index + 1
        j_domain = self._domain[j_domain_index]

        for v in j_domain:
            # create a key for the jth hash table
            key = self._key(self._id, j_id, self._domain[0][value], v)

            # access value
            current_cost = j_hash_function[key]
            if current_cost < min_cost:
                min_cost = current_cost

        return min_cost

    # Create the first CPA :D
    def _generate_cpa(self):
        # Generate a CPA class to maintain the problem
        cpa = [None] * (self._total_agents + 1)

        return CPA(0, cpa)

    # Assign next possible domain value for this agent
    def _assign_cpa(self):
        # clear estimates
        self._clear_estimates()

        # if CPA contains an assignment A_i = w_i remove it
        if self._current_cpa.get_cpa()[self._id] is not None:
            self._current_cpa.get_cpa()[self._id] = None  # Add method to modify assignment

        # find a new value (v)
        while True:
            self._current_assignment = self._next_domain_assignment()
            if self._current_assignment is None:
                return self._backtrack()

            assignment_cost = self._possible_cpa_assignment_cost(self._received_cpa.get_cpa(), self._current_assignment)
            f_v = assignment_cost + self._future_cpa_cost(self._current_assignment)

            # Assign Variable
            if (self._past_cpa_cost() + f_v) < self._B:
                # Assign the possible value
                # Update CPA
                self._current_cpa = copy.deepcopy(self._received_cpa)
                self._current_cpa.get_cpa()[self._id] = self._domain[0][self._current_assignment]
                self._current_cpa.set_cost(self._past_cpa_cost() + assignment_cost)

                # Increase timestamp
                self._timestamp.increase_timestamp(self._id)
                break

        # Add value to CPA and calculate cost
        if self._current_cpa.is_full():
            self._CPA_SOLUTION = copy.deepcopy(self._current_cpa)
            self._B = self._current_cpa.get_cost()

            # print(self._CPA_SOLUTION)
            msg_list = []  # broadcast to all agents updated B
            for x in range(1, self._total_agents + 1):
                msg = Message('NEW_SOLUTION', self._timestamp, self._id, x, self._CPA_SOLUTION)
                msg_list.append(msg)

            return msg_list + self._assign_cpa()
        else:
            # send cpa message to next agent in order
            msg_list = [Message('CPA_MSG', self._timestamp, self._id, self._id + 1, self._current_cpa)]

            for x in range(self._id + 1, self._total_agents + 1):
                msg = Message('FB_CPA', self._timestamp, self._id, x, self._current_cpa)
                msg_list.append(msg)
            return msg_list

    # No more possible domain values or pruning search space
    def _backtrack(self):
        # clear estimates
        # Check if in first agent
        self._clear_estimates()
        # print('[Agent-%d] BACKTRACK: %s'%(self._id,str(self._current_cpa)))
        if self._id == 1:
            # Broadcast Terminate
            msg_list = []
            for x in range(2, self._total_agents + 1):
                msg = Message('TERMINATE', self._timestamp, self._id, x, None)
                msg_list.append(msg)

            return msg_list

        else:
            # send CPA_MSG message to A_i-1
            # print('Agent-%d %s'%(self._id,self._current_assignment))
            msg = Message('CPA_MSG', self._timestamp, self._id, self._id - 1, self._received_cpa)
            return [msg]
#!/usr/bin/python

import sys

## change this path to where Timestamp.py
load_path = 'C:\\users\\vladimir\\programming\\nasa_wlog\\py_nasa_log\\'

if __name__ == '__main__':
    if not load_path in sys.path:
        sys.path.append(load_path)

from Timestamp import Timestamp

txt = '01/Jul/1995:00:00:01 -0400'
tm2 = Timestamp()
if tm2 != None:
    tm2.setDay('01')
    tm2.setMonth('Jul')
    tm2.setYear('1995')
    tm2.setHour('00')
    tm2.setMins('00')
    tm2.setSecs('01')
    print 'string representation: ' + tm2.toString()
else:
    print 'no timestamp created' + "\n",

        True,
        "\t")
            
    print("Done...")
   
print("Generate timestamps...")
  
timestamps = generateTimestampsForDays(2013, 1, 1, 1)
  
print("Done...") 
  
generateDataGridHourData(
    WORK_DIRECTORY,
    DATA_DIRECTORY + "weather/wu/",
    timestamps, 
    OUTPUT_DIRECTORY + "york_hour_grid_2013.csv")
  
for month in range(1, 13):
    for day in range(1, DAYS_OF_MONTH[month - 1] + 1):
        dayTimestamp = Timestamp().createBasedOnOther(2013, month, day, 1)
        dayTimestampKey = dayTimestamp.key[0:8]
           
        timestamps = generateTimestampsForDays(2013, month, day, day)
           
        generateDataGridHourData(
            WORK_DIRECTORY,
            DATA_DIRECTORY + "weather/wu/",
            timestamps, 
            OUTPUT_DIRECTORY + "data_" + dayTimestampKey + ".csv")

Beispiel #24
0
#!/usr/bin/python

import sys

## change this path to where Timestamp.py
load_path = 'C:\\users\\vladimir\\programming\\nasa_wlog\\py_nasa_log\\'

if __name__ == '__main__':
    if not load_path in sys.path:
        sys.path.append(load_path)

from Timestamp import Timestamp

txt = '01/Jul/1995:00:00:01 -0400'
tm1 = Timestamp()
if tm1.toTimestamp(txt):
    print 'month: ' + tm1.getMonth() + "\n",
    print 'day:   ' + tm1.getDay()   + "\n",
    print 'year:  ' + tm1.getYear()  + "\n",
    print 'hour:  ' + tm1.getHour() + "\n",
    print 'mins:  ' + tm1.getMins()  + "\n",
    print 'secs:  ' + tm1.getSecs()  + "\n",

    print 'string representation: ' + tm1.toString()
else:
    print 'no timestamp created' + "\n",