use of org.ojalgo.access.Mutate2D in project ojAlgo by optimatika.
the class SimplexSolver method build.
static SimplexTableau build(final ConvexSolver.Builder convex) {
final int numbVars = convex.countVariables();
final int numbEqus = convex.countEqualityConstraints();
final int numbInes = convex.countInequalityConstraints();
final SimplexTableau retVal = SimplexTableau.make(numbEqus + numbInes, numbVars + numbVars, numbInes);
final Mutate1D obj = retVal.objective();
final MatrixStore<Double> convexC = convex.getC();
for (int v = 0; v < numbVars; v++) {
final double val = convexC.doubleValue(v);
obj.set(v, -val);
obj.set(numbVars + v, val);
}
final Mutate2D constrBody = retVal.constraintsBody();
final Mutate1D constrRHS = retVal.constraintsRHS();
final MatrixStore<Double> convexAE = convex.getAE();
final MatrixStore<Double> convexBE = convex.getBE();
for (int i = 0; i < numbEqus; i++) {
final double rhs = convexBE.doubleValue(i);
final boolean neg = NumberContext.compare(rhs, ZERO) < 0;
for (int j = 0; j < numbVars; j++) {
final double val = convexAE.doubleValue(i, j);
constrBody.set(i, j, neg ? -val : val);
constrBody.set(i, numbVars + j, neg ? val : -val);
}
constrRHS.set(i, neg ? -rhs : rhs);
}
final RowsSupplier<Double> convexAI = convex.getAI();
final MatrixStore<Double> convexBI = convex.getBI();
for (int i = 0; i < numbInes; i++) {
final int r = i;
final SparseArray<Double> row = convexAI.getRow(r);
final double rhs = convexBI.doubleValue(r);
final boolean neg = NumberContext.compare(rhs, ZERO) < 0;
row.nonzeros().forEach(nz -> constrBody.set(numbEqus + r, nz.index(), neg ? -nz.doubleValue() : nz.doubleValue()));
row.nonzeros().forEach(nz -> constrBody.set(numbEqus + r, numbVars + nz.index(), neg ? nz.doubleValue() : -nz.doubleValue()));
constrBody.set(numbEqus + r, numbVars + numbVars + r, neg ? NEG : ONE);
constrRHS.set(numbEqus + i, neg ? -rhs : rhs);
}
return retVal;
}
Aggregations