//Move misclassified nodes from source graph (src) to destiny graph (dst) void opf_MoveMisclassifiedNodes(Subgraph **src, Subgraph **dst, int *p){ int i, j, k, num_of_misclassified=0; Subgraph *newsrc = NULL, *newdst = NULL; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].truelabel != (*src)->node[i].label) num_of_misclassified++; } *p = num_of_misclassified; if (num_of_misclassified>0){ newsrc = CreateSubgraph((*src)->nnodes-num_of_misclassified); newdst = CreateSubgraph((*dst)->nnodes+num_of_misclassified); newsrc->nfeats = (*src)->nfeats; newdst->nfeats = (*dst)->nfeats; newsrc->nlabels = (*src)->nlabels; newdst->nlabels = (*dst)->nlabels; for (i = 0; i < (*dst)->nnodes; i++) CopySNode(&(newdst->node[i]), &((*dst)->node[i]), newdst->nfeats); j=i; k = 0; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].truelabel == (*src)->node[i].label)// misclassified node CopySNode(&(newsrc->node[k++]), &((*src)->node[i]), newsrc->nfeats); else CopySNode(&(newdst->node[j++]), &((*src)->node[i]), newdst->nfeats); } DestroySubgraph(&(*src)); DestroySubgraph(&(*dst)); *src = newsrc; *dst = newdst; } }
//read subgraph from opf format file Subgraph *ReadSubgraph(char *file){ Subgraph *g = NULL; FILE *fp = NULL; int nnodes, i, j; char msg[256]; size_t result; if((fp = fopen(file, "rb")) == NULL){ sprintf(msg, "%s%s", "Unable to open file ", file); Error(msg,"ReadSubGraph"); } /*reading # of nodes, classes and feats*/ result = fread(&nnodes, sizeof(int), 1, fp); g = CreateSubgraph(nnodes); result = fread(&g->nlabels, sizeof(int), 1, fp); result = fread(&g->nfeats, sizeof(int), 1, fp); /*reading features*/ for (i = 0; i < g->nnodes; i++){ g->node[i].feat = AllocFloatArray(g->nfeats); result = fread(&g->node[i].position, sizeof(int), 1, fp); result = fread(&g->node[i].truelabel, sizeof(int), 1, fp); for (j = 0; j < g->nfeats; j++) result = fread(&g->node[i].feat[j], sizeof(float), 1, fp); } fclose(fp); return g; }
//retorna apenas um classificador sem nenhuma amostra void empty_ensemble(Subgraph *g, Subgraph *gTraining, int posicao,int k, Subgraph **out){ //Subgraph *out = (Subgraph *)calloc(k, sizeof(Subgraph)); int i,j; //Subamostragem int n = g->nnodes; int m = (n/2)/k; printf("alocar\n"); out[posicao] = CreateSubgraph(m);//cria um novo out[posicao]->nfeats = g->nfeats;//copia o numero dos atributos out[posicao]->nlabels = g->nlabels;//copia o numero de rotulos for (j = 0; j <m; j++)//aloca a quantidade de atributos out[posicao]->node[j].feat = AllocFloatArray(out[posicao]->nfeats); printf("Novo grafo vazio criado!\n"); }
// Remove irrelevant nodes void opf_RemoveIrrelevantNodes(Subgraph **sg){ Subgraph *newsg = NULL; int i,k,num_of_irrelevants=0; for (i=0; i < (*sg)->nnodes; i++) { if (!(*sg)->node[i].relevant) num_of_irrelevants++; } if (num_of_irrelevants>0){ newsg = CreateSubgraph((*sg)->nnodes - num_of_irrelevants); newsg->nfeats = (*sg)->nfeats; // for (i=0; i < newsg->nnodes; i++) // newsg->node[i].feat = AllocFloatArray(newsg->nfeats); k=0; newsg->nlabels = (*sg)->nlabels; for (i=0; i < (*sg)->nnodes; i++){ if ((*sg)->node[i].relevant){// relevant node CopySNode(&(newsg->node[k]), &((*sg)->node[i]), newsg->nfeats); k++; } } newsg->nlabels=(*sg)->nlabels; DestroySubgraph(sg); *sg=newsg; } }
//Move irrelevant nodes from source graph (src) to destiny graph (dst) void opf_MoveIrrelevantNodes(Subgraph **src, Subgraph **dst){ int i, j, k, num_of_irrelevants=0; Subgraph *newsrc = NULL, *newdst = NULL; for (i=0; i < (*src)->nnodes; i++){ if (!(*src)->node[i].relevant) num_of_irrelevants++; } if (num_of_irrelevants>0){ newsrc = CreateSubgraph((*src)->nnodes-num_of_irrelevants); newdst = CreateSubgraph((*dst)->nnodes+num_of_irrelevants); newsrc->nfeats = (*src)->nfeats; newdst->nfeats = (*dst)->nfeats; newsrc->nlabels = (*src)->nlabels; newdst->nlabels = (*dst)->nlabels; // for (i=0; i < newsrc->nnodes; i++) // newsrc->node[i].feat = AllocFloatArray(newsrc->nfeats); // for (i=0; i < newdst->nnodes; i++) // newdst->node[i].feat = AllocFloatArray(newdst->nfeats); for (i = 0; i < (*dst)->nnodes; i++) CopySNode(&(newdst->node[i]), &((*dst)->node[i]), newdst->nfeats); j=i; k = 0; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].relevant)// relevant node CopySNode(&(newsrc->node[k++]), &((*src)->node[i]), newsrc->nfeats); else CopySNode(&(newdst->node[j++]), &((*src)->node[i]), newdst->nfeats); } DestroySubgraph(&(*src)); DestroySubgraph(&(*dst)); *src = newsrc; *dst = newdst; } }
//read subgraph from opf model file Subgraph *opf_ReadModelFile(char *file){ Subgraph *g = NULL; FILE *fp = NULL; int nnodes, i, j; char msg[256]; size_t result; if((fp = fopen(file, "rb")) == NULL){ sprintf(msg, "%s%s", "Unable to open file ", file); Error(msg,"ReadSubGraph"); } /*reading # of nodes, classes and feats*/ result = fread(&nnodes, sizeof(int), 1, fp); g = CreateSubgraph(nnodes); result = fread(&g->nlabels, sizeof(int), 1, fp); result = fread(&g->nfeats, sizeof(int), 1, fp); /*reading df*/ result = fread(&g->df, sizeof(float), 1, fp); // for supervised opf by pdf result = fread(&g->K, sizeof(float), 1, fp); result = fread(&g->mindens, sizeof(float), 1, fp); result = fread(&g->maxdens, sizeof(float), 1, fp); /*reading features*/ for (i = 0; i < g->nnodes; i++){ g->node[i].feat = (float *)malloc(g->nfeats*sizeof(float)); result = fread(&g->node[i].position, sizeof(int), 1, fp); result = fread(&g->node[i].truelabel, sizeof(int), 1, fp); result = fread(&g->node[i].pred, sizeof(int), 1, fp); result = fread(&g->node[i].label, sizeof(int), 1, fp); result = fread(&g->node[i].pathval, sizeof(float), 1, fp); result = fread(&g->node[i].radius, sizeof(float), 1, fp); for (j = 0; j < g->nfeats; j++){ result = fread(&g->node[i].feat[j], sizeof(float), 1, fp); } } for (i = 0; i < g->nnodes; i++){ result = fread(&g->ordered_list_of_nodes[i], sizeof(int), 1, fp); } fclose(fp); return g; }
//Merge two subgraphs Subgraph *opf_MergeSubgraph(Subgraph *sg1, Subgraph *sg2){ if(sg1->nfeats != sg2->nfeats) Error("Invalid number of feats!","MergeSubgraph"); Subgraph *out = CreateSubgraph(sg1->nnodes+sg2->nnodes); int i = 0, j; if(sg1->nlabels > sg2->nlabels) out->nlabels = sg1->nlabels; else out->nlabels = sg2->nlabels; out->nfeats = sg1->nfeats; for (i = 0; i < sg1->nnodes; i++) CopySNode(&out->node[i], &sg1->node[i], out->nfeats); for (j = 0; j < sg2->nnodes; j++){ CopySNode(&out->node[i], &sg2->node[j], out->nfeats); i++; } return out; }
// Copy subgraph (does not copy Arcs) Subgraph *CopySubgraph(Subgraph *g){ Subgraph *clone = NULL; int i; if(g != NULL){ clone = CreateSubgraph(g->nnodes); clone->bestk = g->bestk; clone->df = g->df; clone->nlabels = g->nlabels; clone->nfeats = g->nfeats; clone->mindens = g->mindens; clone->maxdens = g->maxdens; clone->K = g->K; for(i=0; i< g->nnodes; i++){ CopySNode(&clone->node[i], &g->node[i], g->nfeats); clone->ordered_list_of_nodes[i] = g->ordered_list_of_nodes[i]; } return clone; }else return NULL; }
// Split subgraph into two parts such that the size of the first part // is given by a percentual of samples. void opf_SplitSubgraph(Subgraph *sg, Subgraph **sg1, Subgraph **sg2, float perc1){ int *label=AllocIntArray(sg->nlabels+1),i,j,i1,i2; int *nelems=AllocIntArray(sg->nlabels+1),totelems; srandom((int)time(NULL)); for (i=0; i < sg->nnodes; i++) { sg->node[i].status = 0; label[sg->node[i].truelabel]++; } for (i=0; i < sg->nnodes; i++) { nelems[sg->node[i].truelabel]=MAX((int)(perc1*label[sg->node[i].truelabel]),1); } free(label); totelems=0; for (j=1; j <= sg->nlabels; j++) totelems += nelems[j]; *sg1 = CreateSubgraph(totelems); *sg2 = CreateSubgraph(sg->nnodes-totelems); (*sg1)->nfeats = sg->nfeats; (*sg2)->nfeats = sg->nfeats; for (i1=0; i1 < (*sg1)->nnodes; i1++) (*sg1)->node[i1].feat = AllocFloatArray((*sg1)->nfeats); for (i2=0; i2 < (*sg2)->nnodes; i2++) (*sg2)->node[i2].feat = AllocFloatArray((*sg2)->nfeats); (*sg1)->nlabels = sg->nlabels; (*sg2)->nlabels = sg->nlabels; i1=0; while(totelems > 0){ i = RandomInteger(0,sg->nnodes-1); if (sg->node[i].status!=NIL){ if (nelems[sg->node[i].truelabel]>0){// copy node to sg1 (*sg1)->node[i1].position = sg->node[i].position; for (j=0; j < (*sg1)->nfeats; j++) (*sg1)->node[i1].feat[j]=sg->node[i].feat[j]; (*sg1)->node[i1].truelabel = sg->node[i].truelabel; i1++; nelems[sg->node[i].truelabel] = nelems[sg->node[i].truelabel] - 1; sg->node[i].status = NIL; totelems--; } } } i2=0; for (i=0; i < sg->nnodes; i++){ if (sg->node[i].status!=NIL){ (*sg2)->node[i2].position = sg->node[i].position; for (j=0; j < (*sg2)->nfeats; j++) (*sg2)->node[i2].feat[j]=sg->node[i].feat[j]; (*sg2)->node[i2].truelabel = sg->node[i].truelabel; i2++; } } free(nelems); }
exit(-1); } SearchSpace *s = NULL; int i, j, iteration = atoi(argv[4]), m_train, n_train, m_test, n_test; double alpha, errorTrain, errorTest, *w = NULL; double **XTrain = NULL, **XTest = NULL; double *YTrain = NULL, *YTest = NULL; Subgraph *Train = NULL, *Test = NULL; FILE *f = NULL; LoadData(argv[1], &XTrain, &YTrain, &m_train, &n_train); LoadData(argv[1], &XTest, &YTest, &m_test, &n_test); /* mapping training data to LibOPF format */ Train = CreateSubgraph(m_train); Train->nfeats = n_train; Train->nlabels = 2; for (i = 0; i < m_train; i++) { Train->node[i].feat = AllocFloatArray(n_train); for (j = 0; j < n_train; j++) Train->node[i].feat[j] = XTrain[i][j]; Train->node[i].truelabel = YTrain[i]; } /* mapping testing data to LibOPF format */ Test = CreateSubgraph(m_test); Test->nfeats = n_test; Test->nlabels = 2; for (i = 0; i < m_test; i++)