Question
Please i need to change this to matlab programing import random source = jiKnp4bqpmAbp target = Hello, World! def fitness(source, target): fitval = 0 for
Please i need to change this to matlab programing
import random
source = "jiKnp4bqpmAbp"
target = "Hello, World!"
def fitness(source, target):
fitval = 0
for i in range(0, len(source)):
fitval += (ord(target[i]) - ord(source[i])) ** 2
return(fitval)
def mutate(source):
charpos = random.randint(0, len(source) - 1)
parts = list(source)
parts[charpos] = chr(ord(parts[charpos]) + random.randint(-1,1))
return(''.join(parts))
random.seed()
fitval = fitness(source, target)
i = 0
while True:
i += 1
m = mutate(source)
fitval_m = fitness(m, target)
if fitval_m < fitval:
fitval = fitval_m
source = m
print "%5i %5i %14s" % (i, fitval_m, m)
if fitval == 0:
break
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