Commit 4c85d5e7 authored by Morten Welinder's avatar Morten Welinder

Solver: handle target evaluation in GnmSolver.

parent 43196c09
......@@ -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;
......
......@@ -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>
......
......@@ -814,6 +814,15 @@ gnm_solver_dispose (GObject *obj)
gnm_solver_parent_class->dispose (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;
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment