Hai i need help in C++.. this is my code. By using this code I have to alter it and do Evolutionary Text/ Poem.
The poem is :
**“Berburu ke padang datar,
Dapat rusa berbelang kaki,
Berguru kepalang ajar,
Bagai bunga kembang tak jadi.” **

**There are 3 files.. Evo0.cpp(below code), mtrand.h and mtrand.cpp Click Here **

#include <iostream>
#include <ctime>
#include <windows.h>
#include <fstream>
#include "mtrand.h"

using namespace std;

int main () {
    const int chromosome_length = 40;
    const int pop_size = 20;    
    int max_generation = 200;
    int mutate_position;        // pos to mutate
    int xover_position; // pos to xover
    int rand_val;       // to hold random value, i.e. allele
    MTRand_int32 mt;    // create MTRand object
    mt.seed( time(NULL) ); // seed the RNG object
    MTRand mtreal;  // create MTRand object, real value of [0, 1) interval
    mtreal.seed( time(NULL) ); // seed the RNG object

    int pop[ pop_size ][ chromosome_length] = {0}; // un-init pop
    int mating_pool[ pop_size ][ chromosome_length] = {0}; // un-init pop

    int best[ chromosome_length ] = {0}; // un-init pop
    int best_fitness = 0; // var to store best[] fitness value
    int total_fitness = 0;  // var to store total sum of fitness values
    double avg_fitness = 0.0;   // var to store avg fitness value

    int fitness_values[ pop_size ]; // array to store fit value for all ind

    ofstream myfile;
    myfile.open ("output.txt");

    // randomize pop here!----------------------------------------
    for ( int i = 0; i < pop_size ; i++ ) {
        cout << i << ": ";

        for ( int j = 0; j < chromosome_length; j++ ) {
            // get random value 
            //srand ( time(NULL) );     /* initialize random seed */            

            rand_val = 0; // re-init random value
            rand_val = mt() % 2;    /* generate random value - 0 or 1 */

            pop[ i ][ j ] = rand_val;

            //Sleep(1000);

            cout << pop[ i ][ j ];
        } // end for - inner loop

        cout << endl;
    } // end for - outer loop
    cout << endl;

    // randomize pop ENDS!----------------------------------------

    int gen_num = 0; // init gen num
    while ( gen_num < max_generation ) {
        // re-init total & avg fitness values
        total_fitness = 0;
        avg_fitness = 0.0;

        // output best ind here - use (highest) fitness value
        int ind_fitness; // declare var for fitness
        for ( int i = 0; i < pop_size; i++ ) {
            ind_fitness = 0; // re-init fitness value

            for ( int j = 0; j < chromosome_length; j++ ) {              
                ind_fitness = ind_fitness + pop[ i ][ j ];
            } // end for - calc fitness value for one ind

            fitness_values[ i ] = ind_fitness;  // store every ind fitness

            //compare current ind with (currently) best ind here
            if ( ind_fitness > best_fitness ) {
                for ( int k = 0; k < chromosome_length; k++ ) {
                    best[ k ] = pop[ i ][ k ];
                } // end for loop - copy best ind

                best_fitness = ind_fitness; // copy the current fitness into best ind
            } // replace the best ind with current ind

            // output (currently) best ind here - just to have a look!
            cout << gen_num << ":" << i << " Current best ind: ";
            // loop to print out best chromosome
            for ( int m = 0; m < chromosome_length; m++ ) {
                cout << best[ m ];        
            } // end for - print out best chromosome
            cout << gen_num << ":" << i << " Current Best Fitness value: " << best_fitness << endl;

            // summation of ind fitness values
            total_fitness = total_fitness + fitness_values[ i ];

        } // end for - calc fitness value for all ind

        // avg fitness here!
        avg_fitness = total_fitness / pop_size;

        // dump data here!
        myfile << gen_num << " " << best_fitness;
        myfile << " " << avg_fitness << endl;     

        // parent selection----------------------------------------
        int parent1;
        int parent2;
        int selected;
        int z = 0;
        int k = 2;      // tournament selection parameter

        // parent - tournament selection method
        for ( int count = 0; count < pop_size; count++ ) {
            parent1 = mt() % pop_size;  /* generate random value, [0,pop_size) */
            parent2 = mt() % pop_size;  /* generate random value, [0,pop_size) */

            if ( fitness_values[ parent1 ] > fitness_values[ parent2 ] ) {
                selected = parent1;
            } else {
                selected = parent2;
            }

            for ( int m = 0; m < chromosome_length; m++ ) {          
                mating_pool[ z ][ m ] = pop[ selected ][ m ];
            } // end for - copy winner into parent pool

            z = z + 1; // increment parent pool counter
        } // end for - loop each two individual
        // print out parent pop
        // parent selection ENDS----------------------------------

        // xover here----------------------------------------
        int p1[ chromosome_length ]; // temp parent1
        int p2[ chromosome_length ]; // temp parent2
        int offspring[ pop_size ][ chromosome_length ]; // offsprings array
        int j = 0; // counter for index of 2nd parent
        // prob of recombination
        double Pc = 0.6;    // arbitrary threshold value (higher than Pm)
        double prob_recombine;  // var to hold the generated prob

        for ( int i = 0; i < pop_size; i = i + 2) {
            j = i + 1;

            for ( int k = 0; k < chromosome_length; k++ ) {
                p1[ k ] = mating_pool[ i ][ k ];
                p2[ k ] = mating_pool[ j ][ k ]; // p2 gets the next ind
            }

            //copy the chromosome array, i.e. the ind
            for ( int z = 0; z < chromosome_length; z++ ) {
                offspring[ i ][ z ] = p1[ z ];
                offspring[ j ][ z ] = p2[ z ];
            }

            // get random position here for xover   
            // NO NEED to seed RNG object, already hv list of random numbers
            //mt.seed( time(NULL) ); // seed the RNG object         

            xover_position = 0; // re-init pos value
            // NEED to CHECK LOWER VALUE: should be
            // 0 till (chromosome_length - 1)           
            xover_position = mt() % chromosome_length;  /* generate xover position */

            prob_recombine = mtreal();

            // do recombination if prob LESS than threshold!
            if ( prob_recombine < Pc ) {
                for ( int counter = xover_position; counter < chromosome_length - 1; counter++ ) {
                    offspring[ i ][ counter ] = p2[ counter ];
                    offspring[ j ][ counter ] = p1[ counter ];
                } // end for - loop for xover starting from xover_position
            } // else here is just to display, actually no need!
            else {  // DO NOT do recombination if prob GREATER than threshold!
                // maintain offspring i and j values as is!
                cout << gen_num << ":" << i << " did not do recombination" << endl;
            }
        } // end for - loop select 2 parents        
        // xover ENDS----------------------------------------

        // mutate here----------------------------------------
        // prob of mutation
        double Pm = 0.4;    // arbitrary threshold value (lower than Pc)
        double prob_mutate; // var to hold the generated prob

        // go thru ALL ind
        for ( int i = 0; i < pop_size; i++ ) {
            // get random position here 
            // NO NEED to seed RNG object, already hv list of random numbers
            //mt.seed( time(NULL) ); // seed the RNG object         

            mutate_position = 0; // re-init pos value
            mutate_position = mt() % chromosome_length; /* generate position */

            prob_mutate = mtreal();

            // do mutation if prob LESS than threshold!
            if ( prob_mutate < Pm ) {
                // bit-flip the value at position
                if ( offspring[ i ][ mutate_position ] == 0 ) {
                    offspring[ i ][ mutate_position ] = 1;
                } else {
                    offspring[ i ][ mutate_position ] = 0;
                } // end if-else bit-flip
            } // else here is just to display, actually no need!
            else { // DO NOT do mutation if prob GREATER than threshold!
                cout << gen_num << ":" << i << " did not do mutation" << endl;
            }
        } // end for - mutate all ind
        // mutate ENDS----------------------------------------

        // survivor select here----------------------------------------
        // since offspring is NOT in pop[][],
        // so MUST write back into pop[][]
        for ( int i = 0; i < pop_size; i++ ) {
            for ( int j = 0; j < chromosome_length; j++ ) {
                pop[ i ][ j ] = offspring[ i ][ j ];
            } // end for - loop each ind 
        } // end for -loop write offspring into pop
        // survivor select ENDS----------------------------------------

        gen_num = gen_num + 1; // increment gen num
    } // end while loop - GA finish

    // output end best ind here
    // output (the final) best ind here - just to have a look!
    cout << "FINAL best ind: ";
    // loop to print out best chromosome
    for ( int j = 0; j < chromosome_length; j++ ) {
        cout << best[ j ];        
    } // end for - print out best chromosome

    cout << " FINAL Fitness value: " << best_fitness << endl;

    myfile.close();

    return 0;
} // end main

Hai i need help in C++

What help you want with the code you posted. We don't compile your code, you have to specify the error you face.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.