0

I am trying to generate a random sequence of letters and it should equal a specific string. A completely random iteration does take ages, so I tried a hill-climbing approach. It works find, until the similarity of the target and the generated sentence is nearly identical but it never gets to 100%. Any idea what the issue is here?

import string
import random

sim = []
sen_list = []

def random_sen():
    goal = "The value of the intrinsic motivation cannot be underestimated"
    sen = []
    i = 0
    for i in range(len(goal)): 
        sen.append(random.choice(string.ascii_letters + ' '))
        i += 1

    sen = ''.join(sen)
    sen = sen.lower()
    return sen

def test_similarity(sen):
    i = 0
    # sen = random_sen()
    sen = updating_sen(sen)
    similarity_value = 0
    goal = "The value of the intrinsic motivation cannot be underestimated"

    for i in range(len(sen)):
        if goal[i] == sen[i]:
            similarity_value = similarity_value + 1

    similarity_value = similarity_value/len(sen)
    return similarity_value


def updating_sen(sen_max):
    goal = "The value of the intrinsic motivation cannot be underestimated"
    i = 0
    sen = []
    for i in range(len(goal)):
        if goal[i] == sen_max[i]:
            sen.append(goal[i])
        else:
            sen.append(random.choice(string.ascii_letters + ' '))
        i += 1

    sen = ''.join(sen)
    sen = sen.lower()
    return sen


def play():
    counter = 1 
    sen_max = random_sen()

    while(1):
        sen = updating_sen(sen_max)
        similarity_value = test_similarity(sen)
        sim.append(similarity_value)
        sen_list.append(sen)

        if similarity_value == 1:
            print("The monkey can call himself Shakespear now!! The sentence is %s" % (sen_max))
            break
        if similarity_value != 1:
            counter = counter + 1
        if counter % 10000 == 0:
            sim_max = max(sim)
            max_index = sim.index(sim_max)
            sen_max = sen_list[max_index]
            print ("Try number %d. This is the sim_max value: %s. The sentence is: %s" % (counter,sim_max, sen_max))
    return  sen_max

sen_max = play()
christheliz
  • 176
  • 2
  • 15
  • 1
    research local maxima. It is increasingly more difficult to make the last blib vanish. you can optimize if you eleminate wrong things at the same position - theoretically you could randomly get 20 "a" for the last missing thing. Remember what you already tried and remove that from the randomly generated substitutes. – Patrick Artner Jul 09 '21 at 16:47
  • I was wondering because it takes a few hundred thousand updates to vanish the last two letters which doesn't make a lot of sense to me because there is a probability of 1/(26 + space) that the correct value is found. Can you give a hint on what to search for exactly regarding local maxima in python?:) – christheliz Jul 09 '21 at 16:56
  • 1
    100% similarity is not possible with your code, You're generating a sentence that is all lower case - but the goal contains one capital letter. – jasonharper Jul 09 '21 at 17:36
  • @jasonharper nice catch. That's why it didn't work out in the first place – christheliz Jul 10 '21 at 15:26

0 Answers0