1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 205 206 207 208 209 210 211 212 213 214
|
/* split free variables
*
* PCx 1.1 11/97
*
* Authors: Joe Czyzyk, Sanjay Mehrotra, Michael Wagner, Steve Wright.
*
* (C) 1996 University of Chicago. See COPYRIGHT in main directory.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "memory.h"
#include "main.h"
int SplitFreeVars(LP)
LPtype *LP;
{
int col, NextCol, FreeCounter, NextEnt, entry, NumberFree,
NewCols, NewEnts, MoreEnts, NextBound, NewBounds;
LPtype *MakeTranspose();
/* count number of free variables */
for (col = 0, NumberFree = 0, MoreEnts = 0; col < LP->Cols; col++)
if (LP->VarType[col] == FREE) {
NumberFree++;
MoreEnts += (LP->A.pEndRow[col] - LP->A.pBeginRow[col] + 1);
}
/* Reallocate memory to allow for larger vectors, etc. */
NewEnts = LP->Ents + MoreEnts;
NewCols = LP->Cols + NumberFree;
if (LP->FreePlus) Free((char *) LP->FreePlus);
LP->FreePlus = NewInt(NumberFree, "LP->FreePlus");
if (LP->FreeMinus) Free((char *) LP->FreeMinus);
LP->FreeMinus = NewInt(NumberFree, "LP->FreeMinus");
LP->c = (double *)
Realloc(LP->c, NewCols * sizeof(double), "LP->c");
LP->VarType = (int *)
Realloc(LP->VarType, NewCols * sizeof(int), "LP->VarType");
LP->UpBound = (double *)
Realloc(LP->UpBound, NewCols * sizeof(double), "LP->UpBound");
LP->A.pBeginRow = (int *)
Realloc(LP->A.pBeginRow, NewCols * sizeof(int), "LP->A.pBeginRow");
LP->A.pEndRow = (int *)
Realloc(LP->A.pEndRow, NewCols * sizeof(int), "LP->A.pEndRow");
LP->A.Row = (int *)
Realloc(LP->A.Row, NewEnts * sizeof(int), "LP->A.Row");
LP->A.Value = (double *)
Realloc(LP->A.Value, NewEnts * sizeof(double), "LP->A.Value");
LP->A.NumCols = NewCols; LP->A.Nonzeros = NewEnts;
/* LP->A.transpose already has the right number of columns, so just
allocate extra space for its additional nonzeros */
LP->Atranspose.Row = (int *)
Realloc(LP->Atranspose.Row, NewEnts * sizeof(int),
"LP->Atranspose.Row");
LP->Atranspose.Value = (double *)
Realloc(LP->Atranspose.Value, NewEnts * sizeof(double),
"LP->Atranspose.Value");
LP->Atranspose.NumRows = NewCols; LP->Atranspose.Nonzeros = NewEnts;
NextCol = LP->Cols; NextEnt = LP->Ents;
for (col = 0, FreeCounter = 0; col < LP->Cols; col++)
if (LP->VarType[col] == FREE) {
/* reset this column to NORMAL */
LP->VarType[col] = NORMAL;
LP->FreePlus[FreeCounter] = col;
/* make additional variable such that x = x^+ - x^- */
LP->VarType[NextCol] = NORMAL; /* add new split variable at end */
LP->FreeMinus[FreeCounter] = NextCol;
/* copy constraint matrix elements from original variable - flip signs */
LP->A.pBeginRow[NextCol] = NextEnt + 1;
for (entry = LP->A.pBeginRow[col] - 1; entry <= LP->A.pEndRow[col] - 1; entry++) {
LP->A.Row[NextEnt] = LP->A.Row[entry];
LP->A.Value[NextEnt] = -LP->A.Value[entry];
NextEnt++;
}
LP->A.pEndRow[NextCol] = NextEnt;
LP->c[NextCol] = -LP->c[col];
NextCol++;
FreeCounter++;
}
LP->Cols = NewCols;
LP->A.NumCols = NewCols;
LP->Ents = NewEnts;
LP->A.Nonzeros = NewEnts;
LP->NumberFree = 0;
LP->NumberSplit = NumberFree;
if (NextEnt != NewEnts) {
printf("Error splitting free variables: ");
printf("NextEnt (%d) != NewEnts (%d)\n", NextEnt, NewEnts);
}
if (NextCol != NewCols) {
printf("Error splitting free variables: ");
printf("NextCol (%d) != NewCols (%d)\n", NextCol, NewCols);
}
if (FreeCounter != NumberFree) {
printf("Error splitting free variables: ");
printf("FreeCounter (%d) != NumberFree (%d)\n", FreeCounter, NumberFree);
}
LP = MakeTranspose(LP);
return 0;
}
int UnSplitFreeVars(LP, Solution)
LPtype *LP;
solution *Solution;
{
int i, plus, minus;
for (i = 0; i < LP->NumberSplit; i++) {
plus = LP->FreePlus[i];
minus = LP->FreeMinus[i];
Solution->x[plus] = Solution->x[plus] - Solution->x[minus];
Solution->DualLower[plus] = 0.0;
LP->VarType[plus] = FREE;
}
LP->Cols -= LP->NumberSplit;
LP->A.NumCols -= LP->NumberSplit;
Solution->Columns -= LP->NumberSplit;
return 0;
}
/* Implement the split-shift heuristic for dealing with free vars */
int ShiftSplitVariables(LP, Current)
LPtype *LP;
Iterate *Current;
{
int i, im, ip;
int NumCols, NumBounds;
double mutemp=0.0, splitshift, xmax, xmin,
goal, avx, sxmax=0.0, smin=10.e0, smax=100.e0;
NumCols = LP->Cols;
NumBounds = LP->NumberBounds;
mutemp = 0.0; avx = 0.0;
for (i = 0; i < NumCols; i++) {
mutemp += Current->x[i] * Current->s[i];
avx += Current->x[i];
}
for (i = 0; i < NumBounds; i++) {
mutemp += Current->w[i] * Current->r[i];
avx += Current->w[i];
}
/* Shifts each split-variable pair so that the smaller of the two takes on
* the value MID(smin, larger-smaller, smax). Adjusts the s components
* accordingly. */
for (i = 0; i < LP->NumberSplit; i++) {
im = LP->FreeMinus[i];
ip = LP->FreePlus[i];
mutemp -= ( Current->x[ip] * Current->s[ip] +
Current->x[im] * Current->s[im]);
avx -= (Current->x[ip] + Current->x[im]);
sxmax = MAX(Current->x[ip], Current->x[im]);
}
mutemp /= (NumCols + NumBounds - 2*LP->NumberSplit);
avx /= (NumCols + NumBounds - 2*LP->NumberSplit);
for (i = 0; i < LP->NumberSplit; i++) {
im = LP->FreeMinus[i];
ip = LP->FreePlus[i];
xmax = MAX(Current->x[ip], Current->x[im]);
xmin = MIN(Current->x[ip], Current->x[im]);
/* shift so that the geometric mean of the shifted var equals avx */
splitshift = (xmax-xmin)*(xmax-xmin) + 4.0*avx*avx;
splitshift = sqrt(splitshift) - (xmax-xmin);
splitshift/= 2.0;
if (splitshift > smax) splitshift = smax;
if (splitshift < smin) splitshift = smin;
splitshift = xmin - splitshift;
Current->x[ip] -= splitshift;
Current->x[im] -= splitshift;
}
/* finally, adjust duals so that the average pairwise product is
achieved by the split vars */
goal = 1.e-2;
for (i = 0; i < LP->NumberSplit; i++) {
im = LP->FreeMinus[i];
ip = LP->FreePlus[i];
if (Current->s[im] * Current->x[im] < goal*mutemp)
Current->s[im] = goal * mutemp / Current->x[im];
if (Current->s[ip] * Current->x[ip] < goal*mutemp)
Current->s[ip] = goal * mutemp / Current->x[ip];
}
return 0;
}
|