int main(int argc, char **argv){ long i; int j; int swap; int rndpos; double threshold1, threshold2, threshold3; double sd1, sd2, sd3; if(argc < 2){ fprintf(stderr, "Not enough parameter\n"); return(2); } readtable(argv[1]); initrnd(); copy2vec(); sd(&threshold1, &threshold2, &threshold3); printf("cycle\tdiff1\tdiff2\tdiff3\tviolate\n"); for(i = 0; i < SIMNUM; i++){ sd(&sd1, &sd2, &sd3); if(sd1 <= threshold1 && sd2 <= threshold2 && sd3 <= threshold3){ printf("%ld\t%f\t%f\t%f\t1\n", i, sd1, sd2, sd3); } else{ printf("%ld\t%f\t%f\t%f\t0\n", i, sd1, sd2, sd3); } /* Knuth shuffle in three groups */ for(j = 0; j < count; j++){ rndpos = getint(j, count - 1); swap = shuffled[j]; shuffled[j] = shuffled[rndpos]; shuffled[rndpos] = swap; rndpos = getint(count, count * 2 - 1); swap = shuffled[count + j]; shuffled[count + j] = shuffled[rndpos]; shuffled[rndpos] = swap; rndpos = getint(count * 2, count * 3 - 1); swap = shuffled[count * 2 + j]; shuffled[count * 2 + j] = shuffled[rndpos]; shuffled[rndpos] = swap; } } free(vector); free(shuffled); return(EXIT_SUCCESS); }
void CRex::vInitialize( int iGenerationNum, int iGenNum, int iGenVectorData, int iParentNumberData, int iChildrenNumberData ) { int i; CRexException cre; // 親の生成数を設定します。 iParentNumber = iParentNumberData > iGenNum ? iGenNum : iParentNumberData; // 子供の生成数を設定します。 iChildrenNumber = iChildrenNumberData; // 実数値GAの初期化を実行します。 CRealCodedGa::vInitialize( iGenerationNum, iGenNum, iGenVectorData ); try { // 親選択用配列です。 piParentLoc = new int[iGenNum]; if( piParentLoc == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } // 子供のデータを作成します。 pplfChildren = new double*[iChildrenNumber]; if( pplfChildren == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } for( i = 0;i < iChildrenNumber; i++ ) { pplfChildren[i] = new double[iGenVector]; if( pplfChildren[i] == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } } // REX計算用ベクトルデータ一時保存変数です。 plfTempVector = new double[iGenVector]; if( plfTempVector == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } // 重心の計算結果を保持します。 plfCentroid = new double[iGenVector]; if( plfCentroid == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } plfChildVector = new double[iGenVector]; if( plfChildVector == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } plfNormalizeRand = new double[iParentNumber]; if( plfNormalizeRand == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } plfChildrenCentroid = new double[iGenVector]; if( plfChildrenCentroid == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } plfUpperEvalChildrenCentroid = new double[iGenVector]; if( plfUpperEvalChildrenCentroid == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } plfCentroidSteep = new double[iGenVector]; if( plfCentroidSteep == NULL ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } for( i = 0;i < iChildrenNumber; i++ ) { memset( pplfChildren[i], 0, iGenVector*sizeof(double) ); } for( i = 0;i < iGenVector; i++ ) { plfCentroid[i] = 0.0; plfChildVector[i] = 0.0; plfTempVector[i] = 0.0; plfUpperEvalChildrenCentroid[i] = 0.0; plfChildrenCentroid[i] = 0.0; plfCentroidSteep[i] = 0.0; } for( i = 0;i < iParentNumber; i++ ) { plfNormalizeRand[i] = 0.0; piParentLoc[i] = i; } for( i = 0;i < iGenNum; i++ ) { piParentLoc[i] = i; } initrand( (unsigned long)time(NULL) ); initrnd(); } catch( std::bad_alloc ba ) { cre.SetErrorInfo( REX_MEMORY_ALLOCATE_ERROR, "vInitialize", "CRex", "メモリ確保に失敗しました。", __LINE__ ); throw( cre ); } catch( ... ) { cre.SetErrorInfo( REX_ARRAY_INDEX_ERROR, "vInitialize", "CRex", "配列範囲外を参照しました。", __LINE__ ); throw( cre ); } }