int midi_play_graph (const char *fname, const char *userdir, const char *midiplayer) { char outname[FILENAME_MAX]; midi_spec spec; midi_track track; dataset dset; sprintf(outname, "%sgretl.mid", userdir); spec.fp = gretl_fopen(outname, "wb"); if (spec.fp == NULL) { fprintf(stderr, "Couldn't write to '%s'\n", outname); return 1; } if (read_datafile(fname, &dset)) { audio_graph_error("Error reading data file"); fclose(spec.fp); return 1; } spec.ntracks = 1 + dset.series2; spec.nticks = 96; spec.dset = &dset; spec.nsecs = 8; four_four_header(&spec); print_dataset_comments(&dset); play_dataset(&spec, &track, &dset); dataset_free(&dset); fclose(spec.fp); midi_fork(outname, midiplayer); return 0; }
static int read_datafile (const char *fname, dataset *dset) { #ifdef PLAY_AUTOFIT_LINE int fitline = 0; #endif char line[256]; int i, err = 0; int got_e = 0, y2data = 0; FILE *fdat; dataset_init(dset); fdat = gretl_fopen(fname, "r"); if (fdat == NULL) { fprintf(stderr, "Couldn't open '%s'\n", fname); return 1; } else { fprintf(stderr, "Reading %s...\n", fname); } while (fgets(line, sizeof line, fdat)) { tailstrip(line); if (get_comment(line, dset)) { continue; } else if (!strcmp(line, "e")) { fprintf(stderr, "Got end of data marker\n"); got_e++; if (got_e == 2) { /* can't handle more than two series! */ break; } } else if (strstr(line, "automatic fitted")) { #ifdef PLAY_AUTOFIT_LINE fitline = 1; #endif continue; } else if (isdigit((unsigned char) line[0])) { if (strstr(line, "title")) { #ifdef PLAY_AUTOFIT_LINE if (fitline) { get_fit_params(line, dset); } #endif continue; } if (!got_e) { dset->n += 1; } else if (!y2data) { y2data = 1; } } } if (dset->n == 0) { fprintf(stderr, "No data in '%s'\n", fname); err = 1; goto bailout; } dset->points = malloc(dset->n * sizeof *dset->points); if (dset->points == NULL) { err = 1; fputs("Out of memory\n", stderr); goto bailout; } if (y2data) { dset->y2 = malloc(dset->n * sizeof *dset->y2); if (dset->y2 == NULL) { err = 1; fputs("Out of memory\n", stderr); goto bailout; } dset->series2 = 1; } rewind(fdat); i = got_e = 0; while (!err && fgets(line, 256, fdat)) { tailstrip(line); if (!strcmp(line, "e")) { got_e++; if (got_e == 2) { break; } i = 0; } else if (isdigit((unsigned char) line[0])) { double x, y; if (strstr(line, "title")) { continue; } if (get_data_x_y(line, &x, &y)) { fprintf(stderr, "Couldn't read data on line %d\n", i + 1); err = 1; } else { if (!got_e) { dset->points[i].x = x; dset->points[i].y = y; } else { dset->y2[i] = y; } } i++; } } bailout: fclose(fdat); if (err) { dataset_free(dset); } return err; }
int main(int argc, char *argv[]) { char *program_name = argv[0]; double l2_sigma_sq = 0.0; int grafting = 0; int grafting_light = 0; lbfgs_parameter_t params; lbfgs_parameter_init(¶ms); params.past = 1; params.delta = 1e-7; int ch; while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) { switch (ch) { case OPTION_FTOL: params.ftol = str_to_double(optarg); break; case OPTION_GTOL: params.gtol = str_to_double(optarg); break; case OPTION_GRAFTING: grafting = str_to_int(optarg); break; case OPTION_GRAFTING_LIGHT: grafting_light = str_to_int(optarg); break; case OPTION_L1: params.orthantwise_c = str_to_double(optarg); break; case OPTION_L2: l2_sigma_sq = str_to_double(optarg); break; case OPTION_LINESEARCH: if (strcmp(optarg, "armijo") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_ARMIJO; else if (strcmp(optarg, "backtracking") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING; else if (strcmp(optarg, "wolfe") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_WOLFE; else if (strcmp(optarg, "strong_wolfe") == 0) params.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE; else { usage(program_name); return 1; } break; case OPTION_MINSTEP: fprintf(stderr,"backtracking\n"); params.min_step = str_to_double(optarg); break; case OPTION_MAXSTEP: params.max_step = str_to_double(optarg); break; case '?': default: usage(program_name); return 1; } } argc -= optind; argv += optind; if (argc != 0 && argc != 1) { usage(program_name); return 1; } if (grafting && grafting_light) { fprintf(stderr, "Grafting and grafting-light cannot be used simultaneously..."); return 1; } if ((grafting || grafting_light) && params.orthantwise_c == 0.) { fprintf(stderr, "Grafting requires a l1 norm coefficient..."); return 1; } fprintf(stderr, "l1 norm coefficient: %.4e\n", params.orthantwise_c); fprintf(stderr, "l2 prior sigma^2: %.4e\n\n", l2_sigma_sq); dataset_t ds; int fd = 0; if (argc == 1 && (fd = open(argv[0], O_RDONLY)) == -1) { fprintf(stderr, "Could not open %s\n", argv[0]); return 1; } int r = read_tadm_dataset(fd, &ds); if (r != TADM_OK) { fprintf(stderr, "Error reading data...\n"); return 1; } fprintf(stderr, "Features: %zu\n", ds.n_features); fprintf(stderr, "Contexts: %zu\n\n", ds.n_contexts); if (params.orthantwise_c != 0.0) { params.orthantwise_end = ds.n_features; // l1 prior only works with backtracking linesearch. params.linesearch = LBFGS_LINESEARCH_BACKTRACKING; } model_t model; if (grafting || grafting_light) model_new(&model, ds.n_features, true); else model_new(&model, ds.n_features, false); fprintf(stderr, "Iter\t-LL\t\txnorm\t\tgnorm\n\n"); if (grafting) r = maxent_lbfgs_grafting(&ds, &model, ¶ms, l2_sigma_sq, grafting); else if (grafting_light) r = maxent_lbfgs_grafting_light(&ds, &model, ¶ms, l2_sigma_sq, grafting_light); else r = maxent_lbfgs_optimize(&ds, &model, ¶ms, l2_sigma_sq); dataset_free(&ds); if (r != LBFGS_STOP && r != LBFGS_SUCCESS && r != LBFGS_ALREADY_MINIMIZED) { fprintf(stderr, "%s\n\n", err_to_string(lbfgs_errs, r)); model_free(&model); return 1; } for (int i = 0; i < ds.n_features; ++i) printf("%.8f\n", model.params[i]); model_free(&model); return 0; }
void dataset_destroy(dataset_t* const ds) { assert(ds != NULL); dataset_free(ds); free(ds); }