[gnumeric] solver: respect time limit.
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnumeric] solver: respect time limit.
- Date: Wed, 28 Dec 2011 22:05:08 +0000 (UTC)
commit fc028049f99b1e036d656638f5b1accc24477f54
Author: Morten Welinder <terra gnome org>
Date: Wed Dec 28 16:29:14 2011 -0500
solver: respect time limit.
NEWS | 1 +
src/dialogs/ChangeLog | 4 ++++
src/dialogs/dialog-solver.c | 34 +++++++++++++++++++++++++++-------
src/tools/ChangeLog | 4 ++++
src/tools/gnm-solver.c | 21 +++++++++++++++++++++
src/tools/gnm-solver.h | 2 ++
6 files changed, 59 insertions(+), 7 deletions(-)
---
diff --git a/NEWS b/NEWS
index b320f01..b90eb87 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ Andreas:
Morten:
* Improve parsing on numbers with non-ascii digits.
+ * Make solver check for exceeding the time limit.
--------------------------------------------------------------------------
Gnumeric 1.11.1
diff --git a/src/dialogs/ChangeLog b/src/dialogs/ChangeLog
index 0595be0..fbb4afb 100644
--- a/src/dialogs/ChangeLog
+++ b/src/dialogs/ChangeLog
@@ -1,3 +1,7 @@
+2011-12-28 Morten Welinder <terra gnome org>
+
+ * dialog-solver.c (cb_timer_tick): Check for timeout.
+
2011-12-27 Jean Brefort <jean brefort normalesup org>
* cell-sort.ui: more GtkTable eradication.
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index a5cc776..33f0100 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -87,6 +87,7 @@ typedef struct {
GtkWidget *objective_value_widget;
GtkWidget *stop_button;
GtkWidget *ok_button;
+ char *reason; /* for cancel or error */
gulong sig_notify_result, sig_notify_status;
} run;
@@ -360,6 +361,7 @@ free_state (SolverState *state)
{
if (state->orig_params)
g_object_unref (state->orig_params);
+ g_free (state->run.reason);
g_free (state);
}
@@ -539,7 +541,16 @@ cb_notify_status (SolverState *state)
break;
}
- gtk_label_set_text (GTK_LABEL (state->run.status_widget), text);
+ if (state->run.reason) {
+ char *text2 = g_strconcat (text,
+ " (", state->run.reason, ")",
+ NULL);
+ gtk_label_set_text (GTK_LABEL (state->run.status_widget),
+ text2);
+ g_free (text2);
+ } else {
+ gtk_label_set_text (GTK_LABEL (state->run.status_widget), text);
+ }
if (finished) {
if (state->run.timer_source) {
@@ -599,7 +610,8 @@ cb_notify_result (SolverState *state)
static gboolean
cb_timer_tick (SolverState *state)
{
- double dsecs = gnm_solver_elapsed (state->run.solver);
+ GnmSolver *sol = state->run.solver;
+ double dsecs = gnm_solver_elapsed (sol);
int secs = (int)CLAMP (dsecs, 0, INT_MAX);
int hh = secs / 3600;
int mm = secs / 60 % 60;
@@ -611,6 +623,12 @@ cb_timer_tick (SolverState *state)
gtk_label_set_text (GTK_LABEL (state->run.timer_widget), txt);
g_free (txt);
+ if (gnm_solver_check_timeout (sol, TRUE)) {
+ g_free (state->run.reason);
+ state->run.reason = g_strdup (_("Timeout"));
+ cb_notify_status (state);
+ }
+
return TRUE;
}
@@ -642,6 +660,8 @@ run_solver (SolverState *state, GnmSolverParameters *param)
}
state->run.solver = sol;
+ g_free (state->run.reason);
+ state->run.reason = NULL;
vinput = gnm_solver_param_get_input (param);
gnm_sheet_range_from_value (&sr, vinput);
@@ -924,7 +944,7 @@ dialog_init (SolverState *state)
if (state->dialog == NULL)
return TRUE;
-/* buttons */
+ /* buttons */
state->solve_button = go_gtk_builder_get_widget (state->gui, "solvebutton");
g_signal_connect (G_OBJECT (state->solve_button), "clicked",
G_CALLBACK (cb_dialog_solve_clicked), state);
@@ -1019,7 +1039,7 @@ dialog_init (SolverState *state)
gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->max_time_entry),
param->options.max_time_sec);
-/* lhs_entry */
+ /* lhs_entry */
table = GTK_TABLE (go_gtk_builder_get_widget (state->gui, "edit-table"));
state->lhs.entry = gnm_expr_entry_new (state->wbcg, TRUE);
gnm_expr_entry_set_flags (state->lhs.entry,
@@ -1041,7 +1061,7 @@ dialog_init (SolverState *state)
gnm_expr_entry_get_entry (GNM_EXPR_ENTRY (state->lhs.entry)),
"activate", G_CALLBACK (cb_dialog_add_clicked), state);
-/* rhs_entry */
+ /* rhs_entry */
table = GTK_TABLE (go_gtk_builder_get_widget (state->gui, "edit-table"));
state->rhs.entry = gnm_expr_entry_new (state->wbcg, TRUE);
gnm_expr_entry_set_flags (state->rhs.entry,
@@ -1064,7 +1084,7 @@ dialog_init (SolverState *state)
gnm_expr_entry_get_entry (GNM_EXPR_ENTRY (state->rhs.entry)),
"activate", G_CALLBACK (cb_dialog_add_clicked), state);
-/* type_menu */
+ /* type_menu */
state->type_combo = GTK_COMBO_BOX
(go_gtk_builder_get_widget (state->gui, "type_menu"));
gtk_combo_box_set_active (state->type_combo, 0);
@@ -1072,7 +1092,7 @@ dialog_init (SolverState *state)
G_CALLBACK (dialog_set_sec_button_sensitivity),
state);
-/* constraint_list */
+ /* constraint_list */
state->constraint_list = GTK_TREE_VIEW (go_gtk_builder_get_widget
(state->gui, "constraint_list"));
diff --git a/src/tools/ChangeLog b/src/tools/ChangeLog
index 3f102d4..fb4aedd 100644
--- a/src/tools/ChangeLog
+++ b/src/tools/ChangeLog
@@ -1,3 +1,7 @@
+2011-12-28 Morten Welinder <terra gnome org>
+
+ * gnm-solver.c (gnm_solver_check_timeout): New function.
+
2011-11-27 Morten Welinder <terra gnome org>
* Release 1.11.1
diff --git a/src/tools/gnm-solver.c b/src/tools/gnm-solver.c
index eda06a7..50db8c9 100644
--- a/src/tools/gnm-solver.c
+++ b/src/tools/gnm-solver.c
@@ -915,6 +915,27 @@ gnm_solver_elapsed (GnmSolver *solver)
return endtime - solver->starttime;
}
+gboolean
+gnm_solver_check_timeout (GnmSolver *solver, gboolean act)
+{
+ GnmSolverParameters *sp;
+
+ g_return_val_if_fail (GNM_IS_SOLVER (solver), FALSE);
+
+ sp = solver->params;
+
+ if (solver->status != GNM_SOLVER_STATUS_RUNNING)
+ return FALSE;
+
+ if (gnm_solver_elapsed (solver) <= sp->options.max_time_sec)
+ return FALSE;
+
+ if (act)
+ gnm_solver_stop (solver, NULL);
+
+ return TRUE;
+}
+
void
gnm_solver_store_result (GnmSolver *sol)
{
diff --git a/src/tools/gnm-solver.h b/src/tools/gnm-solver.h
index ee21c8c..7cbd752 100644
--- a/src/tools/gnm-solver.h
+++ b/src/tools/gnm-solver.h
@@ -226,6 +226,8 @@ void gnm_solver_store_result (GnmSolver *solver);
double gnm_solver_elapsed (GnmSolver *solver);
+gboolean gnm_solver_check_timeout (GnmSolver *solver, gboolean act);
+
gboolean gnm_solver_finished (GnmSolver *solver);
gboolean gnm_solver_has_solution (GnmSolver *solver);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]