bool adjust_dists() { printf("Adjusting distances...\n"); int i; size_t k; double deltabeta = compute_deltabeta(); printf("deltabeta: %.15lf\n", deltabeta); beta += deltabeta; printf("beta: %.15lf\n", beta); deltabeta /= 2.0; double idcol[objc]; for(i = 0; i < objc; ++i) { idcol[i] = 0.0; } bool hasneg = false; for(i = 0; i < objc; ++i) { for(k = 0; k < clustc; ++k) { idcol[i] = 1.0; set(&dists, k, i, get(&dists, k, i) + deltabeta * sqdeuclid_dist(membvec[k].mtx, idcol, objc)); idcol[i] = 0.0; if(!hasneg && dlt(get(&dists, k, i), 0.0)) { hasneg = true; } } } return hasneg; }
void SightLightScene::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event* event) { Point tar(0,0); //光线的端点 Point cur(0,0); //光线与线段的交点 float distance = 0; //光源与交点的距离 _touchDraw->clear(); auto pos = touch->getLocation(); //_touchDraw->drawDot(pos,5,Color4F::RED); //计算极角,并添加两个偏移1e-4的极角 initAngles(pos); //极角排序 std::sort(_angles.begin(), _angles.end(), [](float x,float y){ return x < y; }); std::vector<cocos2d::Vec2> vertex; //找最近的交点 // std::vector<Point> vertex; for (auto angle:_angles) { Vec2 dlt(cos(angle),sin(angle)); float closest = -1; for (auto s:_segments) { if (getIntersection(Line(pos,pos + dlt),s,cur,distance)) { if (closest == -1 || closest > distance) { closest = distance; tar = cur; } } } if (closest != -1) { vertex.push_back(tar); } } //画三角形 //下面2个循环第3个参数可以写为vertex[(i+1) % vertex.size()],合并成1个循环 //但是显然,取余操作效率太低,分开写更好一些 int limit = vertex.size() - 1; for (int i = 0; i < limit; i++) { _touchDraw->drawTriangle(pos, vertex[i], vertex[i + 1], Color4F::WHITE); } if(limit > 0) { _touchDraw->drawTriangle(pos, vertex[limit], vertex[0], Color4F::WHITE); } // 画三角形的边,Debug用 // for(auto v : vertex) { // _touchDraw->drawSegment(pos, v, 0.5f, Color4F::RED); // _touchDraw->drawDot(v, 3, Color4F::RED); // } }
// Computes the adequacy and stores the contribution of each object // into 'parc_obj_adeq'. double adequacy_obj(bool check) { size_t e; size_t i; size_t j; size_t k; double sum; double sumd; double sumw; double adeq = 0.0; for(i = 0; i < objc; ++i) { sum = 0.0; for(k = 0; k < clustc; ++k) { sumw = 0.0; for(j = 0; j < dmatrixc; ++j) { sumd = 0.0; for(e = 0; e < medoids_card; ++e) { sumd += dmatrix[j][i][medoids[k][j][e]]; } sumw += weights[k][j] * sumd; } sum += pow(memb[i][k], mfuz) * sumw; } if(check) { if(dlt(parc_obj_adeq[i], sum)) { printf("Msg: adequacy for object %d is greater " "than previous (%.15lf).\n", i, sum - parc_obj_adeq[i]); } } parc_obj_adeq[i] = sum; adeq += sum; } if(check) { if(dlt(prev_adeq, adeq)) { printf("Msg: overall adequacy is greater than " "previous (%.15lf).\n", adeq - prev_adeq); } } prev_adeq = adeq; return adeq; }
PPI::PPI(const uint8_t *buffer, uint32_t total_sz) { if(total_sz < sizeof(_header)) throw malformed_packet(); std::memcpy(&_header, buffer, sizeof(_header)); if(length() > total_sz || length() < sizeof(_header)) throw malformed_packet(); buffer += sizeof(_header); total_sz -= sizeof(_header); // There are some options const size_t options_length = length() - sizeof(_header); if(options_length > 0) { _data.assign(buffer, buffer + options_length); buffer += options_length; total_sz -= static_cast<uint32_t>(options_length); } if(total_sz > 0) { switch(dlt()) { case DLT_IEEE802_11: #ifdef HAVE_DOT11 parse_80211(buffer, total_sz); #else throw protocol_disabled(); #endif break; case DLT_EN10MB: if(Internals::is_dot3(buffer, total_sz)) inner_pdu(new Dot3(buffer, total_sz)); else inner_pdu(new EthernetII(buffer, total_sz)); break; case DLT_IEEE802_11_RADIO: #ifdef HAVE_DOT11 inner_pdu(new RadioTap(buffer, total_sz)); #else throw protocol_disabled(); #endif break; case DLT_NULL: inner_pdu(new Loopback(buffer, total_sz)); break; case DLT_LINUX_SLL: inner_pdu(new Tins::SLL(buffer, total_sz)); break; } } }
int main() { struct node *p; int f;f=1; p=NULL; append(&p,1); //p is the pointer to the first struct node variable append(&p,2); //every variable has it's own data and the address to he next unnamed dynamically allocated variable of the struct type append(&p,3);append(&p,4); append(&p,6);append(&p,10); addatbeg(&p,1); addafter(&p,8,6); display(p); add(&p,9);add(&p,11); printf(" p->link->link->num= %d ",p->link->link->num); dlt(&p,f); printf("\nAftre deletion of node %d : ",f); display(p); reverse(&p); printf("\nAfter reversal:"); display(p); getch(); return 0; }
void print_weights(st_matrix *weights) { printf("Weights:\n"); size_t j; size_t k; double sum; double val; for(k = 0; k < clustc; ++k) { sum = 0.0; for(j = 0; j < dmatrixc; ++j) { val = get(weights, k, j); if(dlt(val, 0.0)) { printf("!"); } printf("%lf ", val); sum += val; } printf("[%lf]", sum); if(!deq(sum, 1.0)) { printf(" =/= 1.0?\n"); } else { printf("\n"); } } }
int main(int argc, char **argv) { verbose = true; FILE *cfgfile = fopen(argv[1], "r"); if(!cfgfile) { printf("Error: could not open config file.\n"); return 1; } fscanf(cfgfile, "%d", &objc); if(objc <= 0) { printf("Error: objc <= 0.\n"); fclose(cfgfile); return 1; } // reading labels int classc; int labels[objc]; fscanf(cfgfile, "%d", &classc); size_t i; for(i = 0; i < objc; ++i) { fscanf(cfgfile, "%d", &labels[i]); } // reading labels end fscanf(cfgfile, "%d", &dmatrixc); if(dmatrixc <= 0) { printf("Error: dmatrixc <= 0.\n"); fclose(cfgfile); return 1; } char filenames[dmatrixc][BUFF_SIZE]; size_t j; for(j = 0; j < dmatrixc; ++j) { fscanf(cfgfile, "%s", filenames[j]); } char outfilename[BUFF_SIZE]; fscanf(cfgfile, "%s", outfilename); fscanf(cfgfile, "%d", &clustc); if(clustc <= 0) { printf("Error: clustc <= 0.\n"); fclose(cfgfile); return 1; } int insts; fscanf(cfgfile, "%d", &insts); if(insts <= 0) { printf("Error: insts <= 0.\n"); fclose(cfgfile); return 1; } fscanf(cfgfile, "%d", &max_iter); if(insts <= 0) { printf("Error: max_iter <= 0.\n"); fclose(cfgfile); return 1; } fscanf(cfgfile, "%lf", &epsilon); if(dlt(epsilon, 0.0)) { printf("Error: epsilon < 0.\n"); fclose(cfgfile); return 1; } fscanf(cfgfile, "%lf", &mfuz); if(dlt(mfuz, 1.0)) { printf("Error: mfuz < 1.0.\n"); fclose(cfgfile); return 1; } fscanf(cfgfile, "%lf", &qexp); if(dlt(qexp, 1.0)) { printf("Error: qexp < 1.0.\n"); fclose(cfgfile); return 1; } fclose(cfgfile); freopen(outfilename, "w", stdout); printf("###Configuration summary:###\n"); printf("Number of objects: %d\n", objc); printf("Number of clusters: %d\n", clustc); printf("Number of instances: %d\n", insts); printf("Maximum interations: %d\n", max_iter); printf("Parameter m: %lf\n", mfuz); printf("Parameter q: %lf\n", qexp); printf("############################\n"); st_matrix best_memb; st_matrix best_dists; st_matrix best_weights; // memory allocation start dmatrix = malloc(sizeof(st_matrix) * dmatrixc); for(j = 0; j < dmatrixc; ++j) { init_st_matrix(&dmatrix[j], objc, objc); } init_st_matrix(&memb, objc, clustc); init_st_matrix(&best_memb, objc, clustc); size_t k; membvec = malloc(sizeof(st_matrix) * clustc); global_dmatrix = malloc(sizeof(st_matrix) * clustc); for(k = 0; k < clustc; ++k) { init_st_matrix(&membvec[k], objc, 1); init_st_matrix(&global_dmatrix[k], objc, objc); } init_st_matrix(&dists, clustc, objc); init_st_matrix(&best_dists, clustc, objc); init_st_matrix(&weights, clustc, dmatrixc); init_st_matrix(&best_weights, clustc, dmatrixc); // memory allocation end for(j = 0; j < dmatrixc; ++j) { if(!load_data(filenames[j], &dmatrix[j])) { printf("Error: could not load matrix file.\n"); goto END; } } mfuzval = 1.0 / (mfuz - 1.0); qexpval = 1.0 / (qexp - 1.0); double avg_partcoef; double avg_modpcoef; double avg_partent; double avg_aid; st_matrix dists_t; init_st_matrix(&dists_t, dists.ncol, dists.nrow); st_matrix agg_dmatrix; init_st_matrix(&agg_dmatrix, objc, objc); silhouet *csil; silhouet *fsil; silhouet *ssil; silhouet *avg_csil; silhouet *avg_fsil; silhouet *avg_ssil; int *pred; st_matrix *groups; srand(time(NULL)); size_t best_inst; double best_inst_adeq; double cur_inst_adeq; for(i = 1; i <= insts; ++i) { printf("Instance %d:\n", i); cur_inst_adeq = run(); pred = defuz(&memb); groups = asgroups(pred, objc, classc); transpose_(&dists_t, &dists); aggregate_dmatrices(&agg_dmatrix, &weights); csil = crispsil(groups, &agg_dmatrix); fsil = fuzzysil(csil, groups, &memb, mfuz); ssil = simplesil(pred, &dists_t); if(i == 1) { avg_partcoef = partcoef(&memb); avg_modpcoef = modpcoef(&memb); avg_partent = partent(&memb); avg_aid = avg_intra_dist(&memb, &dists_t, mfuz); avg_csil = csil; avg_fsil = fsil; avg_ssil = ssil; } else { avg_partcoef = (avg_partcoef + partcoef(&memb)) / 2.0; avg_modpcoef = (avg_modpcoef + modpcoef(&memb)) / 2.0; avg_partent = (avg_partent + partent(&memb)) / 2.0; avg_aid = (avg_aid + avg_intra_dist(&memb, &dists_t, mfuz)) / 2.0; avg_silhouet(avg_csil, csil); avg_silhouet(avg_fsil, fsil); avg_silhouet(avg_ssil, ssil); free_silhouet(csil); free(csil); free_silhouet(fsil); free(fsil); free_silhouet(ssil); free(ssil); } free(pred); free_st_matrix(groups); free(groups); if(i == 1 || cur_inst_adeq < best_inst_adeq) { mtxcpy(&best_memb, &memb); mtxcpy(&best_dists, &dists); mtxcpy(&best_weights, &weights); best_inst_adeq = cur_inst_adeq; best_inst = i; } } printf("\n"); printf("Best adequacy %.15lf on instance %d.\n", best_inst_adeq, best_inst); printf("\n"); print_memb(&best_memb); print_weights(&best_weights); pred = defuz(&best_memb); groups = asgroups(pred, objc, classc); print_header("Partitions", HEADER_SIZE); print_groups(groups); print_header("Average indexes", HEADER_SIZE); printf("\nPartition coefficient: %.10lf\n", avg_partcoef); printf("Modified partition coefficient: %.10lf\n", avg_modpcoef); printf("Partition entropy: %.10lf (max: %.10lf)\n", avg_partent, log(clustc)); printf("Average intra cluster distance: %.10lf\n", avg_aid); transpose_(&dists_t, &best_dists); print_header("Best instance indexes", HEADER_SIZE); printf("\nPartition coefficient: %.10lf\n", partcoef(&best_memb)); printf("Modified partition coefficient: %.10lf\n", modpcoef(&best_memb)); printf("Partition entropy: %.10lf (max: %.10lf)\n", partent(&best_memb), log(clustc)); printf("Average intra cluster distance: %.10lf\n", avg_intra_dist(&best_memb, &dists_t, mfuz)); print_header("Averaged crisp silhouette", HEADER_SIZE); print_silhouet(avg_csil); print_header("Averaged fuzzy silhouette", HEADER_SIZE); print_silhouet(avg_fsil); print_header("Averaged simple silhouette", HEADER_SIZE); print_silhouet(avg_ssil); aggregate_dmatrices(&agg_dmatrix, &best_weights); csil = crispsil(groups, &agg_dmatrix); print_header("Best instance crisp silhouette", HEADER_SIZE); print_silhouet(csil); fsil = fuzzysil(csil, groups, &best_memb, mfuz); print_header("Best instance fuzzy silhouette", HEADER_SIZE); print_silhouet(fsil); ssil = simplesil(pred, &dists_t); print_header("Best instance simple silhouette", HEADER_SIZE); print_silhouet(ssil); free_silhouet(avg_csil); free(avg_csil); free_silhouet(avg_fsil); free(avg_fsil); free_silhouet(avg_ssil); free(avg_ssil); free_silhouet(csil); free(csil); free_silhouet(fsil); free(fsil); free_silhouet(ssil); free(ssil); free(pred); free_st_matrix(groups); free(groups); free_st_matrix(&dists_t); free_st_matrix(&agg_dmatrix); END: fclose(stdout); for(j = 0; j < dmatrixc; ++j) { free_st_matrix(&dmatrix[j]); } free(dmatrix); free_st_matrix(&memb); free_st_matrix(&best_memb); for(k = 0; k < clustc; ++k) { free_st_matrix(&membvec[k]); free_st_matrix(&global_dmatrix[k]); } free(membvec); free(global_dmatrix); free_st_matrix(&dists); free_st_matrix(&best_dists); free_st_matrix(&weights); free_st_matrix(&best_weights); return 0; }
int main(int argc, char **argv) { verbose = false; int insts; // Opening and reading config file. FILE *cfgfile = fopen(argv[1], "r"); if(!cfgfile) { printf("Error: could not open config file.\n"); return 1; } fscanf(cfgfile, "%d", &objc); if(objc <= 0) { printf("Error: objc <= 0.\n"); return 2; } int labels[objc]; // reading labels int classc; fscanf(cfgfile, "%d", &classc); size_t i; for(i = 0; i < objc; ++i) { fscanf(cfgfile, "%d", &labels[i]); } // reading labels end fscanf(cfgfile, "%d", &dmatrixc); if(dmatrixc <= 0) { printf("Error: dmatrixc <= 0.\n"); return 2; } char dmtx_file_name[dmatrixc][BUFF_SIZE]; size_t j; for(j = 0; j < dmatrixc; ++j) { fscanf(cfgfile, "%s", dmtx_file_name[j]); } char out_file_name[BUFF_SIZE]; fscanf(cfgfile, "%s", out_file_name); fscanf(cfgfile, "%d", &clustc); if(clustc <= 0) { printf("Error: clustc <= 0.\n"); return 2; } fscanf(cfgfile, "%d", &medoids_card); if(medoids_card <= 0) { printf("Error: medoids_card <= 0.\n"); return 2; } fscanf(cfgfile, "%d", &insts); if(insts <= 0) { printf("Error: insts <= 0.\n"); return 2; } fscanf(cfgfile, "%lf", &theta); if(dlt(theta, 0.0)) { printf("Error: theta < 0.\n"); return 2; } fscanf(cfgfile, "%d", &max_iter); fscanf(cfgfile, "%lf", &epsilon); if(dlt(epsilon, 0.0)) { printf("Error: epsilon < 0.\n"); return 2; } fscanf(cfgfile, "%lf", &mfuz); if(!dgt(mfuz, 0.0)) { printf("Error: mfuz <= 0.\n"); return 2; } fclose(cfgfile); // Done reading config file. freopen(out_file_name, "w", stdout); mfuzval = 1.0 / (mfuz - 1.0); printf("######Config summary:######\n"); printf("Number of clusters: %d.\n", clustc); printf("Medoids cardinality: %d.\n", medoids_card); printf("Number of iterations: %d.\n", max_iter); printf("Epsilon: %.15lf.\n", epsilon); printf("Theta: %.15lf.\n", theta); printf("Parameter m: %.15lf.\n", mfuz); printf("Number of instances: %d.\n", insts); printf("###########################\n"); size_t k; // Allocating memory start parc_cluster_adeq = malloc(sizeof(double) * clustc); parc_obj_adeq = malloc(sizeof(double) * objc); dmatrix = malloc(sizeof(double **) * dmatrixc); for(j = 0; j < dmatrixc; ++j) { dmatrix[j] = malloc(sizeof(double *) * objc); for(i = 0; i < objc; ++i) { dmatrix[j][i] = malloc(sizeof(double) * objc); } } medoids = malloc(sizeof(size_t **) * clustc); size_t ***best_medoids = malloc(sizeof(size_t **) * clustc); for(k = 0; k < clustc; ++k) { medoids[k] = malloc(sizeof(size_t *) * dmatrixc); best_medoids[k] = malloc(sizeof(size_t *) * dmatrixc); for(j = 0; j < dmatrixc; ++j) { medoids[k][j] = malloc(sizeof(size_t) * medoids_card); best_medoids[k][j] = malloc(sizeof(size_t) * medoids_card); } } weights = malloc(sizeof(double *) * clustc); double **best_weights = malloc(sizeof(double *) * clustc); for(k = 0; k < clustc; ++k) { weights[k] = malloc(sizeof(double) * dmatrixc); best_weights[k] = malloc(sizeof(double) * dmatrixc); } memb = malloc(sizeof(double *) * objc); double **best_memb = malloc(sizeof(double *) * objc); for(i = 0; i < objc; ++i) { memb[i] = malloc(sizeof(double) * clustc); best_memb[i] = malloc(sizeof(double) * clustc); } // Allocating memory end for(j = 0; j < dmatrixc; ++j) { if(!load_data(dmtx_file_name[j], dmatrix[j], objc, objc)) { printf("Error: could not load %s.\n", dmtx_file_name[j]); goto END; } } size_t best_inst; double best_inst_adeq; double cur_inst_adeq; srand(time(NULL)); // Random seed. // Start main program loop. for(i = 1; i <= insts; ++i) { printf("Instance %u:\n", i); cur_inst_adeq = run(); if(i == 1 || cur_inst_adeq < best_inst_adeq) { // Saves the best configuration based on the adequacy. mtxcpy_d(best_memb, memb, objc, clustc); mtxcpy_d(best_weights, weights, clustc, dmatrixc); for(k = 0; k < clustc; ++k) { mtxcpy_size_t(best_medoids[k], medoids[k], dmatrixc, medoids_card); } best_inst_adeq = cur_inst_adeq; best_inst = i; } } // Print the best configuration, confusion matrix and the CR // index. printf("\n"); printf("Best adequacy %.15lf on instance %d.\n", best_inst_adeq, best_inst); printf("\n"); print_medoids(best_medoids); printf("\n"); print_memb(best_memb); printf("\n"); print_weights(best_weights); printf("\n"); global_energy(); printf("\n"); int *pred = defuz(memb, objc, clustc); print_groups(pred, objc, clustc); double **confmtx = confusion(pred, labels, objc); printf("\nConfusion matrix (class x predicted):\n"); print_mtx_d(confmtx, classc, classc, 0); ++classc; for(i = 0; i < classc; ++i) { free(confmtx[i]); } free(confmtx); printf("Corrected Rand: %.7lf\n", corand(pred, labels, objc)); free(pred); // Freeing memory. END: fclose(stdout); for(i = 0; i < dmatrixc; ++i) { for(j = 0; j < objc; ++j) { free(dmatrix[i][j]); } free(dmatrix[i]); } free(dmatrix); for(k = 0; k < clustc; ++k) { for(j = 0; j < dmatrixc; ++j) { free(medoids[k][j]); free(best_medoids[k][j]); } free(medoids[k]); free(best_medoids[k]); free(weights[k]); free(best_weights[k]); } free(medoids); free(best_medoids); free(weights); free(best_weights); for(i = 0; i < objc; ++i) { free(memb[i]); free(best_memb[i]); } free(memb); free(best_memb); free(parc_cluster_adeq); free(parc_obj_adeq); return 0; }