예제 #1
0
def start_Genetic_Algorithm(st, goal):
    startBlock = st
    destBlock = goal

    # to increase accuracy, increase "iterations"
    chromosomeSize = 20
    iterations = 10

    pop = population(20, True, startBlock, destBlock, chromosomeSize)
    print("Initial distance (Before Genetic Algorithm): ",
          pop.getFittest().getDistance())

    pop = GA.evolvePopulation(pop)

    for i in range(iterations):
        pop = GA.evolvePopulation(pop)

    return pop
예제 #2
0
    def __init__(self, name, message, number):
        #Encryption of plain text
        messageUtf = message.encode('utf-8')
        key = Fernet.generate_key()
        print("The key generated for this encryption is: ", key)
        print()
        time.sleep(1)
        cipher_suite = Fernet(key)
        cipher_text = cipher_suite.encrypt(messageUtf)
        plain_text = cipher_suite.decrypt(cipher_text)
        data = cipher_text.decode('utf-8')
        data = data + '~'
        cipher = []
        for i in data:
            cipher.append(ord(i))
        qwerty = cipher[:]

        #Extract pixel
        img = Image.open(name)
        arr = array(img)
        row = len(arr)
        column = len(arr[0])
        testing = row
        testing1 = column

        print('Initializing the initial population ')
        time.sleep(1)
        chromosomes = [None] * (row * column)
        index = 0

        #Initialise chromosomes with pixel values
        for x in range(column):
            for y in range(row):
                chromosomes[index] = Chromosome(index, arr[y][x][0],
                                                arr[y][x][1], arr[y][x][2], -1,
                                                y, x, -1)
                index += 1

        print("Starting the genetic algorithm...")
        time.sleep(1)
        geneticAlgoritm = GA(cipher, len(chromosomes))
        for i in range(len(cipher)):
            geneticAlgoritm.initializePopulation(chromosomes)
            pop = geneticAlgoritm.evolvePopulation()
            chroms = pop.getAllChromosome()
            for j in range(len(chroms)):
                chromosomes[j] = chroms[j]
        print("Genetic algorithm has completed execution..")
        time.sleep(1)

        print("Started burning the data into the image")
        time.sleep(1.5)
        if len(cipher) > (row * column):
            print('The image will not be able to accomodate the data')
            print("Try with some other image")
        else:
            print('The image will be able to accomodate the data')
            time.sleep(1.5)
            i = 0
            j = 0
            k = 0
            a = arr[:]
            count = 0
            for i in range(row):
                for j in range(column):
                    for k in range(len(chromosomes)):
                        if chromosomes[k].getX() == i and chromosomes[k].getY(
                        ) == j:
                            a[i][j][0] = chromosomes[k].getGene(0)
                            a[i][j][1] = chromosomes[k].getGene(1)
                            a[i][j][2] = chromosomes[k].getGene(2)
            print('We have now completed building the new image...')
            time.sleep(1)
            final_img = Image.fromarray(a)
            final_img.save('final' + str(number) + '.png')
            print(
                'The image has been saved as final.png in the same folder as this program.'
            )
            print()
            print()
            time.sleep(1.5)

            print('Visual cryptography algorithm has started to run')
            time.sleep(1.5)
            objVisual = visual_cryptography("final" + str(number) + ".png",
                                            number)
            keyVisual = objVisual.encrypt()
            print()

            print("Generating the pixel index table....")
            time.sleep(1.5)
            pitTable = []
            for i in range(len(cipher)):
                pitTable.append(Pit())
            pitTableIndex = 0

            for i in range(len(cipher)):
                for j in range(len(chromosomes)):
                    if chromosomes[j].getSolutionId() == i:
                        test = chromosomes[j].getAllGenes()
                        minDxs = getMinDx(chromosomes[j].getAllGenes(),
                                          cipher[i])
                        pitTable[pitTableIndex] = Pit(chromosomes[j].getX(),
                                                      chromosomes[j].getY(),
                                                      minDxs)
                        pitTableIndex += 1
            for i in range(len(pitTable)):
                f = open("pit1" + str(number) + ".csv", 'a')
                f.write(pitTable[i].toString() + "\n")
            f.close()
            print('The Pixel Index Table file has been generated.')
        print('The key for decryption of the visual cryptography is: ',
              keyVisual)
        time.sleep(1.5)
        print('The key for decryption of message is: ', key.decode('utf-8'))
        time.sleep(1.5)

        pass1 = "" + keyVisual
        pass2 = "" + key.decode('utf-8')
        file = open('Passwords' + str(number), 'w')
        file.write(pass1)
        file.write('~')
        file.write(pass2)
        print(
            'The keys for decryption are stored in Passwords file in the same folder as this code.'
        )
예제 #3
0
#Initialize a TourManager object and add cities to its list of destinations
tourmanager = TourManager()
for i in range(len(cities)):
    tourmanager.addCity(cities[i])

start_time = time.time()
#Init a population of individual tours
pop = Population(tourmanager, len(cities), True)

#print intitual distance
print("Initial distance: " + str(pop.getFittest().getDistance()))

#Evolve population for 100 generations
ga = GA(tourmanager)
pop = ga.evolvePopulation(pop)
for i in range(0, 100):
    pop = ga.evolvePopulation(pop)

#Final results
finalDist = str(pop.getFittest().getDistance())
#Returns a tour object
bestTour = pop.getFittest()
print("Finished!")
print("Final distance: " + finalDist)
print("Time to calculate best tour:")
print(time.time() - start_time)
print("Best tour:")
print(bestTour)

#Write to file