| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package net.sourceforge.combean.samples.mathprog.lp.matrixrounding; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.Matrix; |
| 27 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.SparseVector; |
| 28 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorOrientation; |
| 29 | |
import net.sourceforge.combean.interfaces.mathprog.lp.LPConstraint; |
| 30 | |
import net.sourceforge.combean.interfaces.mathprog.lp.LPVariable; |
| 31 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPConstraintSequence; |
| 32 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelRows; |
| 33 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelSolver; |
| 34 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPSparseVector; |
| 35 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPVariableSequence; |
| 36 | |
import net.sourceforge.combean.mathprog.linalg.SparseVectorWithConstantPattern; |
| 37 | |
import net.sourceforge.combean.mathprog.linalg.statics.SparseVectorUtil; |
| 38 | |
import net.sourceforge.combean.mathprog.lp.DoubleLPConstraint; |
| 39 | |
import net.sourceforge.combean.mathprog.lp.DoubleLPVariable; |
| 40 | |
import net.sourceforge.combean.mathprog.lp.model.AbstractSimpleIndexLPConstrainedRowsWithVars; |
| 41 | |
import net.sourceforge.combean.mathprog.lp.model.SparseVectorAsLPVector; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class MatrixToRoundAsLP |
| 50 | |
extends AbstractSimpleIndexLPConstrainedRowsWithVars |
| 51 | |
implements LPModelRows, LPConstraintSequence, LPVariableSequence { |
| 52 | |
|
| 53 | 6 | private Matrix m = null; |
| 54 | |
|
| 55 | 6 | private double[] rowsums = null; |
| 56 | 6 | private double[] colsums = null; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public MatrixToRoundAsLP(Matrix matrixToRound) { |
| 64 | 6 | super("", ""); |
| 65 | |
|
| 66 | 6 | this.m = matrixToRound; |
| 67 | |
|
| 68 | 6 | this.rowsums = new double[this.m.getNumRows()]; |
| 69 | 24 | for (int row = 0; row < this.rowsums.length; row++) { |
| 70 | 18 | this.rowsums[row] = |
| 71 | |
SparseVectorUtil.sum(this.m.getRowVector(row)); |
| 72 | |
} |
| 73 | |
|
| 74 | 6 | this.colsums = new double[this.m.getNumColumns()]; |
| 75 | 24 | for (int col = 0; col < this.colsums.length; col++) { |
| 76 | 18 | this.colsums[col] = |
| 77 | |
SparseVectorUtil.sum(this.m.getColumnVector(col)); |
| 78 | |
} |
| 79 | 6 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public int getNumColumns() { |
| 85 | |
|
| 86 | 300 | return this.m.getNumColumns() * this.m.getNumRows(); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public int getNumRows() { |
| 93 | |
|
| 94 | 84 | return 2 * (this.m.getNumColumns() + this.m.getNumRows()); |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public LPVariable getLPVariable(int localColumn) { |
| 102 | 54 | double val = this.m.getDoubleAt( |
| 103 | |
localColumn / this.m.getNumColumns(), |
| 104 | |
localColumn % this.m.getNumColumns()); |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | 54 | return new DoubleLPVariable((localColumn + 1.0)*(localColumn + 1.0) |
| 112 | |
+ getNumVars()*getNumVars()*getNumVars(), |
| 113 | |
Math.floor(val), Math.ceil(val)); |
| 114 | |
|
| 115 | |
|
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public LPSparseVector getRowVector(int localRow) { |
| 122 | 72 | SparseVector result = null; |
| 123 | |
|
| 124 | 72 | if (localRow >= 2*this.m.getNumRows()) { |
| 125 | |
|
| 126 | 36 | int colInOrigM = localRow/2 - this.m.getNumRows(); |
| 127 | 36 | result = new SparseVectorWithConstantPattern( |
| 128 | |
getNumColumns(), |
| 129 | |
1.0, |
| 130 | |
colInOrigM, |
| 131 | |
colInOrigM + this.m.getNumColumns()*(this.m.getNumRows()-1), |
| 132 | |
this.m.getNumColumns()); |
| 133 | 36 | } |
| 134 | |
else { |
| 135 | |
|
| 136 | 36 | int rowInOrigM = localRow / 2; |
| 137 | 36 | result = new SparseVectorWithConstantPattern ( |
| 138 | |
getNumColumns(), |
| 139 | |
1.0, |
| 140 | |
rowInOrigM * this.m.getNumColumns(), |
| 141 | |
(rowInOrigM+1) * this.m.getNumColumns() - 1, |
| 142 | |
1); |
| 143 | |
} |
| 144 | |
|
| 145 | 72 | return new SparseVectorAsLPVector(result, "", |
| 146 | |
VectorOrientation.ROWWISE); |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public LPConstraint getLPConstraint(int localRow) { |
| 153 | 72 | byte rel = (localRow % 2 == 0) ? LPConstraint.REL_GREATER : LPConstraint.REL_LESS; |
| 154 | |
|
| 155 | 72 | double val = 0.0; |
| 156 | 72 | if (localRow >= 2*this.m.getNumRows()) { |
| 157 | |
|
| 158 | 36 | val = this.colsums[localRow/2 - this.m.getNumRows()]; |
| 159 | |
} |
| 160 | |
else { |
| 161 | |
|
| 162 | 36 | val = this.rowsums[localRow/2]; |
| 163 | |
} |
| 164 | 72 | if (rel == LPConstraint.REL_GREATER) { |
| 165 | 36 | val = Math.floor(val); |
| 166 | |
} |
| 167 | |
else { |
| 168 | 36 | val = Math.ceil(val); |
| 169 | |
} |
| 170 | |
|
| 171 | 72 | return new DoubleLPConstraint("row", rel, val); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public double[][] getRoundedMatrix(LPModelSolver solver) { |
| 181 | 6 | double[][] result = |
| 182 | |
new double[this.m.getNumRows()][this.m.getNumColumns()]; |
| 183 | |
|
| 184 | 24 | for (int row = 0; row < this.m.getNumRows(); row++) { |
| 185 | 72 | for (int col = 0; col < this.m.getNumColumns(); col++) { |
| 186 | 54 | result[row][col] = |
| 187 | |
solver.getSolution(getColumnModelIndex( |
| 188 | |
row * this.m.getNumColumns() + col)); |
| 189 | |
} |
| 190 | |
} |
| 191 | |
|
| 192 | 6 | return result; |
| 193 | |
} |
| 194 | |
} |