Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a program to print diffrent values for De Jong 1 function in 5 dimension but when I compile it the program return the same value. First I generate a binary vector then I devide it in 5 vectors which represents xi. I calculate the decimal value for each binary vector with the function BinToDec then I represent the value in the interval [-5.12,5.12] with the funct nr_interval.

What I have tried:

C++
#include <bits/stdc++.h>
using namespace std;
#define CROSSOVER_RATE            0.3
#define MUTATION_RATE             0.01
#define POP_SIZE                  50          
#define CHROMO_LENGTH             150
#define GENE_LENGTH               30

int random_num(int start, int end)
{
    int range = (end-start)+1;
    int random_int = start+(rand()%range);
    return random_int;
}

string  GetRandomBits(int length)
{  srand(time(NULL));
    string bits;
        for (int i=0; i<length; i++)
    {
        if (random_num(0,1) == 1)
 
            bits += "1";
 
        else
 
            bits += "0";
    }
 
    return bits;
}

int BinToDec(string bits)
{    int val = 0;
    int p=1;
    for (int i = bits.length(); i > 0; i--)
    { if (bits.at(i-1) == '1')
        {val += p;}
        p *= 2;
    }
    return val;
}

double nr_interval(int valoare, double a, double b)
{
    double result;
    result=0.0;
    result=a+valoare*((b-a)/(pow(2,30)-1));
    return result;
}

double De_Jong(string bits){
    double x=0.0;
     for(size_t i=0; i<sizeof(bits); i=i+GENE_LENGTH)
     {  x = x + nr_interval(BinToDec(bits.substr(0,i+GENE_LENGTH)),-5.12,5.12)* nr_interval(BinToDec(bits.substr(0,i+GENE_LENGTH)),-5.12,5.12);
         
     }
 return x;   
}

int main(){
     
    for(int x=1; x<=POP_SIZE;x++)
     {  cout<<De_Jong(GetRandomBits(CHROMO_LENGTH));
        cout<<endl;
     }
    //valoare=BinToDec(GetRandomBits(l));
   //cout<<valoare;
    
    return 0;
}
Posted
Updated 1-Dec-18 7:34am

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Unit testing is also a good idea.
Unit testing - Wikipedia[^]
 
Share this answer
 
v2
You should use the debugger to find the problem. My best guess is
C++
{val += p;}
because pp isnt changed.

Tip: make some output of the result of every function.

why not this way;
C++
for(size_t i=0; i<sizeof(bits); i=i+GENE_LENGTH)
{ 
  double nr = nr_interval(BinToDec(bits.substr(0,i+GENE_LENGTH)),-5.12,5.12);
  x = x + nr*nr;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900