int get_tree_size(struct node *tree_node) { int i = 0; if (tree_node == NULL) return 0; i= 1 + get_tree_size(tree_node -> left) + get_tree_size(tree_node -> right); return i; }
void mutate (int j) { int p = tournament_selection(); population[j].root = copy_tree(population[p].root); int ind = (rand() % get_tree_size(population[j].root)) + 1; struct node *n_node = NULL; struct node *e_node = NULL; if ( ind == 1 ) { destroy_tree(population[j].root); population[j].root = create_random_node(); population[j].age = 0; } else { int i = 0; n_node = find_node(ind,&i,population[j].root); e_node = create_random_node(); destroy_tree(n_node -> left); destroy_tree(n_node -> right); n_node -> key_type = e_node -> key_type; n_node -> key_value = e_node -> key_value; n_node -> left = e_node -> left; n_node -> right = e_node -> right; free(e_node); } }
double generation_individual_update() { int i = 0; int best_index = 0; double best_so_far = 100000; for(i=0 ; i < population_size ; i++) { if (changed[i] == True) { population[i].fitness = evaluate_on_test(i); population[i].size = get_tree_size(population[i].root); changed[i] = False; } population[i].age ++; if (population[i].fitness < best_so_far) { best_so_far = population[i].fitness; best_index = i; } } strcpy(equation_text,""); get_the_equation_text(population[best_index].root); printf("Best Solution: %s\n",equation_text); printf("The Fitness : %f\n",best_so_far); return best_so_far; }
/* in : - tree is the tree (or subtree) you want to know the number of non-leaf nodes in out : - the subtree size */ int get_tree_size(const struct node_t *tree) { int ret = 0; int i; if(tree->nb_children == 0) return 0; for(i = 0; i < tree->nb_children; ++i) /*again use recursion to test each node*/ ret += get_tree_size(tree->children[i]); return ret+1; }
void initialize_population() { int i; for(i=0 ; i < population_size ; i++) { population[i].root = create_random_node(); population[i].size = get_tree_size(population[i].root); population[i].age = 0; population[i].cost = 0; population[i].fitness = 0; changed[i] = True; dominated[i] = False; } }
double generation_individual_update() { int i = 0; int best_index = 0; double best_so_far = 100000; for(i=0 ; i < population_size ; i++) { if (changed[i] == True) { population[i].fitness = evaluate_on_test(i); population[i].size = get_tree_size(population[i].root); changed[i] = False; } population[i].age ++; if (population[i].fitness < best_so_far) { best_so_far = population[i].fitness; best_index = i; } } return best_so_far; }
int Zoltan_PHG_2ways_hyperedge_partition ( ZZ *zz, /* Input : Zoltan data structure */ HGraph *hg, Partition parts, Zoltan_PHG_Tree *tree, struct Zoltan_DD_Struct * gnoToGID, struct Zoltan_DD_Struct **dd, int *numParts, int **sizeParts ) { int ierr = ZOLTAN_OK; char *yo = "Zoltan_PHG_2ways_hyperedge_partition"; int nEdge, hEdge; int *interval; int *partnumber = NULL; int tree_size; int *rowpart =NULL; int *rowGNO = NULL; ZOLTAN_ID_PTR rowGID=NULL; int index; int offset; ZOLTAN_TRACE_ENTER(zz, yo); nEdge = hg->nEdge; fprintf (stderr, "HG (%d %d x %d) : %d %d\n", hg->comm->myProc, hg->comm->myProc_x, hg->comm->myProc_y, hg->nVtx, nEdge); interval = (int*)ZOLTAN_MALLOC(nEdge*2*sizeof(int)); if ((nEdge > 0 ) && (interval == NULL)) MEMORY_ERROR; tree_size = get_tree_size(tree) + 1; for (index = 0 ; index < nEdge ; ++index){ SET_MIN_NODE(interval, index, tree_size); SET_MAX_NODE(interval, index, -1); } /* Update interval with the local knowledge */ /* XXX: I loop on the hyperedges, as I think it's more cache friendly * and it allows me to discoupled the computation if a non blocking MPI_Reduce is * available */ for (hEdge = 0 ; hEdge < nEdge ; ++hEdge){ int part; int max = -1; /* Trick : we use the initialized values */ int min = tree_size + 1; for (index = hg->hindex[hEdge] ; index < hg->hindex[hEdge+1] ; ++ index) { part = parts[hg->hvertex[index]]; max = MAX(max, part); min = MIN(min, part); } SET_MIN_NODE(interval, hEdge, min); SET_MAX_NODE(interval, hEdge, max); } /* Update results to view the complete hyperedges */ Zoltan_AllReduceInPlace (interval, 2*nEdge, MPI_INT, MPI_MAX, hg->comm->row_comm); /* Now I have to compute the partition of hyperedges according to the "interval" * and the tree */ /* First, compute the partition number corresponding to the nodes in the tree */ partnumber = compute_part_number(tree); if (partnumber == NULL) { ierr = ZOLTAN_FATAL; goto End; } (*numParts) = get_tree_size(tree); rowpart = (int*) ZOLTAN_MALLOC(nEdge*sizeof(int)); if ((nEdge > 0) && (rowpart == NULL)) MEMORY_ERROR; rowGNO = (int*) ZOLTAN_MALLOC(nEdge*sizeof(int)); if ((nEdge > 0) && (rowGNO == NULL)) MEMORY_ERROR; (*sizeParts) = (int*)ZOLTAN_CALLOC((*numParts), sizeof(int)); if (*numParts && (*sizeParts) == NULL) MEMORY_ERROR; offset = hg->dist_y[hg->comm->myProc_y]; /* Then we search we is the hyperedge in the tree */ for (hEdge = 0 ; hEdge < nEdge ; ++hEdge) { int node; node = find_interval_in_tree(tree, interval+2*hEdge); rowpart[hEdge] = partnumber[node]; (*sizeParts)[rowpart[hEdge]] ++; rowGNO[hEdge] = EDGE_LNO_TO_GNO(hg, hEdge); #if 0 fprintf (stderr, "%d : %d (%d : %d - %d)\n", rowGNO[hEdge], rowpart[hEdge], node, -interval[2*hEdge], interval[2*hEdge+1]); #endif } partnumber += 1; ZOLTAN_FREE(&partnumber); ZOLTAN_FREE(&interval); /* Compute number of elements per parts */ /* TODO: support processor which are not part of the distribution */ /* Update results to view the complete hyperedges */ Zoltan_AllReduceInPlace ((*sizeParts), (*numParts), MPI_INT, MPI_SUM, hg->comm->col_comm); /* Export results to data directory */ /* First, get the GIDs of our edges */ rowGID = ZOLTAN_MALLOC_GID_ARRAY(zz, nEdge); if (nEdge && rowGID == NULL) MEMORY_ERROR; ierr = Zoltan_DD_Find (gnoToGID , (ZOLTAN_ID_PTR)rowGNO, rowGID, NULL, NULL, nEdge, NULL); ZOLTAN_FREE(&rowGNO); ierr = Zoltan_DD_Create (dd, zz->Communicator, zz->Num_GID, 1, 0, nEdge, 0); CHECK_IERR; /* Make our new numbering public */ Zoltan_DD_Update (*dd, (ZOLTAN_ID_PTR)rowGID, (ZOLTAN_ID_PTR) rowpart, NULL, NULL, nEdge); #ifdef CEDRIC_PRINT for (hEdge = 0 ; hEdge < nEdge ; ++hEdge) { fprintf (stderr, "%d : %d\n", rowGID[hEdge], rowpart[hEdge]); } #endif End: ZOLTAN_FREE(&rowGID); ZOLTAN_FREE(&rowGNO); ZOLTAN_FREE(&rowpart); if (partnumber != NULL) partnumber += 1; ZOLTAN_FREE(&partnumber); ZOLTAN_FREE(&interval); ZOLTAN_TRACE_EXIT(zz, yo); return (ierr); }