GFileOutputStream *
_g_local_file_output_stream_append  (const char        *filename,
				     GFileCreateFlags   flags,
				     GCancellable      *cancellable,
				     GError           **error)
{
  int mode;

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;

  if (flags & G_FILE_CREATE_PRIVATE)
    mode = 0600;
  else
    mode = 0666;

  return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
                             cancellable, error);
}
GFileOutputStream *
_g_local_file_output_stream_open  (const char        *filename,
				   gboolean          readable,
				   GCancellable      *cancellable,
				   GError           **error)
{
  int open_flags;

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;

  open_flags = O_BINARY;
  if (readable)
    open_flags |= O_RDWR;
  else
    open_flags |= O_WRONLY;

  return output_stream_open (filename, open_flags, 0666, cancellable, error);
}
GFileOutputStream *
_g_local_file_output_stream_create  (const char        *filename,
				     gboolean          readable,
				     GFileCreateFlags   flags,
                                     GFileInfo         *reference_info,
				     GCancellable      *cancellable,
				     GError           **error)
{
  int mode;
  int open_flags;

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;

  mode = mode_from_flags_or_info (flags, reference_info);

  open_flags = O_CREAT | O_EXCL | O_BINARY;
  if (readable)
    open_flags |= O_RDWR;
  else
    open_flags |= O_WRONLY;

  return output_stream_open (filename, open_flags, mode, cancellable, error);
}
Exemple #4
0
int generation_information(int gen, multipop *mpop, int stt_interval, int bestn) {
	int i, j;
	int newbest;
	static int fd = -1;
	popstats *gen_stats;
	int ret = 0;
	FILE *bout, *hout;

	/* number of decimal digits to use when printing fitness values. */
	if (fd == -1)
		fd = atoi(get_parameter("output.digits"));

	/* allocate stats records for the current generation. */
	gen_stats = (popstats *) MALLOC((mpop->size + 1) * sizeof(popstats));
	for (i = 0; i < mpop->size + 1; ++i) {
		gen_stats[i].bestn = bestn;
		gen_stats[i].size = -1;
	}

	oprintf( OUT_GEN, 90, "=== GENERATION %d ===\n", gen);
	oprintf( OUT_PRG, 90, "=== GENERATION %d ===\n", gen);

	/* for each subpopulation... */
	for (i = 0; i < mpop->size; ++i) {
		/* calculate stats for subpopulation. */
		calculate_pop_stats(gen_stats + i + 1, mpop->pop[i], gen, i);
		/* accumulate that into stats for whole popluation... */
		accumulate_pop_stats(gen_stats, gen_stats + i + 1);
		/* ...and stats for this subpopulation over the whole run. */
		accumulate_pop_stats(run_stats + i + 1, gen_stats + i + 1);

		/* if only one subpop, don't print out the subpop stuff. */
		if (mpop->size == 1)
			continue;

		/** print much stuff to .gen, .prg, and .stt files. */

		if (test_detail_level(90)) {
			oprintf( OUT_GEN, 90, "    subpopulation %d:\n", i + 1);
			oprintf( OUT_GEN, 90, "        generation:\n");
			oprintf( OUT_GEN, 90,
					"            mean:   nodes: %.3lf (%d-%d); depth: %.3lf (%d-%d)\n",
					(double) gen_stats[i + 1].totalnodes
							/ gen_stats[i + 1].size, gen_stats[i + 1].minnodes,
					gen_stats[i + 1].maxnodes,
					(double) gen_stats[i + 1].totaldepth
							/ gen_stats[i + 1].size, gen_stats[i + 1].mindepth,
					gen_stats[i + 1].maxdepth);
			oprintf( OUT_GEN, 90, "            best:   nodes: %d; depth: %d\n",
					gen_stats[i + 1].bestnodes, gen_stats[i + 1].bestdepth);
			oprintf( OUT_GEN, 90, "            worst:  nodes: %d; depth: %d\n",
					gen_stats[i + 1].worstnodes, gen_stats[i + 1].worstdepth);
			oprintf( OUT_GEN, 90, "        run:   (%d trees)\n",
					run_stats[i + 1].size);
			oprintf( OUT_GEN, 90,
					"            mean:   nodes: %.3lf (%d-%d); depth: %.3lf (%d-%d)\n",
					(double) run_stats[i + 1].totalnodes
							/ run_stats[i + 1].size, run_stats[i + 1].minnodes,
					run_stats[i + 1].maxnodes,
					(double) run_stats[i + 1].totaldepth
							/ run_stats[i + 1].size, run_stats[i + 1].mindepth,
					run_stats[i + 1].maxdepth);
			oprintf( OUT_GEN, 90, "            best:   nodes: %d; depth: %d\n",
					run_stats[i + 1].bestnodes, run_stats[i + 1].bestdepth);
			oprintf( OUT_GEN, 90, "            worst:  nodes: %d; depth: %d\n",
					run_stats[i + 1].worstnodes, run_stats[i + 1].worstdepth);
		}

		if (test_detail_level(90)) {
			oprintf( OUT_PRG, 90, "    subpopulation %d:\n", i + 1);
			oprintf( OUT_PRG, 90, "        generation stats:\n");
			oprintf( OUT_PRG, 90,
					"            mean:   hits: %.3lf (%d-%d); standardized fitness: %.*lf\n",
					(double) gen_stats[i + 1].totalhits / gen_stats[i + 1].size,
					gen_stats[i + 1].minhits, gen_stats[i + 1].maxhits, fd,
					(double) gen_stats[i + 1].totalfit / gen_stats[i + 1].size);
			oprintf( OUT_PRG, 90,
					"            best:   hits: %d; standardized fitness: %.*lf\n",
					gen_stats[i + 1].besthits, fd,
					(double) gen_stats[i + 1].bestfit);
			oprintf( OUT_PRG, 90,
					"            worst:  hits: %d; standardized fitness: %.*lf\n",
					gen_stats[i + 1].worsthits, fd,
					(double) gen_stats[i + 1].worstfit);
			oprintf( OUT_PRG, 90, "        run stats:     (%d trees)\n",
					run_stats[i + 1].size);
			oprintf( OUT_PRG, 90,
					"            mean:   hits: %.3lf (%d-%d); standardized fitness: %.*lf\n",
					(double) run_stats[i + 1].totalhits / run_stats[i + 1].size,
					run_stats[i + 1].minhits, run_stats[i + 1].maxhits, fd,
					(double) run_stats[i + 1].totalfit / run_stats[i + 1].size);
			oprintf( OUT_PRG, 90,
					"            best:   hits: %d; standardized fitness: %.*lf; generation: %d\n",
					run_stats[i + 1].besthits, fd,
					(double) run_stats[i + 1].bestfit,
					run_stats[i + 1].bestgen);
			oprintf( OUT_PRG, 90,
					"            worst:  hits: %d; standardized fitness: %.*lf; generation: %d\n",
					run_stats[i + 1].worsthits, fd,
					(double) run_stats[i + 1].worstfit,
					run_stats[i + 1].worstgen);
		}

		if (gen % stt_interval == 0) {
			oprintf( OUT_STT, 50, "%d %d ", gen, i + 1);
			oprintf( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd,
					gen_stats[i + 1].totalfit / gen_stats[i + 1].size, fd,
					gen_stats[i + 1].bestfit, fd, gen_stats[i + 1].worstfit);
			oprintf( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ",
					(double) gen_stats[i + 1].totalnodes
							/ gen_stats[i + 1].size,
					(double) gen_stats[i + 1].totaldepth
							/ gen_stats[i + 1].size, gen_stats[i + 1].bestnodes,
					gen_stats[i + 1].bestdepth, gen_stats[i + 1].worstnodes,
					gen_stats[i + 1].worstdepth);
			oprintf( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd,
					run_stats[i + 1].totalfit / run_stats[i + 1].size, fd,
					run_stats[i + 1].bestfit, fd, run_stats[i + 1].worstfit);
			oprintf( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ",
					(double) run_stats[i + 1].totalnodes
							/ run_stats[i + 1].size,
					(double) run_stats[i + 1].totaldepth
							/ run_stats[i + 1].size, run_stats[i + 1].bestnodes,
					run_stats[i + 1].bestdepth, run_stats[i + 1].worstnodes,
					run_stats[i + 1].worstdepth);
			oprintf( OUT_STT, 50, "\n");
		}

	}

	/* merge stats for current generation into overall run stats. */
	newbest = accumulate_pop_stats(run_stats, gen_stats);

	/** more printing. **/

	if (test_detail_level(90)) {
		oprintf( OUT_GEN, 90, "    total population:\n");
		oprintf( OUT_GEN, 90, "        generation:\n");
		oprintf( OUT_GEN, 90,
				"            mean:   nodes: %.3lf (%d-%d); depth: %.3lf (%d-%d)\n",
				(double) gen_stats[0].totalnodes / gen_stats[0].size,
				gen_stats[0].minnodes, gen_stats[0].maxnodes,
				(double) gen_stats[0].totaldepth / gen_stats[0].size,
				gen_stats[0].mindepth, gen_stats[0].maxdepth);
		oprintf( OUT_GEN, 90, "            best:   nodes: %d; depth: %d\n",
				gen_stats[0].bestnodes, gen_stats[0].bestdepth);
		oprintf( OUT_GEN, 90, "            worst:  nodes: %d; depth: %d\n",
				gen_stats[0].worstnodes, gen_stats[0].worstdepth);
		oprintf( OUT_GEN, 90, "        run:    (%d trees)\n",
				run_stats[0].size);
		oprintf( OUT_GEN, 90,
				"            mean:   nodes: %.3lf (%d-%d); depth: %.3lf (%d-%d)\n",
				(double) run_stats[0].totalnodes / run_stats[0].size,
				run_stats[0].minnodes, run_stats[0].maxnodes,
				(double) run_stats[0].totaldepth / run_stats[0].size,
				run_stats[0].mindepth, run_stats[0].maxdepth);
		oprintf( OUT_GEN, 90, "            best:   nodes: %d; depth: %d\n",
				run_stats[0].bestnodes, run_stats[0].bestdepth);
		oprintf( OUT_GEN, 90, "            worst:  nodes: %d; depth: %d\n",
				run_stats[0].worstnodes, run_stats[0].worstdepth);
	}

	if (test_detail_level(90)) {
		oprintf( OUT_PRG, 90, "    total population:\n");
		oprintf( OUT_PRG, 90, "        generation stats:\n");
		oprintf( OUT_PRG, 90,
				"            mean:   hits: %.3lf (%d-%d); standardized fitness: %.*lf\n",
				(double) gen_stats[0].totalhits / gen_stats[0].size,
				gen_stats[0].minhits, gen_stats[0].maxhits, fd,
				(double) gen_stats[0].totalfit / gen_stats[0].size);
		oprintf( OUT_PRG, 90,
				"            best:   hits: %d; standardized fitness: %.*lf\n",
				gen_stats[0].besthits, fd, (double) gen_stats[0].bestfit);
		oprintf( OUT_PRG, 90,
				"            worst:  hits: %d; standardized fitness: %.*lf\n",
				gen_stats[0].worsthits, fd, (double) gen_stats[0].worstfit);
		oprintf( OUT_PRG, 90, "        run stats:     (%d trees)\n",
				run_stats[0].size);
		oprintf( OUT_PRG, 90,
				"            mean:   hits: %.3lf (%d-%d); standardized fitness: %.*lf\n",
				(double) run_stats[0].totalhits / run_stats[0].size,
				run_stats[0].minhits, run_stats[0].maxhits, fd,
				(double) run_stats[0].totalfit / run_stats[0].size);
		oprintf( OUT_PRG, 90,
				"            best:   hits: %d; standardized fitness: %.*lf; generation: %d\n",
				run_stats[0].besthits, fd, (double) run_stats[0].bestfit,
				run_stats[0].bestgen);
		oprintf( OUT_PRG, 90,
				"            worst:  hits: %d; standardized fitness: %.*lf; generation: %d\n",
				run_stats[0].worsthits, fd, (double) run_stats[0].worstfit,
				run_stats[0].worstgen);
	}

	if (gen % stt_interval == 0) {
		if (test_detail_level(50)) {
			oprintf( OUT_STT, 50, "%d 0 ", gen);
			oprintf( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd,
					gen_stats[0].totalfit / gen_stats[0].size, fd,
					gen_stats[0].bestfit, fd, gen_stats[0].worstfit);
			oprintf( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ",
					(double) gen_stats[0].totalnodes / gen_stats[0].size,
					(double) gen_stats[0].totaldepth / gen_stats[0].size,
					gen_stats[0].bestnodes, gen_stats[0].bestdepth,
					gen_stats[0].worstnodes, gen_stats[0].worstdepth);
			oprintf( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd,
					run_stats[0].totalfit / run_stats[0].size, fd,
					run_stats[0].bestfit, fd, run_stats[0].worstfit);
			oprintf( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ",
					(double) run_stats[0].totalnodes / run_stats[0].size,
					(double) run_stats[0].totaldepth / run_stats[0].size,
					run_stats[0].bestnodes, run_stats[0].bestdepth,
					run_stats[0].worstnodes, run_stats[0].worstdepth);
			oprintf( OUT_STT, 50, "\n");
		}
	}

	/* rewrite the .bst file, and append to the .his file. */

	output_stream_open( OUT_BST);

	oprintf( OUT_BST, 10, "=== BEST-OF-RUN ===\n");
	oprintf( OUT_BST, 10, "              generation: %d\n",
			run_stats[0].bestgen);
	if (mpop->size > 1)
		oprintf( OUT_BST, 10, "           subpopulation: %d\n",
				run_stats[0].bestpop + 1);
	oprintf( OUT_BST, 10, "                   nodes: %d\n",
			run_stats[0].bestnodes);
	oprintf( OUT_BST, 10, "                   depth: %d\n",
			run_stats[0].bestdepth);
	oprintf( OUT_BST, 10, "                    hits: %d\n",
			run_stats[0].besthits);

	oprintf( OUT_HIS, 10, "=== BEST-OF-RUN ===\n");
	oprintf( OUT_HIS, 10, "      current generation: %d\n", gen);
	oprintf( OUT_HIS, 10, "              generation: %d\n",
			run_stats[0].bestgen);
	if (mpop->size > 1)
		oprintf( OUT_HIS, 10, "           subpopulation: %d\n",
				run_stats[0].bestpop + 1);
	oprintf( OUT_HIS, 10, "                   nodes: %d\n",
			run_stats[0].bestnodes);
	oprintf( OUT_HIS, 10, "                   depth: %d\n",
			run_stats[0].bestdepth);
	oprintf( OUT_HIS, 10, "                    hits: %d\n",
			run_stats[0].besthits);

	/* retrieve the (FILE *) for the .bst and .his files, so that
	 the trees can be printed to them. */

	bout = output_filehandle( OUT_BST);
	hout = output_filehandle( OUT_HIS);

	if (run_stats[0].bestn == 1) {
		oprintf( OUT_BST, 20, "TOP INDIVIDUAL:\n\n");
		oprintf( OUT_HIS, 20, "TOP INDIVIDUAL:\n\n");
	} else {
		oprintf( OUT_BST, 20, "TOP %d INDIVIDUALS (in order):\n\n",
				run_stats[0].bestn);
		oprintf( OUT_HIS, 20, "TOP %d INDIVIDUALS (in order):\n\n",
				run_stats[0].bestn);
	}

	for (i = 0; i < run_stats[0].bestn; ++i) {
		oprintf( OUT_BST, 20, "\n\n-- #%d --\n", i + 1);

		oprintf( OUT_BST, 20, "                    hits: %d\n",
				run_stats[0].best[i]->ind->hits);
		oprintf( OUT_BST, 20, "             raw fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->r_fitness);
		oprintf( OUT_BST, 20, "    standardized fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->s_fitness);
		oprintf( OUT_BST, 20, "        adjusted fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->a_fitness);

		oprintf( OUT_HIS, 20, "\n\n-- #%d --\n", i + 1);

		oprintf( OUT_HIS, 20, "                    hits: %d\n",
				run_stats[0].best[i]->ind->hits);
		oprintf( OUT_HIS, 20, "             raw fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->r_fitness);
		oprintf( OUT_HIS, 20, "    standardized fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->s_fitness);
		oprintf( OUT_HIS, 20, "        adjusted fitness: %.*lf\n", fd,
				run_stats[0].best[i]->ind->a_fitness);

		/* print the tree to both files here. */
		if (test_detail_level(20)) {
			pretty_print_individual(run_stats[0].best[i]->ind, bout);
			pretty_print_individual(run_stats[0].best[i]->ind, hout);
		}
	}

	/* call the end-of-evaluation callback.  returns 1 if user termination
	 criterion is met, 0 otherwise. */
	ret = app_end_of_evaluation(gen, mpop, newbest, gen_stats, run_stats);

	/* close the .bst file. */
	output_stream_close( OUT_BST);

	/* free stats structures for current generation. */
	for (i = 0; i < mpop->size + 1; ++i) {
		for (j = 0; j < gen_stats[i].bestn; ++j)
			--gen_stats[i].best[j]->refcount;
		FREE(gen_stats[i].best);
	}
	FREE(gen_stats);

	/* deallocate saved individuals that are no longer needed. */
	saved_individual_gc();

	/* return value the application callback gave us. */
	if(termination_override==1)
		{termination_override=0;
		return 1;}
	return ret;

}