为什么我的程序打印相同的值? [英] Why my program print the same value?

查看:51
本文介绍了为什么我的程序打印相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,用于在5维中打印De Jong 1函数的不同值,但是当我编译它时,程序返回相同的值。首先,我生成一个二元向量,然后将其分为5个代表xi的向量。我使用函数BinToDec计算每个二进制向量的十进制值,然后我用函数nr_interval表示区间[-5.12,5.12]中的值。



我尝试过:



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:

#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;
}

推荐答案

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里显示你的代码是什么正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。



单位测试也是一个好主意。

单元测试 - 维基百科 [ ^ ]
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[^]


您应该使用调试器来查找问题。我最好的猜测是
You should use the debugger to find the problem. My best guess is
{val += p;}

因为pp没有改变。



提示:输出每个函数的结果。



为什么不这样;

because pp isnt changed.

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

why not this way;

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;
}


这篇关于为什么我的程序打印相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆