[gnumeric] Solver: handle target evaluation in GnmSolver.



commit 4c85d5e75d7d03eb78035ff6cf257bba190c62f2
Author: Morten Welinder <terra gnome org>
Date:   Sat Apr 25 13:26:37 2015 -0400

    Solver: handle target evaluation in GnmSolver.

 plugins/nlsolve/gnm-nlsolve.c |   16 +++-------------
 src/tools/ChangeLog           |    2 ++
 src/tools/gnm-solver.c        |   24 ++++++++++++++++++++++++
 src/tools/gnm-solver.h        |    6 +++++-
 4 files changed, 34 insertions(+), 14 deletions(-)
---
diff --git a/plugins/nlsolve/gnm-nlsolve.c b/plugins/nlsolve/gnm-nlsolve.c
index 046d4f4..fbe2140 100644
--- a/plugins/nlsolve/gnm-nlsolve.c
+++ b/plugins/nlsolve/gnm-nlsolve.c
@@ -44,7 +44,6 @@ typedef struct {
 
        /* Input/output cells.  */
        GPtrArray *vars;
-       GnmCell *target;
        GnmCellPos origin;
        int input_width, input_height;
        gboolean maximize; /* See note above */
@@ -148,19 +147,12 @@ set_vector (GnmNlsolve *nl, const gnm_float *xs)
                set_value (nl, i, xs[i]);
 }
 
+/* Get the target value as-if we were minimizing.  */
 static gnm_float
 get_value (GnmNlsolve *nl)
 {
-       GnmValue const *v;
-
-       gnm_cell_eval (nl->target);
-       v = nl->target->value;
-
-       if (VALUE_IS_NUMBER (v) || VALUE_IS_EMPTY (v)) {
-               gnm_float y = value_get_as_float (v);
-               return nl->maximize ? 0 - y : y;
-       } else
-               return gnm_nan;
+       gnm_float y = gnm_solver_get_target_value (GNM_SOLVER (nl->parent));
+       return nl->maximize ? 0 - y : y;
 }
 
 static void
@@ -734,8 +726,6 @@ nlsolve_solver_factory (GnmSolverFactory *factory, GnmSolverParameters *params)
        nl->debug = gnm_solver_debug ();
        nl->min_factor = 1e-10;
 
-       nl->target = gnm_solver_param_get_target_cell (params);
-
        nl->vars = gnm_solver_param_get_input_cells (params);
        n = nl->vars->len;
 
diff --git a/src/tools/ChangeLog b/src/tools/ChangeLog
index c10f211..dee794e 100644
--- a/src/tools/ChangeLog
+++ b/src/tools/ChangeLog
@@ -2,6 +2,8 @@
 
        * gnm-solver.c (gnm_solver_param_get_input_cells): Return result
        as a GPtrArray for random access.  All callers changed.
+       (gnm_solver_get_target_value): New function.  Handle target value
+       in GnmSolver.
 
 2015-04-24  Morten Welinder  <terra gnome org>
 
diff --git a/src/tools/gnm-solver.c b/src/tools/gnm-solver.c
index 6c9abce..722e45c 100644
--- a/src/tools/gnm-solver.c
+++ b/src/tools/gnm-solver.c
@@ -815,6 +815,15 @@ gnm_solver_dispose (GObject *obj)
 }
 
 static void
+gnm_solver_constructed (GObject *obj)
+{
+       GnmSolver *sol = GNM_SOLVER (obj);
+       GnmSolverParameters *params = sol->params;
+       sol->target = gnm_solver_param_get_target_cell (params);
+       gnm_solver_parent_class->constructed (obj);
+}
+
+static void
 gnm_solver_get_property (GObject *object, guint property_id,
                         GValue *value, GParamSpec *pspec)
 {
@@ -1642,12 +1651,27 @@ gnm_solver_create_report (GnmSolver *solver, const char *name)
 #undef ADD_HEADER
 #undef MARK_BAD
 
+gnm_float
+gnm_solver_get_target_value (GnmSolver *solver)
+{
+       GnmValue const *v;
+
+       gnm_cell_eval (solver->target);
+       v = solver->target->value;
+
+       if (VALUE_IS_NUMBER (v) || VALUE_IS_EMPTY (v))
+               return value_get_as_float (v);
+       else
+               return gnm_nan;
+}
+
 static void
 gnm_solver_class_init (GObjectClass *object_class)
 {
        gnm_solver_parent_class = g_type_class_peek_parent (object_class);
 
        object_class->dispose = gnm_solver_dispose;
+       object_class->constructed = gnm_solver_constructed;
        object_class->set_property = gnm_solver_set_property;
        object_class->get_property = gnm_solver_get_property;
 
diff --git a/src/tools/gnm-solver.h b/src/tools/gnm-solver.h
index 5711837..90e8a4c 100644
--- a/src/tools/gnm-solver.h
+++ b/src/tools/gnm-solver.h
@@ -200,6 +200,9 @@ typedef struct {
        GnmSolverParameters *params;
        GnmSolverResult *result;
        double starttime, endtime;
+
+       /* Derived information */
+       GnmCell *target;
 } GnmSolver;
 
 typedef struct {
@@ -245,6 +248,8 @@ gboolean gnm_solver_saveas (GnmSolver *solver, WorkbookControl *wbc,
 
 gboolean gnm_solver_debug (void);
 
+gnm_float gnm_solver_get_target_value (GnmSolver *solver);
+
 /* ------------------------------------------------------------------------- */
 /* Solver subclass for subprocesses. */
 
@@ -313,7 +318,6 @@ typedef struct {
        GnmSolver parent;
 
        guint64 iterations;
-
        guint idle_tag;
 } GnmIterSolver;
 


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