⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.cpp

📁 这是遗传算法的源代码
💻 CPP
字号:
// -*- c++ -*-////  File:         utility.cpp////  Description:  utility functions and procedures.////  Author:       Fernando Lobo////  Date:         June/1999////  Extended to deal with chi-ary problems by Luis de la Ossa//  GCC 3.4 and 4 series compliance by Kumara Sastry ////  Date:         March/2006  #include <iostream>#include <fstream>#include <stdlib.h>#include <math.h>#include "utility.hpp"#include "random.hpp"#include "parameter.hpp"extern randomG RANDOM;// outputs an error msg and stops the program void error( char *msg ){  std::cout << msg << std::endl;  exit(1);}// if 'condition' is false, print an error message and abort the programvoid errorcheck( char *str, bool condition ){  if( !condition ) {    std::cout << "ERROR: " << str << std::endl;    exit( 1 );  }}double sqr( double x ){  return x*x;}int min( int x, int y ){  if (x<y) return x;  else return y;}int max( int x, int y ){  if (x>y) return x;  else return y;}// Decode vector of integers as unsigned integer given their// positions and thus their ranges.long decode( int *vec, int* positions, int length ){  int j;  long accum, powerofbase;  accum = 0; powerofbase = 1;  for( j = length-1; j>= 0; j-- ) {    accum += powerofbase*vec[j];    powerofbase *= parameter::ranges[positions[j]];  }  return accum;}// Encode x given the size of the array and the positions of the genes.void encode( int x, int length, int *vec, int* positions ){  int i;  for( i=0; i<length; i++ ) vec[i] = 0;  i = length-1;  while( x != 0 && i >= 0 ) {    vec[i] = x%parameter::ranges[positions[i]];    x = x/parameter::ranges[positions[i]];    i--;  }}// Encodes a number in base 2.  void encodebin( int x, int nsymbols, int *vec ){  int base = 2;  int i;  for( i=0; i<nsymbols; i++ ) vec[i] = 0;  i = nsymbols-1;  while( x != 0 && i >= 0 ) {    vec[i] = x%base;    x = x/base;    i--;  }}   // return log x base bdouble log( double x, double b ){  return log(x)/log(b); }// merge arrays A and B into array C.// both A and B are sorted.// C will be sorted at the end.//void mergeSort( int *A, int sizeA, int *B, int sizeB, int *C ){  int sizeC = sizeA + sizeB;  int a=0,b=0,c=0;  while( c < sizeC ) {    if( a >= sizeA || b >= sizeB ) break;    if( A[a] < B[b] ) {      C[c] = A[a];      a++;    }    else {      C[c] = B[b];      b++;    }    c++;  }  if( a >= sizeA )    // copy elements from B    for( int i=b; i< sizeB; i++ ) {      C[c] = B[i];      c++;    }  if( b >= sizeB )    // copy elements from A    for( int i=a; i< sizeA; i++ ) {      C[c] = A[i];      c++;    }}  // make a random n-permutation of the numbers 0,1,2,...,n-1void makeshuffle( int *shufflearray, const int n ){  int i;  // initialize  for( i=0; i<n; i++ ) shufflearray[i] = i;  // shuffle  for( i=0; i<n-1; i++ ) {    int other = RANDOM.uniform( i, n-1 );    // swap( shufflearray[other], shufflearray[i] );       int temp = shufflearray[other];    shufflearray[other] = shufflearray[i];    shufflearray[i] = temp;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -