[gnumeric] solver: even more cleanups.



commit 7d3aa25ecf88a7696e34d261763c7cd3a9febd40
Author: Morten Welinder <terra gnome org>
Date:   Wed Nov 4 20:33:05 2009 -0500

    solver: even more cleanups.

 plugins/lpsolve/lpsolve-write.c  |    2 +-
 src/dialogs/dialog-solver.c      |   13 +-------
 src/ranges.c                     |    2 +-
 src/solver.h                     |    5 ++-
 src/tools/solver/reports-write.c |    4 +-
 src/tools/solver/reports.c       |    8 +++---
 src/tools/solver/solver.c        |   56 +++++++++++++++++++++++++++-----------
 7 files changed, 53 insertions(+), 37 deletions(-)
---
diff --git a/plugins/lpsolve/lpsolve-write.c b/plugins/lpsolve/lpsolve-write.c
index 986e482..6ec114e 100644
--- a/plugins/lpsolve/lpsolve-write.c
+++ b/plugins/lpsolve/lpsolve-write.c
@@ -222,7 +222,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
 		}
 
 		for (i = 0;
-		     gnm_solver_constraint_get_part (c, sheet, i,
+		     gnm_solver_constraint_get_part (c, sp, i,
 						     &lhs, &cl,
 						     &rhs, &cr);
 		     i++) {
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index b74ff40..ed5a995 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -76,8 +76,6 @@ typedef struct {
 	SolverConstraint    *constr;
 	GtkWidget           *warning_dialog;
 
-	gboolean             cancelled;
-
 	Sheet		    *sheet;
 	WBCGtk              *wbcg;
 } SolverState;
@@ -138,9 +136,10 @@ dialog_set_sec_button_sensitivity (G_GNUC_UNUSED GtkWidget *dummy,
 	gboolean select_ready = (state->constr != NULL);
 	SolverConstraint *test = gnm_solver_constraint_new (NULL);
 	gboolean ready, has_rhs;
+	SolverParameters const *param = state->sheet->solver_parameters;
 
 	constraint_fill (test, state);
-	ready = gnm_solver_constraint_valid (test);
+	ready = gnm_solver_constraint_valid (test, param);
 	has_rhs = gnm_solver_constraint_has_rhs (test);
 	gnm_solver_constraint_free (test);
 
@@ -351,7 +350,6 @@ static void
 cb_dialog_close_clicked (G_GNUC_UNUSED GtkWidget *button,
 			 SolverState *state)
 {
-	state->cancelled = FALSE;
 	gtk_widget_destroy (state->dialog);
 }
 
@@ -598,8 +596,6 @@ cb_dialog_solve_clicked (G_GNUC_UNUSED GtkWidget *button,
 	workbook_recalc (state->sheet->workbook);
 
 	if (res != NULL) {
-		state->cancelled = FALSE;
-
 		/* WARNING : The dialog may be deleted by the reports
 		 * solver_reporting will return FALSE if state is gone and cleared */
 		if (solver_reporting (state, res) &&
@@ -968,10 +964,6 @@ dialog_solver (WBCGtk *wbcg, Sheet *sheet)
 {
         SolverState *state;
 
-	if (wbcg == NULL) {
-		return;
-	}
-
 	/* Only pop up one copy per workbook */
 	if (gnumeric_dialog_raise_if_exists (wbcg, SOLVER_KEY))
 		return;
@@ -980,7 +972,6 @@ dialog_solver (WBCGtk *wbcg, Sheet *sheet)
 	state->wbcg           = wbcg;
 	state->sheet          = sheet;
 	state->warning_dialog = NULL;
-	state->cancelled      = TRUE;
 
 	if (dialog_init (state)) {
 		go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
diff --git a/src/ranges.c b/src/ranges.c
index 39e2a5a..15ca36b 100644
--- a/src/ranges.c
+++ b/src/ranges.c
@@ -1124,7 +1124,7 @@ global_range_list_foreach (GSList *gr_list, GnmEvalPos const *ep,
  * @a:
  * @b:
  *
- * return true if a is containde in b
+ * return true if a is contained in b
  * we do not handle 3d ranges
  **/
 gboolean
diff --git a/src/solver.h b/src/solver.h
index 006504a..829f106 100644
--- a/src/solver.h
+++ b/src/solver.h
@@ -113,9 +113,10 @@ void gnm_solver_constraint_set_old (SolverConstraint *c,
 				    int cols, int rows);
 
 gboolean gnm_solver_constraint_has_rhs (SolverConstraint const *c);
-gboolean gnm_solver_constraint_valid (SolverConstraint const *c);
+gboolean gnm_solver_constraint_valid (SolverConstraint const *c,
+				      SolverParameters const *sp);
 gboolean gnm_solver_constraint_get_part (SolverConstraint const *c,
-					 Sheet *sheet, int i,
+					 SolverParameters const *sp, int i,
 					 GnmCell **lhs, gnm_float *cl,
 					 GnmCell **rhs, gnm_float *cr);
 
diff --git a/src/tools/solver/reports-write.c b/src/tools/solver/reports-write.c
index 90f830b..02cd25a 100644
--- a/src/tools/solver/reports-write.c
+++ b/src/tools/solver/reports-write.c
@@ -138,7 +138,7 @@ solver_answer_report (WorkbookControl *wbc,
 		char *str = gnm_solver_constraint_as_str (c, sheet);
 		GnmCell *lhs, *rhs;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, res->param, 0,
 						&lhs, NULL,
 						&rhs, NULL);
 
@@ -299,7 +299,7 @@ solver_sensitivity_report (WorkbookControl *wbc,
 	        SolverConstraint *c = res->constraints_array[i];
 		GnmCell *lhs, *rhs;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, res->param, 0,
 						&lhs, NULL,
 						&rhs, NULL);
 
diff --git a/src/tools/solver/reports.c b/src/tools/solver/reports.c
index a5f847c..3fef10c 100644
--- a/src/tools/solver/reports.c
+++ b/src/tools/solver/reports.c
@@ -72,7 +72,7 @@ get_constraint_names (SolverResults *res, Sheet *sheet)
 	        SolverConstraint *c = solver_get_constraint (res, i);
 		GnmCell *lhs;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, res->param, 0,
 						&lhs, NULL, NULL, NULL);
 
 		res->constraint_names[i] = lhs
@@ -109,7 +109,7 @@ is_still_feasible (Sheet *sheet, SolverResults *res, int col, gnm_float value)
 	        SolverConstraint *c = solver_get_constraint (res, i);
 		GnmCell *cell;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, res->param, 0,
 						NULL, NULL, &cell, NULL);
 
 		c_value = 0;
@@ -156,7 +156,7 @@ calculate_limits (Sheet *sheet, SolverParameters *param, SolverResults *res)
 		SolverConstraint *c = res->constraints_array[i];
 		GnmCell             *lcell, *rcell;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, res->param, 0,
 						&lcell, NULL, &rcell, NULL);
 		rhs   = value_get_as_float (rcell->value);
 		lhs   = value_get_as_float (lcell->value);
@@ -281,7 +281,7 @@ solver_prepare_reports_success (SolverProgram *program, SolverResults *res,
 	        SolverConstraint const *c = solver_get_constraint (res, i);
 		GnmCell *lhs;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, param, 0,
 						&lhs, NULL,
 						NULL, NULL);
 
diff --git a/src/tools/solver/solver.c b/src/tools/solver/solver.c
index a5d7992..6d30b63 100644
--- a/src/tools/solver/solver.c
+++ b/src/tools/solver/solver.c
@@ -169,7 +169,7 @@ gnm_solver_param_get_target (SolverParameters const *sp)
 		? gnm_expr_top_get_cellref (sp->target.texpr)
 		: NULL;
 }
-      
+
 GnmCell *
 gnm_solver_param_get_target_cell (SolverParameters const *sp)
 {
@@ -231,7 +231,7 @@ gnm_solver_param_valid (SolverParameters const *sp, GError **err)
 
 	for (i = 1, l = sp->constraints; l; i++, l = l->next) {
 		SolverConstraint *c = l->data;
-		if (!gnm_solver_constraint_valid (c)) {
+		if (!gnm_solver_constraint_valid (c, sp)) {
 			g_set_error (err,
 				     go_error_invalid (),
 				     0,
@@ -488,9 +488,11 @@ gnm_solver_constraint_has_rhs (SolverConstraint const *c)
 }
 
 gboolean
-gnm_solver_constraint_valid (SolverConstraint const *c)
+gnm_solver_constraint_valid (SolverConstraint const *c,
+			     SolverParameters const *sp)
 {
 	GnmValue const *lhs;
+
 	g_return_val_if_fail (c != NULL, FALSE);
 
 	lhs = gnm_solver_constraint_get_lhs (c);
@@ -516,7 +518,28 @@ gnm_solver_constraint_valid (SolverConstraint const *c)
 			return FALSE;
 	}
 
-#warning "We need to check that int/bool constraints refer to input cells."
+	switch (c->type) {
+	case SolverINT:
+	case SolverBOOL: {
+		GnmValue const *vinput = gnm_solver_param_get_input (sp);
+		GnmSheetRange sr_input, sr_c;
+
+		if (!vinput)
+			break; /* No need to blame contraint.  */
+
+		gnm_sheet_range_from_value (&sr_input, vinput);
+		gnm_sheet_range_from_value (&sr_c, lhs);
+
+		if (eval_sheet (sr_input.sheet, sp->sheet) !=
+		    eval_sheet (sr_c.sheet, sp->sheet) ||
+		    !range_contained (&sr_c.range, &sr_input.range))
+			return FALSE;
+		break;
+	}
+
+	default:
+		break;
+	}
 
 	return TRUE;
 }
@@ -576,7 +599,8 @@ gnm_solver_constraint_set_rhs (SolverConstraint *c, GnmValue *v)
 
 
 gboolean
-gnm_solver_constraint_get_part (SolverConstraint const *c, Sheet *sheet, int i,
+gnm_solver_constraint_get_part (SolverConstraint const *c,
+				SolverParameters const *sp, int i,
 				GnmCell **lhs, gnm_float *cl,
 				GnmCell **rhs, gnm_float *cr)
 {
@@ -589,7 +613,7 @@ gnm_solver_constraint_get_part (SolverConstraint const *c, Sheet *sheet, int i,
 	if (lhs) *lhs = NULL;
 	if (rhs) *rhs = NULL;
 
-	if (!gnm_solver_constraint_valid (c))
+	if (!gnm_solver_constraint_valid (c, sp))
 		return FALSE;
 
 	vl = gnm_solver_constraint_get_lhs (c);
@@ -605,7 +629,7 @@ gnm_solver_constraint_get_part (SolverConstraint const *c, Sheet *sheet, int i,
 		return FALSE;
 
 	if (lhs)
-		*lhs = sheet_cell_get (sheet,
+		*lhs = sheet_cell_get (sp->sheet,
 				       r.start.col + dx, r.start.row + dy);
 
 	if (gnm_solver_constraint_has_rhs (c)) {
@@ -615,7 +639,7 @@ gnm_solver_constraint_get_part (SolverConstraint const *c, Sheet *sheet, int i,
 		} else {
 			range_init_value (&r, vr);
 			if (rhs)
-				*rhs = sheet_cell_get (sheet,
+				*rhs = sheet_cell_get (sp->sheet,
 						       r.start.col + dx,
 						       r.start.row + dy);
 		}
@@ -626,7 +650,7 @@ gnm_solver_constraint_get_part (SolverConstraint const *c, Sheet *sheet, int i,
 
 static gboolean
 gnm_solver_constraint_get_part_val (SolverConstraint const *c,
-				    Sheet *sheet, int i,
+				    SolverParameters const *sp, int i,
 				    GnmValue **lhs, GnmValue **rhs)
 {
 	GnmRange r;
@@ -636,7 +660,7 @@ gnm_solver_constraint_get_part_val (SolverConstraint const *c,
 	if (lhs) *lhs = NULL;
 	if (rhs) *rhs = NULL;
 
-	if (!gnm_solver_constraint_valid (c))
+	if (!gnm_solver_constraint_valid (c, sp))
 		return FALSE;
 
 	vl = gnm_solver_constraint_get_lhs (c);
@@ -654,7 +678,7 @@ gnm_solver_constraint_get_part_val (SolverConstraint const *c,
 	r.start.col += dx;
 	r.start.row += dy;
 	r.end = r.start;
-	if (lhs) *lhs = value_new_cellrange_r (sheet, &r);
+	if (lhs) *lhs = value_new_cellrange_r (sp->sheet, &r);
 
 	if (rhs && gnm_solver_constraint_has_rhs (c)) {
 		if (VALUE_IS_FLOAT (vr)) {
@@ -664,7 +688,7 @@ gnm_solver_constraint_get_part_val (SolverConstraint const *c,
 			r.start.col += dx;
 			r.start.row += dy;
 			r.end = r.start;
-			*rhs = value_new_cellrange_r (sheet, &r);
+			*rhs = value_new_cellrange_r (sp->sheet, &r);
 		}
 	}
 
@@ -877,7 +901,7 @@ lp_qp_solver_init (Sheet *sheet, const SolverParameters *param,
 		const GnmValue *rval;
 		gnm_float lx, rx;
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, param, 0,
 						&target, NULL,
 						NULL, NULL);
 		if (target) {
@@ -924,7 +948,7 @@ lp_qp_solver_init (Sheet *sheet, const SolverParameters *param,
 			}
 		}
 
-		gnm_solver_constraint_get_part (c, sheet, 0,
+		gnm_solver_constraint_get_part (c, param, 0,
 						NULL, NULL, &target, NULL);
 		if (target) {
 			gnm_cell_eval (target);
@@ -1072,7 +1096,7 @@ check_program_definition_failures (Sheet            *sheet,
 		int N = gnm_solver_constraint_get_size (sc);
 
 		if (sc->type == SolverINT)
-		        param->n_int_constraints += N;			        
+		        param->n_int_constraints += N;
 		else if (sc->type == SolverBOOL)
 		        param->n_bool_constraints += N;
 		else
@@ -1089,7 +1113,7 @@ check_program_definition_failures (Sheet            *sheet,
 		GnmValue *lhs, *rhs;
 
 		for (j = 0;
-		     gnm_solver_constraint_get_part_val (sc, sheet, j,
+		     gnm_solver_constraint_get_part_val (sc, param, j,
 							 &lhs, &rhs);
 		     j++) {
 			SolverConstraint *nc =



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