[gnumeric] nlsolve: make maximize work.



commit 9a0a1262fbea7949da1d41fa0e31575b0d290853
Author: Morten Welinder <terra gnome org>
Date:   Sat May 29 21:45:16 2010 -0400

    nlsolve: make maximize work.

 plugins/nlsolve/ChangeLog     |    6 ++++++
 plugins/nlsolve/gnm-nlsolve.c |   21 ++++++++-------------
 2 files changed, 14 insertions(+), 13 deletions(-)
---
diff --git a/plugins/nlsolve/ChangeLog b/plugins/nlsolve/ChangeLog
index 2b09e8f..dfe408f 100644
--- a/plugins/nlsolve/ChangeLog
+++ b/plugins/nlsolve/ChangeLog
@@ -1,3 +1,9 @@
+2010-05-29  Morten Welinder  <terra gnome org>
+
+	* gnm-nlsolve.c (get_value, gnm_nlsolve_set_solution): Flip sign
+	for maximize.
+	(check_program): Don't reject maximize.
+
 2010-05-23  Morten Welinder  <terra gnome org>
 
 	* gnm-nlsolve.c (gnm_nlsolve_idle): Use the axis-method even if
diff --git a/plugins/nlsolve/gnm-nlsolve.c b/plugins/nlsolve/gnm-nlsolve.c
index eaecef0..2cba648 100644
--- a/plugins/nlsolve/gnm-nlsolve.c
+++ b/plugins/nlsolve/gnm-nlsolve.c
@@ -21,6 +21,7 @@ typedef struct {
 	GnmCell *target;
 	GnmCellPos origin;
 	int input_width, input_height;
+	gboolean maximize;
 
 	/* Initial point.  */
 	gnm_float *x0;
@@ -78,15 +79,6 @@ check_program (const GnmSolverParameters *params, GError **err)
 			goto no_discrete;
 	}
 
-	if (params->problem_type != GNM_SOLVER_MINIMIZE) {
-		/* Not a fundamental problem, just not done.  */
-		g_set_error (err,
-			     go_error_invalid (),
-			     0,
-			     _("This solver handles only minimization."));
-		return FALSE;
-	}
-
 	return TRUE;
 
 no_discrete:
@@ -128,9 +120,10 @@ get_value (GnmNlsolve *nl)
 	gnm_cell_eval (nl->target);
 	v = nl->target->value;
 
-	if (VALUE_IS_NUMBER (v) || VALUE_IS_EMPTY (v))
-		return value_get_as_float (v);
-	else
+	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;
 }
 
@@ -152,7 +145,7 @@ gnm_nlsolve_set_solution (GnmNlsolve *nl)
 	int i;
 
 	result->quality = GNM_SOLVER_RESULT_FEASIBLE;
-	result->value = nl->yk;
+	result->value = nl->maximize ? 0 - nl->yk : nl->yk;
 	result->solution = value_new_array_empty (nl->input_width,
 						  nl->input_height);
 	for (i = 0; i < n; i++) {
@@ -738,6 +731,8 @@ nlsolve_solver_factory (GnmSolverFactory *factory, GnmSolverParameters *params)
 
 	nl->parent = GNM_SOLVER (res);
 
+	nl->maximize = (params->problem_type == GNM_SOLVER_MAXIMIZE);
+
 	eval_pos_init_sheet (&ep, params->sheet);
 	if (vinput) {
 		gnm_cellref_make_abs (&origin, &vinput->v_range.cell.a, &ep);



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