Exemplo n.º 1
0
def main():

    parser = argparse.ArgumentParser(description="Instrucciones del script")
    parser.add_argument('-f', dest='textfile', default=True)
    args = parser.parse_args()
    filename = args.textfile

    # borra los registros de casos de prueba anteriores
    clean_log_history('./Devices_Logs')

    f = open(filename, 'r')
    # has un recorrido por cada linea del file.txt
    for line in f.readlines():
        try:
            instruction, args2 = myParser.parse(line)
        except TypeError:
            print("Parse Error wrong sintax")
            return
        try:
            caller[instruction](args2)
        except ValueError:
            print("Excution Error")
            return
    # sigue recorriendo la red luego de leer todas las instrucciones de entrada
    # para ver si quedo alguna actividad de los host por hacer
    handler.finished_network_transmission()
Exemplo n.º 2
0
def main():
    start_time = time.time()
    agents, tasks, costs, resources, capacities = myParser.parse("PAG2017.txt")
    nbP = 3
    children, bestSol = solveProblemFirst(agents[nbP], tasks[nbP], costs[nbP], resources[nbP], capacities[nbP],5)
    printSolution(bestSol)

    tabu(bestSol, agents[nbP], tasks[nbP], costs[nbP], resources[nbP], capacities[nbP])
    print("Total execution time:", round((time.time() - start_time)))
Exemplo n.º 3
0
def main():
    """
    Perform searches for the given flight parameters.
    """
    args = myParser.parse()

    if args.company == "Southwest":
        real_total = swaScraper.scrape(args)

    notifier.sendNotification(real_total, args.max_price)
Exemplo n.º 4
0
def main():
    """
    Perform searches for the given flight parameters.
    """
    args = myParser.parse()

    if args.company == "Southwest":
        real_total = swaScraper.scrape(args)

    # If the user doesn't want text notifications, just print the result to the console.
    if args.no_text:
        from datetime import datetime

        print("[%s] Found a deal. Max Total: $%s. Current Total: $%s." %
              (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), args.max_price,
               str(real_total)))
    else:
        # Send a text notifying the user that a lower price was found.
        import notifier
        notifier.sendNotification(real_total, args.max_price)
Exemplo n.º 5
0
def Z_IT(itexpr_str, Xtrain):
    
    nsamples, nvars = Xtrain.shape
    
    # Avoidind problems with the dot on the parser
    itexpr_str = itexpr_str.replace('sqrt.abs', 'SQRTABS')
    
    # variables should be labeled x_{i}
    for i in range(nvars):
        itexpr_str = itexpr_str.replace(f'x{i}', f'x_{i}')
    
    # Making sure exp is denoted by '^
    itexpr_str = itexpr_str.replace('**', '^')

    # obtaining the terms
    itexpr_its = itexpr_str.split(' + ')

    nterms     = len(itexpr_its)
    
    
    Z = np.zeros( (nsamples, nterms) )
    
    for col, it in enumerate(itexpr_its):
        Z[:, col] = myParser.parse(it, Xtrain)

    # Store all disentanglements calculated
    disentanglements = []
    
    for col, _ in enumerate(itexpr_its):
        for comparison, _ in enumerate(itexpr_its):
            if col != comparison:
                corr, p = stats.pearsonr( Z[:, col], Z[:, comparison] )
                
                # Pearson's correlation divides by the std, and the
                # existante of a result is not guaranted. Handling possible NaNs
                corr = 0.0 if np.isnan(corr) else corr
                
                disentanglements.append(corr**2)
        
    return np.mean(disentanglements)
def Z_FEATFull(feat_str, Xtrain):

    nsamples, nvars = Xtrain.shape

    # Changing the sqrt(|x|)
    feat_str = feat_str.replace('sqrt(|', 'SQRTABS((')
    feat_str = feat_str.replace('|', ')')

    feat_str = feat_str.replace("if-then-else", "if")

    # replace >= and <= with geq and leq, avoiding matching with > or <
    feat_str = feat_str.replace('>=', ' geq ')
    feat_str = feat_str.replace('<=', ' leq ')
    feat_str = feat_str.replace('==', ' eq ')

    feat_strs = re.findall(r'\[([^]]*)\]', feat_str)

    nterms = len(feat_strs)

    Z = np.zeros((nsamples, nterms))

    for col, feat in enumerate(feat_strs):
        Z[:, col] = myParser.parse(feat, Xtrain)

    disentanglements = []

    for col, _ in enumerate(feat_strs):
        for comparison, _ in enumerate(feat_strs):
            if col != comparison:
                corr, p = stats.pearsonr(Z[:, col], Z[:, comparison])

                # Pearson's correlation divides by the std, and the
                # existante of a result is not guaranted. Handling possible NaNs
                corr = 0.0 if np.isnan(corr) else corr

                disentanglements.append(corr**2)

    return np.mean(disentanglements)
Exemplo n.º 7
0
]

#lastValues = [[[]]]*15
try:
    while True:
        json_read = open(fileName, "r")
        data = json.load(json_read)
        json_read.close()

        for i in range(0, len(stocks)):
            stockName = stocks[i]
            print(
                stockName,
                end=" ",
            )
            currentPrice, table = parser.parse(stockName)
            if currentPrice != -1:  #and lastValues[i] != table:
                #print("new reading detected")
                #lastValues[i]=table
                newParse = {
                    "kademeDeğerleri": table,
                    "stockPrice": currentPrice
                }
                data.get("stocks")[i].get("parses").append(newParse)
            if currentPrice == -1:
                '''errNo = data["stocks"][i]["errors"]
                data["stocks"][i]["errors"] = errNo+1'''
        print("DON'T CLOSE THE PROGRAM, SSAVING THE FILE")
        json_write = open(fileName, "w")
        json.dump(data, json_write)
        json_write.close()
Exemplo n.º 8
0
 def testNum(self):
     data = ['1']  #1
     result = parse(data)
     self.assertEqual(['1', [], []], result)
Exemplo n.º 9
0
 def testUnaryParenthesis(self):
     data = ['7', '*', ['3', '+', '4'], '!']
     result = parse(data)
     self.assertEqual([
         '*', ['7', [], []], ['!', ['+', ['3', [], []], ['4', [], []]], []]
     ], result)
Exemplo n.º 10
0
 def testParenthesis(self):
     data = [['6', '-', '2'], '/', '8']  #(6-2)/8
     result = parse(data)
     self.assertEqual(
         ['/', ['-', ['6', [], []], ['2', [], []]], ['8', [], []]], result)
Exemplo n.º 11
0
 def testBinary(self):
     data = ['2', '*', '9']
     result = parse(data)
     self.assertEqual(['*', ['2', [], []], ['9', [], []]], result)
Exemplo n.º 12
0
 def testUnary(self):
     data = ['3', '!']  #3!
     result = parse(data)
     self.assertEqual(['!', ['3', [], []], []], result)
Exemplo n.º 13
0
import myLexer
import myParser

f = open("Construct.txt", "r")
p = f.read()
tokens = myLexer.tokenize(p)
print("---Output of Lexer--- \n", tokens, "\n\n---Output of parser---")
myParser.parse(tokens)
Exemplo n.º 14
0
def main():

    problemSolvedNumber = int(
        input("Choose the set of Data used to solve the problem: 0/1/2/3: ")
    )  # 0 - 3
    start_time_total = time.time()
    agents, tasks, costs, resources, capacities = myParser.parse("PAG2017.txt")

    sizeOfPopulation = setParam1(problemSolvedNumber)
    #parents = generateSolutionProb1.solveProblem(agents[problemSolvedNumber], tasks[problemSolvedNumber], resources[problemSolvedNumber], capacities[problemSolvedNumber],25)
    parents = generateSolutionProb1.solveProblem(
        agents[problemSolvedNumber], tasks[problemSolvedNumber],
        resources[problemSolvedNumber], capacities[problemSolvedNumber],
        sizeOfPopulation)
    print("First generation generated in: -- %s seconds ---" % round(
        (time.time() - start_time_total), 2))

    # we sort the solutions by effectiveness
    effectivenessOfPopulation = [0 for i in range(sizeOfPopulation)]
    for i in range(sizeOfPopulation):
        effectivenessOfPopulation[i] = [
            evaluateFitness(parents[i], costs[problemSolvedNumber]), i
        ]
        # generateSolutionProb1.printSolution(parents[i])
    effectivenessOfPopulation.sort(key=lambda data: data[0])
    # print ("score: ",effectivenessOfPopulation)

    bestSolutionYet = parents[effectivenessOfPopulation[0][1]]
    bestScoreYet = evaluateFitness(bestSolutionYet, costs[problemSolvedNumber])
    print("Best Score: ", bestScoreYet)
    # print(effectivenessOfPopulation[numeroPodiumSolution][1])->numero individu, numeroPodiumSolution >= 0 and numeroPodiumSolution< sizeOfPopulation
    countTotalGenerationGenerated = sizeOfPopulation

    #**************************************************************************************************************************

    BestIndividuals = []
    BestIndividuals.append(bestSolutionYet)

    sizeOfFirstGeneration = setParam1(problemSolvedNumber)
    sizeOfPopulation = setSizeOffspringPopulation(problemSolvedNumber)
    first = 1
    doIt = 1
    numberOfIterationBeforeAskingToStop = -1
    countIteration = 0
    numberOfGenerationWithoutAmeliorationBeforeStoping = 5000
    countIterationGenerationWithoutAmelioration = 0
    thresholdProbaAssignment = setThresholdProbaAssignment(problemSolvedNumber)

    thresholdProbaAssignmentInitial = thresholdProbaAssignment
    timeLimitInSeconds = setTimeLimit(problemSolvedNumber)
    swapForBest = 0
    start_time = time.time()
    #**************************************************************************************************************************

    while (doIt > 0 and (time.time() - start_time) < timeLimitInSeconds):
        countGeneFrom1 = 0
        countGeneFrom2 = 0
        countGeneFromBoth = 0
        countGeneFromMutation = 0
        children = [[[0 for x in range(tasks[problemSolvedNumber])]
                     for y in range(agents[problemSolvedNumber])]
                    for z in range(sizeOfPopulation)]
        currentSolutionGenerated = 0
        couplesWhoHaveMated = []
        startTimeGene = time.time()
        while (currentSolutionGenerated != sizeOfPopulation):
            #select 2 solutions (better solutions have better chance to be chosen)
            isThisANewCouple = 0
            while (isThisANewCouple != 1):
                if (first):
                    numeroSolution1, numeroSolution2 = chooseMatingCandidates(
                        parents, effectivenessOfPopulation,
                        sizeOfFirstGeneration, problemSolvedNumber)
                else:
                    numeroSolution1, numeroSolution2 = chooseMatingCandidates(
                        parents, effectivenessOfPopulation, sizeOfPopulation,
                        problemSolvedNumber)
                if (numeroSolution1 == numeroSolution2):
                    if (numeroSolution2 != 0):
                        numeroSolution2 -= 1
                    else:
                        numeroSolution2 += 1
                if ([numeroSolution1, numeroSolution2
                     ] not in couplesWhoHaveMated
                        and [numeroSolution2, numeroSolution1
                             ] not in couplesWhoHaveMated):
                    couplesWhoHaveMated.append(
                        [numeroSolution1, numeroSolution2])
                    isThisANewCouple = 1
                #else:
                #print("THEY ALREADY MATED")

            #print (numeroSolution1, " reproduce with", numeroSolution2)

            numeroSolution1 = effectivenessOfPopulation[numeroSolution1][1]
            numeroSolution2 = effectivenessOfPopulation[numeroSolution2][1]
            #print (numeroSolution1, " reproduce with", numeroSolution2)

            assignedTask = []
            currentTaskNumber = randint(0, tasks[problemSolvedNumber] - 1)
            currentAgentNumber = randint(0, agents[problemSolvedNumber] - 1)

            #while the current solution has not all the task assigned
            while (len(assignedTask) != tasks[problemSolvedNumber]):
                while (currentTaskNumber not in assignedTask):
                    #print ("current task: ", currentTaskNumber, " current agent: ", currentAgentNumber)
                    #randomly check if the task is given to an agent i
                    chance = randint(1,
                                     100)  #generate a number between 1 and 100
                    #print (numeroSolution1, numeroSolution2, len(parents), currentAgentNumber, currentTaskNumber)
                    if (chance >= thresholdProbaAssignment
                            or parents[numeroSolution1][currentAgentNumber]
                        [currentTaskNumber] or parents[numeroSolution2]
                        [currentAgentNumber][currentTaskNumber]):
                        #if the task is given, check if the agent has enough ressource
                        ressourceNeeded = resources[problemSolvedNumber][
                            currentAgentNumber][currentTaskNumber]
                        currentAgentCapacity = capacities[problemSolvedNumber][
                            currentAgentNumber]
                        #if the agent has the capicity to do the task
                        if (ressourceNeeded <= currentAgentCapacity):
                            #j=randint(0,tasks[problemSolvedNumber]-1)
                            j = 0
                            while (generateSolutionProb1.computeResourceLeft(
                                    children[currentSolutionGenerated],
                                    resources[problemSolvedNumber],
                                    currentAgentCapacity, currentAgentNumber) <
                                   ressourceNeeded):
                                if (children[currentSolutionGenerated]
                                    [currentAgentNumber][j]):
                                    children[currentSolutionGenerated][
                                        currentAgentNumber][j] = 0
                                    #print ("task ", j, " removed from agent ", currentAgentNumber)
                                j += 1
                                #if(j>tasks[problemSolvedNumber]-1):
                                #    j=0
                            children[currentSolutionGenerated][
                                currentAgentNumber][currentTaskNumber] = 1
                            assignedTask = generateSolutionProb1.checkAssignedTask(
                                children[currentSolutionGenerated])
                            #print ("task ",currentTaskNumber, "assigned to agent ",currentAgentNumber )  ;
                        #else:
                        #print ("agent ",currentAgentNumber," has not the capacity to do ",currentTaskNumber)
                    #we pass to the next agent
                    currentAgentNumber += 1
                    if (currentAgentNumber == agents[problemSolvedNumber]):
                        currentAgentNumber = 0
                #the task has been assigned
                currentTaskNumber += 1
                if (currentTaskNumber == tasks[problemSolvedNumber]):
                    currentTaskNumber = 0
            #count similiarities
            numberOfGeneFromFirst = 0
            numberOfGeneFromSecond = 0
            numberOfGeneInBoth = 0
            numberFromMutation = 0
            #print("[",end='')
            for i in range(agents[problemSolvedNumber]):
                for j in range(tasks[problemSolvedNumber]):
                    if (children[currentSolutionGenerated][i][j]):
                        score = parents[numeroSolution1][i][j] * 10 + parents[
                            numeroSolution2][i][j]
                        if (score == 10):
                            numberOfGeneFromFirst += 1
            #                print(" 1 ",end='')
                        if (score == 1):
                            numberOfGeneFromSecond += 1
            #                print(" 2 ",end='')
                        if (score == 11):
                            numberOfGeneInBoth += 1
            #                print(" B ",end='')
                        if (score == 0):
                            numberFromMutation += 1
            #                print(" M ",end='')
            #print("]", end='  -----------------  ')
            #if(numberOfGeneFromFirst==0 and numberOfGeneFromSecond==0 and thresholdProbaAssignment > 70):
            #    thresholdProbaAssignment-=1
            #    #print("-")
            #if(numberOfGeneFromFirst > 0 and numberOfGeneFromSecond > 0 and thresholdProbaAssignment < thresholdProbaAssignmentInitial):
            #    thresholdProbaAssignment+=1
            #print("+")
            #print(numeroSolution1," GIVES ",numberOfGeneFromFirst,"|",numeroSolution2," GIVES ",numberOfGeneFromSecond," |  BOTH GIVE ",numberOfGeneInBoth," |  MUTATION: ",numberFromMutation,)
            countGeneFrom1 += numberOfGeneFromFirst
            countGeneFrom2 += numberOfGeneFromSecond
            countGeneFromBoth += numberOfGeneInBoth
            countGeneFromMutation += numberFromMutation
            currentSolutionGenerated += 1

        countTotalGenerationGenerated += currentSolutionGenerated

        effectivenessOfPopulation = [0 for i in range(sizeOfPopulation)]
        for i in range(sizeOfPopulation):
            effectivenessOfPopulation[i] = [
                evaluateFitness(children[i], costs[problemSolvedNumber]), i
            ]
            #generateSolutionProb1.printSolution(parents[i])
        effectivenessOfPopulation.sort(key=lambda data: data[0])
        #print ("score: ",effectivenessOfPopulation)
        bestScoreOfThisGeneration = evaluateFitness(
            children[effectivenessOfPopulation[0][1]],
            costs[problemSolvedNumber])
        if (bestScoreYet > bestScoreOfThisGeneration):
            bestScoreYet = bestScoreOfThisGeneration
        print(
            "Best: ", bestScoreOfThisGeneration, "Best Total:", bestScoreYet,
            "1: ",
            round(
                100 * countGeneFrom1 /
                (sizeOfPopulation * tasks[problemSolvedNumber]), 2), "% 2: ",
            round(
                100 * countGeneFrom2 /
                (sizeOfPopulation * tasks[problemSolvedNumber]), 2), "% B: ",
            round(
                100 * countGeneFromBoth /
                (sizeOfPopulation * tasks[problemSolvedNumber]), 2), "% M: ",
            round(
                100 * countGeneFromMutation /
                (sizeOfPopulation * tasks[problemSolvedNumber]), 2),
            "% total generated: ", countTotalGenerationGenerated, " in:",
            round((time.time() - startTimeGene), 2), "seconds"
        )  #" threshold: ", thresholdProbaAssignment, "nb pop: ", sizeOfPopulation, " nb pop origin: ",sizeOfFirstGeneration)

        if (evaluateFitness(bestSolutionYet, costs[problemSolvedNumber]) >
                bestScoreOfThisGeneration):
            bestSolutionYet = children[effectivenessOfPopulation[0][1]]
            BestIndividuals.append(bestSolutionYet)
            countIterationGenerationWithoutAmelioration = 0
        else:
            countIterationGenerationWithoutAmelioration += 1

        if (len(BestIndividuals) == sizeOfPopulation and swapForBest):
            print("Generation of Best")
            children = BestIndividuals
            BestIndividuals = []
            effectivenessOfPopulation = [0 for i in range(sizeOfPopulation)]
            for i in range(sizeOfPopulation):
                effectivenessOfPopulation[i] = [
                    evaluateFitness(children[i], costs[problemSolvedNumber]), i
                ]
                #generateSolutionProb1.printSolution(parents[i])
            effectivenessOfPopulation.sort(key=lambda data: data[0])

        countIteration += 1
        first = 0

        if (countIterationGenerationWithoutAmelioration ==
                numberOfGenerationWithoutAmeliorationBeforeStoping):
            break
        if (countIteration == numberOfIterationBeforeAskingToStop):
            countIteration = 0
            doIt = int(input("New Generation: 1 , print best solution 0: "))
            #print("You have chosen: ",doIt)
        if (doIt):
            parents = children

    if (len(BestIndividuals) > 0):
        for i in range(len(BestIndividuals)):
            generateSolutionProb1.printSolution(BestIndividuals[i])
            print(
                "Score: ",
                evaluateFitness(BestIndividuals[i],
                                costs[problemSolvedNumber]))
    else:
        print("Best Score: ",
              evaluateFitness(bestSolutionYet, costs[problemSolvedNumber]))
        generateSolutionProb1.printSolution(bestSolutionYet)
    print("Number total of solution tested: ", countTotalGenerationGenerated)
    print("Total execution time --- %s seconds ---" % round(
        (time.time() - start_time_total), 2))
Exemplo n.º 15
0
def downloader():
    response = requests.get('https://api.github.com/', headers=headers)

    print(response)

    if not os.path.exists(config.output_dir):
        os.makedirs(config.output_dir)

    mycursor = config.mydb.cursor()

    while 1 > 0:
        sql = "select * from repos WHERE language = %s AND downloaded = 0 ORDER BY RAND() LIMIT %s"
        val = (config.lang, query_limit)

        mycursor.execute(sql, val)
        myResult = mycursor.fetchall()

        if len(myResult) < 5:
            print("Not enough links to download")
            break

        #============================================== download

        for entry in myResult:
            print(entry)

            print("url: " + entry[1])

            item = requests.get(entry[1], headers=headers).json()

            if len(item) < 6:  #for invalid requests
                print("not found")
                continue

            # Obtain user and repository names
            user = item['owner']['login']
            repository = item['name']

            # Download the zip file of the current project
            # print("Downloading repository '%s' from user '%s' ..." % (repository, user))
            # url = item['clone_url']
            # fileToDownload = url[0:len(url) - 4] + "/archive/master.zip"
            # fileName = item['full_name'] + ".zip"
            jsonName = item['full_name'] + ".json"

            # if not os.path.exists(OUTPUT_FOLDER+user):
            #     os.mkdir(OUTPUT_FOLDER+user)

            # wget.download(fileToDownload, OUTPUT_FOLDER + fileName)

            myParser.parse(item['full_name'])
            print(item['full_name'])

            with open(config.output_dir + jsonName, "w") as jsonFile:
                json.dump(item, jsonFile)

            # print(user)
            # print(repository)

            sql = "UPDATE repos SET downloaded = 1 WHERE id = %s LIMIT 5"
            val = (str(entry[0]), )

            mycursor.execute(sql, val)
            config.mydb.commit()

        time.sleep(1)
Exemplo n.º 16
0
#    myDict[word] = nums
#dict_file.close()

import cPickle as pickle
input = open('dict2.pkl', 'rb')
dict2 = pickle.load(input)
input.close()

#urls_file = codecs.open('urls','r', 'utf-8')
#for line in urls_file.readlines():
#    s = line.strip()
#    urls.append(s)
#urls_file.close()

input = open('urls.pkl', 'rb')
urls = pickle.load(input)
input.close()

ff = codecs.getreader('utf8')(sys.stdin)
sys.stdout = codecs.getwriter('utf8')(sys.stdout)

for line in ff.readlines():
    #    query = [q.strip().lower() for q in line.split("&")]
    print line.strip()
    expr = myParser.parse(line.lower())
    #    print expr.toStr()
    result = expr.getUrls(dict2, len(urls))
    print len(result)
    for r in result:
        print urls[r]