Answered step by step
Verified Expert Solution
Question
1 Approved Answer
use this code as a skeleton: PROJECT HEADER GOES HERE import random #DO NOT CHANGE THIS random.seed ( 1
use this code as a skeleton:
PROJECT HEADER GOES HERE
import random
#DO NOT CHANGE THIS
random.seed
NUMGENERATIONS
NUMPOPULATION
PROBABILITYMUTATION
PROBABILITYCROSSOVER
ALPHABET 'abcdefghijklmnopqrstuvwxyz
BANNER
Welcome to GeneticGuess Sentencer!
This program will attempt to guess a sentence that you input.
Simply input a sentence and the program will attempt to guess it
INPUT
Would you like to continue? yn
Please input the sentence you would like the program to guess:
Incorrect input. Please try again.
GeneticGuess results:"
"Generation:
"I found the sentence early!"
Best Individual:
Thank you for using GeneticGuess Sentencer!"
def fitnesstarget individual:
DOCSTRING
pass
def fivetournamentselectionpopulation target:
DOCSTRING
pass
def makepopulationtarget:
DOCSTRING
pass
def mutationindividual:
DOCSTRING
pass
def singlepointcrossoverindividual individual:
DOCSTRING
pass
def findbestindividualpopulation target:
DOCSTRING
pass
def main:
pass
# These two lines allow this program to be imported into other codes
# such as our function tests code allowing other functions to be run
# and tested without 'main' running. However, when this program is
# run alone, 'main' will execute.
# DO NOT CHANGE THESE lines or Do NOT add code to them. Everything
# you add should be in the 'main' function above.
if namemain:
main also, You are not allowed to use advanced data structures such as lists, Tuples, Sets, Dictionaries, Classes,
etc. Use of these prohibited concepts earns zero points for the whole project Mutation
Mutation is exactly how it sounds sometimes genes randomly mutate and one piece of them will change.
In our case, we have a chance of this mutation occurring PROBABILITYMUTATION First, we iterate
through every character over our individual. Then, we randomly choose a number between and using
random. random If our value is less than or equal to PROBABILITYMUTATION, the letter will be
replaced by a randomly selected letter from the ALPHABET string use the random. choice similar to
makepopulation function If the number is above the probability, then we will keep the letter.
Crossover
Crossover is the same as breeding you take two individuals and cross them to create offspring new
individuals There are a ton of ways to do this, but we will do a singlepoint crossover. This type works by
first checking that crossover happens. Similar to above, we choose a random number using
random. random and make sure it is less than or equal to PROBAILITYCROSSOVER. If it is we can
do our crossover.
Next, we need a random point to crossover our strings Use random. randint We want a number
between and the length of our individual. Then, we "cross" the strings at this point. The first half of the
visual is below, with the crosspoint set to The first half of individual and last half of individual
makes up the new individual The reverse happens for new individual and both are returned.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started