示例#1
0
static void
gnm_nlsolve_set_solution (GnmNlsolve *nl)
{
	GnmSolver *sol = nl->parent;
	GnmSolverResult *result = g_object_new (GNM_SOLVER_RESULT_TYPE, NULL);
	const int n = nl->vars->len;
	int i;

	result->quality = GNM_SOLVER_RESULT_FEASIBLE;
	result->value = nl->maximize ? 0 - nl->yk : nl->yk;
	result->solution = value_new_array_empty (nl->input_width,
						  nl->input_height);
	for (i = 0; i < n; i++) {
		GnmCell *cell = g_ptr_array_index (nl->vars, i);
		value_array_set (result->solution,
				 cell->pos.col - nl->origin.col,
				 cell->pos.row - nl->origin.row,
				 value_new_float (nl->xk[i]));
	}

	g_object_set (sol, "result", result, NULL);
	g_object_unref (result);

	if (!gnm_solver_check_constraints (sol)) {
		g_printerr ("Infeasible solution set\n");
	}
}
示例#2
0
static gboolean
cb_read_stdout (GIOChannel *channel, GIOCondition cond, GnmLPSolve *lp)
{
	const char obj_line_prefix[] = "Value of objective function:";
	size_t obj_line_len = sizeof (obj_line_prefix) - 1;
	const char val_header_line[] = "Actual values of the variables:";
	size_t val_header_len = sizeof (val_header_line) - 1;

	do {
		GIOStatus status;
		gchar *line = NULL;
		gsize tpos;

		status = g_io_channel_read_line (channel,
						 &line, NULL, &tpos,
						 NULL);
		if (status != G_IO_STATUS_NORMAL)
			break;

		line[tpos] = 0;

		if (line[0] == 0 || g_ascii_isspace (line[0]))
			lp->section = SEC_UNKNOWN;
		else if (lp->section == SEC_UNKNOWN &&
			 !strncmp (line, obj_line_prefix, obj_line_len)) {
			GnmSolverResult *r;
			gnm_lpsolve_flush_solution (lp);
			r = gnm_lpsolve_start_solution (lp);
			r->quality = GNM_SOLVER_RESULT_FEASIBLE;
			r->value = g_ascii_strtod (line + obj_line_len, NULL);
		} else if (lp->section == SEC_UNKNOWN &&
			   !strncmp (line, val_header_line, val_header_len)) {
			lp->section = SEC_VALUES;
		} else if (lp->section == SEC_VALUES && lp->result) {
			GnmSolverResult *r = lp->result;
			int x, y;
			double v;
			char *space = strchr (line, ' ');
			GnmCell *cell;

			if (!space) {
				lp->section = SEC_UNKNOWN;
				continue;
			}
			*space = 0;
			cell = gnm_sub_solver_find_cell (lp->parent, line);
			if (!cell) {
				g_printerr ("Strange cell %s in output\n",
					    line);
				lp->section = SEC_UNKNOWN;
				continue;
			}

			v = g_ascii_strtod (space + 1, NULL);
			x = cell->pos.col - lp->srinput.range.start.col;
			y = cell->pos.row - lp->srinput.range.start.row;
			if (x >= 0 &&
			    x < value_area_get_width (r->solution, NULL) &&
			    y >= 0 &&
			    y < value_area_get_height (r->solution, NULL))
				value_array_set (r->solution, x, y,
						 value_new_float (v));
		}
		g_free (line);
	} while (1);

	return TRUE;
}