[gnumeric] Solver: take more constaints into account.



commit f1caff3e3231615122fbfd27de0d10ca77540411
Author: Morten Welinder <terra gnome org>
Date:   Sun Dec 29 11:39:26 2013 -0500

    Solver: take more constaints into account.

 NEWS                          |    1 +
 plugins/nlsolve/ChangeLog     |    5 +++++
 plugins/nlsolve/gnm-nlsolve.c |   32 +++++++++++++++++++-------------
 src/tools/ChangeLog           |    5 +++++
 src/tools/gnm-solver.c        |   17 +++++++++++++----
 5 files changed, 43 insertions(+), 17 deletions(-)
---
diff --git a/NEWS b/NEWS
index 6eaba2c..dda2157 100644
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,7 @@ Morten:
        * Resurrect support for non-ascii filenames on win32.  [#557815]
        * Fix timeout related critical in ItemGrid.
        * Fix win32 print crash.  [#719997]
+       * Fix solver problem with constraints.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.9
diff --git a/plugins/nlsolve/ChangeLog b/plugins/nlsolve/ChangeLog
index 3f8e5a2..c9616b7 100644
--- a/plugins/nlsolve/ChangeLog
+++ b/plugins/nlsolve/ChangeLog
@@ -1,3 +1,8 @@
+2013-12-29  Morten Welinder  <terra gnome org>
+
+       * gnm-nlsolve.c (rosenbrock_iter): Make sure to set the right
+       solution we found!
+
 2013-11-28  Morten Welinder <terra gnome org>
 
        * Release 1.12.9
diff --git a/plugins/nlsolve/gnm-nlsolve.c b/plugins/nlsolve/gnm-nlsolve.c
index 011f869..b43402b 100644
--- a/plugins/nlsolve/gnm-nlsolve.c
+++ b/plugins/nlsolve/gnm-nlsolve.c
@@ -98,6 +98,18 @@ no_discrete:
 }
 
 static void
+print_vector (const char *name, const gnm_float *v, int n)
+{
+       int i;
+
+       if (name)
+               g_printerr ("%s:\n", name);
+       for (i = 0; i < n; i++)
+               g_printerr ("%15.8" GNM_FORMAT_f " ", v[i]);
+       g_printerr ("\n");
+}
+
+static void
 set_value (GnmNlsolve *nl, int i, gnm_float x)
 {
        GnmCell *cell = g_ptr_array_index (nl->vars, i);
@@ -166,6 +178,10 @@ gnm_nlsolve_set_solution (GnmNlsolve *nl)
 
        g_object_set (sol, "result", result, NULL);
        g_object_unref (result);
+
+       if (!gnm_solver_check_constraints (sol)) {
+               g_printerr ("Infeasible solution set\n");
+       }
 }
 
 static gboolean
@@ -221,18 +237,6 @@ gnm_nlsolve_prepare (GnmSolver *sol, WorkbookControl *wbc, GError **err,
        return ok;
 }
 
-static void
-print_vector (const char *name, const gnm_float *v, int n)
-{
-       int i;
-
-       if (name)
-               g_printerr ("%s:\n", name);
-       for (i = 0; i < n; i++)
-               g_printerr ("%15.8" GNM_FORMAT_f " ", v[i]);
-       g_printerr ("\n");
-}
-
 static gnm_float *
 compute_gradient (GnmNlsolve *nl, const gnm_float *xs)
 {
@@ -551,8 +555,10 @@ rosenbrock_iter (GnmNlsolve *nl)
 
                /* ---------------------------------------- */
 
-               if (!nl->tentative)
+               if (!nl->tentative) {
+                       set_vector (nl, nl->xk);
                        gnm_nlsolve_set_solution (nl);
+               }
 
                if (nl->tentative) {
                        if (nl->yk < nl->tentative_yk) {
diff --git a/src/tools/ChangeLog b/src/tools/ChangeLog
index a142704..11f2434 100644
--- a/src/tools/ChangeLog
+++ b/src/tools/ChangeLog
@@ -1,3 +1,8 @@
+2013-12-29  Morten Welinder  <terra gnome org>
+
+       * gnm-solver.c (gnm_solver_check_constraints): Evalutate
+       constraint cells as needed.
+
 2013-12-10  Morten Welinder  <terra gnome org>
 
        * tabulate.c (do_tabulation): Fix cut-off when we go off sheet.
diff --git a/src/tools/gnm-solver.c b/src/tools/gnm-solver.c
index f729bad..d78431d 100644
--- a/src/tools/gnm-solver.c
+++ b/src/tools/gnm-solver.c
@@ -593,6 +593,7 @@ gnm_solver_param_valid (GnmSolverParameters const *sp, GError **err)
                             _("Invalid solver target"));
                return FALSE;
        }
+       gnm_cell_eval (target_cell);
 
        if (!gnm_cell_has_expr (target_cell) ||
            target_cell->value == NULL ||
@@ -1109,7 +1110,10 @@ gnm_solver_check_constraints (GnmSolver *solver)
 
                for (l = input_cells; l; l = l->next) {
                        GnmCell *cell = l->data;
-                       gnm_float val = value_get_as_float (cell->value);
+                       gnm_float val;
+
+                       gnm_cell_eval (cell);
+                       val = value_get_as_float (cell->value);
                        if (sp->options.assume_non_negative && val < 0)
                                break;
                        if (sp->options.assume_discrete &&
@@ -1133,10 +1137,14 @@ gnm_solver_check_constraints (GnmSolver *solver)
                                                     &lhs, &cl,
                                                     &rhs, &cr);
                     i++) {
-                       if (lhs)
+                       if (lhs) {
+                               gnm_cell_eval (lhs);
                                cl = value_get_as_float (lhs->value);
-                       if (rhs)
+                       }
+                       if (rhs) {
+                               gnm_cell_eval (rhs);
                                cr = value_get_as_float (rhs->value);
+                       }
 
                        switch (c->type) {
                        case GNM_SOLVER_INTEGER:
@@ -1293,7 +1301,7 @@ cell_in_cr (GnmCell const *cell, GnmSheetRange *sr, gboolean follow,
 }
 
 static gboolean
-cell_is_constant (GnmCell const *cell, gnm_float *pc)
+cell_is_constant (GnmCell *cell, gnm_float *pc)
 {
        if (!cell)
                return TRUE;
@@ -1301,6 +1309,7 @@ cell_is_constant (GnmCell const *cell, gnm_float *pc)
        if (cell->base.texpr)
                return FALSE;
 
+       gnm_cell_eval (cell);
        *pc = value_get_as_float (cell->value);
        return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]