Beispiel #1
0
/**
 * cb_dialog_solve_clicked:
 * @button:
 * @state:
 *
 *
 **/
static void
cb_dialog_solve_clicked (G_GNUC_UNUSED GtkWidget *button,
			 SolverState *state)
{
	GnmSolverResult *res;
	GnmSolverParameters *param = state->sheet->solver_parameters;
	GError *err = NULL;

	if (state->warning_dialog != NULL) {
		gtk_widget_destroy (state->warning_dialog);
		state->warning_dialog = NULL;
	}

	extract_settings (state);

	if (!gnm_solver_param_valid (param, &err)) {
		GtkWidget *top = gtk_widget_get_toplevel (state->dialog);
		go_gtk_notice_dialog (GTK_WINDOW (top), GTK_MESSAGE_ERROR,
				      "%s", err->message);
		goto out;
	}

	check_for_changed_options (state);

	res = run_solver (state, param);

	gnm_app_recalc ();

	if (res != NULL) {
		if ((res->quality == GNM_SOLVER_RESULT_OPTIMAL ||
		     res->quality == GNM_SOLVER_RESULT_FEASIBLE) &&
		    param->options.add_scenario)
			solver_add_scenario (state, res,
					     param->options.scenario_name);

		g_object_unref (res);
	} else if (err) {
		go_gtk_notice_nonmodal_dialog
			(GTK_WINDOW (state->dialog),
			 &(state->warning_dialog),
			 GTK_MESSAGE_ERROR,
			 "%s", err->message);
	}

 out:
	if (err)
		g_error_free (err);
}
Beispiel #2
0
static gint
gnm_nlsolve_idle (gpointer data)
{
	GnmNlsolve *nl = data;
	GnmSolver *sol = nl->parent;
	const int n = nl->vars->len;
	gboolean ok;
	gboolean call_again = TRUE;

	if (nl->k == 0)
		rosenbrock_init (nl);

	if (nl->debug) {
		g_printerr ("Iteration %d at %.15" GNM_FORMAT_g "\n",
			    nl->k, nl->yk);
		print_vector ("Current point", nl->xk, n);
	}

	nl->k++;
	ok = rosenbrock_iter (nl);

	if (!ok && !nl->tentative) {
		ok = polish_iter (nl);
	}

	if (!ok) {
		gnm_solver_set_status (sol, GNM_SOLVER_STATUS_DONE);
		call_again = FALSE;
	}

	if (call_again && nl->k >= nl->max_iter) {
		gnm_solver_set_status (sol, GNM_SOLVER_STATUS_DONE);
		call_again = FALSE;
	}

	if (!call_again) {
		set_vector (nl, nl->x0);
		gnm_app_recalc ();

		rosenbrock_shutdown (nl);
	}

	if (!call_again)
		nl->idle_tag = 0;

	return call_again;
}
Beispiel #3
0
void
glpk_file_save (GOFileSaver const *fs, GOIOContext *io_context,
                WorkbookView const *wb_view, GsfOutput *output)
{
    GError *err = NULL;
    GString *prg;
    GnmLocale *locale;
    GnmSolver *sol = NULL;
    GnmSubSolver *ssol = g_object_get_data (G_OBJECT (fs), "solver");

    if (!ssol) {
        // Create a temporary solver just functional enough to
        // write the program
        Sheet *sheet = wb_view_cur_sheet (wb_view);
        sol = glpk_solver_create (sheet->solver_parameters);
        ssol = GNM_SUB_SOLVER (sol);
    }

    go_io_progress_message (io_context,
                            _("Writing glpk file..."));

    locale = gnm_push_C_locale ();
    prg = glpk_create_program (ssol, io_context, &err);
    gnm_pop_C_locale (locale);

    gnm_app_recalc ();

    if (!prg) {
        go_cmd_context_error_import (GO_CMD_CONTEXT (io_context),
                                     err ? err->message : "?");
        goto fail;
    }

    gsf_output_write (output, prg->len, prg->str);
    g_string_free (prg, TRUE);

fail:
    go_io_progress_unset (io_context);
    if (err)
        g_error_free (err);

    if (sol)
        g_object_unref (sol);
}