long get_int(char *ch, int no) { int len = 100; char *tstr; char str[100]; printf("%s: ", ch); tstr = fgets(str, len, stdin); if (tstr == NULL) return(no); return(oatoi(str, no)); }
int global_options(int argc, char **argv) { char *s; /* set program name */ setprogname(argv[0]); #ifndef NO_PIPED_COMMANDS /* command for compressing */ s = getenv("LVQSOM_COMPRESS_COMMAND"); if (s) compress_command = s; s = extract_parameter(argc, argv, "-compress_cmd", OPTION); if (s) compress_command = s; /* command for uncompressing */ s = getenv("LVQSOM_UNCOMPRESS_COMMAND"); if (s) uncompress_command = s; s = extract_parameter(argc, argv, "-uncompress_cmd", OPTION); if (s) uncompress_command = s; #endif /* NO_PIPED_COMMANDS */ /* string that identifies a vector component to be ignored in files */ s = getenv("LVQSOM_MASK_STR"); if (s) masked_string = s; s = extract_parameter(argc, argv, "-mask_str", OPTION); if (s) masked_string = s; if (extract_parameter(argc, argv, "-version", OPTION2)) fprintf(stderr, "Version: %s\n", get_version()); verbose(oatoi(extract_parameter(argc, argv, VERBOSE, OPTION), 1)); return 0; }
int main(int argc, char **argv) { long nod, buffer; float qerror; char *in_data_file; char *in_code_file; struct teach_params teach; int qmode; struct entries *data, *codes; global_options(argc, argv); if (extract_parameter(argc, argv, "-help", OPTION2)) { printhelp(); exit(0); } in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS); in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS); buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0); teach.radius = oatof(extract_parameter(argc, argv, TRAINING_RADIUS, OPTION), 1.0); qmode = oatoi(extract_parameter(argc, argv, "-qetype", OPTION), 0); label_not_needed(1); ifverbose(2) fprintf(stdout, "Input entries are read from file %s\n", in_data_file); data = open_entries(in_data_file); if (data == NULL) { fprintf(stderr, "Can't open datafile: %s\n", in_data_file); exit(1); } ifverbose(2) fprintf(stdout, "Codebook entries are read from file %s\n", in_code_file); codes = open_entries(in_code_file); if (codes == NULL) { fprintf(stderr, "Can't open code file '%s'\n", in_code_file); close_entries(data); exit(1); } if (codes->topol < TOPOL_HEXA) { fprintf(stderr, "File %s is not a map file\n", in_code_file); close_entries(data); close_entries(codes); exit(1); } if (data->dimension != codes->dimension) { fprintf(stderr, "Data and codebook vectors have different dimensions (%d != %d)", data->dimension, codes->dimension); close_entries(data); close_entries(codes); exit(1); } set_teach_params(&teach, codes, data, buffer); set_som_params(&teach); if (qmode > 0) qerror = find_qerror2(&teach); else qerror = find_qerror(&teach); nod = teach.data->num_entries; ifverbose(1) fprintf(stdout, "Quantization error of %s with map %s is %f per sample (%d samples)\n", in_data_file, in_code_file, qerror / (float) nod, nod); else
int main(int argc, char **argv) { int not, bnot, error; int xdim, ydim; int topol, neigh; float alpha1, radius1; int fixed, weights; float alpha2, radius2; float qerror, qerrorb; char *in_data_file, *in_test_file, *out_code_file, *alpha_s; struct entries *data = NULL; struct entries *testdata = NULL; struct entries *codes = NULL; struct entries *codess = NULL; struct entries *tmp; struct teach_params params; long buffer, length1, length2, noc, nod; int qmode; struct typelist *type_tmp; error = 0; global_options(argc, argv); print_description(); not = get_int("Give the number of trials", 0); in_data_file = get_str("Give the input data file name"); in_test_file = get_str("Give the input test file name"); out_code_file = get_str("Give the output map file name"); topol = topol_type(get_str("Give the topology type")); if (topol == TOPOL_UNKNOWN) { ifverbose(2) fprintf(stderr, "Unknown topology type, using hexagonal\n"); topol = TOPOL_HEXA; } neigh = neigh_type(get_str("Give the neighborhood type")); if (neigh == NEIGH_UNKNOWN) { ifverbose(2) fprintf(stderr, "Unknown neighborhood type, using bubble\n"); neigh = NEIGH_BUBBLE; } xdim = get_int("Give the x-dimension", 0); ydim = get_int("Give the y-dimension", 0); length1 = get_int("Give the training length of first part", 0); alpha1 = get_float("Give the training rate of first part", 0.0); radius1 = get_float("Give the radius in first part", 0.0); length2 = get_int("Give the training length of second part", 0); alpha2 = get_float("Give the training rate of second part", 0.0); radius2 = get_float("Give the radius in second part", 0.0); printf("\n"); fixed = (int) oatoi(extract_parameter(argc, argv, FIXPOINTS, OPTION), 0); weights = (int) oatoi(extract_parameter(argc, argv, WEIGHTS, OPTION), 0); buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0); alpha_s = extract_parameter(argc, argv, "-alpha_type", OPTION); qmode = oatoi(extract_parameter(argc, argv, "-qetype", OPTION), 0); use_fixed(fixed); use_weights(weights); label_not_needed(1); ifverbose(2) fprintf(stderr, "Input entries are read from file %s\n", in_data_file); data = open_entries(in_data_file); if (data == NULL) { fprintf(stderr, "Can't open data file '%s'\n", in_data_file); error = 1; goto end; } set_buffer(data, buffer); ifverbose(2) fprintf(stderr, "Test entries are read from file %s\n", in_test_file); testdata = open_entries(in_test_file); if (testdata == NULL) { fprintf(stderr, "Can't open test data file '%s'\n", in_test_file); error = 1; goto end; } set_buffer(testdata, buffer); noc = xdim * ydim; if (noc <= 0) { fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim); error = 1; goto end; } if (xdim < 0) { fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim); error = 1; goto end; } if (alpha_s) { type_tmp = get_type_by_str(alpha_list, alpha_s); if (type_tmp->data == NULL) { fprintf(stderr, "Unknown alpha type %s\n", alpha_s); error = 1; goto end; } } else type_tmp = get_type_by_id(alpha_list, ALPHA_LINEAR); params.alpha_type = type_tmp->id; params.alpha_func = type_tmp->data; codess = NULL; qerrorb = FLT_MAX; bnot = 0; while (not) { init_random(not); ifverbose(2) fprintf(stderr, "Initializing codebook\n"); codes = randinit_codes(data, topol, neigh, xdim, ydim); if (codes == NULL) { fprintf(stderr, "Error initializing random codebook, aborting\n"); error = 1; goto end; } set_teach_params(¶ms, codes, NULL, 0); set_som_params(¶ms); params.data = data; params.length = length1; params.alpha = alpha1; params.radius = radius1; ifverbose(2) fprintf(stderr, "Training map, first part, rlen: %ld alpha: %f\n", params.length, params.alpha); codes = som_training(¶ms); params.length = length2; params.alpha = alpha2; params.radius = radius2; ifverbose(2) fprintf(stderr, "Training map, second part, rlen: %ld alpha: %f\n", params.length, params.alpha); codes = som_training(¶ms); params.data = testdata; ifverbose(2) fprintf(stderr, "Calculating quantization error\n"); if (qmode > 0) qerror = find_qerror2(¶ms); else qerror = find_qerror(¶ms); nod = testdata->num_entries; if (qerror < qerrorb) { qerrorb = qerror; bnot = not; tmp = codess; codess = codes; codes = tmp; } close_entries(codes); codes = NULL; ifverbose(1) fprintf(stderr, "%3d: %f\n", not, qerror/(float) nod); not--; } if (codess != NULL) { ifverbose(2) fprintf(stdout, "Codebook entries are saved to file %s\n", out_code_file); save_entries(codess, out_code_file); ifverbose(1) fprintf(stdout, "Smallest error with random seed %3d: %f\n", bnot, qerrorb/(float) nod); } end: if (codess) close_entries(codess); if (data) close_entries(data); if (testdata) close_entries(testdata); return(error); }
int main(int argc, char** argv) { char* configfile; const char* socketfile = NULL; mode_t permission = S_IRUSR | S_IWUSR; int socket; int lircdfd; struct sigaction act; struct sockaddr_un addr; char dir[FILENAME_MAX + 1] = { 0 }; lirc_log_open("lircrcd", 0, LIRC_NOLOG); while (1) { int c; static struct option long_options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'v' }, { "permission", required_argument, NULL, 'p' }, { "output", required_argument, NULL, 'o' }, { 0, 0, 0, 0 } }; c = getopt_long(argc, argv, "hvp:o:", long_options, NULL); if (c == -1) break; switch (c) { case 'h': printf("Usage: %s [options] config-file\n", progname); printf("\t -h --help\t\t\tdisplay this message\n"); printf("\t -v --version\t\t\tdisplay version\n"); printf("\t -p --permission=mode\t\tfile permissions for socket\n"); printf("\t -o --output=socket\t\toutput socket filename\n"); return EXIT_SUCCESS; case 'v': printf("%s %s\n", progname, VERSION); return EXIT_SUCCESS; case 'p': if (oatoi(optarg) == -1) { fprintf(stderr, "%s: invalid mode\n", progname); return EXIT_FAILURE; } permission = oatoi(optarg); break; case 'o': socketfile = optarg; break; default: printf("Usage: %s [options] config-file\n", progname); return EXIT_FAILURE; } } if (optind == argc - 1) { configfile = argv[optind]; } else { fprintf(stderr, "%s: invalid argument count\n", progname); return EXIT_FAILURE; } lircdfd = lirc_init("lircrcd", 0); if (lircdfd == -1) return EXIT_FAILURE; /* read config file */ if (lirc_readconfig_only(configfile, &config, NULL) != 0) { lirc_deinit(); return EXIT_FAILURE; } /* open socket */ socket = opensocket(config->lircrc_class, socketfile, permission, &addr); if (socket == -1) { lirc_freeconfig(config); lirc_deinit(); return EXIT_FAILURE; } /* fork */ if (getcwd(dir, sizeof(dir)) == NULL) { lirc_freeconfig(config); lirc_deinit(); perror("getcwd()"); return EXIT_FAILURE; } if (daemon(0, 0) == -1) { perror("daemon() failed"); shutdown(socket, 2); close(socket); lirc_deinit(); lirc_freeconfig(config); return EXIT_FAILURE; } daemonized = 1; openlog(progname, LOG_CONS | LOG_PID, LOG_USER); umask(0); signal(SIGPIPE, SIG_IGN); act.sa_handler = sigterm; sigfillset(&act.sa_mask); act.sa_flags = SA_RESTART; /* don't fiddle with EINTR */ sigaction(SIGTERM, &act, NULL); sigaction(SIGINT, &act, NULL); sigaction(SIGHUP, &act, NULL); log_notice("%s started", progname); loop(socket, lircdfd); closelog(); shutdown(socket, 2); close(socket); if (chdir(dir) == 0) unlink(addr.sun_path); lirc_freeconfig(config); lirc_deinit(); return EXIT_SUCCESS; }