use of org.ojalgo.access.Structure2D.IntRowColumn in project ojAlgo by optimatika.
the class ExpressionsBasedModel method objective.
/**
* This is generated on demend – you should not cache this. More specifically, modifications made to this
* expression will not be part of the optimisation model. You define the objective by setting the
* {@link Variable#weight(Number)}/{@link Expression#weight(Number)} on one or more variables and/or
* expressions.
*
* @return The generated/aggregated objective function
*/
public Expression objective() {
final Expression retVal = new Expression(OBJECTIVE, this);
Variable tmpVariable;
for (int i = 0; i < myVariables.size(); i++) {
tmpVariable = myVariables.get(i);
if (tmpVariable.isObjective()) {
retVal.set(i, tmpVariable.getContributionWeight());
}
}
BigDecimal tmpOldVal = null;
BigDecimal tmpDiff = null;
BigDecimal tmpNewVal = null;
for (final Expression tmpExpression : myExpressions.values()) {
if (tmpExpression.isObjective()) {
final BigDecimal tmpContributionWeight = tmpExpression.getContributionWeight();
// To avoid multiplication by 1.0
final boolean tmpNotOne = tmpContributionWeight.compareTo(ONE) != 0;
if (tmpExpression.isAnyLinearFactorNonZero()) {
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
tmpOldVal = retVal.get(tmpKey);
tmpDiff = tmpExpression.get(tmpKey);
tmpNewVal = tmpOldVal.add(tmpNotOne ? tmpContributionWeight.multiply(tmpDiff) : tmpDiff);
final Number value = tmpNewVal;
retVal.set(tmpKey, value);
}
}
if (tmpExpression.isAnyQuadraticFactorNonZero()) {
for (final IntRowColumn tmpKey : tmpExpression.getQuadraticKeySet()) {
tmpOldVal = retVal.get(tmpKey);
tmpDiff = tmpExpression.get(tmpKey);
tmpNewVal = tmpOldVal.add(tmpNotOne ? tmpContributionWeight.multiply(tmpDiff) : tmpDiff);
final Number value = tmpNewVal;
retVal.set(tmpKey, value);
}
}
}
}
return retVal;
}
use of org.ojalgo.access.Structure2D.IntRowColumn in project ojAlgo by optimatika.
the class ConvexSolver method copy.
public static void copy(final ExpressionsBasedModel sourceModel, final ConvexSolver.Builder destinationBuilder) {
destinationBuilder.reset();
final List<Variable> tmpFreeVariables = sourceModel.getFreeVariables();
final Set<IntIndex> tmpFixedVariables = sourceModel.getFixedVariables();
final int tmpFreeVarDim = tmpFreeVariables.size();
// final Array1D<Double> tmpCurrentSolution = Array1D.PRIMITIVE.makeZero(tmpFreeVarDim);
// for (int i = 0; i < tmpFreeVariables.size(); i++) {
// final BigDecimal tmpValue = tmpFreeVariables.get(i).getValue();
// if (tmpValue != null) {
// tmpCurrentSolution.set(i, tmpValue.doubleValue());
// }
// }
// final Optimisation.Result tmpKickStarter = new Optimisation.Result(Optimisation.State.UNEXPLORED, Double.NaN, tmpCurrentSolution);
// AE & BE
final List<Expression> tmpEqExpr = sourceModel.constraints().filter((final Expression c) -> c.isEqualityConstraint() && !c.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpEqExprDim = tmpEqExpr.size();
if (tmpEqExprDim > 0) {
final SparseStore<Double> tmpAE = SparseStore.PRIMITIVE.make(tmpEqExprDim, tmpFreeVarDim);
final PhysicalStore<Double> tmpBE = FACTORY.makeZero(tmpEqExprDim, 1);
for (int i = 0; i < tmpEqExprDim; i++) {
final Expression tmpExpression = tmpEqExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAE.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBE.set(i, 0, tmpExpression.getAdjustedUpperLimit());
}
destinationBuilder.equalities(tmpAE, tmpBE);
}
// Q & C
final Expression tmpObjExpr = sourceModel.objective().compensate(tmpFixedVariables);
PhysicalStore<Double> tmpQ = null;
if (tmpObjExpr.isAnyQuadraticFactorNonZero()) {
tmpQ = FACTORY.makeZero(tmpFreeVarDim, tmpFreeVarDim);
final BinaryFunction<Double> tmpBaseFunc = sourceModel.isMaximisation() ? SUBTRACT : ADD;
UnaryFunction<Double> tmpModifier;
for (final IntRowColumn tmpKey : tmpObjExpr.getQuadraticKeySet()) {
final int tmpRow = sourceModel.indexOfFreeVariable(tmpKey.row);
final int tmpColumn = sourceModel.indexOfFreeVariable(tmpKey.column);
if ((tmpRow >= 0) && (tmpColumn >= 0)) {
tmpModifier = tmpBaseFunc.second(tmpObjExpr.getAdjustedQuadraticFactor(tmpKey));
tmpQ.modifyOne(tmpRow, tmpColumn, tmpModifier);
tmpQ.modifyOne(tmpColumn, tmpRow, tmpModifier);
}
}
}
PhysicalStore<Double> tmpC = null;
if (tmpObjExpr.isAnyLinearFactorNonZero()) {
tmpC = FACTORY.makeZero(tmpFreeVarDim, 1);
if (sourceModel.isMinimisation()) {
for (final IntIndex tmpKey : tmpObjExpr.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpC.set(tmpIndex, 0, -tmpObjExpr.getAdjustedLinearFactor(tmpKey));
}
}
} else {
for (final IntIndex tmpKey : tmpObjExpr.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpC.set(tmpIndex, 0, tmpObjExpr.getAdjustedLinearFactor(tmpKey));
}
}
}
}
destinationBuilder.objective(tmpQ, tmpC);
// AI & BI
final List<Expression> tmpUpExpr = sourceModel.constraints().filter((final Expression c2) -> c2.isUpperConstraint() && !c2.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpUpExprDim = tmpUpExpr.size();
final List<Variable> tmpUpVar = sourceModel.bounds().filter((final Variable c4) -> c4.isUpperConstraint()).collect(Collectors.toList());
final int tmpUpVarDim = tmpUpVar.size();
final List<Expression> tmpLoExpr = sourceModel.constraints().filter((final Expression c1) -> c1.isLowerConstraint() && !c1.isAnyQuadraticFactorNonZero()).collect(Collectors.toList());
final int tmpLoExprDim = tmpLoExpr.size();
final List<Variable> tmpLoVar = sourceModel.bounds().filter((final Variable c3) -> c3.isLowerConstraint()).collect(Collectors.toList());
final int tmpLoVarDim = tmpLoVar.size();
if ((tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim) > 0) {
final SparseStore<Double> tmpAI = SparseStore.PRIMITIVE.make(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim, tmpFreeVarDim);
final PhysicalStore<Double> tmpBI = FACTORY.makeZero(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + tmpLoVarDim, 1);
if (tmpUpExprDim > 0) {
for (int i = 0; i < tmpUpExprDim; i++) {
final Expression tmpExpression = tmpUpExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAI.set(i, tmpIndex, tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBI.set(i, 0, tmpExpression.getAdjustedUpperLimit());
}
}
if (tmpUpVarDim > 0) {
for (int i = 0; i < tmpUpVarDim; i++) {
final Variable tmpVariable = tmpUpVar.get(i);
tmpAI.set(tmpUpExprDim + i, sourceModel.indexOfFreeVariable(tmpVariable), tmpVariable.getAdjustmentFactor());
tmpBI.set(tmpUpExprDim + i, 0, tmpVariable.getAdjustedUpperLimit());
}
}
if (tmpLoExprDim > 0) {
for (int i = 0; i < tmpLoExprDim; i++) {
final Expression tmpExpression = tmpLoExpr.get(i).compensate(tmpFixedVariables);
for (final IntIndex tmpKey : tmpExpression.getLinearKeySet()) {
final int tmpIndex = sourceModel.indexOfFreeVariable(tmpKey.index);
if (tmpIndex >= 0) {
tmpAI.set(tmpUpExprDim + tmpUpVarDim + i, tmpIndex, -tmpExpression.getAdjustedLinearFactor(tmpKey));
}
}
tmpBI.set(tmpUpExprDim + tmpUpVarDim + i, 0, -tmpExpression.getAdjustedLowerLimit());
}
}
if (tmpLoVarDim > 0) {
for (int i = 0; i < tmpLoVarDim; i++) {
final Variable tmpVariable = tmpLoVar.get(i);
tmpAI.set(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + i, sourceModel.indexOfFreeVariable(tmpVariable), -tmpVariable.getAdjustmentFactor());
tmpBI.set(tmpUpExprDim + tmpUpVarDim + tmpLoExprDim + i, 0, -tmpVariable.getAdjustedLowerLimit());
}
}
destinationBuilder.inequalities(tmpAI, tmpBI);
}
}
Aggregations