Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Phyton program #!/usr/bin/python import random import string target = \"Hello, W

ID: 3707484 • Letter: P

Question

Phyton program

#!/usr/bin/python

import random

import string

target = "Hello, World!"

def calc_fitness(source, target):

    fitval = 0

    for i in range(0, len(source)):

        fitval += (ord(target[i]) - ord(source[i])) ** 2

    return(fitval)

def mutate(parent1, parent2):

    child_dna = parent1['dna'][:]

    # Mix both DNAs

    start = random.randint(0, len(parent2['dna']) - 1)

    stop = random.randint(0, len(parent2['dna']) - 1)

    if start > stop:

        stop, start = start, stop

    child_dna[start:stop] = parent2['dna'][start:stop]

    # Mutate one position

    charpos = random.randint(0, len(child_dna) - 1)

    child_dna[charpos] = chr(ord(child_dna[charpos]) + random.randint(-1,1))

    child_fitness = calc_fitness(child_dna, target)

    return({'dna': child_dna, 'fitness': child_fitness})

def random_parent(genepool):

    wRndNr = random.random() * random.random() * (GENSIZE - 1)

    wRndNr = int(wRndNr)

    return(genepool[wRndNr])

def dump_genepool(generation, genepool):

    for candidate in genepool:

        print "%6i %6i %15s" % (

            generation,

            candidate['fitness'],

            ''.join(candidate['dna'])

        )

    print

GENSIZE = 20

genepool = []

for i in range(0, GENSIZE):

    dna = [random.choice(string.printable[:-5]) for j in range(0, len(target))]

    fitness = calc_fitness(dna, target)

    candidate = {'dna': dna, 'fitness': fitness }

    genepool.append(candidate)

generation = 0

while True:

    generation += 1

    genepool.sort(key=lambda candidate: candidate['fitness'])

    dump_genepool(generation, genepool)

    if genepool[0]['fitness'] == 0:

        # Target reached

        break

    parent1 = random_parent(genepool)

    parent2 = random_parent(genepool)

    child = mutate(parent1, parent2)

    if child['fitness'] < genepool[-1]['fitness']:

        genepool[-1] = child

Matlab program

clear;close all;clc;  

target = input('Enter a string (max 30 chars in length): ', 's');               

GENSIZE = 20;                              

genepool = []

for i:length(GENEZIZE):

for i in range(0, GENSIZE):

    dna = [random.choice(string.printable[:-5]) for j in range(0, len(target))]

    fitness = calc_fitness(dna, target)

    candidate = {'dna': dna, 'fitness': fitness }

    genepool.append(candidate)

generation = 0

%fitval = fitness(child, target);

while child_fitness ~=0

    generation +=1

    genepool.sort(key=lambda candidate:candidate['fitness'])

      dump_genepool(generation, genepool)

    parent1 = get_parent(genepool);

    parent2 = get_parent(genepool);

      child_= mutate(parent1, parent2);

    child_fitness = calc_fitness(child, target);

   

end

def dump_genepool(generation, genepool):

    for candidate in genepool:

        print "%6i %6i %15s" % (

            generation,

            candidate['fitness'],

            ''.join(candidate['dna'])

        )

    print

function child_fitness = calc_fitness(source, target)%def calc_fitness;

child_fitness = 0;

for i = 1 : length(source)

   child_fitness= child_fitness + (double(target(i)) - double(source(i))) ^ 2;

end

end

function [child_dna,child_fitness]=mutate(parent1,parent2)

child_dna =parent1(1,:);

start=randi(size(parent2(1,:)),1,1);

stop=randi(size(parent2(1,:)),1,1);

if(start>stop)

    tmp=start;

    start=stop;

    stop=start;

end

child_dna(start:stop)=parent2(1,start:stop);

charpos=randi(size(child_dna),1,1);

child_dna(charpos)=char(uint8(child_dna(charpos))+randi(3,1,1)-2);

child_fitness = calc_fitness(child_dna, target)

end

function [x] = get_parent(genepool , GENSIZE)

          wRndNr = rand() * rand() * ( GENSIZE - 1 );

          wRndNr = int32(wRndNr);

          x = genepool(wRndNr);

end

Please I need some help I am trying to convert the first phyton program to matlab I have tried but it is not work please any one would correct me thanks

Explanation / Answer

Instead of converting the Python code to the Matlab , you could actually call python scripts from the Matlab, please find the following info :

This example shows how to call a MATLAB® script to compute the area of a triangle from Python®.

In your current folder, create a MATLAB script in a file named triarea.m.

After you save the file, start Python and call the script.

Specify nargout=0. Although the script prints output, it returns no output arguments to Python.

Convert the script to a function and call the function from the engine. To edit the file, open the MATLAB editor.

Delete the three statements. Then add a function declaration and save the file.

Call the new triarea function from the engine.

The triarea function returns only one output argument, so there is no need to specify nargout.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote