// Expected value using row rearrangement //[[Rcpp::export]] arma::mat Expected(const arma::mat& grid, const arma::mat& value, const arma::cube& disturb, const arma::vec& weight) { // Passing R objects to C++ const std::size_t n_grid = grid.n_rows; const std::size_t n_dim = grid.n_cols; const std::size_t n_disturb = disturb.n_slices; // Computing the continuation value function arma::mat continuation(n_grid, n_dim, arma::fill::zeros); arma::mat d_value(n_grid, n_dim); for (std::size_t dd = 0; dd < n_disturb; dd++) { d_value = value * disturb.slice(dd); continuation += weight(dd) * Optimal(grid, d_value); } return continuation; }
int main() { int choice; //以菜单的形式显示出来 while(1) { system("cls");//清屏 manu(); scanf("%d",&choice); switch(choice) { case 1:FIFO();break; case 2:LRU();break; case 3:Optimal();break; case 0:exit(0); default:printf("您的输入有误,请重新输入\n"); } system("pause"); } return 0; }
int main() { int icount = 0; //指示 int gencount = 0; int tim; //指示TIME int res = 0; //指示RESPONSE FILE *fp; fp = fopen("TimeSequence.txt","r+t"); while(res < RESPONSE) { for(gencount = 0; gencount < GENENUM; gencount++) { for (tim = 0; tim < TIME; tim++) { fscanf(fp, "%lf", &data[res][gencount][tim]); } } res = res + 1; } fclose(fp); int flag = 0; srand((unsigned)time(NULL)); grpGA grp; pgrpGA pgrp = &grp; grp.generationCount = 0; grp.generationMax = 5000; //遗传算法迭代次数 grp.probCross = 0.9; grp.probMutat = 0.1; grp.size = SIZEGROUP; //种群大小 genGA ind[SIZEGROUP]; pgenGA pind[SIZEGROUP]; for(icount = 0; icount < SIZEGROUP; icount++) { pind[icount] = &ind[icount]; } int u = 0; for(u = 0; u < GENENUM; u++) { printf("%d node learn\n", u); FILE *fp1; fp1 = fopen("Individuals.txt","r+t"); icount = 0; while(icount < SIZEGROUP) { for (gencount = 0; gencount < GENENUM; gencount++) { fscanf(fp1, "%d", &populationMirror[icount][gencount]); } icount = icount + 1; } fclose(fp1); genExpect = 0; for(res = 0; res < RESPONSE; res++) { for(gencount = 0; gencount < GENENUM; gencount++) { genWigh[res][gencount] = 0; } } for(gencount = 0; gencount < GENENUM; gencount++) { genTest[gencount] = 0; genWighMean[gencount] = 0; elite[gencount] = 0; eliteWeight[gencount] = 0; } eliteFitness = 0; do { Initial(pind); ErrorCmp(pind, data, populationMirror, genTest, genWigh, genWighMean, genExpect, &u); Fitness(pind); Optimal(pind, elite, &eliteFitness, populationMirror, genWighMean, eliteWeight); flag = Termination(pgrp); if(flag) { break; } BinaryTourment(pind, pgrp, populationMirror); Crossover(pind, pgrp, populationMirror); Mutation(pind, pgrp, populationMirror); }while(1); ResultOutput(pind, elite, pgrp, eliteWeight); } return 0; }