Hello, world!

This notebook contains a simple example of a grammar that describes a formal language in which each string is a variation of the famous Hello, world! phrase.

[1]:
import alogos as al

Search space

Define a grammar

[2]:
ebnf = """
S = GREETING ", " ADJ " " OBJ "!"
GREETING = "Hello" | "Hi" | "Hola" | "Hallo" | "Bonjour"
ADJ = "beautiful" | "amazing" | "fantastic" | "great" | "nice"
OBJ = "world" | "universe" | "cosmos" | "eurycosmos" | "ruliad"
"""

grammar = al.Grammar(ebnf_text=ebnf)

Generate its language

[3]:
grammar.generate_language(sort_order='shortlex')
[3]:
['Hi, nice world!',
 'Hi, great world!',
 'Hi, nice cosmos!',
 'Hi, nice ruliad!',
 'Hi, great cosmos!',
 'Hi, great ruliad!',
 'Hola, nice world!',
 'Hallo, nice world!',
 'Hello, nice world!',
 'Hi, amazing world!',
 'Hi, nice universe!',
 'Hola, great world!',
 'Hola, nice cosmos!',
 'Hola, nice ruliad!',
 'Hallo, great world!',
 'Hallo, nice cosmos!',
 'Hallo, nice ruliad!',
 'Hello, great world!',
 'Hello, nice cosmos!',
 'Hello, nice ruliad!',
 'Hi, amazing cosmos!',
 'Hi, amazing ruliad!',
 'Hi, great universe!',
 'Hola, great cosmos!',
 'Hola, great ruliad!',
 'Bonjour, nice world!',
 'Hallo, great cosmos!',
 'Hallo, great ruliad!',
 'Hello, great cosmos!',
 'Hello, great ruliad!',
 'Hi, beautiful world!',
 'Hi, fantastic world!',
 'Hi, nice eurycosmos!',
 'Hola, amazing world!',
 'Hola, nice universe!',
 'Bonjour, great world!',
 'Bonjour, nice cosmos!',
 'Bonjour, nice ruliad!',
 'Hallo, amazing world!',
 'Hallo, nice universe!',
 'Hello, amazing world!',
 'Hello, nice universe!',
 'Hi, amazing universe!',
 'Hi, beautiful cosmos!',
 'Hi, beautiful ruliad!',
 'Hi, fantastic cosmos!',
 'Hi, fantastic ruliad!',
 'Hi, great eurycosmos!',
 'Hola, amazing cosmos!',
 'Hola, amazing ruliad!',
 'Hola, great universe!',
 'Bonjour, great cosmos!',
 'Bonjour, great ruliad!',
 'Hallo, amazing cosmos!',
 'Hallo, amazing ruliad!',
 'Hallo, great universe!',
 'Hello, amazing cosmos!',
 'Hello, amazing ruliad!',
 'Hello, great universe!',
 'Hola, beautiful world!',
 'Hola, fantastic world!',
 'Hola, nice eurycosmos!',
 'Bonjour, amazing world!',
 'Bonjour, nice universe!',
 'Hallo, beautiful world!',
 'Hallo, fantastic world!',
 'Hallo, nice eurycosmos!',
 'Hello, beautiful world!',
 'Hello, fantastic world!',
 'Hello, nice eurycosmos!',
 'Hi, amazing eurycosmos!',
 'Hi, beautiful universe!',
 'Hi, fantastic universe!',
 'Hola, amazing universe!',
 'Hola, beautiful cosmos!',
 'Hola, beautiful ruliad!',
 'Hola, fantastic cosmos!',
 'Hola, fantastic ruliad!',
 'Hola, great eurycosmos!',
 'Bonjour, amazing cosmos!',
 'Bonjour, amazing ruliad!',
 'Bonjour, great universe!',
 'Hallo, amazing universe!',
 'Hallo, beautiful cosmos!',
 'Hallo, beautiful ruliad!',
 'Hallo, fantastic cosmos!',
 'Hallo, fantastic ruliad!',
 'Hallo, great eurycosmos!',
 'Hello, amazing universe!',
 'Hello, beautiful cosmos!',
 'Hello, beautiful ruliad!',
 'Hello, fantastic cosmos!',
 'Hello, fantastic ruliad!',
 'Hello, great eurycosmos!',
 'Bonjour, beautiful world!',
 'Bonjour, fantastic world!',
 'Bonjour, nice eurycosmos!',
 'Hi, beautiful eurycosmos!',
 'Hi, fantastic eurycosmos!',
 'Hola, amazing eurycosmos!',
 'Hola, beautiful universe!',
 'Hola, fantastic universe!',
 'Bonjour, amazing universe!',
 'Bonjour, beautiful cosmos!',
 'Bonjour, beautiful ruliad!',
 'Bonjour, fantastic cosmos!',
 'Bonjour, fantastic ruliad!',
 'Bonjour, great eurycosmos!',
 'Hallo, amazing eurycosmos!',
 'Hallo, beautiful universe!',
 'Hallo, fantastic universe!',
 'Hello, amazing eurycosmos!',
 'Hello, beautiful universe!',
 'Hello, fantastic universe!',
 'Hola, beautiful eurycosmos!',
 'Hola, fantastic eurycosmos!',
 'Bonjour, amazing eurycosmos!',
 'Bonjour, beautiful universe!',
 'Bonjour, fantastic universe!',
 'Hallo, beautiful eurycosmos!',
 'Hallo, fantastic eurycosmos!',
 'Hello, beautiful eurycosmos!',
 'Hello, fantastic eurycosmos!',
 'Bonjour, beautiful eurycosmos!',
 'Bonjour, fantastic eurycosmos!']

Generate random strings

[4]:
for _ in range(5):
    print(grammar.generate_string())
Bonjour, beautiful universe!
Bonjour, nice ruliad!
Hello, beautiful world!
Bonjour, fantastic world!
Hi, amazing cosmos!

Search goal

Define an objective function

The objective function gets a string of the grammar’s language as input and needs to return a number as output, which represents the quality or fitness of the string. In this case, the function simply measures the length of the string. Our goal will be to find a string with maximum length.

[5]:
def objective_function(string):
    fitness = len(string)
    return fitness

Test it on a random string

[6]:
string = grammar.generate_string()
fitness = objective_function(string)

print(string)
print(fitness)
Hola, beautiful eurycosmos!
27

Search method

Search for an optimal string

[8]:
best_individual = ea.run()
Progress         Generations      Evaluations      Runtime (sec)    Best fitness
..... .....      10               50               0.0              30.0
..

Finished         12               56               0.0              30.0
[9]:
print(best_individual.phenotype)
Bonjour, beautiful eurycosmos!