dialog-solver.c 35 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
/*
3
 * dialog-solver.c:
4 5 6
 *
 * Author:
 *  Jukka-Pekka Iivonen <iivonen@iki.fi>
7
 *
8
 * (C) Copyright 2000, 2002 by Jukka-Pekka Iivonen <iivonen@iki.fi>
9
 * (C) Copyright 2009-2013 Morten Welinder (terra@gnome.org)
jpekka's avatar
jpekka committed
10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23 24
 */

25
#include <gnumeric-config.h>
26
#include <glib/gi18n-lib.h>
27 28
#include <gnumeric.h>
#include "dialogs.h"
29
#include "help.h"
30 31 32

#include <gui-util.h>
#include <func.h>
33
#include <dialogs/tool-dialogs.h>
34 35 36
#include <value.h>
#include <cell.h>
#include <sheet.h>
37
#include <sheet-view.h>
Morten Welinder's avatar
Morten Welinder committed
38
#include <expr.h>
39
#include <wbc-gtk.h>
40 41 42
#include <workbook.h>
#include <parse-util.h>
#include <ranges.h>
Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
43
#include <commands.h>
44
#include <clipboard.h>
45
#include <selection.h>
46
#include <application.h>
47
#include <tools/gnm-solver.h>
48 49
#include <widgets/gnumeric-expr-entry.h>

50
#include <goffice/goffice.h>
51
#include <string.h>
52
#include <tools/scenarios.h>
53 54 55 56

#define SOLVER_KEY            "solver-dialog"

typedef struct {
Morten Welinder's avatar
Morten Welinder committed
57
	int                 ref_count;
58
	GtkBuilder          *gui;
59
	GtkWidget           *dialog;
60
	GtkWidget           *notebook;
61 62
	GnmExprEntry	    *target_entry;
	GnmExprEntry	    *change_cell_entry;
63 64
	GtkWidget           *max_iter_entry;
	GtkWidget           *max_time_entry;
65
	GtkWidget           *gradient_order_entry;
66 67
	GtkWidget           *solve_button;
	GtkWidget           *close_button;
Morten Welinder's avatar
Morten Welinder committed
68 69
	GtkWidget           *stop_button;
	GtkWidget           *help_button;
70 71 72
	GtkWidget           *add_button;
	GtkWidget           *change_button;
	GtkWidget           *delete_button;
73
	GtkWidget           *scenario_name_entry;
74
	struct {
Morten Welinder's avatar
Morten Welinder committed
75 76
		GnmExprEntry*entry;
		GtkWidget   *label;
77
	} lhs, rhs;
Morten Welinder's avatar
Morten Welinder committed
78 79 80
	GtkComboBox         *type_combo;
	GtkComboBox         *algorithm_combo;
	GtkTreeView         *constraint_list;
81
	GnmSolverConstraint *constr;
82 83
	GtkWidget           *warning_dialog;

84 85 86 87 88
	struct {
		GnmSolver   *solver;
		GtkWidget   *timer_widget;
		guint       timer_source;
		GtkWidget   *status_widget;
89 90
		GtkWidget   *problem_status_widget;
		GtkWidget   *objective_value_widget;
91
		guint       obj_val_source;
Morten Welinder's avatar
Morten Welinder committed
92 93
		GtkWidget   *spinner;
		guint        in_main;
94 95
	} run;

96
	Sheet		    *sheet;
Morten Welinder's avatar
Morten Welinder committed
97
	WBCGtk              *wbcg;
98 99

	GnmSolverParameters *orig_params;
100 101 102
} SolverState;


Jody Goldberg's avatar
Jody Goldberg committed
103
static char const * const problem_type_group[] = {
104 105 106
	"min_button",
	"max_button",
	"equal_to_button",
107
	NULL
108
};
109

Jody Goldberg's avatar
Jody Goldberg committed
110
static char const * const model_type_group[] = {
111 112 113
	"lp_model_button",
	"qp_model_button",
	"nlp_model_button",
114
	NULL
115 116
};

117
static void
Morten Welinder's avatar
Morten Welinder committed
118
constraint_fill (GnmSolverConstraint *c, SolverState *state)
119
{
120 121
	Sheet *sheet = state->sheet;

122 123
	c->type = gtk_combo_box_get_active (state->type_combo);

124 125 126
	gnm_solver_constraint_set_lhs
		(c,
		 gnm_expr_entry_parse_as_value (state->lhs.entry, sheet));
127

128 129 130 131 132
	gnm_solver_constraint_set_rhs
		(c,
		 gnm_solver_constraint_has_rhs (c)
		 ? gnm_expr_entry_parse_as_value (state->rhs.entry, sheet)
		 : NULL);
133
}
134

135
static gboolean
Morten Welinder's avatar
Morten Welinder committed
136
dialog_set_sec_button_sensitivity (G_GNUC_UNUSED GtkWidget *dummy,
137
				   SolverState *state)
138
{
139
	gboolean select_ready = (state->constr != NULL);
Morten Welinder's avatar
Morten Welinder committed
140
	GnmSolverConstraint *test = gnm_solver_constraint_new (NULL);
141
	gboolean ready, has_rhs;
142
	GnmSolverParameters const *param = state->sheet->solver_parameters;
143

144
	constraint_fill (test, state);
145
	ready = gnm_solver_constraint_valid (test, param);
146 147
	has_rhs = gnm_solver_constraint_has_rhs (test);
	gnm_solver_constraint_free (test);
148 149 150 151

	gtk_widget_set_sensitive (state->add_button, ready);
	gtk_widget_set_sensitive (state->change_button, select_ready && ready);
	gtk_widget_set_sensitive (state->delete_button, select_ready);
152 153
	gtk_widget_set_sensitive (GTK_WIDGET (state->rhs.entry), has_rhs);
	gtk_widget_set_sensitive (GTK_WIDGET (state->rhs.label), has_rhs);
154 155 156

	/* Return TRUE iff the current constraint is valid.  */
	return ready;
157
}
158

159
static void
160
constraint_select_click (GtkTreeSelection *Selection,
Morten Welinder's avatar
Morten Welinder committed
161
			 SolverState * state)
162
{
163 164
	GtkTreeModel *store;
	GtkTreeIter iter;
Morten Welinder's avatar
Morten Welinder committed
165
	GnmSolverConstraint const *c;
166
	GnmValue const *lhs, *rhs;
167

168 169 170 171
	if (gtk_tree_selection_get_selected (Selection, &store, &iter))
		gtk_tree_model_get (store, &iter, 1, &state->constr, -1);
	else
		state->constr = NULL;
172
	dialog_set_sec_button_sensitivity (NULL, state);
173

174
	if (state->constr == NULL)
175
		return; /* Fail? */
Morten Welinder's avatar
Morten Welinder committed
176 177
	c = state->constr;

178 179
	lhs = gnm_solver_constraint_get_lhs (c);
	if (lhs) {
Morten Welinder's avatar
Morten Welinder committed
180
		GnmExprTop const *texpr =
181
			gnm_expr_top_new_constant (value_dup (lhs));
Morten Welinder's avatar
Morten Welinder committed
182 183 184 185 186 187 188 189 190 191
		GnmParsePos pp;

		gnm_expr_entry_load_from_expr
			(state->lhs.entry,
			 texpr,
			 parse_pos_init_sheet (&pp, state->sheet));
		gnm_expr_top_unref (texpr);
	} else
		gnm_expr_entry_load_from_text (state->lhs.entry, "");

192 193
	rhs = gnm_solver_constraint_get_rhs (c);
	if (rhs && gnm_solver_constraint_has_rhs (c)) {
Morten Welinder's avatar
Morten Welinder committed
194
		GnmExprTop const *texpr =
195
			gnm_expr_top_new_constant (value_dup (rhs));
Morten Welinder's avatar
Morten Welinder committed
196 197 198 199 200 201 202
		GnmParsePos pp;

		gnm_expr_entry_load_from_expr
			(state->rhs.entry,
			 texpr,
			 parse_pos_init_sheet (&pp, state->sheet));
		gnm_expr_top_unref (texpr);
jpekka's avatar
jpekka committed
203
	} else
204
		gnm_expr_entry_load_from_text (state->rhs.entry, "");
205

Morten Welinder's avatar
Morten Welinder committed
206
	gtk_combo_box_set_active (state->type_combo, c->type);
207 208
}

209 210 211 212 213
/**
 * cb_dialog_delete_clicked:
 * @button:
 * @state:
 *
214
 *
215 216
 **/
static void
Morten Welinder's avatar
Morten Welinder committed
217
cb_dialog_delete_clicked (G_GNUC_UNUSED GtkWidget *button, SolverState *state)
218
{
219
	if (state->constr != NULL) {
220 221
		GtkTreeIter iter;
		GtkTreeModel *store;
222
		GnmSolverParameters *param = state->sheet->solver_parameters;
223

224 225 226
		param->constraints =
			g_slist_remove (param->constraints, state->constr);
		gnm_solver_constraint_free (state->constr);
227
		state->constr = NULL;
228 229 230 231 232 233

		if (gtk_tree_selection_get_selected (
			    gtk_tree_view_get_selection (state->constraint_list),
			    &store, &iter))
			gtk_list_store_remove ((GtkListStore*)store, &iter);
	}
234
}
235

236
static void
237
constraint_fill_row (SolverState *state, GtkListStore *store, GtkTreeIter *iter)
238
{
239
	char         *text;
Morten Welinder's avatar
Morten Welinder committed
240
	GnmSolverConstraint *c = state->constr;
241

242 243
	constraint_fill (c, state);

244
	text = gnm_solver_constraint_as_str (c, state->sheet);
245
	gtk_list_store_set (store, iter, 0, text, 1, c, -1);
246 247 248 249 250
	g_free (text);
	gtk_tree_selection_select_iter (gtk_tree_view_get_selection (state->constraint_list), iter);
}

static void
251
cb_dialog_add_clicked (SolverState *state)
252
{
253 254 255
	if (dialog_set_sec_button_sensitivity (NULL, state)) {
		GtkTreeIter   iter;
		GtkListStore *store = GTK_LIST_STORE (gtk_tree_view_get_model (state->constraint_list));
256
		GnmSolverParameters *param = state->sheet->solver_parameters;
257 258

		gtk_list_store_append (store, &iter);
259
		state->constr = gnm_solver_constraint_new (state->sheet);
260
		constraint_fill_row (state, store, &iter);
261 262
		param->constraints =
			g_slist_append (param->constraints, state->constr);
263
	}
264 265 266
}

static void
Morten Welinder's avatar
Morten Welinder committed
267
cb_dialog_change_clicked (GtkWidget *button, SolverState *state)
268
{
269
	if (state->constr != NULL) {
270 271 272 273 274 275
		GtkTreeIter iter;
		GtkTreeModel *store;

		if (gtk_tree_selection_get_selected (
			    gtk_tree_view_get_selection (state->constraint_list),
			    &store, &iter))
276
			constraint_fill_row (state, (GtkListStore *)store, &iter);
277
	}
278
}
279

280
static void
Morten Welinder's avatar
Morten Welinder committed
281
dialog_set_main_button_sensitivity (G_GNUC_UNUSED GtkWidget *dummy,
282
				    SolverState *state)
283
{
284
	gboolean ready;
285

286 287 288 289
	ready = gnm_expr_entry_is_cell_ref (state->target_entry, state->sheet,
					    FALSE)
		&& gnm_expr_entry_is_cell_ref (state->change_cell_entry,
					       state->sheet, TRUE);
290
	gtk_widget_set_sensitive (state->solve_button, ready);
291
}
292

293
static gboolean
294
fill_algorithm_combo (SolverState *state, GnmSolverModelType type)
295 296 297 298
{
	GtkListStore *store =
		gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
	GSList *solvers, *l;
299 300
	int sel = 0, i;
	GnmSolverParameters *param =state->sheet->solver_parameters;
301 302 303 304 305 306 307 308 309 310

	gtk_combo_box_set_model (state->algorithm_combo, GTK_TREE_MODEL (store));

	l = NULL;
	for (solvers = gnm_solver_db_get (); solvers; solvers = solvers->next) {
		GnmSolverFactory *entry = solvers->data;
		if (type != entry->type)
			continue;
		l = g_slist_prepend (l, entry);
	}
311
	solvers = g_slist_reverse (l);
312 313 314 315 316 317

	gtk_widget_set_sensitive (GTK_WIDGET (state->solve_button),
				  solvers != NULL);
	if (!solvers)
		return FALSE;

318
	for (l = solvers, i = 0; l; l = l->next, i++) {
319 320 321
		GnmSolverFactory *factory = l->data;
		GtkTreeIter iter;

322 323 324
		if (param->options.algorithm == factory)
			sel = i;

325 326 327 328 329 330 331 332
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store, &iter,
				    0, factory->name,
				    1, factory,
				    -1);
	}
	g_slist_free (solvers);

333
	gtk_combo_box_set_active (state->algorithm_combo, sel);
334

Morten Welinder's avatar
Morten Welinder committed
335 336
	g_object_unref (store);

337 338 339
	return TRUE;
}

340
static void
Morten Welinder's avatar
Morten Welinder committed
341
cb_dialog_model_type_clicked (G_GNUC_UNUSED GtkWidget *button,
342
			      SolverState *state)
343
{
344
	GnmSolverModelType type;
345
	gboolean any;
346

347
	type = gnm_gui_group_value (state->gui, model_type_group);
348 349 350 351 352 353 354 355 356 357
	any = fill_algorithm_combo (state, type);

	if (!any) {
		go_gtk_notice_nonmodal_dialog
			(GTK_WINDOW (state->dialog),
			 &(state->warning_dialog),
			 GTK_MESSAGE_INFO,
			 _("Looking for a subject for your thesis? "
			   "Maybe you would like to write a solver for "
			   "Gnumeric?"));
358
	}
359 360
}

361
static void
Morten Welinder's avatar
Morten Welinder committed
362
unref_state (SolverState *state)
363
{
Morten Welinder's avatar
Morten Welinder committed
364 365 366 367
	state->ref_count--;
	if (state->ref_count > 0)
		return;

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	if (state->orig_params)
		g_object_unref (state->orig_params);
	g_free (state);
}

static GOUndo *
set_params (Sheet *sheet, GnmSolverParameters *params)
{
	return go_undo_binary_new
		(sheet, g_object_ref (params),
		 (GOUndoBinaryFunc)gnm_sheet_set_solver_params,
		 NULL, g_object_unref);
}

#define GET_BOOL_ENTRY(name_, field_)					\
383
	do {								\
Morten Welinder's avatar
Morten Welinder committed
384
		GtkWidget *w_ = go_gtk_builder_get_widget (state->gui, (name_)); \
385 386
		param->field_ = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w_)); \
	} while (0)
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

static void
extract_settings (SolverState *state)
{
	GnmSolverParameters *param = state->sheet->solver_parameters;
	GtkTreeIter iter;
	GnmValue *target_range;
	GnmValue *input_range;
	GnmSolverFactory *factory = NULL;

	target_range = gnm_expr_entry_parse_as_value (state->target_entry,
						      state->sheet);
	input_range = gnm_expr_entry_parse_as_value (state->change_cell_entry,
						     state->sheet);

	gnm_solver_param_set_input (param, input_range);

	gnm_solver_param_set_target (param,
				     target_range
				     ? &target_range->v_range.cell.a
				     : NULL);

	param->problem_type =
410
		gnm_gui_group_value (state->gui, problem_type_group);
411
	param->options.model_type =
412
		gnm_gui_group_value (state->gui, model_type_group);
413

414 415 416 417 418 419
	if (gtk_combo_box_get_active_iter (state->algorithm_combo, &iter)) {
		gtk_tree_model_get (gtk_combo_box_get_model (state->algorithm_combo),
				    &iter, 1, &factory, -1);
		gnm_solver_param_set_algorithm (param, factory);
	} else
		gnm_solver_param_set_algorithm (param, NULL);
420 421 422 423 424

	param->options.max_iter = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->max_iter_entry));
	param->options.max_time_sec = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->max_time_entry));
425 426
	param->options.gradient_order = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->gradient_order_entry));
427

428
	GET_BOOL_ENTRY ("autoscale_button", options.automatic_scaling);
429 430 431
	GET_BOOL_ENTRY ("non_neg_button", options.assume_non_negative);
	GET_BOOL_ENTRY ("all_int_button", options.assume_discrete);
	GET_BOOL_ENTRY ("program", options.program_report);
432
	GET_BOOL_ENTRY ("sensitivity", options.sensitivity_report);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	g_free (param->options.scenario_name);
	param->options.scenario_name = g_strdup
		(gtk_entry_get_text (GTK_ENTRY (state->scenario_name_entry)));

	GET_BOOL_ENTRY ("optimal_scenario", options.add_scenario);

	value_release (target_range);
}

#undef GET_BOOL_ENTRY

static void
check_for_changed_options (SolverState *state)
{
	Sheet *sheet = state->sheet;

	if (!gnm_solver_param_equal (sheet->solver_parameters,
				     state->orig_params)) {
		GOUndo *undo = set_params (sheet, state->orig_params);
		GOUndo *redo = set_params (sheet, sheet->solver_parameters);
Morten Welinder's avatar
Morten Welinder committed
454
		cmd_generic (GNM_WBC (state->wbcg),
455 456
			     _("Changing solver parameters"),
			     undo, redo);
457 458 459 460 461 462 463 464

		g_object_unref (state->orig_params);
		state->orig_params =
			gnm_solver_param_dup (sheet->solver_parameters,
					      sheet);
	}
}

465 466
static void
cb_dialog_solver_destroy (SolverState *state)
467
{
468
	g_return_if_fail (state != NULL);
469

Morten Welinder's avatar
Morten Welinder committed
470 471 472 473 474
	if (state->run.solver) {
		gnm_solver_stop (state->run.solver, NULL);
		g_object_set (state->run.solver, "result", NULL, NULL);
	}

475 476 477 478
	extract_settings (state);

	check_for_changed_options (state);

479
	if (state->gui != NULL) {
480
		g_object_unref (state->gui);
481
		state->gui = NULL;
482 483
	}

484
	wbcg_edit_finish (state->wbcg, WBC_EDIT_REJECT, NULL);
485

486
	state->dialog = NULL;
487 488
}

489
static void
Morten Welinder's avatar
Morten Welinder committed
490
cb_dialog_close_clicked (G_GNUC_UNUSED GtkWidget *button,
491
			 SolverState *state)
492
{
493
	gtk_widget_destroy (state->dialog);
494 495
}

496 497 498 499 500 501 502 503 504 505 506
static void
cb_stop_solver (SolverState *state)
{
	GnmSolver *sol = state->run.solver;

	switch (sol->status) {
	case GNM_SOLVER_STATUS_RUNNING: {
		gboolean ok = gnm_solver_stop (sol, NULL);
		if (!ok) {
			g_warning ("Failed to stop solver!");
		}
507
		g_object_set (sol, "result", NULL, NULL);
508 509 510 511 512 513 514 515
		break;
	}

	default:
		break;
	}
}

Morten Welinder's avatar
Morten Welinder committed
516 517 518 519 520 521 522 523 524
static void
remove_timer_source (SolverState *state)
{
	if (state->run.timer_source) {
		g_source_remove (state->run.timer_source);
		state->run.timer_source = 0;
	}
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
static void
remove_objective_value_source (SolverState *state)
{
	if (state->run.obj_val_source) {
		g_source_remove (state->run.obj_val_source);
		state->run.obj_val_source = 0;
	}
}

static void
update_obj_value (SolverState *state)
{
	GnmSolver *sol = state->run.solver;
	GnmSolverResult *r = sol->result;
	char *valtxt;
	const char *txt;

	switch (r ? r->quality : GNM_SOLVER_RESULT_NONE) {
	default:
	case GNM_SOLVER_RESULT_NONE:
		txt = "";
		break;

	case GNM_SOLVER_RESULT_FEASIBLE:
		txt = _("Feasible");
		break;

	case GNM_SOLVER_RESULT_OPTIMAL:
		txt = _("Optimal");
		break;

	case GNM_SOLVER_RESULT_INFEASIBLE:
		txt = _("Infeasible");
		break;

	case GNM_SOLVER_RESULT_UNBOUNDED:
		txt = _("Unbounded");
		break;
	}
	gtk_label_set_text (GTK_LABEL (state->run.problem_status_widget), txt);

	if (gnm_solver_has_solution (sol)) {
		txt = valtxt = gnm_format_value (go_format_general (),
						 r->value);
	} else {
		valtxt = NULL;
Morten Welinder's avatar
Morten Welinder committed
571
		txt = "";
572 573 574 575 576 577 578 579 580
	}

	gtk_label_set_text (GTK_LABEL (state->run.objective_value_widget),
			    txt);
	g_free (valtxt);

	remove_objective_value_source (state);
}

581 582 583 584 585 586
static void
cb_notify_status (SolverState *state)
{
	GnmSolver *sol = state->run.solver;
	const char *text;
	gboolean finished = gnm_solver_finished (sol);
Morten Welinder's avatar
Morten Welinder committed
587
	gboolean running = FALSE;
588 589 590 591 592 593 594 595 596 597 598 599 600

	switch (sol->status) {
	case GNM_SOLVER_STATUS_READY:
		text = _("Ready");
		break;
	case GNM_SOLVER_STATUS_PREPARING:
		text = _("Preparing");
		break;
	case GNM_SOLVER_STATUS_PREPARED:
		text = _("Prepared");
		break;
	case GNM_SOLVER_STATUS_RUNNING:
		text = _("Running");
Morten Welinder's avatar
Morten Welinder committed
601
		running = TRUE;
602 603 604 605 606 607 608 609 610 611 612 613 614
		break;
	case GNM_SOLVER_STATUS_DONE:
		text = _("Done");
		break;
	default:
	case GNM_SOLVER_STATUS_ERROR:
		text = _("Error");
		break;
	case GNM_SOLVER_STATUS_CANCELLED:
		text = _("Cancelled");
		break;
	}

615
	if (sol->reason) {
616
		char *text2 = g_strconcat (text,
617
					   " (", sol->reason, ")",
618 619 620 621 622 623 624
					   NULL);
		gtk_label_set_text (GTK_LABEL (state->run.status_widget),
				    text2);
		g_free (text2);
	} else {
		gtk_label_set_text (GTK_LABEL (state->run.status_widget), text);
	}
625

Morten Welinder's avatar
Morten Welinder committed
626 627 628 629 630
	gtk_widget_set_visible (state->run.spinner, running);
	gtk_widget_set_visible (state->stop_button, !finished);
	gtk_widget_set_sensitive (state->solve_button, finished);
	gtk_widget_set_sensitive (state->close_button, finished);

631 632 633
	if (state->run.obj_val_source)
		update_obj_value (state);

634
	if (finished) {
Morten Welinder's avatar
Morten Welinder committed
635 636 637
		remove_timer_source (state);
		if (state->run.in_main)
			gtk_main_quit ();
638 639 640
	}
}

641 642 643 644 645 646 647 648
static gboolean
cb_obj_val_tick (SolverState *state)
{
	state->run.obj_val_source = 0;
	update_obj_value (state);
	return FALSE;
}

649 650 651
static void
cb_notify_result (SolverState *state)
{
652 653 654
	if (state->run.obj_val_source == 0)
		state->run.obj_val_source = g_timeout_add
			(100, (GSourceFunc)cb_obj_val_tick, state);
655 656 657 658 659
}

static gboolean
cb_timer_tick (SolverState *state)
{
660 661
	GnmSolver *sol = state->run.solver;
	double dsecs = gnm_solver_elapsed (sol);
662
	int secs = (int)CLAMP (dsecs, 0, INT_MAX);
663 664 665 666 667 668 669 670 671 672
	int hh = secs / 3600;
	int mm = secs / 60 % 60;
	int ss = secs % 60;
	char *txt = hh
		? g_strdup_printf ("%02d:%02d:%02d", hh, mm, ss)
		: g_strdup_printf ("%02d:%02d", mm, ss);

	gtk_label_set_text (GTK_LABEL (state->run.timer_widget), txt);
	g_free (txt);

673
	if (gnm_solver_check_timeout (sol)) {
674 675 676
		cb_notify_status (state);
	}

677 678 679
	return TRUE;
}

680 681 682 683
static void
create_report (GnmSolver *sol, SolverState *state)
{
	Sheet *sheet = state->sheet;
684
	char *base = g_strdup_printf (_("%s %%s Report"), sheet->name_unquoted);
685 686 687 688 689
	gnm_solver_create_report (sol, base);
	g_free (base);
}


690
static GnmSolverResult *
691
run_solver (SolverState *state, GnmSolverParameters *param)
692 693 694 695 696 697 698 699 700
{
	GError *err = NULL;
	gboolean ok;
	GnmSheetRange sr;
	GOUndo *undo = NULL;
	GnmSolver *sol = NULL;
	GnmValue const *vinput;
	GtkWindow *top = GTK_WINDOW (gtk_widget_get_toplevel (state->dialog));
	GnmSolverResult *res = NULL;
Morten Welinder's avatar
Morten Welinder committed
701 702

	state->ref_count++;
703

704 705
	sol = gnm_solver_factory_functional (param->options.algorithm,
					     state->wbcg)
706 707
		? gnm_solver_factory_create (param->options.algorithm, param)
		: NULL;
708 709
	if (!sol) {
		go_gtk_notice_dialog (top, GTK_MESSAGE_ERROR,
710
				      _("The chosen solver is not functional."));
711 712 713
		goto fail;
	}

714 715
	gtk_notebook_set_current_page (GTK_NOTEBOOK (state->notebook), -1);

716 717 718 719 720 721 722
	state->run.solver = sol;

	vinput = gnm_solver_param_get_input (param);
	gnm_sheet_range_from_value (&sr, vinput);
	if (!sr.sheet) sr.sheet = param->sheet;
	undo = clipboard_copy_range_undo (sr.sheet, &sr.range);

Morten Welinder's avatar
Morten Welinder committed
723 724 725 726 727 728 729
	g_signal_connect_swapped (G_OBJECT (sol),
				  "notify::status",
				  G_CALLBACK (cb_notify_status),
				  state);
	g_signal_connect_swapped (G_OBJECT (sol),
				  "notify::reason",
				  G_CALLBACK (cb_notify_status),
730 731 732
				  state);
	cb_notify_status (state);

Morten Welinder's avatar
Morten Welinder committed
733 734 735 736
	g_signal_connect_swapped (G_OBJECT (sol),
				  "notify::result",
				  G_CALLBACK (cb_notify_result),
				  state);
737 738 739 740 741 742 743 744 745
	cb_notify_result (state);

	state->run.timer_source = g_timeout_add_seconds
		(1, (GSourceFunc)cb_timer_tick, state);
	cb_timer_tick (state);

	/* ---------------------------------------- */

	ok = gnm_solver_start (sol,
Morten Welinder's avatar
Morten Welinder committed
746
			       GNM_WBC (state->wbcg),
747 748
			       &err);
	if (ok) {
Morten Welinder's avatar
Morten Welinder committed
749 750 751 752 753 754
		state->run.in_main++;
		go_cmd_context_set_sensitive (GO_CMD_CONTEXT (state->wbcg), FALSE);
		gtk_main ();
		go_cmd_context_set_sensitive (GO_CMD_CONTEXT (state->wbcg), TRUE);
		state->run.in_main--;
		ok = gnm_solver_has_solution (sol);
755 756
	} else if (err) {
		gnm_solver_set_reason (sol, err->message);
757
	}
758
	g_clear_error (&err);
759

760
	remove_objective_value_source (state);
Morten Welinder's avatar
Morten Welinder committed
761
	remove_timer_source (state);
762

763 764
	/* ---------------------------------------- */

Morten Welinder's avatar
Morten Welinder committed
765
	if (ok) {
766 767 768
		GOUndo *redo;

		gnm_solver_store_result (sol);
769 770
		redo = clipboard_copy_range_undo (sr.sheet, &sr.range);

771 772
		if (param->options.program_report ||
		    param->options.sensitivity_report) {
773 774 775 776 777 778 779 780 781 782 783
			Workbook *wb = param->sheet->workbook;
			GOUndo *undo_report, *redo_report;

			undo_report = go_undo_binary_new
				(wb,
				 workbook_sheet_state_new (wb),
				 (GOUndoBinaryFunc)workbook_sheet_state_restore,
				 NULL,
				 (GFreeFunc)workbook_sheet_state_free);
			undo = go_undo_combine (undo, undo_report);

784 785
			create_report (sol, state);

786 787 788 789 790 791 792 793 794
			redo_report = go_undo_binary_new
				(wb,
				 workbook_sheet_state_new (wb),
				 (GOUndoBinaryFunc)workbook_sheet_state_restore,
				 NULL,
				 (GFreeFunc)workbook_sheet_state_free);
			redo = go_undo_combine (redo, redo_report);
		}

Morten Welinder's avatar
Morten Welinder committed
795
		cmd_generic (GNM_WBC (state->wbcg),
796 797
			     _("Running solver"),
			     undo, redo);
798 799 800 801 802 803 804 805 806 807 808 809 810
		res = g_object_ref (sol->result);
		undo = redo = NULL;
	}

fail:
	if (undo)
		g_object_unref (undo);

	if (state->run.solver) {
		g_object_unref (state->run.solver);
		state->run.solver = NULL;
	}

Morten Welinder's avatar
Morten Welinder committed
811 812
	unref_state (state);

813 814 815 816
	return res;
}


817
static void
818
solver_add_scenario (SolverState *state, GnmSolverResult *res, gchar const *name)
819
{
820
	GnmSolverParameters *param = state->sheet->solver_parameters;
821 822 823
	GnmValue const *vinput;
	GnmScenario *sc;
	GnmSheetRange sr;
Morten Welinder's avatar
Morten Welinder committed
824
	WorkbookControl *wbc = GNM_WBC (state->wbcg);
825

826 827 828 829
	vinput = gnm_solver_param_get_input (param);
	gnm_sheet_range_from_value (&sr, vinput);

	sc = gnm_sheet_scenario_new (param->sheet, name);
830 831 832 833 834 835 836 837 838 839 840 841
	switch (res->quality) {
	case GNM_SOLVER_RESULT_OPTIMAL:
		gnm_scenario_set_comment
			(sc, _("Optimal solution created by solver.\n"));
		break;
	case GNM_SOLVER_RESULT_FEASIBLE:
		gnm_scenario_set_comment
			(sc, _("Feasible solution created by solver.\n"));
		break;
	default:
		break;
	}
Morten Welinder's avatar
Morten Welinder committed
842
	gnm_scenario_add_area (sc, &sr);
843

844
	cmd_scenario_add (wbc, sc, sc->sheet);
845
}
Morten Welinder's avatar
Morten Welinder committed
846

847 848 849 850 851
/**
 * cb_dialog_solve_clicked:
 * @button:
 * @state:
 *
852
 *
853 854
 **/
static void
Morten Welinder's avatar
Morten Welinder committed
855
cb_dialog_solve_clicked (G_GNUC_UNUSED GtkWidget *button,
856
			 SolverState *state)
857
{
858 859
	GnmSolverResult *res;
	GnmSolverParameters *param = state->sheet->solver_parameters;
Morten Welinder's avatar
Morten Welinder committed
860
	GError *err = NULL;
861

862
	if (state->warning_dialog != NULL) {
863
		gtk_widget_destroy (state->warning_dialog);
864 865
		state->warning_dialog = NULL;
	}
866

867
	extract_settings (state);
868

869 870 871 872
	if (!gnm_solver_param_valid (param, &err)) {
		GtkWidget *top = gtk_widget_get_toplevel (state->dialog);
		go_gtk_notice_dialog (GTK_WINDOW (top), GTK_MESSAGE_ERROR,
				      "%s", err->message);
873 874 875
		goto out;
	}

876 877
	check_for_changed_options (state);

878 879
	res = run_solver (state, param);

880
	gnm_app_recalc ();
881

Jody Goldberg's avatar
Jody Goldberg committed
882
	if (res != NULL) {
883 884
		if ((res->quality == GNM_SOLVER_RESULT_OPTIMAL ||
		     res->quality == GNM_SOLVER_RESULT_FEASIBLE) &&
885
		    param->options.add_scenario)
886
			solver_add_scenario (state, res,
887
					     param->options.scenario_name);
888

889 890
		g_object_unref (res);
	} else if (err) {
Morten Welinder's avatar
Morten Welinder committed
891 892 893 894
		go_gtk_notice_nonmodal_dialog
			(GTK_WINDOW (state->dialog),
			 &(state->warning_dialog),
			 GTK_MESSAGE_ERROR,
895
			 "%s", err->message);
896
	}
Morten Welinder's avatar
Morten Welinder committed
897

898
 out:
Morten Welinder's avatar
Morten Welinder committed
899 900
	if (err)
		g_error_free (err);
901
}
902

903 904
#define INIT_BOOL_ENTRY(name_, field_)					\
do {									\
Morten Welinder's avatar
Morten Welinder committed
905
	GtkWidget *w_ = go_gtk_builder_get_widget (state->gui, (name_));	\
906 907 908 909
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w_),		\
				      param->field_);			\
} while (0)

910

911
/**
912
 * dialog_solver_init:
913 914 915 916 917 918
 * @state:
 *
 * Create the dialog (guru).
 *
 **/
static gboolean
919
dialog_solver_init (SolverState *state)
920
{
921
	GtkGrid *grid;
922
	GnmSolverParameters *param;
923 924 925
	GtkCellRenderer *renderer;
	GtkListStore *store;
	GtkTreeViewColumn *column;
926
	GSList *cl;
927 928
	GnmCell *target_cell;
	GnmValue const *input;
929
	int i;
930

931
	param = state->sheet->solver_parameters;
932

933
	state->gui = gnm_gtk_builder_load ("res:ui/solver.ui", NULL, GO_CMD_CONTEXT (state->wbcg));
934 935 936
        if (state->gui == NULL)
                return TRUE;

Morten Welinder's avatar
Morten Welinder committed
937
	state->dialog = go_gtk_builder_get_widget (state->gui, "Solver");
938 939 940
        if (state->dialog == NULL)
                return TRUE;

941 942
	state->notebook = go_gtk_builder_get_widget (state->gui, "solver_notebook");

943
	/*  buttons  */
Morten Welinder's avatar
Morten Welinder committed
944
	state->solve_button  = go_gtk_builder_get_widget (state->gui, "solvebutton");
jpekka's avatar
jpekka committed
945 946
	g_signal_connect (G_OBJECT (state->solve_button), "clicked",
			  G_CALLBACK (cb_dialog_solve_clicked), state);
947

Morten Welinder's avatar
Morten Welinder committed
948
	state->close_button  = go_gtk_builder_get_widget (state->gui, "closebutton");
jpekka's avatar
jpekka committed
949 950
	g_signal_connect (G_OBJECT (state->close_button), "clicked",
			  G_CALLBACK (cb_dialog_close_clicked), state);
951

Morten Welinder's avatar
Morten Welinder committed
952 953
	state->help_button = go_gtk_builder_get_widget (state->gui, "helpbutton");
	gnm_init_help_button (state->help_button, GNUMERIC_HELP_LINK_SOLVER);
954

Morten Welinder's avatar
Morten Welinder committed
955
	state->add_button  = go_gtk_builder_get_widget (state->gui, "addbutton");
956
	gtk_button_set_alignment (GTK_BUTTON (state->add_button), 0.5, .5);
957 958
	g_signal_connect_swapped (G_OBJECT (state->add_button), "clicked",
		G_CALLBACK (cb_dialog_add_clicked), state);
959

Morten Welinder's avatar
Morten Welinder committed
960
	state->change_button = go_gtk_builder_get_widget (state->gui,
jpekka's avatar
jpekka committed
961 962 963
						     "changebutton");
	g_signal_connect (G_OBJECT (state->change_button), "clicked",
			  G_CALLBACK (cb_dialog_change_clicked), state);
964

Morten Welinder's avatar
Morten Welinder committed
965
	state->delete_button = go_gtk_builder_get_widget (state->gui,
jpekka's avatar
jpekka committed
966
						     "deletebutton");
967
	gtk_button_set_alignment (GTK_BUTTON (state->delete_button), 0.5, .5);
jpekka's avatar
jpekka committed
968 969
	g_signal_connect (G_OBJECT (state->delete_button), "clicked",
			  G_CALLBACK (cb_dialog_delete_clicked), state);
970

Morten Welinder's avatar
Morten Welinder committed
971 972 973 974 975
	state->stop_button = go_gtk_builder_get_widget (state->gui, "stopbutton");
	g_signal_connect_swapped (G_OBJECT (state->stop_button),
				  "clicked", G_CALLBACK (cb_stop_solver),
				  state);

Morten Welinder's avatar
Morten Welinder committed
976
	/* target_entry */
977 978
	grid = GTK_GRID (go_gtk_builder_get_widget (state->gui,
						 "parameter-grid"));
979
	state->target_entry = gnm_expr_entry_new (state->wbcg, TRUE);
980
	gnm_expr_entry_set_flags (state->target_entry,
981
		GNM_EE_SINGLE_RANGE |
982
		GNM_EE_FORCE_ABS_REF |
983
		GNM_EE_SHEET_OPTIONAL, GNM_EE_MASK);
984
	gtk_widget_set_hexpand (GTK_WIDGET (state->target_entry), TRUE);
985
	gtk_grid_attach (grid, GTK_WIDGET (state->target_entry), 1, 0, 2, 1);
Morten Welinder's avatar
Morten Welinder committed
986
	gnm_editable_enters (GTK_WINDOW (state->dialog),
987
				  GTK_WIDGET (state->target_entry));
988
	gtk_widget_show (GTK_WIDGET (state->target_entry));
jpekka's avatar
jpekka committed
989 990 991
	g_signal_connect_after (G_OBJECT (state->target_entry),	"changed",
			G_CALLBACK (dialog_set_main_button_sensitivity),
				state);
992

Morten Welinder's avatar
Morten Welinder committed
993
	/* change_cell_entry */
994
	state->change_cell_entry = gnm_expr_entry_new (state->wbcg, TRUE);
995
	gnm_expr_entry_set_flags (state->change_cell_entry,
996
		GNM_EE_SINGLE_RANGE |
997
		GNM_EE_FORCE_ABS_REF |
998
		GNM_EE_SHEET_OPTIONAL, GNM_EE_MASK);
999 1000
	gtk_widget_set_hexpand (GTK_WIDGET (state->change_cell_entry), TRUE);
	gtk_grid_attach (grid,
1001
	                 GTK_WIDGET (state->change_cell_entry), 1, 2, 2, 1);
Morten Welinder's avatar
Morten Welinder committed
1002
	gnm_editable_enters (GTK_WINDOW (state->dialog),
1003
				  GTK_WIDGET (state->change_cell_entry));
1004
	gtk_widget_show (GTK_WIDGET (state->change_cell_entry));
jpekka's avatar
jpekka committed
1005
	g_signal_connect_after (G_OBJECT (state->change_cell_entry), "changed",
1006
		G_CALLBACK (dialog_set_main_button_sensitivity), state);
1007

1008
	/* Algorithm */
1009
	state->algorithm_combo = GTK_COMBO_BOX
Morten Welinder's avatar
Morten Welinder committed
1010
		(go_gtk_builder_get_widget (state->gui, "algorithm_combo"));
1011 1012
	renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (state->algorithm_combo), renderer, TRUE);
1013 1014 1015 1016
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (state->algorithm_combo), renderer,
					"text", 0,
					NULL);
	fill_algorithm_combo (state, param->options.model_type);
1017

1018 1019
	for (i = 0; model_type_group[i]; i++) {
		const char *bname = model_type_group[i];
Morten Welinder's avatar
Morten Welinder committed
1020
		GtkWidget *w = go_gtk_builder_get_widget(state->gui, bname);
1021 1022 1023
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w),
					      param->options.model_type ==
					      (GnmSolverModelType)i);
1024 1025 1026
		g_signal_connect (G_OBJECT (w), "clicked",
				  G_CALLBACK (cb_dialog_model_type_clicked), state);
	}
1027

1028
	/* Options */
Morten Welinder's avatar
Morten Welinder committed
1029
	state->max_iter_entry = go_gtk_builder_get_widget (state->gui,
jpekka's avatar
jpekka committed
1030
						      "max_iter_entry");
1031 1032
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->max_iter_entry),
				   param->options.max_iter);
1033

Morten Welinder's avatar
Morten Welinder committed
1034
	state->max_time_entry = go_gtk_builder_get_widget (state->gui,
jpekka's avatar
jpekka committed
1035
						      "max_time_entry");
1036 1037
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->max_time_entry),
				   param->options.max_time_sec);
1038

1039 1040 1041 1042 1043
	state->gradient_order_entry = go_gtk_builder_get_widget (state->gui,
								 "gradient_order_entry");
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (state->gradient_order_entry),
				   param->options.gradient_order);

1044
	/* lhs_entry */
1045 1046
	grid = GTK_GRID (go_gtk_builder_get_widget (state->gui,
	                                            "constraints-grid"));
1047 1048
	state->lhs.entry = gnm_expr_entry_new (state->wbcg, TRUE);
	gnm_expr_entry_set_flags (state->lhs.entry,
1049
		GNM_EE_SINGLE_RANGE |
1050
		GNM_EE_FORCE_ABS_REF |
1051
		GNM_EE_SHEET_OPTIONAL, GNM_EE_MASK);
1052 1053
	gtk_widget_set_hexpand (GTK_WIDGET (state->lhs.entry), TRUE);
	gtk_grid_attach (grid, GTK_WIDGET (state->lhs.entry), 0, 4, 1, 1);
Morten Welinder's avatar
Morten Welinder committed
1054
	state->lhs.label = go_gtk_builder_get_widget (state->gui, "lhs_label");
1055 1056 1057 1058
	gtk_label_set_mnemonic_widget (GTK_LABEL (state->lhs.label),
		GTK_WIDGET (state->lhs.entry));
	gtk_widget_show (GTK_WIDGET (state->lhs.entry));
	g_signal_connect_after (G_OBJECT (state->lhs.entry),
1059 1060
		"changed",
		G_CALLBACK (dialog_set_sec_button_sensitivity), state);
1061 1062 1063
	g_signal_connect_swapped (
		gnm_expr_entry_get_entry (GNM_EXPR_ENTRY (state->lhs.entry)),
		"activate", G_CALLBACK (cb_dialog_add_clicked), state);
1064

1065
	/* rhs_entry */
1066 1067
	state->rhs.entry = gnm_expr_entry_new (state->wbcg, TRUE);
	gnm_expr_entry_set_flags (state->rhs.entry,
1068 1069 1070 1071 1072
				  GNM_EE_SINGLE_RANGE |
				  GNM_EE_FORCE_ABS_REF |
				  GNM_EE_SHEET_OPTIONAL |
				  GNM_EE_CONSTANT_ALLOWED,
				  GNM_EE_MASK);
1073 1074
	gtk_widget_set_hexpand (GTK_WIDGET (state->rhs.entry), TRUE);
	gtk_grid_attach (grid, GTK_WIDGET (state->rhs.entry), 2, 4, 1, 1);
1075
	gtk_widget_show (GTK_WIDGET (state->rhs.entry));
Morten Welinder's avatar
Morten Welinder committed
1076
	state->rhs.label = go_gtk_builder_get_widget (state->gui, "rhs_label");
1077 1078 1079
	gtk_label_set_mnemonic_widget (
		GTK_LABEL (state->rhs.label), GTK_WIDGET (state->rhs.entry));
	g_signal_connect_after (G_OBJECT (state->rhs.entry),
1080 1081
		"changed",
		G_CALLBACK (dialog_set_sec_button_sensitivity), state);
1082 1083 1084
	g_signal_connect_swapped (
		gnm_expr_entry_get_entry (GNM_EXPR_ENTRY (state->rhs.entry)),
		"activate", G_CALLBACK (cb_dialog_add_clicked), state);
1085

1086
	/* type_menu */
1087
	state->type_combo = GTK_COMBO_BOX
Morten Welinder's avatar
Morten Welinder committed
1088
		(go_gtk_builder_get_widget (state->gui, "type_menu"));
1089 1090
	gtk_combo_box_set_active (state->type_combo, 0);
	g_signal_connect (state->type_combo, "changed",
jpekka's avatar
jpekka committed
1091 1092
			  G_CALLBACK (dialog_set_sec_button_sensitivity),
			  state);
1093

1094
	/* constraint_list */
Morten Welinder's avatar
Morten Welinder committed
1095
	state->constraint_list = GTK_TREE_VIEW (go_gtk_builder_get_widget
1096
					    (state->gui, "constraint_list"));
1097

1098 1099
	state->constr = NULL;
	g_signal_connect (G_OBJECT (gtk_tree_view_get_selection (state->constraint_list)), "changed",
jpekka's avatar
jpekka committed
1100
			  G_CALLBACK (constraint_select_click), state);
1101
	gtk_tree_view_set_reorderable (state->constraint_list, TRUE);
1102 1103
	store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
	gtk_tree_view_set_model (state->constraint_list, GTK_TREE_MODEL(store));
1104
	renderer = gtk_cell_renderer_text_new ();
1105
	column = gtk_tree_view_column_new_with_attributes (
1106 1107
			_("Subject to the Constraints:"),
			renderer, "text", 0, NULL);
1108 1109
	gtk_tree_view_column_set_expand (column, TRUE);
	gtk_tree_view_append_column (state->constraint_list, column);
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	{
		GtkWidget *w = GTK_WIDGET (state->constraint_list);
		int width, height, vsep;
		PangoLayout *layout =
			gtk_widget_create_pango_layout (w, "Mg19");

		gtk_widget_style_get (w,
				      "vertical_separator", &vsep,
				      NULL);

		pango_layout_get_pixel_size (layout, &width, &height);
		gtk_widget_set_size_request (w,
					     -1,
					     (2 * height + vsep) * (4 + 1));
		g_object_unref (layout);
	}

jpekka's avatar
jpekka committed
1128
/* Loading the old solver specs... from param  */
1129

1130
	for (cl = param->constraints; cl; cl = cl->next) {
Morten Welinder's avatar
Morten Welinder committed
1131
		GnmSolverConstraint const *c = cl->data;
1132 1133 1134 1135
		GtkTreeIter iter;
		char *str;

		gtk_list_store_append (store, &iter);
1136
		str = gnm_solver_constraint_as_str (c, state->sheet);
1137 1138 1139
		gtk_list_store_set (store, &iter, 0, str, 1, c, -1);
		g_free (str);
	}
Morten Welinder's avatar
Morten Welinder committed
1140
	g_object_unref (store);
1141

1142
	INIT_BOOL_ENTRY ("autoscale_button", options.automatic_scaling);
1143 1144 1145
	INIT_BOOL_ENTRY ("non_neg_button", options.assume_non_negative);
	INIT_BOOL_ENTRY ("all_int_button", options.assume_discrete);
	INIT_BOOL_ENTRY ("program", options.program_report);
1146
	INIT_BOOL_ENTRY ("sensitivity", options.sensitivity_report);
1147

1148 1149
	input = gnm_solver_param_get_input (param);
	if (input != NULL)
1150
		gnm_expr_entry_load_from_text (state->change_cell_entry,
1151 1152 1153
					       value_peek_string (input));
	target_cell = gnm_solver_param_get_target_cell (param);
	if (target_cell)
1154
		gnm_expr_entry_load_from_text (state->target_entry,
1155
					       cell_name (target_cell));
1156
	else {
Morten Welinder's avatar
Morten Welinder committed
1157
		SheetView *sv = wb_control_cur_sheet_view
Morten Welinder's avatar
Morten Welinder committed
1158
			(GNM_WBC (state->wbcg));
1159 1160
		if (sv) {
			GnmRange first = {sv->edit_pos, sv->edit_pos};
1161
			gnm_expr_entry_load_from_range (state->target_entry,
1162
							state->sheet, &first);
1163 1164
		}
	}
1165

1166
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
Morten Welinder's avatar
Morten Welinder committed
1167
		go_gtk_builder_get_widget(state->gui, "max_button")),
1168
			param->problem_type == GNM_SOLVER_MAXIMIZE);
1169
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
Morten Welinder's avatar
Morten Welinder committed
1170
		go_gtk_builder_get_widget(state->gui, "min_button")),
1171
			param->problem_type == GNM_SOLVER_MINIMIZE);
1172
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
Morten Welinder's avatar
Morten Welinder committed
1173
		go_gtk_builder_get_widget(state->gui, "no_scenario")),
1174 1175
			! param->options.add_scenario);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
Morten Welinder's avatar
Morten Welinder committed
1176
		go_gtk_builder_get_widget(state->gui, "optimal_scenario")),
1177 1178
			param->options.add_scenario);

Morten Welinder's avatar
Morten Welinder committed
1179
	state->scenario_name_entry = go_gtk_builder_get_widget
1180
		(state->gui, "scenario_name_entry");
Morten Welinder's avatar
Morten Welinder committed
1181
	gtk_entry_set_text (GTK_ENTRY (state->scenario_name_entry),
1182
			    param->options.scenario_name);
1183

1184 1185 1186 1187
	state->run.status_widget = go_gtk_builder_get_widget (state->gui, "solver_status_label");
	state->run.problem_status_widget = go_gtk_builder_get_widget (state->gui, "problem_status_label");
	state->run.objective_value_widget = go_gtk_builder_get_widget (state->gui, "objective_value_label");
	state->run.timer_widget = go_gtk_builder_get_widget (state->gui, "elapsed_time_label");
Morten Welinder's avatar
Morten Welinder committed
1188 1189
	state->run.spinner = go_gtk_builder_get_widget (state->gui, "run_spinner");

1190

1191
/* Done */
1192
	gnm_expr_entry_grab_focus (state->target_entry, FALSE);
1193
	wbcg_set_entry (state->wbcg, state->target_entry);
1194 1195 1196 1197

	dialog_set_main_button_sensitivity (NULL, state);
	dialog_set_sec_button_sensitivity (NULL, state);

1198 1199
/* dialog */
	wbc_gtk_attach_guru (state->wbcg, state->dialog);