lpkit.c 20.3 KB
Newer Older
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * lpkit.c: LP SOLVE Toolkit.
 *
 * Authors:
 *   Jeroen Dirks (jeroend@tor.numetrix.com)
 *   Michel Berkelaar (michel@ics.ele.tue.nl)
 *   Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

Morten Welinder's avatar
Morten Welinder committed
24
#include <gnumeric-config.h>
25 26 27 28 29 30 31 32 33 34
#include <glib.h>
#include "lpkit.h"
#include "lpglob.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>


/* Globals */
35 36 37
int        lp_solve_Level;
gnum_float lp_solve_Trej;
gnum_float lp_solve_Extrad;
38 39


Morten Welinder's avatar
Morten Welinder committed
40 41
lprec *
lp_solve_make_lp (int rows, int columns)
42 43 44 45 46
{
        lprec *newlp;
	int   i, sum;  

	if (rows < 0 || columns < 0)
Morten Welinder's avatar
Morten Welinder committed
47
	        g_print ("rows < 0 or columns < 0");
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	sum = rows + columns;

	newlp = g_new (lprec, 1);

	strcpy (newlp->lp_name, "unnamed");

	newlp->verbose = FALSE;
	newlp->print_duals = FALSE;
	newlp->print_sol = FALSE;
	newlp->debug = FALSE;
	newlp->print_at_invert = FALSE;
	newlp->trace = FALSE;

	newlp->rows = rows;
	newlp->columns = columns;
	newlp->sum = sum;
	newlp->rows_alloc = rows;
	newlp->columns_alloc = columns;
	newlp->sum_alloc = sum;
	newlp->names_used = FALSE;

	newlp->obj_bound = DEF_INFINITE;
	newlp->infinite = DEF_INFINITE;
	newlp->epsilon = DEF_EPSILON;
	newlp->epsb = DEF_EPSB;
	newlp->epsd = DEF_EPSD;
	newlp->epsel = DEF_EPSEL;
	newlp->non_zeros = 0;
	newlp->mat_alloc = 1;
	newlp->mat = g_new0 (matrec, newlp->mat_alloc);
	newlp->col_no = g_new0 (int, newlp->mat_alloc + 1);
	newlp->col_end = g_new0 (int, columns + 1);
	newlp->row_end = g_new0 (int, rows + 1);
	newlp->row_end_valid = FALSE;
	newlp->orig_rh = g_new0 (gnum_float, rows + 1);
	newlp->rh = g_new0 (gnum_float, rows + 1);
	newlp->rhs = g_new0 (gnum_float, rows + 1);

Morten Welinder's avatar
Morten Welinder committed
87
	newlp->must_be_int = g_new0 (char, sum + 1);
88 89 90 91 92 93 94 95 96 97 98 99 100
	for (i = 0; i <= sum; i++)
	        newlp->must_be_int[i] = FALSE;

	newlp->orig_upbo = g_new0 (gnum_float, sum + 1);
	for (i = 0; i <= sum; i++)
	        newlp->orig_upbo[i] = newlp->infinite;

	newlp->upbo = g_new0 (gnum_float, sum + 1);
	newlp->orig_lowbo = g_new0 (gnum_float, sum + 1);
	newlp->lowbo = g_new0 (gnum_float, sum + 1);

	newlp->basis_valid = TRUE;
	newlp->bas = g_new0 (int, rows+1);
Morten Welinder's avatar
Morten Welinder committed
101 102
	newlp->basis = g_new0 (char, sum + 1);
	newlp->lower = g_new0 (char, sum + 1);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	for (i = 0; i <= rows; i++) {
	        newlp->bas[i] = i;
		newlp->basis[i] = TRUE;
	}

	for (i = rows + 1; i <= sum; i++)
	        newlp->basis[i] = FALSE;


	for (i = 0 ; i <= sum; i++)
	        newlp->lower[i] = TRUE;
 
	newlp->eta_valid = TRUE;
	newlp->eta_size = 0;
	newlp->eta_alloc = INITIAL_MAT_SIZE;
	newlp->max_num_inv = DEFNUMINV;

	newlp->nr_lagrange = 0;

	newlp->eta_value = g_new0 (gnum_float, newlp->eta_alloc);
	newlp->eta_row_nr = g_new0 (int, newlp->eta_alloc);

	/* +1 reported by Christian Rank */
	newlp->eta_col_end = g_new0 (int, newlp->rows_alloc +
Morten Welinder's avatar
Morten Welinder committed
128
				     newlp->max_num_inv + 1);
129 130 131 132 133 134 135

	newlp->bb_rule = FIRST_NI;
	newlp->break_at_int = FALSE;
	newlp->break_value = 0;

	newlp->iter = 0;
	newlp->total_iter = 0;
Jukka-Pekka Iivonen's avatar
Wrote.  
Jukka-Pekka Iivonen committed
136
	newlp->max_total_iter = 1000; /* Default */
137 138 139 140 141 142 143 144 145 146 147

	newlp->solution = g_new0 (gnum_float, sum + 1);
	newlp->best_solution = g_new0 (gnum_float, sum + 1);
	newlp->duals = g_new0 (gnum_float, rows + 1);

	newlp->maximise = FALSE;
	newlp->floor_first = TRUE;

	newlp->scaling_used = FALSE;
	newlp->columns_scaled = FALSE;

Morten Welinder's avatar
Morten Welinder committed
148
	newlp->ch_sign = g_new0 (char, rows + 1);
149 150 151 152 153 154 155 156 157 158
	for (i = 0; i <= rows; i++)
	        newlp->ch_sign[i] = FALSE;

	newlp->valid = FALSE; 

	/* create two hash tables for names */

	return (newlp);
}

Morten Welinder's avatar
Morten Welinder committed
159 160
void
lp_solve_delete_lp (lprec *lp)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
        int i; 

	if (lp->names_used) {
	        g_free (lp->row_name);
		g_free (lp->col_name);
	}

	g_free (lp->mat);
	g_free (lp->col_no);
	g_free (lp->col_end);
	g_free (lp->row_end);
	g_free (lp->orig_rh);
	g_free (lp->rh);
	g_free (lp->rhs);
	g_free (lp->must_be_int);
	g_free (lp->orig_upbo);
	g_free (lp->orig_lowbo);
	g_free (lp->upbo);
	g_free (lp->lowbo);
	g_free (lp->bas);
	g_free (lp->basis);
	g_free (lp->lower);
	g_free (lp->eta_value);
	g_free (lp->eta_row_nr);
	g_free (lp->eta_col_end);
	g_free (lp->solution);
	g_free (lp->best_solution);
	g_free (lp->duals);
	g_free (lp->ch_sign);
	if (lp->scaling_used)
	        g_free (lp->scale);
	if (lp->nr_lagrange > 0) {
	        g_free (lp->lag_rhs);
		g_free (lp->lambda);
		g_free (lp->lag_con_type);
		for (i = 0; i < lp->nr_lagrange; i++)
		        g_free (lp->lag_row[i]);
		g_free (lp->lag_row);
	}

	g_free (lp);
}  

205
static void
Morten Welinder's avatar
Morten Welinder committed
206
inc_mat_space (lprec *lp, int maxextra)
207 208 209 210 211 212 213 214 215 216 217 218 219 220
{
        if (lp->non_zeros + maxextra >= lp->mat_alloc) {
	        /* let's allocate at least INITIAL_MAT_SIZE  entries */
	        if (lp->mat_alloc < INITIAL_MAT_SIZE)
		        lp->mat_alloc = INITIAL_MAT_SIZE;
     
		/* increase the size by 50% each time it becomes too small */
		while (lp->non_zeros + maxextra >= lp->mat_alloc)
		        lp->mat_alloc *= 1.5;

		lp->mat = g_renew (matrec, lp->mat, lp->mat_alloc);
		lp->col_no = g_renew (int, lp->col_no, lp->mat_alloc + 1);
	}
}
Jukka-Pekka Iivonen's avatar
Wrote.  
Jukka-Pekka Iivonen committed
221 222 223 224 225 226

void
lp_solve_set_max_iter (lprec *lp, int max)
{
        lp->max_total_iter = max;
}
227 228 229 230 231 232 233 234

void
lp_solve_set_max_time (lprec *lp, int max, gnum_float st)
{
        lp->max_time   = max;
	lp->start_time = st;
}

Morten Welinder's avatar
Morten Welinder committed
235
void
236
lp_solve_set_mat (lprec *lp, int Row, int Column, gnum_float Value)
237 238 239 240 241 242 243 244
{
        int elmnr, lastelm, i;

	/* This function is very inefficient if used to add new matrix
	 * entries in other places than at the end of the matrix. OK for
	 * replacing existing non-zero values */

	if (Row > lp->rows || Row < 0)
Morten Welinder's avatar
Morten Welinder committed
245
	        g_print ("Row out of range");
246
	if (Column > lp->columns || Column < 1)
Morten Welinder's avatar
Morten Welinder committed
247
	        g_print ("Column out of range");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

	/* scaling is performed twice? MB */
	if (lp->scaling_used)
	        Value *= lp->scale[Row] * lp->scale[lp->rows + Column];
  
	if (lp->basis[Column] == TRUE && Row > 0)
	        lp->basis_valid = FALSE;
	lp->eta_valid = FALSE;

	/* find out if we already have such an entry */
	elmnr = lp->col_end[Column - 1];
	while ((elmnr < lp->col_end[Column]) && (lp->mat[elmnr].row_nr != Row))
	        elmnr++;
  
	if ((elmnr != lp->col_end[Column]) && (lp->mat[elmnr].row_nr == Row)) {
	        /* there is an existing entry */
Morten Welinder's avatar
Morten Welinder committed
264
	        if (ABS (Value) > lp->epsilon) {
265 266 267 268
		        /* we replace it by something non-zero */
		        if (lp->scaling_used) {
			        if (lp->ch_sign[Row])
				        lp->mat[elmnr].value = -Value *
Morten Welinder's avatar
Morten Welinder committed
269
						lp->scale[Row] * lp->scale[Column];
270 271
				else
				        lp->mat[elmnr].value = Value *
Morten Welinder's avatar
Morten Welinder committed
272
						lp->scale[Row] * lp->scale[Column];
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
			}
			else { /* no scaling */
			        if (lp->ch_sign[Row])
				        lp->mat[elmnr].value = -Value;
				else
				        lp->mat[elmnr].value = Value;
			}
		}
		else { /* setting existing non-zero entry to zero. Remove
			* the entry */

		        /* this might remove an entire column, or leave
			 * just a bound. No nice solution for that yet */
      
		        /* Shift the matrix */
		        lastelm = lp->non_zeros; 
			for (i = elmnr; i < lastelm ; i++)
			        lp->mat[i] = lp->mat[i + 1];
			for (i = Column; i <= lp->columns; i++)
			        lp->col_end[i]--;
      
			lp->non_zeros--;
		}
	}
Morten Welinder's avatar
Morten Welinder committed
297
	else if (ABS (Value) > lp->epsilon) {
298 299
	        /* no existing entry. make new one only if not nearly zero */
	        /* check if more space is needed for matrix */
Morten Welinder's avatar
Morten Welinder committed
300
	        inc_mat_space (lp, 1);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    
		/* Shift the matrix */
		lastelm = lp->non_zeros; 
		for (i = lastelm; i > elmnr ; i--)
		        lp->mat[i] = lp->mat[i - 1];
		for (i = Column; i <= lp->columns; i++)
		        lp->col_end[i]++;
    
		/* Set new element */
		lp->mat[elmnr].row_nr = Row;
    
		if (lp->scaling_used) {
		        if (lp->ch_sign[Row])
			        lp->mat[elmnr].value = -Value * 
				        lp->scale[Row] * lp->scale[Column];
			else
			        lp->mat[elmnr].value = Value * lp->scale[Row] *
				        lp->scale[Column];
		}
		else { /* no scaling */
		        if (lp->ch_sign[Row])
			        lp->mat[elmnr].value = -Value;
			else
			        lp->mat[elmnr].value = Value;
		}
    
		lp->row_end_valid = FALSE;
    
		lp->non_zeros++;
	}
}

Morten Welinder's avatar
Morten Welinder committed
333
void
334
lp_solve_set_upbo (lprec *lp, int column, gnum_float value)
335 336
{
        if (column > lp->columns || column < 1)
Morten Welinder's avatar
Morten Welinder committed
337
	        g_print ("Column out of range");
338 339 340
	if (lp->scaling_used)
	        value /= lp->scale[lp->rows + column];
	if (value < lp->orig_lowbo[lp->rows + column])
Morten Welinder's avatar
Morten Welinder committed
341
	        g_print ("Upperbound must be >= lowerbound"); 
342 343 344 345
	lp->eta_valid = FALSE;
	lp->orig_upbo[lp->rows + column] = value;
}

Morten Welinder's avatar
Morten Welinder committed
346
void
347
lp_solve_set_lowbo (lprec *lp, int column, gnum_float value)
348 349
{
        if (column > lp->columns || column < 1)
Morten Welinder's avatar
Morten Welinder committed
350
	        g_print ("Column out of range");
351 352 353
	if (lp->scaling_used)
	        value /= lp->scale[lp->rows + column];
	if (value > lp->orig_upbo[lp->rows + column])
Morten Welinder's avatar
Morten Welinder committed
354
	        g_print ("Upperbound must be >= lowerbound"); 
355 356
	/*
	  if (value < 0)
Morten Welinder's avatar
Morten Welinder committed
357
	  g_print ("Lower bound cannot be < 0");
358 359 360 361 362
	*/
	lp->eta_valid = FALSE;
	lp->orig_lowbo[lp->rows + column] = value;
}

Morten Welinder's avatar
Morten Welinder committed
363 364
void
lp_solve_set_int (lprec *lp, int column, gboolean must_be_int)
365 366
{
        if (column > lp->columns || column < 1)
Morten Welinder's avatar
Morten Welinder committed
367
	        g_print ("Column out of range");
368 369 370
	lp->must_be_int[lp->rows + column] = must_be_int;
	if (must_be_int == TRUE)
	        if (lp->columns_scaled)
371
		        lp_solve_unscale_columns (lp);
372 373
}

Morten Welinder's avatar
Morten Welinder committed
374
void
375
lp_solve_set_rh (lprec *lp, int row, gnum_float value)
376 377
{
        if (row > lp->rows || row < 0)
Morten Welinder's avatar
Morten Welinder committed
378
	        g_print ("Row out of Range");
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

	if ((row == 0) && (!lp->maximise))  /* setting of RHS of OF IS
					     * meaningful */
	        value = -value;
	if (lp->scaling_used) {
	        if (lp->ch_sign[row])
		        lp->orig_rh[row] = -value * lp->scale[row];
		else
		        lp->orig_rh[row] = value * lp->scale[row];
	}
	else
	        if (lp->ch_sign[row])
		        lp->orig_rh[row] = -value;
		else
		        lp->orig_rh[row] = value;
	lp->eta_valid = FALSE;
} 

Morten Welinder's avatar
Morten Welinder committed
397
void
398
lp_solve_set_maxim (lprec *lp)
399 400 401 402 403 404 405 406 407 408 409 410 411
{
        int i;
	if (lp->maximise == FALSE) {
	        for (i = 0; i < lp->non_zeros; i++)
		        if (lp->mat[i].row_nr == 0)
			        lp->mat[i].value *= -1;
		lp->eta_valid = FALSE;
		lp->orig_rh[0] *= -1;
	} 
	lp->maximise = TRUE;
	lp->ch_sign[0] = TRUE;
}

Morten Welinder's avatar
Morten Welinder committed
412
void
413
lp_solve_set_minim (lprec *lp)
414 415 416 417 418 419 420 421 422 423 424 425 426
{
        int i;
	if (lp->maximise == TRUE) {
	        for (i = 0; i < lp->non_zeros; i++)
		        if (lp->mat[i].row_nr == 0)
			        lp->mat[i].value = -lp->mat[i].value;
		lp->eta_valid = FALSE;
		lp->orig_rh[0] *= -1;
	} 
	lp->maximise = FALSE;
	lp->ch_sign[0] = FALSE;
}

Morten Welinder's avatar
Morten Welinder committed
427
void
428
lp_solve_set_constr_type (lprec *lp, int row, SolverConstraintType con_type)
429 430 431
{
        int i;
	if (row > lp->rows || row < 1)
Morten Welinder's avatar
Morten Welinder committed
432
	        g_print ("Row out of Range");
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	if (con_type == SolverEQ) {
	        lp->orig_upbo[row] = 0;
		lp->basis_valid = FALSE;
		if (lp->ch_sign[row]) {
		        for (i = 0; i < lp->non_zeros; i++)
			        if (lp->mat[i].row_nr == row)
				        lp->mat[i].value *= -1;
			lp->eta_valid = FALSE;
			lp->ch_sign[row] = FALSE;
			if (lp->orig_rh[row] != 0)
			        lp->orig_rh[row] *= -1;
		}
	} else if (con_type == SolverLE) {
	        lp->orig_upbo[row] = lp->infinite;
		lp->basis_valid = FALSE;
		if (lp->ch_sign[row]) {
		        for (i = 0; i < lp->non_zeros; i++)
			        if (lp->mat[i].row_nr == row)
				        lp->mat[i].value *= -1;
			lp->eta_valid = FALSE;
			lp->ch_sign[row] = FALSE;
			if (lp->orig_rh[row] != 0)
			        lp->orig_rh[row] *= -1;
		}
	} else if (con_type == SolverGE) {
	        lp->orig_upbo[row] = lp->infinite;
		lp->basis_valid = FALSE;
		if (!lp->ch_sign[row]) {
		        for (i = 0; i < lp->non_zeros; i++)
			        if (lp->mat[i].row_nr == row)
				        lp->mat[i].value *= -1;
			lp->eta_valid = FALSE;
			lp->ch_sign[row] = TRUE;
			if (lp->orig_rh[row] != 0)
			        lp->orig_rh[row] *= -1;
		}
	} else
Morten Welinder's avatar
Morten Welinder committed
470
	        g_print ("Constraint type not (yet) implemented");
471 472
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
473
/* This is useful for debugging. */
Morten Welinder's avatar
Morten Welinder committed
474
void
475
lp_solve_print_lp (lprec *lp)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 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
{
        int        i, j;
	gnum_float *fatmat;

	fatmat = g_new (gnum_float, (lp->rows + 1) * lp->columns);
	for (i = 1; i <= lp->columns; i++)
	        for (j = lp->col_end[i - 1]; j < lp->col_end[i]; j++)
		        fatmat[(i - 1) * (lp->rows + 1) + lp->mat[j].row_nr] =
			        lp->mat[j].value;

	printf ("problem name: %s\n", lp->lp_name);
	printf ("          ");
	for (j = 1; j <= lp->columns; j++)
	        if (lp->names_used)
		        printf ("%8s ", lp->col_name[j]);
		else
		        printf ("Var[%3d] ", j);
	if (lp->maximise) {
	        printf ("\nMaximise  ");
		for (j = 0; j < lp->columns; j++)
		        printf ("%8g ", (double) -fatmat[j * (lp->rows + 1)]);
	} else {
	        printf ("\nMinimize  ");
		for (j = 0; j < lp->columns; j++)
		        printf ("%8g ", (double) fatmat[j * (lp->rows + 1)]);
	}
	printf ("\n");
	for (i = 1; i <= lp->rows; i++) {
	        if (lp->names_used)
		        printf ("%-9s ", lp->row_name[i]);
		else
		        printf ("Row[%3d]  ", i);
		for (j = 0; j < lp->columns; j++)
		        if (lp->ch_sign[i] && fatmat[j*(lp->rows+1) + i] != 0)
			        printf ("%8g ",
					(double)-fatmat[j * (lp->rows+1) + i]);
			else
			        printf ("%8g ",
					(double)fatmat[j * (lp->rows + 1) + i]);
		if (lp->orig_upbo[i] != 0) {
		        if (lp->ch_sign[i])
			        printf (">= ");
			else
			        printf ("<= ");
		} else
		        printf (" = ");
		if (lp->ch_sign[i])
		        printf ("%8g", (double)-lp->orig_rh[i]);
		else
		        printf ("%8g", (double)lp->orig_rh[i]);
		if (lp->orig_lowbo[i] != 0) {
		        printf ("  %s = %8g", (lp->ch_sign[i]) ? "lowbo" :
				"upbo", (double)lp->orig_lowbo[i]);
		}
		if ((lp->orig_upbo[i] != lp->infinite) 
		    && (lp->orig_upbo[i] != 0.0)) {
		        printf ("  %s = %8g", (lp->ch_sign[i]) ? "upbo" :
				"lowbo", (double)lp->orig_upbo[i]);
		}
		printf ("\n");
	}
	printf ("Type      ");
	for (i = 1; i <= lp->columns; i++)
	        if (lp->must_be_int[lp->rows + i] == TRUE)
		        printf ("     Int ");
		else
		        printf ("    Real ");
	printf ("\nupbo      ");
	for (i = 1; i <= lp->columns; i++)
	        if (lp->orig_upbo[lp->rows + i] == lp->infinite)
		        printf (" Infinite");
		else
		        printf ("%8g ", (double)lp->orig_upbo[lp->rows + i]);
	printf ("\nlowbo     ");
	for (i = 1; i <= lp->columns; i++)
	        printf ("%8g ", (double)lp->orig_lowbo[lp->rows + i]);
	printf ("\n");
	for (i = 0; i < lp->nr_lagrange; i++) {
	        printf ("lag[%d]  ", i);
		for (j = 1; j <= lp->columns; j++)
		        printf ("%8g ", (double)lp->lag_row[i][j]);
		if (lp->orig_upbo[i] == lp->infinite) {
		        if (lp->lag_con_type[i] == SolverGE)
			        printf (">= ");
			else if (lp->lag_con_type[i] == SolverLE)
			        printf ("<= ");
			else if (lp->lag_con_type[i] == SolverEQ)
			        printf (" = ");
		}
		printf ("%8g\n", (double)lp->lag_rhs[i]);
	}

	g_free (fatmat);
}  

Morten Welinder's avatar
Morten Welinder committed
571 572
static gnum_float
minmax_to_scale (gnum_float min, gnum_float max)
573 574 575 576 577
{
        gnum_float scale;

	/* should do something sensible when min or max is 0, MB */
	if ((min == 0) || (max == 0))
Morten Welinder's avatar
Morten Welinder committed
578
	        return 1;
579

Morten Welinder's avatar
Morten Welinder committed
580
	/* scale = 1 / pow (10, (log10(min) + log10(max)) / 2); */
581
	/* Jon van Reet noticed: can be simplified to: */
Morten Welinder's avatar
Morten Welinder committed
582
	scale = 1 / sqrtgnum (min * max);
583

Morten Welinder's avatar
Morten Welinder committed
584
	return scale;
585 586
}

Morten Welinder's avatar
Morten Welinder committed
587
void
588
lp_solve_unscale_columns (lprec *lp)
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
{
        int i, j;

	/* unscale mat */
	for (j = 1; j <= lp->columns; j++)
	        for (i = lp->col_end[j - 1]; i < lp->col_end[j]; i++)
		        lp->mat[i].value /= lp->scale[lp->rows + j];

	/* unscale bounds as well */
	for (i = lp->rows + 1; i <= lp->sum; i++) { /* was <, changed by PN */
	        if (lp->orig_lowbo[i] != 0)
		        lp->orig_lowbo[i] *= lp->scale[i];
		if (lp->orig_upbo[i] != lp->infinite)
		        lp->orig_upbo[i] *= lp->scale[i];
	}
    
	for (i = lp->rows + 1; i<= lp->sum; i++)
	        lp->scale[i] = 1;
	lp->columns_scaled = FALSE;
	lp->eta_valid = FALSE;
}

Morten Welinder's avatar
Morten Welinder committed
611
void
612
lp_solve_auto_scale (lprec *lp)
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
        int        i, j, row_nr;
	gnum_float *row_max, *row_min, *scalechange, absval;
	gnum_float col_max, col_min;
  
	if (!lp->scaling_used) {
	        lp->scale = g_new (gnum_float, lp->sum_alloc + 1);
		for (i = 0; i <= lp->sum; i++)
		        lp->scale[i] = 1;
	}
  
	row_max     = g_new (gnum_float, lp->rows + 1);
	row_min     = g_new (gnum_float, lp->rows + 1);
	scalechange = g_new (gnum_float, lp->sum + 1);

	/* initialise min and max values */
	for (i = 0; i <= lp->rows; i++) {
	        row_max[i] = 0;
		row_min[i] = lp->infinite;
	}

	/* calculate min and max absolute values of rows */
	for (j = 1; j <= lp->columns; j++)
	        for (i = lp->col_end[j - 1]; i < lp->col_end[j]; i++) {
		        row_nr = lp->mat[i].row_nr;
Morten Welinder's avatar
Morten Welinder committed
638
			absval = ABS (lp->mat[i].value);
639
			if (absval != 0) {
Morten Welinder's avatar
Morten Welinder committed
640 641 642 643
			        row_max[row_nr] = MAX (row_max[row_nr],
						       absval);
				row_min[row_nr] = MIN (row_min[row_nr],
						       absval);
644 645 646 647
			}
		}    
	/* calculate scale factors for rows */
	for (i = 0; i <= lp->rows; i++) {
Morten Welinder's avatar
Morten Welinder committed
648
	        scalechange[i] = minmax_to_scale (row_min[i], row_max[i]);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
		lp->scale[i] *= scalechange[i];
	}

	/* now actually scale the matrix */
	for (j = 1; j <= lp->columns; j++)
	        for (i = lp->col_end[j - 1]; i < lp->col_end[j]; i++)
		        lp->mat[i].value *= scalechange[lp->mat[i].row_nr];

	/* and scale the rhs and the row bounds (RANGES in MPS!!) */
	for (i = 0; i <= lp->rows; i++) {
	        lp->orig_rh[i] *= scalechange[i];

		if ((lp->orig_upbo[i] < lp->infinite)
		    && (lp->orig_upbo[i] != 0))
		        lp->orig_upbo[i] *= scalechange[i];

		if (lp->orig_lowbo[i] != 0)
		        lp->orig_lowbo[i] *= scalechange[i];
	}

	g_free (row_max);
	g_free (row_min);
  
	/* calculate column scales */
	for (j = 1; j <= lp->columns; j++) {
	        if (lp->must_be_int[lp->rows + j]) {
		        /* do not scale integer columns */
		        scalechange[lp->rows + j] = 1;
		} else {
		        col_max = 0;
			col_min = lp->infinite;
			for (i = lp->col_end[j - 1]; i < lp->col_end[j]; i++) {
			        if (lp->mat[i].value != 0) {
Morten Welinder's avatar
Morten Welinder committed
682 683 684 685
				        col_max = MAX (col_max,
						       ABS (lp->mat[i].value));
					col_min = MIN (col_min,
						       ABS (lp->mat[i].value));
686 687
				}
			}
Morten Welinder's avatar
Morten Welinder committed
688 689
			scalechange[lp->rows + j]  = minmax_to_scale (col_min,
								      col_max);
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
			lp->scale[lp->rows + j] *= scalechange[lp->rows + j];
		}
	}
  
	/* scale mat */
	for (j = 1; j <= lp->columns; j++)
	        for (i = lp->col_end[j - 1]; i < lp->col_end[j]; i++)
		        lp->mat[i].value *= scalechange[lp->rows + j];
  
	/* scale bounds as well */
	for (i = lp->rows + 1; i <= lp->sum; i++) { /* was <, changed by PN */
	        if (lp->orig_lowbo[i] != 0)
		        lp->orig_lowbo[i] /= scalechange[i];
		if (lp->orig_upbo[i] != lp->infinite)
		        lp->orig_upbo[i] /= scalechange[i];
	}
	lp->columns_scaled = TRUE;

	g_free (scalechange);
	lp->scaling_used = TRUE;
	lp->eta_valid = FALSE;
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
713
/* This is useful for debugging. */
Morten Welinder's avatar
Morten Welinder committed
714
void
715
lp_solve_print_solution (lprec *lp)
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
{
        int  i;
	FILE *stream;

	stream = stdout;

	fprintf (stream, "Value of objective function: %g\n",
		 (double)lp->best_solution[0]);

	/* print normal variables */
	for (i = 1; i <= lp->columns; i++)
	        if (lp->names_used)
		        fprintf (stream, "%-20s %g\n", lp->col_name[i],
				 (double)lp->best_solution[lp->rows + i]);
		else
		        fprintf (stream, "Var [%d] %g\n", i,
				 (double)lp->best_solution[lp->rows + i]);

	/* print achieved constraint values */
	if (lp->verbose) {
	        fprintf (stream, "\nActual values of the constraints:\n");
		for (i = 1; i <= lp->rows; i++)
		        if (lp->names_used)
			        fprintf (stream, "%-20s %g\n", lp->row_name[i],
					 (double)lp->best_solution[i]);
			else
			        fprintf (stream, "Row [%d] %g\n", i,
					 (double)lp->best_solution[i]);  
	}

	if ((lp->verbose || lp->print_duals)) {
	        if (lp->max_level != 1)
		        fprintf (stream,
				 "These are the duals from the node that "
				 "gave the optimal solution.\n");
		else
		        fprintf (stream, "\nDual values:\n");
		for (i = 1; i <= lp->rows; i++)
		        if (lp->names_used)
			        fprintf (stream, "%-20s %g\n", lp->row_name[i],
					 (double)lp->duals[i]);
			else
			        fprintf (stream, "Row [%d] %g\n", i,
					 (double)lp->duals[i]); 
	}
Morten Welinder's avatar
Morten Welinder committed
761 762
	fflush (stream);
}