35 #include <sys/timeb.h>
56 setMethodParameterNumber(3);
57 std::vector <COptAlgorithmParameter> & AlgorithmParameters = getMethodParameters();
59 AlgorithmParameters.resize(3);
61 AlgorithmParameters[0].setName(
"Population Size");
62 AlgorithmParameters[0].setValue(100);
64 AlgorithmParameters[1].setName(
"Generation Number");
65 AlgorithmParameters[1].setValue(100.0);
67 AlgorithmParameters[2].setName(
"Mutation Variance");
68 AlgorithmParameters[2].setValue(0.1);
82 std::vector <COptAlgorithmParameter> &AlgmParams = getMethodParameters();
84 for (i = 0; i < getMethodParameterNumber(); i++)
86 if (AlgmParams[i].getName() ==
"Population Size")
87 mPopSize =
unsigned(AlgmParams[i].getValue());
88 else if (AlgmParams[i].getName() ==
"Generation Number")
89 mGener =
unsigned(AlgmParams[i].getValue());
90 else if (AlgmParams[i].getName() ==
"Mutation Variance")
91 mMutVar = AlgmParams[i].getValue();
121 mIndV[0][i] = 1.1 + dr250();
128 catch (
unsigned int e)
289 struct timeb init_time;
291 r250_init(init_time.millitm);
335 fitness = fitness0 + pow(
mIndV[i][j], 4.0) - 16.0 * pow(
mIndV[i][j], 2.0) + 5.0 *
mIndV[i][j];
339 fitness = fitness0 / 2.0;
341 catch (
unsigned int e)
404 mut =
mMin + std::numeric_limits< C_FLOAT64 >::epsilon();
413 mut =
mMax - std::numeric_limits< C_FLOAT64 >::epsilon();
430 void CGA::crossover(
unsigned int p1,
unsigned int p2,
unsigned int c1,
unsigned int c2)
433 unsigned int pp1, pp2, tmp, l;
464 mCrp[i] = 1 + mCrp[i - 1] + (l == 0 ? 0 : r250n(l));
489 for (j = s; j < e; j++)
496 catch (
unsigned int e)
502 unsigned int i, a, b, tmp;
507 for (i = 0; i < mPopSize / 2; i++)
541 unsigned int i, j, nopp, opp;
552 copy(i, i - mPopSize);
570 for (j = 0; j < nopp; j++)
573 opp = r250n(mPopSize * 2);
582 for (j = i + 1; j < 2*
mPopSize; j++)
620 for (i = l; i < u; i++)
631 mMin = std::numeric_limits< C_FLOAT64 >::epsilon();
647 mIndV[i][j] =
mMin * pow(10.0, la * dr250());
649 catch (
unsigned int e)
660 catch (
unsigned int e)
682 std::cout <<
"#" << i <<
"\t" <<
mCandX[
mBest] << std::endl;
698 unsigned int i, last_update, u100, u300, u500;
701 struct timeb before, after;
711 std::cout <<
"mMin = " <<
mMin <<
", mMax= " <<
mMax <<
", mParamNum = " <<
mParamNum << std::endl;
712 std::cout <<
"mRealProblem.getParameterNum() = " <<
mRealProblem.getParameterNum() << std::endl;
737 u100 = u300 = u500 = 0;
740 std::cout << std::endl;
741 std::cout <<
"Initial populaiton has successfully created!!!!!" << std::endl;
746 std::cout <<
"-----------------------------best result at each generation---------------------" << std::endl;
747 std::cout <<
"Generation\t" <<
"Best candidate value for object function\t" <<
"Display " <<
mParamNum <<
" parameters" << std::endl;
748 std::cout << std::endl;
756 std::cout << std::endl;
757 std::cout <<
"GA is processing at generation " << i << std::endl;
784 if ((u500 == 0) && (i - last_update > 500))
792 else if ((u300 == 0) && (i - last_update > 300))
794 creation(
unsigned(psize*0.7), psize);
800 else if ((u100 == 0) && (i - last_update > 100))
802 creation(
unsigned(psize*0.9), psize);
808 std::ofstream tout(
"time.dat");
812 std::cout <<
" tout cannot output!" << std::endl;
816 tout <<
"CPU's Calculation Time [ms]:" << 1000*(after.time - before.time) + (after.millitm - before.millitm) << std::endl;
817 tout <<
" It has taken about " << time(NULL) - dTime <<
" seconds!" << std::endl;
820 std::cout << std::endl;
821 std::cout <<
"GA has successfully done!" << std::endl;
822 std::cout <<
" It has taken about " << time(NULL) - dTime <<
" seconds!" << std::endl;
823 std::cout <<
"and it is ready to exit now!" << std::endl;
void dumpData(unsigned int i)
void setGeneration(int num)
double getBestCandidate()
void mutate(unsigned int i)
qreal linear(qreal a, qreal b, qreal t)
void setCandx(int i, double num)
void setBest(unsigned int num)
CGA & operator=(const CGA &source)
double evaluate(unsigned int i)
void swap(unsigned int o, unsigned int d)
CRealProblem mRealProblem
void setIndv(int i, int j, double num)
void creation(unsigned int l, unsigned int u)
void initFirstGeneration()
unsigned int fittest(void)
void crossover(unsigned int p1, unsigned int p2, unsigned int c1, unsigned int c2)
void setParamNum(int num)
void setRealProblem(CRealProblem &aProb)
unsigned int getGeneration()
void copy(unsigned int o, unsigned int d)
void setMutVar(double num)